/* * * * * * * * * * * * * * * * * * * * * * * *
 * Miscellaneous jQuery functions and the like *
 * * * * * * * * * * * * * * * * * * * * * * * */
var down = null;
var toggleSpeed = 200; // toggle speed in milliseconds
var visible = false;



function darkenPage() {
	if (jQuery.browser.msie) { // jQuery stuffs up IE filters, so reset these
		jQuery('#darken_page').css('-ms-filter', 'progid:DXImageTransform.Microsoft.Alpha(Opacity=90);');
		jQuery('#darken_page').css('filter', 'alpha(opacity=90);');
	}
	jQuery('#darken_page').stop().fadeIn(200);

	
	jQuery('img.fade').stop().animate({
        opacity: 1
    }, 250);
}

function lightenPage() {
	jQuery('#darken_page').stop().delay(400).fadeOut(200);

	jQuery('img.fade').stop().delay(400).animate({
        opacity: 0
    }, 250);

	
}

// hover over the menu
jQuery.fn.mmHover = function() {
	jQuery(this).hover(function() {
		if (!visible) {
			jQuery('.menu_dd').css('visibility', 'visible');
			jQuery('#menu-main-menu li ul').css('visibility', 'visible');
			visible = true;
		}
	},
	function() {
		lightenPage();
	});
}


// hover over a top level menu item with a dropdown
jQuery.fn.mmItemHoverDD = function() {
	var elementId; // get list item ID
	
	jQuery(this).hover(function() {
		elementId = jQuery(this).attr('id');
		jQuery(this).addClass('active');
	},
	function() {
		jQuery(this).removeClass('active');
	});
	
	jQuery(this).hoverIntent({    
	    sensitivity: 4, // number = sensitivity threshold (must be 1 or higher)    
	    interval: 100, // number = milliseconds for onMouseOver polling interval  
	    
		    over:function(){
		        	elementId = jQuery(this).attr('id');
					darkenPage();
					if (down != '#' + elementId) {
						if (down != null) {
							jQuery(down + ' .menu_dd').stop(true, true).hide('slide', { direction: 'up' }, toggleSpeed);
							jQuery(down).removeClass('active');
							jQuery('#' + elementId + ' .menu_dd').delay(toggleSpeed).show('slide', { direction: 'up' }, toggleSpeed);
							jQuery('#menu-main-menu .spotlight').stop(true, true).delay(200).animate({opacity:0.7}, 200).show();
						}
						else {
							jQuery('#' + elementId + ' .menu_dd').stop(true, true).delay(200).show('slide', { direction: 'up' }, toggleSpeed);
							jQuery('#menu-main-menu .spotlight').stop(true, true).animate({opacity:0.7}, 200).delay(200).animate({opacity:0}, 200).show();
						}
						jQuery(this).addClass('active');
						down = '#' + elementId;
					}
		    },
		    out: function(){
		       		jQuery('#menu-main-menu .spotlight').stop(true, true).animate({opacity:0.7}, 200).show();
					jQuery(down + ' .menu_dd').stop(true, true).delay(200).hide('slide', { direction: 'up' }, toggleSpeed);
					jQuery(down).removeClass('active');
					down = null;
		    }
	
	});
}

jQuery.fn.mmItemHover = function() {
	jQuery(this).hover(function() {
		if (down != null) {
			jQuery(down + ' .menu_dd').stop(true, true).delay(200).hide('slide', { direction: 'up' }, toggleSpeed);
			jQuery(down).removeClass('active');
		}
		lightenPage();
		down = null;
	});
}

// hover over a child menu item
jQuery.fn.mmChildItemHover = function() {
	var elementId;
	jQuery(this).hover(function() {
		elementId = jQuery(this).attr('id');
		jQuery('#menu-main-menu .spotlight').stop(true, true).animate({opacity:0.7}, 200).show();
		jQuery('#' + elementId + ' .spotlight').stop(true, true).animate({opacity:0}, 200).hide();
	}, function() {
		jQuery('#menu-main-menu .spotlight').stop(true, true).delay(50).animate({opacity:0}, 200);
	});
}

jQuery.fn.buttonSpotlight = function() {
	var elementId;
	jQuery(this).hover(function() {
		elementId = jQuery(this).attr('id');
		jQuery('#bottom_products .spotlight').stop(true, true).animate({opacity:0.7}, 300).show();
		jQuery('#' + elementId + ' .spotlight').stop(true, true).animate({opacity:0}, 300).hide();
	}, function() {
		jQuery('#bottom_products .spotlight').stop(true, true).delay(50).animate({opacity:0}, 300);
	});
}

jQuery.fn.listHoverify = function() {
	jQuery(this).hover(function() {
		jQuery('li', this).animate({
			opacity: 0.6
		}, 250);
	}, function() {
		jQuery('li', this).animate({
			opacity: 1
		}, 250);
	});
}

jQuery.fn.listHoverify2 = function() {
	var elemId = jQuery(this).attr('id');
	jQuery('li', this).hover(function() {
		jQuery('#' + elemId).toggleClass('inactive');
		Cufon.refresh('li.page_link_title a');
		Cufon.refresh('li.page_link_title span');
	});
}

jQuery(window).resize(function() {
	doResize();
});

function doResize() {
	// calculate width of orange bar pieces
	var window_width = jQuery(window).width();
	var bar_pieces_width = Math.ceil((window_width - 990) / 2);
	jQuery('#top_bar_left').width(bar_pieces_width);
	jQuery('#top_bar_right').width(bar_pieces_width);
	
	if (window_width <= 1054) {
		var diff = 1036 - window_width;
		if (diff < 0) diff = 0;
		else if (diff >= 50) diff = 50;
		jQuery('body').addClass('slimmer');
		jQuery('.post:not(.page-template-threecols-php .post)').css('marginLeft', '-' + diff + 'px');
		jQuery('#breadcrumbs').css('marginLeft', '-' + diff + 'px');
		jQuery('#breadcrumbs_bottom').css('marginLeft', '-' + diff + 'px');
		jQuery('.two_cols_left').css('width', 740 - diff + 'px');
		jQuery('.page-template-threecols-php .container_1080').css('marginLeft', '-' + diff + 'px');
		jQuery('.page-template-threecols-php .featured_image_wrap img').css('marginLeft', '-' + diff + 'px');
	}
	else {
		jQuery('body').removeClass('slimmer');
		jQuery('.post:not(.page-template-threecols-php .post)').css('marginLeft', '');
		jQuery('#breadcrumbs').css('marginLeft', '');
		jQuery('#breadcrumbs_bottom').css('marginLeft', '');
		jQuery('.two_cols_left').css('width', '740px');
		jQuery('.page-template-threecols-php .container_1080').css('marginLeft', '');
		jQuery('.page-template-threecols-php .featured_image_wrap img').css('marginLeft', '');
	}
	
}

jQuery.fn.productGridItem = function() {
	var hover = false;
	jQuery(this).hover(function() {
		if (!hover) {
			jQuery('img', this).hide("slide", { direction: "left" }, toggleSpeed);
			jQuery('h4', this).show("slide", { direction: "right" }, toggleSpeed);
			hover = true;
		}
	},
	function() {
		jQuery('img', this).stop(true, true).show("slide", { direction: "left" }, toggleSpeed);
		jQuery('h4', this).stop(true, true).hide("slide", { direction: "right" }, toggleSpeed);
		hover = false;
	});
}

jQuery.fn.productGrid = function() {
	var hover = false;
	jQuery('.product', this).hover(function() {
		if (!hover) {
			jQuery('img', this).hide("slide", { direction: "left" }, toggleSpeed);
			jQuery('h4', this).show("slide", { direction: "right" }, toggleSpeed);
			hover = true;
		}
	},
	function() {
		jQuery('img', this).stop(true, true).show("slide", { direction: "left" }, toggleSpeed);
		jQuery('h4', this).stop(true, true).hide("slide", { direction: "right" }, toggleSpeed);
		hover = false;
	});
}

var switch_current = 'overview';
jQuery.fn.switchTo = function() {
	jQuery(this).click(function() {
		elementId = jQuery(this).attr('id');
		if (elementId != switch_current) {
			jQuery('#switch_' + switch_current).fadeOut(300, function() {
				jQuery('#' + switch_current).removeClass('current');
				jQuery('#' + elementId).addClass('current');
				Cufon.refresh('#content_switch_menu li a');
				jQuery('#switch_' + elementId).fadeIn();
				switch_current = elementId;
			});
		}
		return false;
	});
}

/* turns an unordered list into a slideshow */
jQuery.fn.listSlideshowify = function(transitionDelay, transitionSpeed) {
	var elementId = jQuery(this).attr('id');
	var numSlides = jQuery('#' + elementId + ' li').size();
	var currentSlide = 0;
	var slides = jQuery('#' + elementId + ' li');
	
	// make the slideshow nav buttons
	var nav = '<ul id="slideshow_nav">';
	for (var i = 0; i < numSlides; i++) {
		if (i == 0) nav += '<li id="nav_' + i + '" class="slide_nav current"></li>';
		else nav += '<li id="nav_' + i + '" class="slide_nav"></li>';
	}
	nav += '</ul><div class="clearfix"></div>';
	jQuery(this).after(nav);
	
	// hide all but the first slide
	jQuery('#' + elementId + ' li:not(#slide_0)').each(function() {
		jQuery(this).hide();
	});
	
	// start the timer
	var timer = setTimeout(function() {
		gotoNextSlide();
	}, transitionDelay);
	
	jQuery('.slide_nav').click(function() {
		var slideNum = jQuery(this).attr('id').substring(4);
		gotoSlide(slideNum);
	});
	
	function gotoSlide(slideNum) {
		slideNum = parseInt(slideNum);
		if (slideNum != currentSlide) {
			clearTimeout(timer); // cancel timer
			// work out slide direction
			if (slideNum > currentSlide || (slideNum == 0 && currentSlide == numSlides - 1)) {
				var slideOutDirection = 'left';
				var slideInDirection = 'right';
			}
			else {
				var slideOutDirection = 'right';
				var slideInDirection = 'left';
			}
			// slide out old
			jQuery(slides[currentSlide]).hide('slide', { direction: slideOutDirection }, transitionSpeed); // slide out current
			// slide in new
			jQuery(slides[slideNum]).delay(transitionSpeed).show('slide', { direction: slideInDirection }, transitionSpeed); // slide in new
			// reset active class on nav
			jQuery('#slideshow_nav #nav_' + currentSlide).removeClass('current');
			jQuery('#slideshow_nav #nav_' + slideNum).addClass('current');
			currentSlide = slideNum;
			// reset timer
			timer = setTimeout(function() {
				gotoNextSlide();
			}, transitionDelay);
		}
	}
	
	function gotoNextSlide() {
		var nextSlideId = currentSlide + 1;
		if (nextSlideId >= numSlides) nextSlideId = 0;
		gotoSlide(nextSlideId);
	}
}

jQuery.fn.listPaginatify = function(itemsPerPage, transitionTime) {
	var elementId = jQuery(this).attr('id');
	var list = jQuery('#' + elementId + ' li');
	
	var numItems = list.size();
	
	jQuery('#commentlist').after('<h4 id="comments_prev" class="circle_arrow">Previous</p>');
	jQuery('#commentlist').after('<h4 id="comments_next" class="circle_arrow">Next</p>');
	jQuery('#comments_prev').hide();
	jQuery('#comments_next').hide();
	
	gotoPage(1);
	
	jQuery('#comments_next').click(function() {
		gotoPage(currentPage + 1);
	});
	jQuery('#comments_prev').click(function() {
		gotoPage(currentPage - 1);
	});
	
	function gotoPage(toPageNum) {
		var start = itemsPerPage * (toPageNum - 1);
		var end = start + itemsPerPage;
		list.fadeOut(transitionTime);
		for (var i = start; i < end; i++) {
			jQuery(list[i]).delay(transitionTime).fadeIn(transitionTime * 2);
		}
		if (toPageNum != 1) jQuery('#comments_prev').show();
		else jQuery('#comments_prev').hide();
		if (numItems - itemsPerPage * toPageNum > 0) jQuery('#comments_next').show();
		else jQuery('#comments_next').hide();
		currentPage = toPageNum;
	}
}

jQuery.fn.hideLastBcSeparator = function() {
	var elementId = jQuery(this).attr('id');
	var list = jQuery('#' + elementId + ' li');
	var numItems = list.size();
	jQuery(list[numItems - 2]).addClass('noseparator');
}






jQuery.fn.generalSpotlight = function() {
	jQuery('.general_spotlight').hover(function() {
		jQuery('.general_spotlight').not(this).each(function() {
			jQuery(this).stop(true, true);
			jQuery(this).animate({opacity: 0.4}, 200);
		});
	}, function() {
		jQuery('.general_spotlight').each(function() {
			jQuery(this).delay(50).animate({opacity: 1}, 200);
		});
	});
}


jQuery.fn.relatedSpotlight = function() {
	jQuery('#related_products .related_product').hover(function() {
		jQuery('#related_products .related_product').not(this).each(function() {
			jQuery(this).stop(true, true);
			jQuery(this).animate({opacity: 0.4}, 200);
		});
	}, function() {
		jQuery('#related_products .related_product').each(function() {
			jQuery(this).animate({opacity: 1}, 200);
		});
	});
}

jQuery.fn.equalHeightColumns = function() {
	var tallest = 0;

	jQuery(this).each(function() {
		if (jQuery(this).outerHeight(true) > tallest) {
			tallest = jQuery(this).outerHeight(true);
		}
	});

	jQuery(this).each(function() {
		var diff = 0;
		diff = tallest - jQuery(this).outerHeight(true);
		jQuery(this).height(jQuery(this).height() + diff);
	});
};




// initialise
jQuery(document).ready(function() {
	
	
		
	
	jQuery("#bottom_4 .button").hover(function(){
			jQuery("#bottom_4 .button").fadeTo("slow", 0.6); 
		},function(){
	   		jQuery("#bottom_4 .button").fadeTo("slow", 1); 
		});

	
	
	jQuery(".search-results .content_wrapper").click(function(){
	     window.location=jQuery(this).find("a").attr("href");
	     return false;
	});
	
	if (navigator.appVersion.indexOf("Mac") != -1 && jQuery.browser.safari) { // safari on mac
		jQuery('#searchLight').css('top', '20px');
	}
	
	

	jQuery('.related_product').equalHeightColumns();
	



	jQuery('img.fade').css('opacity', 0);
	jQuery('#bottom_products .spotlight').css('opacity', 0);
	
	if (!(jQuery.browser.msie && jQuery.browser.version.substr(0,1) < 7)) {
		jQuery('#mainmenu .sub-menu').wrap('<div class="menu_dd" />');

		jQuery('#mainmenu').mmHover();
		jQuery('ul#menu-main-menu li#menu-item-38').mmItemHoverDD();
		jQuery('ul#menu-main-menu li#menu-item-33').mmItemHoverDD();
		jQuery('ul#menu-main-menu li#menu-item-37').mmItemHover();
		jQuery('ul#menu-main-menu li#menu-item-36').mmItemHover();
		jQuery('ul#menu-main-menu li#menu-item-35').mmItemHover();
		jQuery('ul#menu-main-menu li#menu-item-34').mmItemHover();
		jQuery('ul#menu-main-menu li ul li').mmChildItemHover();
		
		//if (!(jQuery.browser.msie && jQuery.browser.version.substr(0,1) < 9)) {
			jQuery('#the_list').listHoverify();
			jQuery('#the_list2').listHoverify2();
		//}

		jQuery('#bottom_1').buttonSpotlight();
		jQuery('#bottom_2').buttonSpotlight();
		jQuery('#bottom_3').buttonSpotlight();
		jQuery('#related_products').relatedSpotlight();
		jQuery('.entry').generalSpotlight();
	

		jQuery('#overview').switchTo();
		jQuery('#ingredients').switchTo();
		jQuery('#application').switchTo();
		
		/*jQuery('#product_display_area .product').each(function(i) {
			jQuery(this).productGridItem();
		});*/
		jQuery('#product_display_area').productGrid();
		
		jQuery('#featured_products .product a').each(function(i) {
			jQuery(this).productGridItem();
		});
		jQuery('#product_landing td.product').each(function(i) {
			jQuery(this).productGridItem();
		});
		
		jQuery('#ingredients_slideshow').listSlideshowify(5000, 300);
		jQuery('#commentlist').listPaginatify(6, 300);
	}
	if (!(jQuery.browser.msie && jQuery.browser.version.substr(0,1) < 9)) {
		jQuery('.cnr_top_left').css('display', 'none');
		jQuery('.cnr_top_right').css('display', 'none');
		jQuery('.cnr_bottom_left').css('display', 'none');
		jQuery('.cnr_bottom_right').css('display', 'none');
	}
	
	Cufon.refresh('#switch_overview');
	
	jQuery('#breadcrumbs').hideLastBcSeparator();
	jQuery('#breadcrumbs_bottom').hideLastBcSeparator();
	
	doResize();
});
