/* Author: Digino */

(function($){ // Start: jQuery Loaded

init_PageContent(); // Comment this to disable Javascript across site
var ie = null;

function init_PageContent() {
	
	// Detect IE version:
	
	if ($('html').hasClass('ie6')) {
		ie = "6";
	} else if ($('html').hasClass('ie7')) {
		ie = "7";
	} else if ($('html').hasClass('ie8')) {
		ie = "8";
	} else if ($('html').hasClass('ie9')) {
		ie = "9";
	}
	
	// Generic site functions:
	
	$('a[href="#"]').click(function() { return false; });
	
	if (ie == null || ie != "6") { init_MegaMenu(); }
	init_BottleTicker();
	init_Cycler();
	
	// Page specific functions:
	// curSection defined on the relevant page
	
	if (typeof curSection != "undefined") {
	
		switch (curSection) {
			
			case("sect-performance"):
				init_MultiTabs();
				init_TabRotator();
				break;
				
			case("sect-sustainability"):
				init_FactsCycler();
				init_SustainProductCycle();
				break;
			
			case("sect-innovation"):
				init_FactsCycler();
				break;
			
			case("sect-whywellman"):
				init_Timeline();
				break;
				
			case("sect-products"):
				init_ProductQuickLinks();
				break;
		}
	
	}
	
	if (typeof vidToLoad != "undefined") {
		loadProdVideo(vidToLoad);
	}
	
} // init_PageContent


function init_MegaMenu() {
	
	var $navindex = $(".button-for-mega");
	var $megamenus = $(".mega-menu");
	var hoverIntent = null;
	var megaTimeOut = null;
	var megaMenuOver = null;
	
	$navindex.find('>a').hover(function () {
		clearTimeout(hoverIntent);
		clearInterval(megaTimeOut);
		var $myListItem = $(this).parent();
		var $myMegaMenu = $myListItem.find('.mega-menu');
		hoverIntent = setTimeout(function () {
			openMega($myListItem,$myMegaMenu);
		}, 300);
	}, function () {
		clearTimeout(hoverIntent);
		var $myListItem = $(this).parent();
		var $myMegaMenu = $myListItem.find('.mega-menu');
		megaTimeOut = setInterval(function () {
			if ($myMegaMenu.attr('id') != megaMenuOver) {
				clearInterval(megaTimeOut);
				closeMega();
			};
		}, 500);
	});
	
	function openMega($myListItem,$myMegaMenu) {
		$navindex.find('>a').removeClass("open-mega");
		$myListItem.find('>a').addClass("open-mega");
		$megamenus.fadeOut(100).filter($myMegaMenu).slideDown(200);
	};
	
	function closeMega() {
		$megamenus.fadeOut(100, function(){
			$navindex.find('>a').removeClass("open-mega");
		});
	};
	
	$megamenus.mouseenter(function() {
		megaMenuOver = $(this).attr('id');
	}).mouseleave(function() {
		megaMenuOver = null;
	});
	
	
	// Smart Fibre Sub-menu:
	$('.mega-menu-col1 a').hover(function () {
		var myID = $(this).attr('id').substr(4);
		$('.mega-menu-col1 a').removeClass('selected');
		$(this).addClass('selected');
		$('.mega-menu-col2').hide();
		$('#sub-'+myID).show();
	}, function () {
		//$(this).removeClass('selected');
	}).filter(':first').addClass('selected');
		
	if((navigator.userAgent.match(/iPhone/i)) || (navigator.userAgent.match(/iPod/i)) || (navigator.userAgent.match(/iPad/i))) {
		$(".button-for-mega>a").click(function(){
			//we just need to attach a click event listener to provoke iPhone/iPod/iPad's hover event.
			return false;
		});
	}
}


// Dropdown menu on Product Pages
function init_ProductQuickLinks() {
	
	$('.prod-drop').hover(function(){
		$('.hp-prod-list').slideDown(150);
	}, function(){
		$('.hp-prod-list').slideUp(150);
	});
	
}


var tabRotation // Interval used in init_TabRotator / Performance page.

// Allows multiple sets of tabs on a page
function init_MultiTabs() {
	
	$('.tabs').show();
	$("div.tab-region").each(
		function(intIndex) {
			
			var myTabContainers = $(this).find('div.tab-panel');
			var myTabs = $(this).find("ul.tabs");
			
			$(this).find('ul.tabs a').click(function () {
				myTabContainers.hide().filter(this.hash).show();
				$(this).parent().parent().find('a').removeClass('selected');
				$(this).addClass('selected');
				clearInterval(tabRotation); // Auto-rotator in performance page
				return false;
			}).filter(':first').click();
			
		}
	);
	
}

// Rotate tabbed panels automatically; use in conjunction with init_MultiTabs
function init_TabRotator() {
	
	var newSlide = 1;
	
	tabRotation = setInterval(function(){
									   
		var chosenTab = $('.performance-details>ul>li').filter(':nth-child('+(newSlide)+')');
		var myAnchor = chosenTab.find('a');
		var myHash = chosenTab.find('a').attr('href');
		
		$('div.tab-panel').hide().filter(myHash).show();
		chosenTab.parent().parent().find('a').removeClass('selected');
		myAnchor.addClass('selected');
		
		newSlide += 1;
		if (newSlide > 5) { newSlide = 1; }
		
	}, 3000);
	
}


function loadProdVideo(vidToLoad) {
	
	jwplayer("video").setup({
		flashplayer: "js/libs/jwplayer5.5/player.swf",
		file:  "videos/"+vidToLoad+".mp4",
		image: "images/products/main/"+vidToLoad+".jpg",
		autostart: true,
		width:  444,
		height: 248,
		controlbar: 'none',
		stretching: 'fill',
		repeat: 'always'
	});
	
}


function init_Cycler() {
	$('.slide-cycler').after('<div id="cycler-nav">').cycle({ 
		fx:			'fade',
		speed:		500,
		timeout:	4000,
		pause:		1,
		pager:  	'#cycler-nav'
	});
}


function init_FactsCycler() {
	$('.fact-cycler').after('<div id="fact-cycler-nav">').cycle({ 
		fx:			'scrollLeft',
		speed:		750,
		timeout:	7000,
		pause:		1,
		pager:  	'#fact-cycler-nav'
	});
}


function init_BottleTicker() {
	
	var tickerElement = $("#bottle-ticker");
	
	function tickerIncrement(i) {
		tickerElement.flipCounter({
			number:i,
			numIntegralDigits:6,
			digitHeight:33,
			digitWidth:25,
			imagePath:"images/ui/bottle-ticker-numbers.gif"
		});
	}
	
	var i = 0,
		bottleTicker = setInterval(function(){
		i += 52;
		tickerIncrement(i);
	}, 1000);
	
	$('.bottles-recycled-ticker').show();
	tickerIncrement(0);
	
}


function init_Timeline() {
	
	$(".timeline").jCarouselLite({
		btnNext: ".next",
		btnPrev: ".back",
		circular: true,
		scroll: 1,
		visible: 4,
		auto: 2000,
		hoverPause: 1
	});
	
}


function init_SustainProductCycle() {
	
	$('.product-cycle').show();
	
	var $descEl = $('.pc-desc li');
	
	var auto = 1,
		prodCycleRotate = setInterval(function(){
			if (auto == 1) {
				switchSlide();
			};
		}, 10000);
	
	
	$('.pc-nav li a').click(function(el){
		var $myLi = $(this).parent();
		var myNum = $myLi.index();
		auto = 0;
		switchSlide(myNum);
	});
	
	var curSlide = 0;
	
	function switchSlide(newSlide) {
		if (newSlide == undefined) {
			var newSlide = curSlide + 1;
			if (newSlide == 3) { newSlide = 0 };
		}
		
		var newPos = -(newSlide * 402);
		$('.pc-img ul').animate({left:newPos},750);
		
		var newBgPos = -(newSlide * 51);
		$('.pc-nav').css({backgroundPosition: '0 '+newBgPos+'px'});
		$('.pc-nav li a').removeClass('selected')
		$('.pc-nav li:nth-child('+(newSlide+1)+') a').addClass('selected');
		
		$descEl.hide().fadeOut('normal').filter('.pc-desc li:nth-child('+(newSlide+1)+')').fadeIn('fast');
		
		curSlide = newSlide;
	}
	
	$descEl.filter(':first').show();
	
}



})(this.jQuery); // End: jQuery Loaded

