jQuery(document).ready(function() { // Run scripts once jQuery and DOM have loaded

// ********************************************************************************************** //
// Determine Functions Relevant to Section to Initialise


// Functions to Load at Start:
$(function(){
	
	$('a[rel="ext"]').attr('target', 'adi2');
	$('a[rel="nofollow"]').attr('target', 'adi2');
	$("a[href='#']").click(function() { return false; });
	
	// Internal Page Scrolling with Inertia:
	$('a[href^="#sub-"]').click(function(){
		var scrollToID = "" + $(this).attr("href");
		$.scrollTo('#'+scrollToID, 200, {offset: {top:-30, left:0}});
		return false;
	});
	
		// Init Navigation / Footer
	init_MainNav();
	init_MegaMenus();
	init_Footer_Social_Rollovers();
	init_Footer_News_Ticker();
	
	switch (curSection) {
		
		case("sect-home"):
			init_HomeSectorScroller();
			init_NewsTickerPanel();
			break;
			
		case("sect-products"):
			init_Gallery();
			init_Tabs();
			break;
			
		case("sect-casestudy-home"):
			init_CS_Selector();
			init_CaseStudyScroller();
			break;
			
		case("sect-casestudy"):
			$('a[rel*="lightbox"]').lightbox();
			break;
			
		case("sect-international"):
			init_OfficeRollovers();
			break;
			
		case("sect-contact"):
			init_DatePicker();
			break;
			
	}
	
	init_ZebraStriping();				// Used on modular/mobile camparison page
	$('.floatyheader').floatyHead();	// Used on modular/mobile camparison page
	$('.cycle').cycle({fx:'fade', speed: 500});
	init_About_Screen_Res_Accordion();  // Used on about screen res page
	
});



// ********************************************************************************************** //
// Site Frame: Main Menu
// ********************************************************************************************** //

function init_MainNav() {
	
	var type = $('body').attr('class').substr(5);
	
	$("ul.nav-core>li>a").click(function(){
		var $myLI = $(this).parent();
		$("ul.nav-core ul").hide();
		$("ul.nav-core>li").each(function() {
			$(this).removeClass("nav-core-active");
		});
		$myLI.addClass("nav-core-active");
		if ($myLI.attr("id") == "nav-sales")  { $("#nav-sales ul").show(); };
		if ($myLI.attr("id") == "nav-rental") { $("#nav-rental ul").show(); };
		return false;
	});
	$('.button-for-mega a').append('<img src="../images/common/arrow-white-menu-down.gif">');
	
};


// ********************************************************************************************** //
// Site Frame: Mega Menus
// ********************************************************************************************** //

function init_MegaMenus() {
	
	var $navindex = $("li.button-for-mega");
	var $megamenus = $(".dropdown");
	var hoverIntent = null;
	var megaTimeOut = null;
	var megaMenuOver = null;
	
	// Define Page Description Rollovers
	$('.page-description').hide();
	$(".product-view li a").mouseover(function(){
		var myID = $(this).attr("id").substr(0,8);
		$('.page-description').hide();
		$('#'+myID+'-desc').show();
	});
	
	$navindex.find('a').hover(function () {
		clearTimeout(hoverIntent);
		clearInterval(megaTimeOut);
		var $el = $(this).parent();
		hoverIntent = setTimeout(function () {
			openMega($el);
		}, 300);
	}, function () {
		clearTimeout(hoverIntent);
		var $el = $(this).parent();
		var anchorName = $el.attr("id").substr(4);
		megaTimeOut = setInterval(function () {
			if (megaMenuOver != anchorName) {
				clearInterval(megaTimeOut);
				closeMega($el);
			};
		}, 500);
	});
	
	function openMega($el) {
		megaMenuOver = null;
		var anchorName = $el.attr("id").substr(4);
		$navindex.removeClass("nav-sub-selected").filter($el.addClass("nav-sub-selected"));
		$megamenus.fadeOut(100).filter("#dropdown-"+anchorName).slideDown(200);
		$('.page-description').hide();
		$('#dropdown-' + anchorName + ' .page-description:first').show();
	};
	
	function closeMega($el) {
		$megamenus.fadeOut(100);
		$navindex.removeClass("nav-sub-selected");
	};
	
	$megamenus.mouseenter(function() {
		megaMenuOver = $(this).attr("id").substr(9);
	}).mouseleave(function() {
		megaMenuOver = null;
	});
	
	//$("#nav-r-solutions").addClass("nav-sub-selected");
	//openMega($("#nav-r-solutions"));
	
	if((navigator.userAgent.match(/iPhone/i)) || (navigator.userAgent.match(/iPod/i)) || (navigator.userAgent.match(/iPad/i))) {
		$("li.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;
		});
	}
	
};



// ********************************************************************************************** //
// Footer: News Ticker
// ********************************************************************************************** //

function init_Footer_News_Ticker() {
	
	var currentNewsItem		= 0;
	var previousNewsItem	= 0;
	var ticker_State		= 1;
	var ticker_Animating	= 0;
	var ticker_Speed		= 4000;
	var ticker_Num_Of_Items	= 7; // Starts from 0
	
	// Advance Ticker Item:
	function newsTicker_UpdateItem(direction) {
		
		if (ticker_Animating == 0) {
			
			previousNewsItem = currentNewsItem;
			currentNewsItem += direction;
			
			if (currentNewsItem > ticker_Num_Of_Items){
				currentNewsItem = 0;
			} else if (currentNewsItem < 0){
				currentNewsItem = ticker_Num_Of_Items;
			};
			
			ticker_Animating = 1;
			$("#newsitem"+previousNewsItem).fadeOut('fast', function(){
				$("#newsitem"+currentNewsItem).fadeIn('normal', function(){
					ticker_Animating = 0;
				});
			});
		};
	};
	
	// Ticker auto-play:
	function newsTicker_Play() {
		newsTimeOut = setInterval(function () {
			newsTicker_UpdateItem(1);
		}, ticker_Speed);
	};
	
	// Pause on Hover:
	$(".news-ticker").hover(function(){
		clearInterval(newsTimeOut);
		$(this).addClass("news-ticker-hover");
	}, function(){
		$(this).removeClass("news-ticker-hover");
		if(ticker_State == 1){
			newsTicker_Play();
		};
	});
	
	// Ticker Controls:
	$("#ticker-playpause a").click(function(){
		if (ticker_State == 1){
			ticker_State = 0;
			clearInterval(newsTimeOut);
			$(this).removeClass("ticker-pause").addClass("ticker-play");
		} else {
			ticker_State = 1;
			$(this).removeClass("ticker-play").addClass("ticker-pause");
		};
	});
	$("#ticker-previous a").click(function(){ newsTicker_UpdateItem(-1); });
	$("#ticker-next a")    .click(function(){ newsTicker_UpdateItem(1); });
	
	// Auto-start Ticker:
	$(".news-ticker-controls").show();
	newsTicker_Play();

};



// ********************************************************************************************** //
// Footer: Social Networking Icons
// ********************************************************************************************** //

function init_Footer_Social_Rollovers() {
	
	var titleStore = null;
	
	$('.social-links a').css('opacity',0.8)
						.hover(function(){
							titleStore = $(this).attr('title');
							$(this).css('opacity',1);
							$(this).attr('title','');
							$('.social-links p').text(titleStore);
						}, function(){
							$(this).css('opacity',0.8);
							$('.social-links p').text('');
							$(this).attr('title',titleStore);
							titleStore = null;
						});
	$('.social-links').append('<p></p>');
	
	$('.social-share a').css('opacity',0.8)
						.hover(function(){
							titleStore = $(this).attr('title');
							$(this).css('opacity',1);
							$(this).attr('title','');
							$('.social-share p').text(titleStore);
						}, function(){
							$(this).css('opacity',0.8);
							$('.social-share p').text('');
							$(this).attr('title',titleStore);
							titleStore = null;
						});
	$('.social-share').append('<p></p>');
	
};



// ********************************************************************************************** //
// Index Page / Homepages: News Ticker Panel
// ********************************************************************************************** //

function init_NewsTickerPanel() {
	
	$(".feed-tray").jCarouselLite({
		btnNext: ".next",
		btnPrev: ".prev",
		vertical: true,
		auto: 2000,
		speed: 800,
		visible: 2,
		hoverPause: 1
	});
	
}



// ********************************************************************************************** //
// Homepages: Main Slider
// ********************************************************************************************** //

function init_HomeSectorScroller() {
	
	var timeChange = 8000;
	if ( $(".type-rental").length > 0 ) {
		timeChange = null;
	}
	
	$(".hp-sector li").css({'display':'block'});
	$(".hp-sector").jCarouselLite({
			btnGo: [".0",".1",".2",".3",".4",".5",".6",".7",".8"],
			auto: timeChange,
			speed: 800,
			visible: 1,
			hoverPause: 1,
			afterEnd: function(slideshow) {
				$('.home-sector-menu a').removeClass('selected');
				$('.home-introtext p.prod-link a').removeClass('selected');
				var myNum = $(slideshow[0]).index();
				switch (myNum) {
					case(1):
						$('.home-introtext p.prod-link a').addClass('selected');
						$('.home-sector-menu a.0').addClass('selected');
						break;
					case(2):
						$('.home-sector-menu a.1').addClass('selected');
						break;
					case(3):
						$('.home-sector-menu a.2').addClass('selected');
						break;
					case(4):
						$('.home-sector-menu a.3').addClass('selected');
						break;
					case(5):
						$('.home-sector-menu a.4').addClass('selected');
						break;
					case(6):
						$('.home-sector-menu a.5').addClass('selected');
						break;
					case(7):
						$('.home-sector-menu a.6').addClass('selected');
						break;
					case(8):
						$('.home-sector-menu a.7').addClass('selected');
						break;
					case(9):
						$('.home-sector-menu a.8').addClass('selected');
						break;
				}
			}
	});
	
}



// ********************************************************************************************** //
// Hire Case Study Homepage: Main Slider
// ********************************************************************************************** //

function init_CaseStudyScroller() {
	
	$(".feature-image").jCarouselLite({
			btnGo: [".1",".2",".3",".4",".5"],
			auto: 4000,
			speed: 800,
			visible: 1,
			hoverPause: 1,
			afterEnd: function(slideshow) {
				$('.feature-menu a').removeClass('selected');
				$('.feature-text').hide();
				switch (slideshow[0].id) {
					case("feature1"):
						$('.feature-menu a.1').addClass('selected');
						$('#feature-text1').show();
						break;
					case("feature2"):
						$('.feature-menu a.2').addClass('selected');
						$('#feature-text2').show();
						break;
					case("feature3"):
						$('.feature-menu a.3').addClass('selected');
						$('#feature-text3').show();
						break;
					case("feature4"):
						$('.feature-menu a.4').addClass('selected');
						$('#feature-text4').show();
						break;
					case("feature5"):
						$('.feature-menu a.5').addClass('selected');
						$('#feature-text5').show();
						break;
				}
				
			}
	});
	
	$('.feature-text').hide().filter(':first').show();
	
}



// ********************************************************************************************** //
// Hire Case Study Homepage: Case Study Matrix Selector
// ********************************************************************************************** //

function init_CS_Selector() {
	
	$('div.study-selector a').click(function() {
		
		$('div.case-overview a').removeClass('selected');
		$('div.study-selector a').removeClass('selected');
		
		var prod = $(this).attr('class');
		
		$('div.case-overview a').each(function() {
			if ($(this).hasClass(prod)) {
				$(this).addClass('selected');
			}
		});
		
		return false;
	});
	
}



// ********************************************************************************************** //
// International: Office Rollovers
// ********************************************************************************************** //

function init_OfficeRollovers() {
	
	$('div.offices-address').hide();
	
	$('div.offices-box').each(
		function(intIndex) {
			
			$(this).find('h3 a').click(function() { return false; });
			var myRollo = $(this);
			$(this).find('h3 a').hover(
				function() { myRollo.find('div.offices-address').slideDown('fast'); },
				function() {}
			);
			myRollo.hover(
				function() {},
				function() { myRollo.find('div.offices-address').slideUp('fast');
			});
		}
	);
	
}



// ********************************************************************************************** //
// Contact Forms: Date Picker
// ********************************************************************************************** //

function init_DatePicker() {
	Date.format = 'dd/mm/yyyy';
	$(".dateinput").datepicker({ dateFormat: 'dd-mm-yy', firstDay: 1 })
}



// ********************************************************************************************** //
// General: New-style Gallery; allows multiple galleries on one page
// ********************************************************************************************** //

function init_Gallery() {
	
	$('.main-image').each(function(i){
		var myPhotoSet = 'photoset'+i;
		$(this).parent().attr('id',myPhotoSet);
		$(this).cycle({ 
			fx:			'fade',
			speed:		500,
			timeout:	0,
			pause:		1,
			pager:  	'#' + myPhotoSet + ' .thumbs', 
			pagerAnchorBuilder: function(idx, slide) { 
				return '#' + myPhotoSet + ' .thumbs a:eq(' + idx + ')'; 
			}
		});
	});
	$('.thumbs img')
		.fadeTo('fast',0.5)
		.hover(function(){
			$(this).fadeTo('fast',1);
		}, function() {
			$(this).fadeTo('slow',0.5);
		});
}



// ********************************************************************************************** //
// General: Page Tabs (Specifically used on ESP Page)
// ********************************************************************************************** //

function init_Tabs() {
	
	$("div.tab-region").each(
		function(intIndex) {
			
			var myTabContainers = $(this).find('div.tab-panel');
			var myTabs = $(this).find("ul.tabs");
			
			//myTabs.show();
			$(this).find('h3.non-tabbed').hide();
			
			$(this).find('ul.tabs a').click(function () {
				myTabContainers.hide().filter(this.hash).show();
				$(this).parent().parent().find('a').removeClass('tab-current');
				$(this).addClass('tab-current');
				return false;
			}).filter('.tab-current').click();
			
		}
	);
	
}

// ********************************************************************************************** //
// General: Zebra Striping (Specifically used on Modular vs Mobile)
// ********************************************************************************************** //

function init_ZebraStriping() {
	$('.zebra').children(':even').addClass('alt');
}


// ********************************************************************************************** //
// General: Accordion (Specifically used on About Screen Res page)
// ********************************************************************************************** //

function init_About_Screen_Res_Accordion()
{
	$('.accordion-content').hide();
	
	
	$('.accordion h3 a').click(
		function() {
			
			var checkLink = $(this);
			var checkLinkLI = $(this).parent().parent();
			var checkElement = $(this).parent().next();
			
			$('.accordion li').removeClass('selected');
			
			if((checkElement.is('div')) && (checkElement.is(':visible'))) {
				$('.accordion-content:visible').slideUp('normal');
				return false;
			}
			
			if((checkElement.is('div')) && (!checkElement.is(':visible'))) {
				$('.accordion-content:visible').slideUp('normal');
				checkElement.slideDown('normal');
				checkLinkLI.addClass('selected');
				
				if (checkLinkLI.index() == 2) {
					$('#pixel-ani3-cycle').cycle('stop').cycle({fx:'fade', pause: 0, timeout: 3000, speed: 1000, next: '#pixel-ani3-cycle'});
				} else if (checkLinkLI.index() == 3) {
					$('#pixel-ani4-cycle').cycle('stop').cycle({fx:'fade', pause: 0, timeout: 3000, speed: 1000, next: '#pixel-ani4-cycle'});;
				} else if (checkLinkLI.index() == 4) {
					$('#pixel-ani5-cycle').cycle('stop').cycle({fx:'fade', pause: 0, timeout: 3000, speed: 1000, next: '#pixel-ani5-cycle'});;
				} else if (checkLinkLI.index() == 5) {
					$('#pixel-ani6-cycle').cycle('stop').cycle({fx:'fade', pause: 0, timeout: 3000, speed: 1000, next: '#pixel-ani6-cycle'});;
				}
				return false;
			}
			
			return false;
			
		}
		
		
	).filter(':first').click();
	
	
}




}); // End Document Ready
