jQuery(document).ready(function(){
	jQuery(window).hashchange( function(){
		//alert("0");
		load_ajax_by_urlhash();
	})

	
	load_ajax_by_urlhash = function(){
		loadedcurcat = false;
		var hash = window.location.hash;
		if(hash.indexOf('#') != -1){
			hash = hash.substring(hash.indexOf('#')+1);
		}
		
		var baseurl = jQuery("#baseurl").val();
		var prevhash = jQuery("#prevurlhash").attr("cval");
		//alert("prev:"+prevhash);

		if((prevhash != hash) && baseurl && (baseurl != "")){
			//alert("prev:"+prevhash+",hash:"+hash);
			if(hash == ""){
				if(window.location.href.replace(baseurl, "") == "featured-products"){
					window.location.reload();
					return;
				}
				hash = jQuery("#curcatajaxurl").attr("cval");
				
				if(hash == ""){
					return;
				}else{
					loadedcurcat = true;
					setNavigationUrl(hash);
				}
			}else{
				setNavigationUrl(baseurl+hash);
			}
		}
		
	}
	
	run_on_ready = function(){
		loadedcurcat = false;
		
		bg_arr = {
			"color" : {
					"1"   : {"onmouseout" : "-66px", "onmousedown" : "-110px", "click" : "0", "onmouseover" : "-88px"},
					"0" : {"onmouseout" : "0", "onmousedown" : "-44px", "click" : "-66px", "onmouseover" : "-22px"}
				},
			"style" : {
					"1"   : {"onmouseout" : "-36px", "onmousedown" : "-36px", "click" : "0", "onmouseover" : "-36px"},
					"0" : {"onmouseout" : "0", "onmousedown" : "-24px", "click" : "-36px", "onmouseover" : "-12px"}
				},
			"largerug" : {
					"1"   : {"onmouseout" : "-304px", "onmousedown" : "-380px", "click" : "0", "onmouseover" : "-304px"},
					"0" : {"onmouseout" : "0", "onmousedown" : "-152px", "click" : "-304px", "onmouseover" : "-76px"}
				},
			"mediumrug" : {
					"1"   : {"onmouseout" : "-187px", "onmousedown" : "-311px", "click" : "0", "onmouseover" : "-249px"},
					"0" : {"onmouseout" : "0", "onmousedown" : "-124px", "click" : "-187px", "onmouseover" : "-62px"}
				},
			"smallrug" : {
					"1"   : {"onmouseout" : "-118px", "onmousedown" : "-196px", "click" : "0", "onmouseover" : "-157px"},
					"0" : {"onmouseout" : "0", "onmousedown" : "-78px", "click" : "-118px", "onmouseover" : "-39px"}
				},
			"runner_rugs" : {
					"1"   : {"onmouseout" : "-238px", "onmousedown" : "-397px", "click" : "0", "onmouseover" : "-317px"},
					"0" : {"onmouseout" : "0", "onmousedown" : "-158px", "click" : "-238px", "onmouseover" : "-79px"}
				},
			"xsign" : {
					"0"   : {"onmouseout" : "0", "onmousedown" : "-26px", "click" : "-26px", "onmouseover" : "-13px"}
				} 
		};
		
		jQuery("#nav_price_left,#nav_price_right").unbind();
		jQuery("[dynamic_chktype]").unbind();
		jQuery("[adyn]").unbind();
		jQuery(".showmore").unbind();
		jQuery(".showless").unbind();
		
		
		
		jQuery("[dynamic_chktype]").each(function(){
				var checked = jQuery(this).attr("dynamic_checked");
				var chktype = jQuery(this).attr("dynamic_chktype");
				
				if(typeof checked === 'undefined' || checked === false){
					checked = "0";
				}
				
				var bg_move = bg_arr[chktype][checked]["onmouseout"];
				
				jQuery(this).css("background-position", "0 "+bg_move);
				
				
				var sizes_arr = ["largerug", "mediumrug", "smallrug", "runner_rugs"];
				if(jQuery.inArray(chktype, sizes_arr) != -1){
					if(checked == "1"){
						jQuery(".colorbox.for"+chktype).show();
						jQuery(".colorbox_sizes.for"+chktype).show();
						jQuery(".sizes_header").show();
					}else{
						jQuery(".colorbox.for"+chktype).hide();
						jQuery(".colorbox_sizes.for"+chktype).hide();
					}
				}
				
		});
		
		
		dc_onmouseout = function(obj){
			var checked = jQuery(obj).attr("dynamic_checked");
			if(typeof checked === 'undefined' || checked === false){
				checked = "0";
			}
			var chktype = jQuery(obj).attr("dynamic_chktype");
			var bg_move = bg_arr[chktype][checked]["onmouseout"];
			
			jQuery(obj).css("background-position", "0 "+bg_move);
		}
		dc_onmousedown = function(obj){
			var checked = jQuery(obj).attr("dynamic_checked");
			if(typeof checked === 'undefined' || checked === false){
				checked = "0";
			}
			var chktype = jQuery(obj).attr("dynamic_chktype");
			var bg_move = bg_arr[chktype][checked]["onmousedown"];
			
			jQuery(obj).css("background-position", "0 "+bg_move);
		}
		
		dc_onmouseclick = function(obj, e){
			var checked = jQuery(obj).attr("dynamic_checked");
      
			if(typeof checked === 'undefined' || checked === false){
				checked = "0";
			}
			
			var chktype = jQuery(obj).attr("dynamic_chktype");
			var ajaxurl = jQuery(obj).attr("ajaxurl");
			var bg_move = bg_arr[chktype][checked]["click"];
			
			jQuery(obj).css("background-position", "0 "+bg_move);
			if(checked == "0"){
				jQuery(obj).attr("dynamic_checked", "1");
			}else{
				jQuery(obj).attr("dynamic_checked", "0");
			}
			
			
			if(ajaxurl != ""){
				setNavigationUrl(ajaxurl);
			}
		}
		
		
		dc_onmouseover = function(obj){
			var checked = jQuery(obj).attr("dynamic_checked");
			if(typeof checked === 'undefined' || checked === false){
				checked = "0";
			}
			
			var chktype = jQuery(obj).attr("dynamic_chktype");
			var bg_move = bg_arr[chktype][checked]["onmouseover"];
			
			jQuery(obj).css("background-position", "0 "+bg_move);

		}
		

		jQuery("[dynamic_chktype]").mousedown(function(){
			dc_onmousedown(this);
		});
		jQuery("[dynamic_chktype]").click(function(e){
			dc_onmouseclick(this, e); 
		});
		jQuery("[dynamic_chktype]").hover(
			function(){
				dc_onmouseover(this);
			},
			function(){
				dc_onmouseout(this);
			}
		);
		
		
		jQuery("#nav_price_left,#nav_price_right").change(function(e){
		
			var pricefrom = jQuery("#nav_price_left").val();
			var priceto	= jQuery("#nav_price_right").val();
			var hccid = jQuery("#hccid").val();
			
			if(pricefrom == "" || priceto == ""){
				return false;
			}
			
			if(priceto != "inf" && parseInt(pricefrom) >= parseInt(priceto)){
				return false;
			}
			
			var ajaxurl = jQuery("#price_generic_link").val();
			ajaxurl = ajaxurl.replace("price_from=0", "price_from="+pricefrom);
			ajaxurl = ajaxurl.replace("price_to=inf", "price_to="+priceto);
			
			if(ajaxurl != ""){
				setNavigationUrl(ajaxurl);
			}
			
		});
		
		
		jQuery("[adyn]").click(function(e){
			jQuery(this).parent().prev().find("[dynamic_chktype]").click();
			e.preventDefault();
		});
		jQuery("[adyn]").mouseover(function(e){
			jQuery(this).parent().prev().find("[dynamic_chktype]").mouseover();
			e.preventDefault();
		});
		jQuery("[adyn]").mouseout(function(e){
			jQuery(this).parent().prev().find("[dynamic_chktype]").mouseout();
			e.preventDefault();
		});
		jQuery("[adyn]").mousedown(function(e){
			jQuery(this).parent().prev().find("[dynamic_chktype]").mousedown();
			e.preventDefault();
		});
		
		
		jQuery(".showmore").click(function(){
			jQuery(this).hide();
			jQuery(this).nextAll(".tohide").show(1500, function(){
				jQuery(this).nextAll(".showless").show();
			});
		});
		
		jQuery(".showless").click(function(){
			jQuery(this).hide();
			jQuery(this).prevAll(".tohide").hide(1500, function(){
				jQuery(this).prevAll(".showmore").show();
			});
		});
		
		
		load_ajax_by_urlhash();
	}
	
});

