/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

function HashObj(){
	
	this.bid = 0;
	this.price_min = 0;
	this.price_max = 0;
	this.cids = new Array();
	this.hashString = new String();
	this.viewId = 3;
	this.productsOrderId = 8;
        this.siteUrl = "<site_url>";
        
	this.init = function(url){
                this.siteUrl = url;
		this.hashString = this.getHash();	// set current hash
		if(this.getPriceMin() != -1 ){this.price_min	= this.getPriceMin(); $('#min_value').val(this.price_min);}
		if(this.getPriceMax() != -1 ){this.price_max 	= this.getPriceMax(); $('#max_value').val(this.price_max);}
		this.getCIds();
		
		$( "#slider" ).slider( "option", "values", [this.price_min,this.price_max] );
		this.bid 		= this.getBrandId();
		if(this.bid > 0){$('#bId-'+this.bid).attr('checked', 'checked'); $('#bId-'+this.bid).siblings("a").addClass('jqTransformChecked'); }
		if(this.cids.length > 0){
			for(var i=0; i < this.cids.length ; i++){
				$('#cId-'+this.cids[i]).attr('checked', 'checked');
				$('#cId-'+this.cids[i]).siblings("a").addClass('jqTransformChecked');
			}
		}
		 
		if( this.getProductsOrderFromHash() != -1 ){this.setProductsOrder( this.getProductsOrderFromHash() );}	
		
		if(this.getViewIdFromHash() != -1){var tmpViewId = this.getViewIdFromHash(); }else{var tmpViewId = this.viewId;}
		
			// filtrar produtos
		filterProducts();
			//set order
		$("#order_price").find("option").each(function(){
		     if( $(this).val() == myHash.getProductsOrder() ) {
		       var myIndex = $(this).index() + 1;
		        $("#order_price").parent('.jqTransformSelectWrapper').find('ul li a').removeClass('selected');
		    	 $("#order_price").parent('.jqTransformSelectWrapper').find('ul li:nth-child(' + myIndex + ') a').addClass('selected');
		    	 $("#order_price").parent('.jqTransformSelectWrapper').find('div span').html($("#order_price").parent('.jqTransformSelectWrapper').find('ul li:nth-child(' + myIndex +') a').html());
		     }
		 });

			//set view
		onChangeView(tmpViewId);
		
			// ordenar produtos
		sortProducts(this.productsOrderId);
	}
	
	
	this.update = function(){
                var newHash = '#';
		var param 	= 0;
		if(this.bid > 0){
			newHash += 'bId=' + this.bid;
			param++;
		}
		if(param > 0){newHash += '&';}
		newHash += 'pMin=' + this.price_min + '&pMax=' + this.price_max;
		if(this.cids.length > 0){
			newHash += '&cIds=';
			for(var i=0; i < this.cids.length; i++){
				if(i > 0){newHash += ',';}
				newHash += this.cids[i];
			}
		}
			//viewId
		newHash += '&vId=' + this.viewId;
			//orderId
		newHash += '&oId=' + this.productsOrderId;
		
		document.location.hash = newHash;
	}
	
	this.getBrandId = function(){
		var myRegex = new RegExp("bId=([[0-9]*]*?)*");
		if(myRegex.test(this.hashString)){
			var param = myRegex.exec(this.hashString);
			
			return parseInt( param[1] );
		}else{
			return 0;
		}	
	}
	
	
	this.getPriceMin = function(){
		var myRegex = new RegExp("pMin=([0-9]+)");
		if(myRegex.test(this.hashString)){
			var param = myRegex.exec(this.hashString);
			
			return parseInt( param[1] );
		}
		return -1;
	}
	
	this.getPriceMax = function(){
		var myRegex = new RegExp("pMax=([0-9]+)");
		if(myRegex.test(this.hashString)){
			var param = myRegex.exec(this.hashString);
			
			return parseInt( param[1] );
		}
		return -1;
	}
	
	this.getCIds = function(){
		var myRegex = new RegExp("cIds=([0-9]*)(\,{0,1}[0-9])+");
		if(myRegex.test(this.hashString)){
			var param = myRegex.exec(this.hashString);
			var getValuesRegex = new RegExp("(([0-9]+))*");
			var values =  param[0].substring(5).split(',');
			for(var i=0; i < values.length; i++){
				this.cids.push(parseInt( values[i] ));
			}
		}
		return;
	}
	
	
	this.getProductsOrderFromHash = function(){
		var myRegex = new RegExp("oId=([0-9]+)");
		if(myRegex.test(this.hashString)){
			var param = myRegex.exec(this.hashString);
			
			return parseInt( param[1] );
		}
		return -1;
	}
	
	
	this.getViewIdFromHash = function(){
		var myRegex = new RegExp("vId=([0-9]+)");
		if(myRegex.test(this.hashString)){
			var param = myRegex.exec(this.hashString);
			var viewId = parseInt(param[1]);
			if(viewId > 0){
				return viewId;
			}
		}
		return -1;
	}
	
        
        this.onChangeProductsOrder = function(orderId){
		orderId = parseInt(orderId);
		//if(myHash.getProductsOrder() != orderId){
                    
			myHash.setProductsOrder(orderId);
			myHash.update();
			$.get(this.siteUrl+"/product/setProductOrder/"+orderId);
		//}
	}
        
        
	
	this.getHash = function(){
		return document.location.hash;
	}
	
	this.getView = function(){
		return this.viewId;
	}
	
	this.getProductsOrder = function(){
		return this.productsOrderId;
	}
	
	
	this.setPriceMin = function(val){
		this.price_min = val;
	}
	
	this.setPriceMax = function(val){
		this.price_max = val;
	}
	
	this.setBrandId = function (val){
		this.bid = parseInt( val );
	}
	
	this.toogleCId = function(cid){
		if(this.cids.length > 0){
			for(var i=0; i < this.cids.length; i++ ){
				if(this.cids[i] == cid){
					this.cids.splice(i, 1);
					return;
				}
			}
			this.cids.push(cid);
		}else{
			this.cids.push(cid);
		}
	}
	
	this.setView = function(viewId){
		this.viewId = parseInt(viewId);
	}
	
	this.setProductsOrder = function(orderId){
		this.productsOrderId = parseInt(orderId);
	}
	
        
        this.addFilterBrand = function(bId){
            myHash.setBrandId(bId);
            myHash.update();
            filterProducts();
        }
	
	this.addFilterCharacteristic = function(cId){
            myHash.toogleCId(cId);
            myHash.update();
            filterProducts();
        }
}

//end myHash
