// Mosho Redirect for mobile clients
document.write(unescape("%3Cscript src='"+document.location.protocol+"//mrhat.mosho.co.uk/redirect.js' type='text/javascript'%3E%3C/script%3E"));
mosho_init();



google.setOnLoadCallback(function() {
	$(document).ready(function(){



		
		$('.wpsc_buy_button').click(function(){
			refreshQuantity();
			var productName = $(this).attr("product");
			
			pageTracker._trackEvent("Shopping Cart", "Add to cart", productName);
			console.log(productName);
		});
		
		$('.emptycart a').click(function(){
			$(this).hide();
			refreshQuantity();
			pageTracker._trackEvent("Shopping Cart", "Empty Cart");
/* 			console.log("Empty Cart"); */

		});
		
		$("#hatsabouttownlink").click(function(){
			pageTracker._trackPageview("/hats-about-town/email-link");
		});
		
		$('#box').click(function(){
			window.location.href='/hats/checkout/';
		});
	
	
/*
------------------ Thumbnail variation selector ---------------------------
*/	
		//Check if the variation element exists
		if($('div#variations').length){
			var productimage = $('.single_product_display img.product_image');  //Define product image element
			var on_load_current_colour = $(".wpsc_select_variation[name='variation[4]'] :selected").val();
			$("li.current img").parent("li").removeClass("current"); //remove current class from last current image
			$('li#hat' + on_load_current_colour).addClass("current"); //set current class on thumbnail

			var bigimage = $('div#variations ul li.current img').attr("src").replace("s.jpg","b.jpg"); //set initial product image from the current thumbnail on load
			productimage.attr("src",bigimage); //Set product image to the one from the first thumbnail
		}
		
		//bind hover function to thumbnails
		$('div#variations ul li img').hover(
		function(){
			var littleimage = $(this).attr("src"); //Get src of thumbnail
			var bigimage = littleimage.replace("s.jpg","b.jpg"); //prepare path of replaced big image
			productimage.attr("src",bigimage); //Replace product image
		},
		function(){
			//rollout function set big image back to the current one
			bigimage = $('div#variations ul li.current img').attr("src").replace("s.jpg","b.jpg");
			productimage.attr("src",bigimage);
		});
		
		//Click function
		$('div#variations ul li img').click(function(){
			$("li.current img").parent("li").removeClass("current"); //remove current class from last current image
			$(this).parent("li").addClass("current"); // Add current class to clicked thumbnail
			var selectval = $(this).parent("li").attr("id").replace("hat",""); // Get reference from thumbnail ID
			$(".wpsc_select_variation[name='variation[4]']").selectOptions(selectval, true); //Set option to the new ID
		});
		//selectbox change function
		$(".wpsc_select_variation[name='variation[4]']").change(function(){
			var on_change_current_colour = $(".wpsc_select_variation[name='variation[4]'] :selected").val();
			$("li.current img").parent("li").removeClass("current"); //remove current class from last current image
			$('li#hat' + on_change_current_colour).addClass("current"); //set current class on thumbnail
			bigimage = $('div#variations ul li.current img').attr("src").replace("s.jpg","b.jpg");
			productimage.attr("src",bigimage);
		});

		

/*
------------------ Sizing Assistance ---------------------------
*/		
		
		$("#sizeguide").hide();
		$("select[name='variation[2]']").parent().append('<a id="sizeHelp" href="#"><span id="shmore">Click to view the hat sizing guide</span></a>');
		$("#sizeHelp").click(function(){
			$("#sizeguide").slideToggle();
			pageTracker._trackEvent("Size Guide", "Show");
			return false;
		});
		
		var on_load_current_size = $(".wpsc_select_variation[name='variation[2]'] :selected").val();  //Find currently selected size (in select box)on page load
		$('#sizeguide table tr.cm td#s' + on_load_current_size).addClass("current"); //add current class to the corresponding TD
		
		$(".wpsc_select_variation[name='variation[2]']").change(function(){
			var on_change_current_size = $(".wpsc_select_variation[name='variation[2]'] :selected").val();
			$('#sizeguide table tr.cm td.current').removeClass("current");
			$('#sizeguide table tr.cm td#s' + on_change_current_size).addClass("current"); //add current class to the corresponding TD
		});


		var av_size_ids = []; //declare array of availabe size ids
		//traverse the selectbox for size variations
		$(".wpsc_select_variation[name='variation[2]']").children().each(function(i, option){
			var thisoneid =  $(option).val(); //get each size id that is available
			av_size_ids[i] = thisoneid; //add value to the array
			/* console.log(thisoneid); */
			$('#sizeguide table tr.cm td#s' + thisoneid).addClass("av"); //set available class on tds where the size is available
		});
		
		$('#sizeguide table tr.cm td.av').click(function(){
			$('td.current').removeClass("current"); //remove current class
			$(this).addClass("current"); // add current class to the clicked td
			var selectsize = $(this).attr("id").replace("s",""); // Get reference from clicked td ID
			$(".wpsc_select_variation[name='variation[2]']").selectOptions(selectsize, true); //Set option to the new ID
		});
		
	
		
	
	});

var emailsent = function(){
	$(document).ready(function(){	
		$("#contactForm").slideUp();
	});
}






	//Konami Code
	var kkeys = [], konami = "38,38,40,40,37,39,37,39,66,65";
	$(document).keydown(function(e) {
	  kkeys.push( e.keyCode );
	  if ( kkeys.toString().indexOf( konami ) >= 0 ){
	    $(document).unbind('keydown',arguments.callee);
	    	//Load in html from index ajax handler
	    	$.get("/",{ajax:"true",action:"offer"},
	        function(returned_data){
	         	var offer = returned_data;
	         	$('body').append(offer);
	    		//Wait a bit so bg can load before annimation starts
	         	$(this).delay(100, function(){
//	         		console.log('code entered');
	         		$('#eepromo').animate({ 
	        		top: "0"
    	  			}, 1500,'swing' );
    	  			pageTracker._trackEvent("Konami Promo", "Code entered");
	         	});
				
      			$('#eepromo #close').click(function(){
      				$(this).parent().fadeOut();
      				pageTracker._trackEvent("Konami Promo", "Close");
				});
	         });	   
	  }
	});



});
// Delay Plugin for jQuery
// - http://www.evanbot.com
// - © 2008 Evan Byrne

jQuery.fn.delay = function(time,func){
	this.each(function(){
		setTimeout(func,time);
	});
	
	return this;
};

jQuery.preloadCssImages = function(){
        var allImgs = [];//new array for all the image urls  
        var k = 0; //iterator for adding images
        var sheets = document.styleSheets;//array of stylesheets
        for(var i = 0; i<sheets .length; i++){//loop through each stylesheet
                var cssPile = '';//create large string of all css rules in sheet
                var csshref = (sheets[i].href) ? sheets[i].href : 'window.location.href';
                var baseURLarr = csshref.split('/');//split href at / to make array
                baseURLarr.pop();//remove file path from baseURL array
                var baseURL = baseURLarr.join('/');//create base url for the images in this sheet (css file's dir)
                if(baseURL!="") baseURL+='/'; //tack on a / if needed
                if(document.styleSheets[i].cssRules){//w3
                        var thisSheetRules = document.styleSheets[i].cssRules; //w3
                        for(var j = 0; j<thisSheetRules.length; j++){
                                cssPile+= thisSheetRules[j].cssText;
                        }
                }
                else {
                        cssPile+= document.styleSheets[i].cssText;
                }
                //parse cssPile for image urls and load them into the DOM
                var imgUrls = cssPile.match(/[^\(]+\.(gif|jpg|jpeg|png)/g);//reg ex to get a string of between a "(" and a ".filename"
                if(imgUrls != null && imgUrls.length>0 && imgUrls != ''){//loop array
                        var arr = jQuery.makeArray(imgUrls);//create array from regex obj        
                        jQuery(arr).each(function(){
                                allImgs[k] = new Image(); //new img obj
                                allImgs[k].src = (this[0] == '/' || this.match('http://')) ? this : baseURL + this;     //set src either absolute or rel to css dir
                                k++;
                        });
                }
        }//loop
        return allImgs;
} 



