/*
//
//	Author:				Mark Rowntree 
//	filename:			/js/main.js
//	function:			The core functions that are used on the MagnoliaSoft Marks and Co. microsite
//	usage:				<script src="/js/main.js" type="" language="JavaScript"></script>
//	created date:		30/04/2009
//	update history:		[dd/mm/yyyy / developer name: description of update, including referencing project | issue | cr number]		
//
*/
	document.observe("dom:loaded", function() {
		renderHPMainImage();
		if(typeof initLightbox == 'function'){
			initLightbox();
		}
		$$('ul#hdr_nav_ul')[0].select('li.menu').each(function(elem){
			var s_id = elem.identify().replace("_ul","");
			//does it have a sub menu to display?
			if($(elem).select("div#" + s_id + "_l2_nav").length){
				//bind an onlick action to the arrow button
				elem.select('span.hdr_nav_button')[0].observe("click", menu_handler.bindAsEventListener(this));
				//add the cursor mouseover class to this element
				elem.select('span.hdr_nav_button')[0].addClassName('mouseOver');
				hdr_nav_array.push(s_id);
			}
		});
		if(document.location.search){
			var a_qs = document.location.search.toQueryParams();
			for(param in a_qs){
				if(param == "prodId"){
					selectProduct(a_qs[param]);
				}
			}
		}
	});

	//fadeInterval is for the main image swapping
	var switchImage_timeout; //major timeout
	var fadeInterval = 10000;
	var fadeDelta = 100;
	var globalTimer = new Array();
	var closeTimeout = 10000;
	
	//the following are for the nav drop down
	var hdr_nav_array = [];
	var interval = 40;
	var delta = 20;
	//pre-cache the hp images
	var a_cacheImages = new Array();
	//create an array of images to cache
	var a_theImages = ["images/main/main_img_1.jpg","images/main/main_img_1.jpg"];
	var i_hp_images = a_theImages.length;

	function cacheImages(s_IMGpath){
		if(typeof s_IMGpath != "undefined" && s_IMGpath != ""){
			var i_cacheImages = a_cacheImages.length;
			a_cacheImages[i_cacheImages] = new Image();
			a_cacheImages[i_cacheImages].src = s_IMGpath;
		}
	}
	//now the cacheImages function has loaded, cache the base images
	for(var i_IMG = 0; i_IMG < i_hp_images; i_IMG++){
		cacheImages(a_theImages[i_IMG]);
	}
	
	function inArray(this_array,this_value){
		s_return = false;
		if((typeof this_array != "undefined" && this_array.length) && (typeof this_value != "undefined" && this_value.length)){
			for(var i = 0; i < this_array.length; i++){
				if(this_array[i] == this_value){
					s_return = true;
					break;
				}
			}
		}
		return s_return;
	}
	
	function menu_handler(e){
		//identify the child div for this element
		var thisID = Event.findElement(e,'li').identify();		
		//iterate over the hdr_nav_array array to ensure that the event is from an legitimate element
		if(inArray(hdr_nav_array,thisID)){
			Event.stop(e);
			//are the other sub menus open? If so, close them
			$$(thisID).each(function(elem){
				itemID = $(elem).identify();
				var divObjs = $(itemID).select('div');
				var ulObjs = $(itemID).select('ul');			
				var divObj = divObjs[0];
				var ulObj = ulObjs[0];			
				bIsDisplay = ($(divObj).getStyle('display') == "none") ? 0 : 1 ;
				if((itemID != thisID) && (bIsDisplay)){
					control_menu(itemID);
				}
			});
			//open this sub menu
			control_menu(thisID);
		}
	}
	
	function control_menu(thisID){
		if(typeof thisID != "undefined" && thisID != ""){
			var divObjs = $(thisID).select('div');
			var ulObjs = $(thisID).select('ul');			
			var divObj = divObjs[0];
			var ulObj = ulObjs[0];
			var bDivIsDisplay = ($(divObj).getStyle('display') == "none") ? 0 : 1 ;			
			var objId = $(ulObj).identify();
			if(bDivIsDisplay){
				//collapse
				clearAnInterval(thisID,"show");
				clearAnInterval(thisID,"hide");
				window["interval_" + thisID + "_hide"] = setInterval("accordion('" + thisID + "','" + objId + "','collapse');",interval);
			}else{
				//expand
				clearAnInterval(thisID,"show");
				clearAnInterval(thisID,"hide");
				window["interval_" + thisID + "_show"] = setInterval("accordion('" + thisID + "','" + objId + "','expand');",interval);
				setTimeout('window["interval_' + thisID + '_hide"] = setInterval("accordion(\'' + thisID + '\',\'' + objId + '\',\'collapse\');",' + interval + ');',closeTimeout);
			}
		}
	}
	
	function accordion(parentId,elId,type){
		if(((typeof parentId != "undefined") && (parentId != "")) && ((typeof elId != "undefined") && (elId != "") && $(elId)) && ((typeof type != "undefined") && (type != ""))){			
			//calculate the total height of the UL
			//var ulHeight = 0;
			var ulHeight = 10; //the div is offset down by 5 pixels to be below the tab
			var ulObj = $$('ul#' + elId)[0];
			var divObj = $$('div#' + elId.replace("_ul",""))[0];
			if((type == "expand") && ($(divObj).getStyle('display') != "block")){
				$(divObj).show();
				$(ulObj).show();
			}
			$(ulObj).select('li').each(function(elem){
				ulHeight += parseInt($(elem).getStyle('height'));
			});
			var divHeight = parseInt($(divObj).getHeight());
			if(type == "expand"){
				if(divHeight < ulHeight - delta){
					//increase the height of the div
					$(divObj).setStyle({'height': (divHeight + delta) + "px"});
				}else{
					//decrease the height of the div
					$(divObj).setStyle({'height': ulHeight + "px"});
					clearAnInterval(parentId,"show");
				}
			}else{
				if(divHeight >= 0 + delta){					
					//decrease the height of the div
					$(divObj).setStyle({'height': (divHeight - delta) + "px"});
				}else{
					$(divObj).setStyle({'height': "0px"});
					$(divObj).hide();
					$(ulObj).hide();					
					clearAnInterval(parentId,"hide");
				}
			}			
		}
	}
	
	function clearAnInterval(elId,type){
		if(((typeof elId != "undefined") && (elId != "") && $(elId)) && ((typeof type != "undefined") && (type != "")) && (typeof window["interval_" + elId + "_" + type] != "undefined")){
			clearInterval(window["interval_" + elId + "_" + type]);
		}
	}
	
	function renderHPMainImage(){
		if($$('DIV#main_img') && $$('DIV#main_img')[0] && $$('DIV#main_img')[0].select('ul')[0].select('LI').length){
			//set up the interval
			if(typeof switchImage_interval == "undefined"){
				switchImage_timeout = setTimeout("switchImage('DIV#main_img');",fadeInterval);
			}
		}
	}
	
	function switchImage(div_id){
		if((typeof div_id != "undefined" && div_id != "") && ($$(div_id) && $$(div_id)[0].select('ul')[0].select('LI').length)){
			//clear the major timeout so that they don't clash! This is purely for the sake of being absolutely certain there's no clash
			clearTimeout(switchImage_timeout);
			var o_li = $$(div_id)[0].select('ul')[0].select('LI');
			var i_li = o_li.length;
			//only one li should be visible, fade it out, and fade the next one in
			var i_current = "";
			o_li.each(function(this_li,this_index){
				if($(this_li).getStyle('display') == "block"){
					i_current = this_index + 1;
				}
			});
			//which LI to fade in?
			i_fadeOut = parseInt(i_current);
			i_fadeIn = (i_fadeOut == i_li) ? 1 : i_fadeOut + 1 ;
			globalTimer.fade = setTimeout("fade('" + div_id + "'," + i_fadeIn + "," + i_fadeOut + ")",fadeDelta);
		}
	}
	
	function fade(div_id,fadeIn,fadeOut){
		if((typeof div_id != "undefined" && div_id != "") && (typeof fadeIn != "undefined" && fadeIn != "") && (typeof fadeOut != "undefined" && fadeOut != "")){
			var o_li = $$(div_id)[0].select('ul')[0].select('LI');
			var i_li = o_li.length;
			var fadeInOpacity = $(o_li)[fadeIn - 1].getStyle('opacity');
			var fadeOutOpacity = $(o_li)[fadeOut - 1].getStyle('opacity');
			if($(o_li)[fadeIn - 1].getStyle('display') != "block" && fadeInOpacity == 0){
				$(o_li)[fadeIn - 1].className = "display";
				$(o_li)[fadeIn - 1].setOpacity(0);
			}
			if(fadeInOpacity == 1 || fadeOutOpacity == 0){
				$(o_li)[fadeOut - 1].className = "hide";
				//set a timeout to call the switchImage function
				switchImage_timeout = setTimeout("switchImage('DIV#main_img');",fadeInterval);
			}else{
				//fade In
				$(o_li)[fadeIn - 1].setOpacity(fadeInOpacity + 0.1);
				//fade out
				$(o_li)[fadeOut - 1].setOpacity(fadeOutOpacity - 0.1);
				//set another timeout to call the function again
				globalTimer.fade = setTimeout("fade('" + div_id + "'," + fadeIn + "," + fadeOut + ")",fadeDelta);
			}
		}
	}
	
	function selectProduct(product){
		if(typeof product != "undefined" && product.length && $("" + product + "_prod")){
			$("" + product + "_prod").checked = true;
		}
	}
