var oGallery = Class.create();

oGallery.prototype = {
	initialize : function(){
		this.cat_id = 0;							
		this.limit = 0;
		this.gallery_img_limit=0;
	},

	genCategoryGalleries : function(iCatId, iGalleryLimit, iPage, iLimit){
		
			$$('.lnk_category_selected').each(function(a){
				a.className = "lnk_category";		
			});

			$('navCat_'+iCatId).className = "lnk_category_selected";
			$('navCat_'+iCatId).blur();

			this.gallery_img_limit = iGalleryLimit;
			this.url = json_gallery;
			this.setCategory(iCatId);
			this.getPage(iPage, iLimit);
	},

	setCategory : function(iCatId){
		this.cat_id = iCatId;						

	},							 
 
	getPage : function(page, limit, order){
		this.order = order;					
		this.limit = limit;
		this.page = page;
		this.refine_search();
	},
											
	getData : function(){
		var url = this.url+'&'+this.q;
		GetRequest(url, '_galleryList', '', 'topLoader'); 
	},								

	refine_search : function(){
		this.params = new Array;

		// Cat Id
		if(this.cat_id)
			this.params.push('cat_id='+this.cat_id);

		// Gallery Img Limit
		if(this.gallery_img_limit)
			this.params.push('gallery_img_limit='+this.gallery_img_limit);

		// Limit
		if(this.limit)
			this.params.push('limit='+this.limit);
		
		try{
			//	ORDER 
			if(this.order)
				this.params.push('o='+this.order);
			else
				if(sOrder)
					this.params.push('o='+sOrder);
		}
		catch(e){}

		// Page
		this.params.push('p='+this.page);

		this.q = this.params.join('&');
		this.getData();
	}
}



function onMySuccess_galleryList(req){
	var json = req.responseText;
	var data = json.evalJSON();
	var count_all = data['res']['count_all'];
	var res = data['res']['data'];

	var tpl = new Array;
	if(count_all > 0){
		res.each(function(item){
			var myTemplate = new Template(tplGalleryImages);										 
			var show = {
				gallery_url: item['img'][0]['id'],
				url_files: url_ifile+item['img'][0]['medium_path'],
				medium_width: item['img'][0]['medium_width'],
				medium_height: item['img'][0]['medium_height'],
				id:item['img'][0]['id'],
				iurl:item['img'][0]['iurl'],
				name: item.name 
			};
			tpl.push(myTemplate.evaluate(show));
		
		});
	
		var HTML = tpl.join('');
	}
	if(count_all > 0 ){
		$('content').innerHTML = HTML;
	}
	else{
		$('content').innerHTML = '<p style="padding:20px;font-size:14px;">Niestety nie znaleziono galerii dla tej kategorii.</p>';	
	}
}
