//main navigation
function header() {
	if ($.browser.msie && $.browser.version >= "8.0") {
		$(".search-box input.btnSearchSubmit").css({"min-width" : "29px"});
	}
}

//banner image carousel
function bannerCarousel() {

	if ($("#banner").length != "") {
		
		$("#bannerCount").append("<ul></ul>");
		
		$(".bannerItem").cycle({ 
			pager: "#bannerCount UL",
		    fx: "fade", 
		    speed: 2000, 
		    timeout: 7000,
		    // callback fn that creates a thumbnail to use as pager anchor 
		    pagerAnchorBuilder: function(idx, slide) { 
		        return '<li><a href="#" title="View slide ' + (idx + 1) + '">' + (idx + 1) + '</a></li>'; 
		    }
		});
		
		bannerControls();
		
	}
}

//show banner nav
function bannerControls() {	
	//carousel auto-starts so hide play button
	$("#bannerNavPlay").hide();
	//show banner controls & counter
	$("#bannerCount").show();
	$("#bannerNav").show();
	//pause button listener
	$("A#pause").click(function() {
		$(".bannerItem").cycle("pause");
		$("#bannerNavPause").hide();
		$("#bannerNavPlay").show();
	});
	//play button listener
	$("A#play").click(function() {
		$("#bannerNavPlay").hide();
		$("#bannerNavPause").show();
		$(".bannerItem").cycle("resume");
	});
}

function preloadImages(imgs){
	var picArr = [];
	for (i = 0; i<imgs.length; i++){
		picArr[i]= new Image(100,100); 
		picArr[i].src=imgs[i]; 
	}
}
	
//nav
function mainNav() {

	$("ul#nav > li > a.navArrow").click(function() {
		var iconId = $(this).attr("id");
		trayId = iconId.replace("Arrow", "Tray");
		liId = iconId.replace("Arrow", "Item");
//		alert(trayId);
		trayItem = $("div#" + trayId);
		liItem = $("LI#" + liId);
		if (trayItem.hasClass("open")) {
			closeNavTray(trayItem);
		}
		else {
			openNavTray(trayItem);
		}
	});
	
	function openNavTray(trayItem) {
		//Close any existing open item and mark as no longer open
		$("UL#nav LI.active").removeClass("active");
		openItem = $("div.navTray.open");
		if (openItem.length == 0) {
			//Open the selected item and mark as open
			trayItem.addClass("open");
			liItem.addClass("active");
			trayItem.slideDown(400);		
		}
		else {
			reOpenNavTray(trayItem, openItem);
		}
		
	}
	
	function reOpenNavTray(trayItem, openItem) {
		openItem.slideUp(400, function() {
			openItem.removeClass("open");
			trayItem.addClass("open");
			liItem.addClass("active");
			trayItem.slideDown(400);
		});
	}

	function closeNavTray(trayItem) {
		trayItem.stop(true,true).slideUp(400, function (){
			trayItem.removeClass("open");
			liItem.removeClass("active");
		});
	}

	function tweakHeights() {
		var heights= $("LI.active > DIV.navTray > UL > LI").map(function() {
		    return $(this).height();
		}).get();
//		alert(heights);
		if (Array.max(heights) <= 100) {
			$("LI.active > DIV.navTray > UL LI").css({"height" : "100px", "min-height" : "100px"});
			$("DIV#main-nav").animate({height:"200px", minHeight:"200px"});
		} else {
			$("LI.active > DIV.navTray > UL LI").css({"height" : Array.max(heights) + "px", "min-height" : Array.max(heights) + "px"});
			$("DIV#main-nav").animate({height: ((Array.max(heights) - 100) + 200) + "px", minHeight: ((Array.max(heights) - 100) + 200) + "px"});
		}
	}

}

Array.max = function(array){
    return Math.max.apply(Math,array);
};

Array.min = function( array ){
   return Math.min.apply( Math, array );
};

function centrePagination() {
	if ($(".pgPage").length != "") {
		var pgDivW = $(".pgPage").width();
		var pgUlW = $(".pgPage > UL").width();
		var offset = ((pgDivW - pgUlW) / 2);
		$(".pgPage > UL").css({"padding-left" : offset + "px"});
	}
}

//load common in-page functions
$(document).ready(function() {
	//site-wide
	header();
	bannerCarousel();
	mainNav();
	centrePagination();
	$("body").attr("id", "has-js");
});

