// Small plugins not worth adding to external files

// hoverInvent
(function($){$.fn.hoverIntent=function(f,g){var cfg={sensitivity:7,interval:100,timeout:0};cfg=$.extend(cfg,g?{over:f,out:g}:f);var cX,cY,pX,pY;var track=function(ev){cX=ev.pageX;cY=ev.pageY};var compare=function(ev,ob){ob.hoverIntent_t=clearTimeout(ob.hoverIntent_t);if((Math.abs(pX-cX)+Math.abs(pY-cY))<cfg.sensitivity){$(ob).unbind("mousemove",track);ob.hoverIntent_s=1;return cfg.over.apply(ob,[ev])}else{pX=cX;pY=cY;ob.hoverIntent_t=setTimeout(function(){compare(ev,ob)},cfg.interval)}};var delay=function(ev,ob){ob.hoverIntent_t=clearTimeout(ob.hoverIntent_t);ob.hoverIntent_s=0;return cfg.out.apply(ob,[ev])};var handleHover=function(e){var ev=jQuery.extend({},e);var ob=this;if(ob.hoverIntent_t){ob.hoverIntent_t=clearTimeout(ob.hoverIntent_t)}if(e.type=="mouseenter"){pX=ev.pageX;pY=ev.pageY;$(ob).bind("mousemove",track);if(ob.hoverIntent_s!=1){ob.hoverIntent_t=setTimeout(function(){compare(ev,ob)},cfg.interval)}}else{$(ob).unbind("mousemove",track);if(ob.hoverIntent_s==1){ob.hoverIntent_t=setTimeout(function(){delay(ev,ob)},cfg.timeout)}}};return this.bind('mouseenter',handleHover).bind('mouseleave',handleHover)}})(jQuery);


// mobileBrowser Detection any mobile browser can be added to the first line with |browser agent|
(function(a){jQuery.browser.mobile=/android.+mobile|avantgo|bada\/|blackberry|iPad|android|blazer|compal|elaine|fennec|hiptop|iemobile|ip(hone|od)|iris|kindle|lge |maemo|midp|mmp|opera m(ob|in)i|palm( os)?|phone|p(ixi|re)\/|plucker|pocket|psp|symbian|treo|up\.(browser|link)|vodafone|wap|windows (ce|phone)|xda|xiino/i.test(a)||/1207|6310|6590|3gso|4thp|50[1-6]i|770s|802s|a wa|abac|ac(er|oo|s\-)|ai(ko|rn)|al(av|ca|co)|amoi|an(ex|ny|yw)|aptu|ar(ch|go)|as(te|us)|attw|au(di|\-m|r |s )|avan|be(ck|ll|nq)|bi(lb|rd)|bl(ac|az)|br(e|v)w|bumb|bw\-(n|u)|c55\/|capi|ccwa|cdm\-|cell|chtm|cldc|cmd\-|co(mp|nd)|craw|da(it|ll|ng)|dbte|dc\-s|devi|dica|dmob|do(c|p)o|ds(12|\-d)|el(49|ai)|em(l2|ul)|er(ic|k0)|esl8|ez([4-7]0|os|wa|ze)|fetc|fly(\-|_)|g1 u|g560|gene|gf\-5|g\-mo|go(\.w|od)|gr(ad|un)|haie|hcit|hd\-(m|p|t)|hei\-|hi(pt|ta)|hp( i|ip)|hs\-c|ht(c(\-| |_|a|g|p|s|t)|tp)|hu(aw|tc)|i\-(20|go|ma)|i230|iac( |\-|\/)|ibro|idea|ig01|ikom|im1k|inno|ipaq|iris|ja(t|v)a|jbro|jemu|jigs|kddi|keji|kgt( |\/)|klon|kpt |kwc\-|kyo(c|k)|le(no|xi)|lg( g|\/(k|l|u)|50|54|e\-|e\/|\-[a-w])|libw|lynx|m1\-w|m3ga|m50\/|ma(te|ui|xo)|mc(01|21|ca)|m\-cr|me(di|rc|ri)|mi(o8|oa|ts)|mmef|mo(01|02|bi|de|do|t(\-| |o|v)|zz)|mt(50|p1|v )|mwbp|mywa|n10[0-2]|n20[2-3]|n30(0|2)|n50(0|2|5)|n7(0(0|1)|10)|ne((c|m)\-|on|tf|wf|wg|wt)|nok(6|i)|nzph|o2im|op(ti|wv)|oran|owg1|p800|pan(a|d|t)|pdxg|pg(13|\-([1-8]|c))|phil|pire|pl(ay|uc)|pn\-2|po(ck|rt|se)|prox|psio|pt\-g|qa\-a|qc(07|12|21|32|60|\-[2-7]|i\-)|qtek|r380|r600|raks|rim9|ro(ve|zo)|s55\/|sa(ge|ma|mm|ms|ny|va)|sc(01|h\-|oo|p\-)|sdk\/|se(c(\-|0|1)|47|mc|nd|ri)|sgh\-|shar|sie(\-|m)|sk\-0|sl(45|id)|sm(al|ar|b3|it|t5)|so(ft|ny)|sp(01|h\-|v\-|v )|sy(01|mb)|t2(18|50)|t6(00|10|18)|ta(gt|lk)|tcl\-|tdg\-|tel(i|m)|tim\-|t\-mo|to(pl|sh)|ts(70|m\-|m3|m5)|tx\-9|up(\.b|g1|si)|utst|v400|v750|veri|vi(rg|te)|vk(40|5[0-3]|\-v)|vm40|voda|vulc|vx(52|53|60|61|70|80|81|83|85|98)|w3c(\-| )|webc|whit|wi(g |nc|nw)|wmlb|wonu|x700|xda(\-|2|g)|yas\-|your|zeto|zte\-/i.test(a.substr(0,4))})(navigator.userAgent||navigator.vendor||window.opera);



// functions called on document ready
function documentReady(){
	homePageElementsHeightWidth();
	productShadowHeight();
	showSearchBtn();
	navigationDropDown(80, 400, 80, 40);
	javaScriptTest();
	newLetterSignUp();
	
}

// functions called on windowload
function windowload(){
	homePageSlider();	
}

// THE DOCUMENT READY FUNCTIONS

function javaScriptTest(){
	jQuery('body').addClass('js');
	if (jQuery.browser.mobile){
		jQuery('body').addClass('mobile');
	}
}

function newLetterSignUp(){
	jQuery('#newsletter .section-one').show();
	jQuery('#newsletter .section-two').hide();
	jQuery('#newsletter .section-one').click(function() {
		jQuery(this).fadeOut(300, function() {
			jQuery('.section-two').fadeIn(300);
		});
	});
	
}

function homePageElementsHeightWidth() {
	if (jQuery('#homepage-content-wrapper').length > 0){

		var currentHighest=0;
	
		jQuery(".home-block .content").each(function () {
		    if ( jQuery(this).outerHeight() > currentHighest ) {
		        currentHighest=jQuery(this).outerHeight();          
		    }
		});
	
		jQuery(".home-block .content").each(function () {
					jQuery(this).height(currentHighest);
				});
	}
	
}

function navigationDropDown(openSpeed, closeSpeed, fadeSpeed, delay){
			// Setup all the styling
			function navigationInit(){
						jQuery('ul#nav li.level0').each(function (index){
								var DoYouHaveInnerLists = jQuery(this).children('ul').size();
					
									if(DoYouHaveInnerLists == 0){
										jQuery(this).children('a').addClass('noChildren');
									}
							});
					
							// runs at doc ready sets classes and heights general layout for the menu
								jQuery('ul#nav li.level0 ul').each(function (index){
									// grab the number of second level items -- with black headers
									var numberOfSecondInnerLists = jQuery(this).children('li').size();
									// grab the number of third level items -- with grey italic text
									var numberOfSecondInnerListsItems = jQuery(this).find('li ul li').size();
									// Add a class to the main containers which shows the number lists inside
									jQuery(this).addClass('lc' + numberOfSecondInnerLists );
									
									// Look to see is this only has one list inside if so add class
									if (numberOfSecondInnerLists <= 1){
										jQuery(this).addClass('HasOneColum');
											
											if (numberOfSecondInnerListsItems >= 6){
												jQuery(this).addClass('HasOneColumLong');
											}
									}
											// Set the height of each ul to it's highest sibling
											var t=0;
											var t_elem;
											jQuery(this).children('li.level1').each(function () {
												// jQuery will not find the height of a hidden object so this displays it quickly
												jQuery(this).parent('ul.level0').css({
												'position':'absolute','visibility':'hidden','display':'block'
												});
											    if (jQuery(this).outerHeight() > t ) {
											        t_elem=this;
											        t = jQuery(this).outerHeight();
											    }
											    jQuery(this).height(t);
												jQuery(this).parent('ul.level0').css({
												'position':'absolute','visibility':'visible','display':'none'
												}); 
											});
								});
			}
		
		// Call the initial styling 
		navigationInit();
		
		var gLastMenuHover = 0;
				
		// Gets called on hover with hoverIntent Plugin of each menu item does the fading and animation			
		function openDropDown() {
			
		
				var ghoverSpeedCal = 0;
				//get the height of the children menus of the item hovered
				var ulHeight = jQuery(this).children('#nav ul').outerHeight();
				//count how many sub menus in this item
				var numberOfInnerLists = jQuery(this).children('ul').size();
				var numberOfThridInnerLists = jQuery(this).children('ul').children('li').size();
				// Looks at the height it needs to slide to normalises the openSpeed
				var ghoverSpeedSum = Math.abs((ulHeight - ghoverSpeedCal)) + openSpeed;
					
					
					// Clear the last menu
					jQuery('#nav li.level0').removeClass('hover');
					jQuery('#nav ul.level0').hide();
					
					// fixing a bug in IE8 that stops fadeTo From fading children elements
						if (jQuery.browser.msie){
							if(jQuery.browser.version.substring(0, 2) == "8."){
								jQuery('#nav ul').hide();
								}
						}
								
				// check to see if the items has a nested menus
				if (numberOfInnerLists >= 1)
				{
					// Grab a store the li that was hovered
					var hoveredLi = jQuery(this);
					jQuery(this).addClass('hover');
					 jQuery(this).parents('.page').children('#nav-spacer');
					 if (ulHeight != 0){
					  		ghoverSpeedCal = ulHeight;
					  }
					  
					 
					 // fixing a bug in IE8 that stop fadeTo From fading children elements
					 if (jQuery.browser.msie){
					 	if(jQuery.browser.version.substring(0, 2) == "8.") {
						 setTimeout(function () {
						  			jQuery(hoveredLi).find('ul ul').stop().fadeTo(0, 1);
						  			jQuery(hoveredLi).children('ul').stop().fadeTo(0, 1);
						  			 
						  }, 100);
					  	}
					  }	
					  
					  
					 
					  function fadeInMenu() {
					  		jQuery(hoveredLi).children('ul').stop().delay(ghoverSpeedSum).fadeTo(fadeSpeed, 1);
					  }
					  
					   // Calling as a bind function after the animation produces a bug in webkit where the menus will flash when hovered over at the correct moment. So calling a named function in the callback 
						jQuery('#nav-spacer').stop().animate({
							height: ulHeight
							 },ghoverSpeedSum, fadeInMenu() );
							 					
				}
				
				else{
					// if the menu hovered has 0 items shrink the menu
					var ulHeight = 0;
							jQuery('ul.level0').hide();
							jQuery('#nav-spacer').stop().animate({height:'0px'},closeSpeed, function(){
							jQuery(this).removeClass('hover');
						});	
					}
					
					var gLastMenuHover = hoveredLi;						
		}
		
		// for when we hover off the menu altogether
		function closeDropDown(){
			jQuery('ul.level0').hide();
			jQuery('ul.level0').stop().fadeOut(fadeSpeed);
			jQuery('.level0').removeClass('hover');
			jQuery('#nav-spacer').stop().animate({height:'0px'},closeSpeed);												
		}
	
		// does nothing
		function donothing(){}
			
		// Lets see if we are dealing with a mobile or desktop browser and attach event handler for each
		if (jQuery.browser.mobile){	
				jQuery.fn.single_double_click = function(single_click_callback, double_click_callback, timeout) {
				  return this.each(function(){
				  
				    var clicks = 0, self = this;
				    jQuery(this).click(function(event){
				      clicks++;
				      var firstClick = true;
				      if (clicks == 1) {
				        setTimeout(function(){
				          if(clicks == 1) {
				            single_click_callback.call(self, event);
				          } 
				          else {
				            double_click_callback.call(self, event);
				          }
				          clicks = 0;
				        }, timeout || 1000);
				      }
				      
				      	// Prevents the first click for doing anything and sets up a timer for the second
				    	 if((jQuery(this).hasClass('noChildren')))
				     		  {}
				     		   
				     		   else {
				     		   if (clicks == 1) {
				     		   	 event.preventDefault();
				     		   	   setTimeout(function(){
				     		   	 clicks = 0;
				     		   	  }, timeout || 2000);}
				     		    }
				      }); 
				  });  
				}
		
				jQuery("#nav li.level-top").children('a').single_double_click(function() {
								jQuery(this) = jQuery(this).parent(li);
				}, function () {})
					jQuery("#nav li.level-top").click(openDropDown);
			}
				
			// run functions for Desktop browser
			else{		
					// config for the hover invent plugin
					var config = { 
						interval:delay,     
					     over: openDropDown, // function = onMouseOver callback (REQUIRED)    
					     timeout:240, // number = milliseconds delay before onMouseOut    
					     out:  donothing // function = onMouseOut callback (REQUIRED)   
					};
					
					var configTwo = {   
						interval: delay, 
					     over: donothing, // function = onMouseOver callback (REQUIRED)    
					     timeout: 300, // number = milliseconds delay before onMouseOut    
					     out: closeDropDown // function = onMouseOut callback (REQUIRED) 
					};
					jQuery("#nav li.level-top").hoverIntent(config);
					jQuery("#nav-wrapper").hoverIntent( configTwo);	
				}
		
}

function productShadowHeight() {
		var productInfoHeight = jQuery(".product-shop").height()-10;
		
		if (productInfoHeight >= 400)
			{
				jQuery(".product-view .product-img-box, #product-overlay").height(productInfoHeight);
			}
}

function showSearchBtn() {
	jQuery(".form-search input").focus(
	function () {
		jQuery('.form-search button').fadeIn(300);
	});	
	
	jQuery(".form-search input").blur(
	function () {
		setTimeout("jQuery('.form-search button').fadeOut(300);", 300);
	});	
}



// THE WINDOW LOAD FUNCTIONS

function homePageSlider(){
	jQuery('.flexslider').flexslider({
		animation: "slide",             //String: Select your animation type, "fade" or "slide"
		slideDirection: "horizontal",   //String: Select the sliding direction, "horizontal" or "vertical"
		slideshow: true,                //Boolean: Animate slider automatically
		slideshowSpeed: 7000,           //Integer: Set the speed of the slideshow cycling, in milliseconds
		animationDuration: 600,         //Integer: Set the speed of animations, in milliseconds
		directionNav: true,             //Boolean: Create navigation for previous/next navigation? (true/false)
		controlNav: true,               //Boolean: Create navigation for paging control of each clide? Note: Leave true for manualControls usage
		keyboardNav: true,              //Boolean: Allow slider navigating via keyboard left/right keys
		mousewheel: false,              //Boolean: Allow slider navigating via mousewheel
		prevText: "Previous",           //String: Set the text for the "previous" directionNav item
		nextText: "Next",               //String: Set the text for the "next" directionNav item
		pausePlay: false,               //Boolean: Create pause/play dynamic element
		pauseText: 'Pause',             //String: Set the text for the "pause" pausePlay item
		playText: 'Play',               //String: Set the text for the "play" pausePlay item
		randomize: false,               //Boolean: Randomize slide order
		slideToStart: 0,                //Integer: The slide that the slider should start on. Array notation (0 = first slide)
		animationLoop: true,            //Boolean: Should the animation loop? If false, directionNav will received "disable" classes at either end
		pauseOnAction: true,            //Boolean: Pause the slideshow when interacting with control elements, highly recommended.
		pauseOnHover: false,            //Boolean: Pause the slideshow when hovering over slider, then resume when no longer hovering
		controlsContainer: "",          //Selector: Declare which container the navigation elements should be appended too. Default container is the flexSlider element. Example use would be ".flexslider-container", "#container", etc. If the given element is not found, the default action will be taken.
		manualControls: "",             //Selector: Declare custom control navigation. Example would be ".flex-control-nav li" or "#tabs-nav li img", etc. The number of elements in your controlNav should match the number of slides/tabs.
		start: function(){},            //Callback: function(slider) - Fires when the slider loads the first slide
		before: function(){},           //Callback: function(slider) - Fires asynchronously with each slider animation
		after: function(){},            //Callback: function(slider) - Fires after each slider animation completes
		end: function(){}               //Callback: function(slider) - Fires when the slider reaches the last slide (asynchronous)
	});
}


	
