var C_showProducts=new Class ({
	type:null,
	holder:null,
	items:null,
	
	init: function(_type, id) {
		this.type=_type;
		this.holder=$_(id);
		this.renderType();
	},
	renderType:function(){
		this.holder.innerHTML='';
		var arrayTypes=new Array();
		ajaxPetition("resources/PHP/API_catalogues.php","command=getCatalogueItems&moduleType="+this.type,function(ajax){
			var r = eval('('+ajax.responseText+')');
			if (r.errorCode>0){ alert(r.errorDescription); return;}
			
			this.items=r.data.items;
			var i=0;
			$A(r.data.fields).each(function(elem){
				if (this.indexType && this.indexName) return;
				if (elem.name=="Tipo") this.indexType = i;
				if (elem.name=="Nombre") this.indexName = i;
				i++;
			}.bind(this));
			
			$A(r.data.items).each(function(elem){
					if (inArray(arrayTypes, elem[this.indexType])) return;
					
					arrayTypes.push(elem[this.indexType]); 
					var itemHolder = $C('DIV', {className:'itemHolder'}, this.holder);
					var photoHolder = $C('DIV', {className:'photoHolder'}, itemHolder);
					var photoFooter = $C('DIV', {className:'photoFooter'}, itemHolder);
					$C('DIV', {className:'texto_peque', innerHTML:elem[this.indexType]}, photoFooter);
					ajaxPetition('resources/PHP/API_catalogues.php', 'command=getItemImages&itemId='+elem[0], function(ajax){
						var s = eval('('+ajax.responseText+')');
						if (s.errorCode>0){ alert(s.errorDescription); return;}
						if (s.data[0])
							$C('IMG', {'.border':'0px', src:'data/catalogues/'+elem[0]+'/crop_176x200_'+s.data[0].name, onclick:function(){this.renderCatalog(elem[this.indexType])}.bind(this)}, photoHolder);
					}.bind(this));
			}.bind(this));
			
			$C('DIV', {'.clear':'both'}, this.holder);
			ajaxPetition('resources/PHP/API_sections.php', 'command=getBodySectionByName&name=texto_enlacesCatalogo', function(ajax){
				var t = eval('('+ajax.responseText+')');
				if (t.errorCode>0){ alert(t.errorDescription); return;}
				
				/*$C('DIV', {'.margin':'10px', className:'texto_grande', innerHTML:t.data.body}, this.holder);*/
				
				$C('DIV', {'.margin':'10px', className:'texto_grande', innerHTML:"<br><a href='http://www.compacmq2.com/web/spanish/home.php' style='text-decoration:none; color:inherit;' target='_blank'><b>COMPAC MARMOL & QUARTZ</b></a><br><br><a href='http://www.silestone.es/' style='text-decoration:none; color:inherit;' target='_blank'><b>SILESTONE</b></a>"}, this.holder);
				
			}.bind(this));
		}.bind(this));
	},
	countItemsType:function(itemType){
		var i=0;
		$A(this.items).each(function(elem){
				if (elem[this.indexType]!=itemType) return;
				i++;
		}.bind(this));	
		
		return i;
	},
	renderCatalog: function(itemType){
		this.holder.innerHTML='';
		var i=0;
		var totalItems=this.countItemsType(itemType);
		var params = 'command=getItemsImages&itemsId=';
		$A(this.items).each(function(elem){
				if (elem[this.indexType]!=itemType) return;
				params+=elem[0]+',';
		}.bind(this));
		ajaxPetition('resources/PHP/API_catalogues.php', params, function(ajax){
			var r = eval('('+ajax.responseText+')');
			if (r.errorCode>0){ alert(r.errorDescription); return;}

			$A(this.items).each(function(elem){
				if (elem[this.indexType]!=itemType) return;
				var itemLittleHolder = $C('DIV', {className:'itemLittleHolder'}, this.holder);
				var photoLittleHolder = $C('DIV', {className:'photoLittleHolder'}, itemLittleHolder);
				var photoLittleFooter = $C('DIV', {className:'photoLittleFooter'}, itemLittleHolder);
				$C('DIV', {className:'texto_peque', innerHTML:elem[this.indexName]}, photoLittleFooter);
				if (r.data[elem[0]][0]){
					var linkLightBox = $C('A',{className:'thickbox', href:'showProduct.php?product='+elem[0]+'&typeProduct='+this.indexType+'&nameProduct='+this.indexName},photoLittleHolder);
					$C('IMG', {'.border':'0px', src:'data/catalogues/'+elem[0]+'/crop_100x150_'+r.data[elem[0]][0].name, onclick:function(){this.renderCatalog(elem[this.indexType])}.bind(this)}, linkLightBox);
				}

			}.bind(this));
			loadScript('js/thickbox.js', function(){});
			$C('DIV', {'.clear':'both'}, this.holder);
			$C('DIV', {className:'button', innerHTML:'Volver', onclick:function(){location.href='#nuestros_productos'; this.renderType();}.bind(this)}, this.holder);
			$C('DIV', {'.clear':'both'}, this.holder);

		}.bind(this));
	}
});
