var Sets = Class.create({
	initialize: function( ) {
		this.order = 'last';
		this.currentPage=0;
		this.pagesCount = 100;
		this.setsPerPage = 5;
		this.currentSelectedFolder = 0;
	},
	initSets: function( genereSearch, ageSearch, currentPage, order, currentSelectedFolder )
	{
		this.order = order;
		$('genereSearch').value=genereSearch;
		$('ageSearch').value=ageSearch;
		this.currentPage=currentPage;
		this.selectSetsFolder(currentSelectedFolder);
		this.search();

	},
	selectLast: function()
	{
		this.order = 'date_updated';
		this.selectSetsFolder(0);
		this.currentPage=0;
		this.resetFields();
		this.search();
	},
	selectFavorites: function()
	{
		this.order = 'favorito';
		this.selectSetsFolder(1);
		this.currentPage=0;
		this.resetFields();
		this.search();
	},
	selectSameCreator: function()
	{
		this.selectSetsFolder(2);
		this.currentPage=0;
		this.resetFields();
		var owner = $(printableSuscriber).innerHTML;
		$('userSearch').value = owner;
		this.search();
	},
	selectSearch: function(id_div_result)
	{
		if($(id_div_result).style.display == 'none')
			Effect.SlideDown(id_div_result);
		
		this.selectSetsFolder(3);
		this.currentPage=0;
		this.search();
	},
	selectSetsFolder: function(num)
	{
	/*	this.currentSelectedFolder = num;
		var setFoldersTable = document.getElementById('tableSetsSearchResult');
		var cols =  setFoldersTable.getElementsByTagName('td');
		var elemsPerFolder = 4;
		var numFolders = parseInt((cols.length+1)/elemsPerFolder);
		var cellCounter = 0;
		for( var i = 0; i < numFolders; i++ )
		{
			var leftCell = cols.item(cellCounter++);
			var labelCell = cols.item(cellCounter++);
			var rightCell = cols.item(cellCounter++);
			cellCounter++;// separator cell
			
			if( i == num )
			{
				labelCell.background="img/pestanaBgOn.gif";	
				labelCell.style.background= "url(img/pestanaBgOn.gif)";
				leftCell.getElementsByTagName('img').item(0).src="img/pestanaOnIzq.gif";
				rightCell.getElementsByTagName('img').item(0).src="img/pestanaOnDer.gif";
			}else
			{
				labelCell.background="img/pestanaBgOff.gif";
				labelCell.style.background= "url(img/pestanaBgOff.gif)";
				leftCell.getElementsByTagName('img').item(0).src="img/pestanaOffIzq.gif";
				rightCell.getElementsByTagName('img').item(0).src="img/pestanaOffDer.gif";
			}
		}*/
	},
	resetFields: function ()
	{
		$('userSearch').value = '';
		$('productSearch').value = '';
//		$('brandSearch').value = '';
		$('genereSearch').value = 'I';
		$('ageSearch').value = '9';	
		//$('colorSearch').value = '';	
	
	},
	search: function()
	{
		//this.currentPage=0;
		this.goNextPage();
	},
	goNextPage: function()
	{	
		if( this.currentPage < this.pagesCount ){
			this.loadSetsPage( ++(this.currentPage) );
			this.updateCurrentPageLabel();
		} 
		
		this.manageArrows();
	},
	
	goPreviousPage: function()
	{
		if( this.currentPage > 1 ) {
			this.loadSetsPage( --(this.currentPage));
			this.updateCurrentPageLabel();
		}
		
		this.manageArrows();
	},

	manageArrows: function()
	{
		var isoff_left = '';
		var isoff_right = '';
		if(this.currentPage >= this.pagesCount || this.pagesCount < 1)
			isoff_right = '_off';
		
		if(this.currentPage < 2)
			isoff_left = '_off';
		
		$('arrowLeft').src = '/images/Pizq' + isoff_left + '.gif';
		$('arrowRight').src = '/images/Pder' + isoff_right + '.gif';
	},
	
	updateCurrentPageLabel: function()
	{
		var currentPage = this.currentPage;
		$('currentPage').innerHTML = currentPage;
	},
	
	updatePagesCount: function()
	{
		var pagesCount = this.pagesCount;
		$('totalPages').innerHTML = pagesCount;
		$('totalPages').show();
	},
	
	getImageId: function(row,col)
	{
		return "imgSet" + row + '_' + col;
	},
	
	getLinkId: function(row,col)
	{
		return "linkSet" + row + '_' + col;
	},
	
	resetSets: function()
	{	
		for (var prodI = 0; prodI < this.setsPerPage; prodI++) 
		{
			var col = prodI % this.setsPerPage;
	        var row = Math.floor(prodI /this.setsPerPage);
			var imgId = this.getImageId(row , col );	               			
	        var img = $(imgId);
	        img.style.visibility = 'hidden';
		}
	},
	markSetsAsWaiting: function( num, resetlinks )
	{
		for (var prodI = 0; prodI < num; prodI++) 
		{
			var col = prodI % this.setsPerPage;
	        var row = Math.floor(prodI /this.setsPerPage);
	        var imgId = this.getImageId(row , col );
	        
	        var img = $( imgId );
	               			
	        img.src='/images/cargando.gif';
	        img.style.visibility = 'visible';
	        
	        if(resetlinks == true) {
	        	var setLinkId = this.getLinkId(row, col);
	        	$(setLinkId).href='#';
	        }
		}
	},
	
	
	loadSetsPage: function ( page )
	{
		var userSearch = $('userSearch').value;
		var productSearch = $('productSearch').value;
		var locationSearch = $('locationSearch').value;
		var genere = $('genereSearch').value;
		var rankAge = $('ageSearch').value;
		var colorSearch = $('colorSearch').value;

		this.loadSets(page,userSearch,productSearch,locationSearch,genere,rankAge,colorSearch,this.order);
	
	},
	
	loadSets: function (page,userSearch,productSearch,locationSearch,genereSearch,ageSearch,colorSearch,order)
	{
		var current = this;
	    var contentUrl = "./ajaxQuerySets/?page=" + page;
	    contentUrl += "&user=" + userSearch + "&product=" + productSearch + "&location=" + locationSearch +"&genere=" + genereSearch + "&ageRank=" + ageSearch + "&color=" + colorSearch;
	    if( order )
	    	contentUrl += "&order=" + order;
	    
	    current.markSetsAsWaiting( '5', true);
	    if($('resultsSearchTable').style.display == 'none')
	    {
        	$('noResultsSearch').hide();
        	$('resultsSearchTable').show(); 
	    }
	    
		new Ajax.Request(contentUrl, {
			method: 'get',
		  	onSuccess: function(transport) {
			
				var xmldoc = transport.responseXML;
            	
            	var root = xmldoc.getElementsByTagName('sets').item(0);
                var sets = root.getElementsByTagName('set');

            	var pagesCount = root.attributes.getNamedItem('pagesCount').nodeValue;
            	current.pagesCount = pagesCount;
            	current.updatePagesCount();
            	current.manageArrows();
                current.resetSets();
                
                setTimeout( 'sets.markSetsAsWaiting( ' + sets.length +', false )', 1);
                
                if ( sets.length == 0)
                {
                	$('resultsSearchTable').hide();          		
                	$('noResultsSearch').show();
                }
                else
                {
                	$('noResultsSearch').hide();
                	$('resultsSearchTable').show(); 
 
                	
	            	for (var iNode = 0; iNode < sets.length; iNode++) {
	            		
	               		var node = sets.item(iNode);
	               		
	               		var setId = node.attributes.getNamedItem('id').nodeValue;
	               		var name = node.getElementsByTagName('name').item(0).firstChild.nodeValue;
	               		var linkSet = node.getElementsByTagName('link').item(0).firstChild.nodeValue;
	               		
//	               		var name = nameNode.firstChild.nodeValue;
	               		
	               		var col = iNode % current.setsPerPage;
	               		var row = Math.floor(iNode /current.setsPerPage);
	               		
	               		var setImgId = current.getImageId(row , col );
	               		var setLinkId = current.getLinkId(row, col);
	               		
//	               		$(setLinkId).href=current.getUrlDetalles(setId);
	               		$(setLinkId).href= linkSet;
	               		var imgSrc = "tset" + setId + ".png?" + Math.random();
	               		       			
	         			setTimeout( 'sets.changeImage('+ setId + ',\'' + setImgId + '\',\'' + imgSrc + '\',\'' + name + '\')', 500);
	            	}
                }
			}
		});
		
	},
	
	getUrlDetalles: function (setId)
	{
		return base_url_amazon_js + setId;
	},
	
	helpMeSelect: function (divId)
	{
		var current = this;
	    var contentUrl = "./ajaxHelpMeSelect/";
	    
		new Ajax.Request(contentUrl, {
			method: 'get',
		  	onSuccess: function(transport) {
				var doc = transport.responseText;
				var divObj = $(divId);
				
				divObj.fade( {afterFinish: function()
                {
					divObj.innerHTML = doc;							
					divObj.appear(); 	
                }
				});			
			}
		});
		
	},
	
	changeImage: function(setId, imgId, imgSrc, alt)
	{
		$(imgId).src = base_url_amazon_js + imgSrc;
		$(imgId).alt = alt;
		var urlSet = '/kimod/kimod/src/php/web/detalles/set/id/' + setId;
		var current = this;
		
		$(imgId).onclick=function(){
			var userSearch =  $('userSearch').value;
			if( userSearch != '' )
				urlSet += '&userSearch=' + escape(userSearch);
				
//			var brandSearch =  $('brandSearch').value;
//			if( brandSearch != '' )
//				urlSet += '&brandSearch=' + escape(brandSearch);
				
			var productSearch =  $('productSearch').value;
			if( productSearch != '' )
				urlSet += '&productSearch=' + escape(productSearch);
				
			var genereSearch =  $('genereSearch').value;
			urlSet += '&genereSearch=' + escape(genereSearch);

			var ageSearch =  $('ageSearch').value;
			urlSet += '&ageSearch=' + escape(ageSearch);

			var colorSearch =  $('colorSearch').value;
			urlSet += '&colorSearch=' + escape(colorSearch);
			
			var currentPage =  parseInt(current.currentPage) - 1;//La busqueda incrementa uno
			urlSet += '&currentPage=' + currentPage;

			var order =  current.order;
			urlSet += '&order=' + order;
			
			urlSet += '&currentSelectedFolder=' + current.currentSelectedFolder;
			
			document.location=urlSet;
			};
	},
	
	searchOnEnter: function(event)
	{
		var evt  = (evt) ? evt : ((event) ? event : null);
		var keycode;
		if (window.event) 
			keycode = window.event.keyCode;
		else{ 
			if (event)
			{ 
				keycode = event.which;			
			}	
			else 
				return true;
		}
		if (keycode == 13)
		{	
			this.selectSearch();
		}	
	},
	selectColor: function(rgbColor)
	{
		$('colorTD').style.backgroundColor = rgbColor;
		$('colorpicker').style.visibility='hidden';
		$('colorSearch').value = rgbColor;	
	}
	
	
});
var sets = new Sets();