// JavaScript Document
// load the javascript includes
var path = "";
var ajaxLinking = false; // if this is true - the whole site speeds up as content is loaded without browser refresh

$A(document.getElementsByTagName("script")).findAll( function(s) {
	return (s.src && s.src.match(/prototype\.js(\?.*)?$/))
}).each( function(s) {
	path = s.src.replace(/prototype\.js(\?.*)?$/,'');
});
document.write("<script type=\"text/javascript\" src=\""+path+"lightbox.js\"></script>\n");
document.write("<script type=\"text/javascript\" src=\""+path+"swfobject.js\"></script>\n");
document.write("<script type=\"text/javascript\" src=\""+path+"aScroller.js\"></script>\n");
document.write("<script type=\"text/javascript\" src=\""+path+"prototip.js\"></script>\n");
document.write("<script type=\"text/javascript\" src=\""+path+"js-obfuscate.php?load=email_addresses.php\"></script>\n");
document.write("<script type=\"text/javascript\" src=\""+path+"js-obfuscate.php?load=contact-form-protection.js\"></script>\n");
document.write("<script src=\"http://www.google.com/jsapi?key=ABQIAAAASLJiIJnnBgqb21tYmoDQcBT5gptFcO7AwdHjPkSKh9b35mT6yxT0KkRknsaye7K7iGqyRWYXp8saaA\" type=\"text/javascript\"></script>\n");
// load the css stylesheets
$A(document.getElementsByTagName("link")).findAll( function(s) {
	return (s.href && s.href.match(/stylesheet\.css(\?.*)?$/))
}).each( function(s) {
	path = s.href.replace(/stylesheet\.css(\?.*)?$/,'');
});
document.write("<link rel=\"stylesheet\" href=\""+path+"dynamic-css.php\" type=\"text/css\" media=\"screen\" />");
document.write("<link rel=\"stylesheet\" href=\""+path+"lightbox.css\" type=\"text/css\" media=\"screen\" />");
document.write("<link rel=\"stylesheet\" href=\""+path+"prototip.css\" type=\"text/css\" media=\"screen\" />");
/**
** Webpage startup events
*/

var queueEffectArray = Array();
var activeSnBtn = "";
var pageURL = ""; // is this required? marked for dep ::TODO::
var activeProduct = "";
var flashShowTimer = -1; // for home page flash feature - holds timeout to re-display it
var flashShowDelay = 3; // delay in s before flash is re-displayed
var elHTMLCache = ''; // var to hold the flash HTML
var rangeAdvert = -1; // current range advert being show
var pageName;

document.observe("dom:loaded", function() {
	// check if there is a hash present that were on the hashed page
	if(ajaxLinking == true) doHashCheck();
	// page specific init
	switch(pageName){
		case "home":
			initHome();
			break;
		case "product":
		case "product-profiles":
		case "product-literature":
		case "product-video":
		case "product-images":
		case "product-news":
			initProduct();
			break;
		default:
			initRange();
			break;
	}
	// init news ticker
	if($('scroll-h')) new Effect.Morph('ticker-holder', {style: 'background-color: #0162AD;'});
});


function doHashCheck(){
	console.log("hashCheck");
	if(location.hash){
		console.log(location.hash);
		console.log(pageName);
		var foo = location.hash;
		var foobar = foo.substr(1).split("-");
		console.log('decode hash page is '+foobar[0]);
		switch(foobar[0]){
			case 'h':
			if(pageName != 'home') location.href = "p-h";
			break;
			case 'r':
			if(pageName != 'range' || range != foobar[1]) location.href = "p-r-"+foobar[1];
			break;
			case 'p':
			if(pageName != 'product' || product != foobar[1]) location.href = "p-p-"+foobar[1];
			break;
			case 'mc':
			if(pageName != 'media' || product != foobar[1]) location.href = "p-mc-"+foobar[1];
			break;
			case 'md':
			if(pageName != 'media'  || product != foobar[1]) location.href = "p-md-"+foobar[1];
			break;
		}
	}
}

function fadeElBgElFg(bgElement, bgCol, fgElement, fgCol){
	var bgQueue = bgElement.identify();
	var fgQueue = bgQueue+"_lnk";
	queueEffectArray[bgQueue] = new Effect.Morph(fgElement, { style: {color: fgCol }, duration: 0.5, queue: { scope: bgQueue, position: 'end' }});
	queueEffectArray[fgQueue] = new Effect.Morph(bgElement, { style: {backgroundColor: bgCol }, duration: 0.5, queue: { scope: fgQueue, position: 'end' }});
}


function initHome(){
	// set up global vars
	range = 0;
	activeProduct = "";
	// any loaded effects here?
	
	// decorate subnav tds with links and javascript enhancement
	$$('td.subnavbtn').each(function(snEl){
		snEl.observe('click', function(event){
			var tdEl = Event.findElement(event, "TD");
			// resolve the lnk number
			var theRange = tdEl.identify().substr(1);
			// load the range
			if(ajaxLinking==true){
				loadRange(theRange);
			} else {
				var aEl = tdEl.getElementsByTagName("A");
				location.href = aEl[0].href;
			}
		});
		snEl.observe('mouseover', function(event){
			var element = Event.findElement(event, "TD");
			var childElement = element.firstChild;
			var bgQueue = element.identify();
			var fgQueue = bgQueue+"_lnk";
			var range = bgQueue.substr(1, bgQueue.length-1);
			if(activeSnBtn != ""){
				if(queueEffectArray[bgQueue] != undefined) Effect.Queues.get(bgQueue).remove(queueEffectArray[bgQueue]);
				if(queueEffectArray[fgQueue] != undefined) Effect.Queues.get(fgQueue).remove(queueEffectArray[fgQueue]);
				// fade the old btn out if its different from current!
				// but if were in a product area - dont unfade the current range button
				if(activeSnBtn != bgQueue){
					var oldElement = $(activeSnBtn);
					var oldChildElement = oldElement.firstChild;		
					fadeElBgElFg(oldElement, '#CCCCCC', oldChildElement, '#990000');
				}
			}
			removeElement('start-feature');
			if($('googlesearchresults')) $('googlesearchresults').hide();
			
			if(flashShowTimer != -1){
				window.clearTimeout(flashShowTimer);
				flashShowTimer = -1;
			}
			
			activeSnBtn = bgQueue;
			childElement.style.color = '#FFFFFF';
			element.style.backgroundColor = '#666666';
			// show the range advert
			$('subnav-view').playRangeAdvert(range);
		});

		snEl.observe('mouseout', function(event){
			/*
			var element = Event.findElement(event, "TD");
			var childElement = element.firstChild;
			fadeElBgElFg(element, '#CCCCCC', childElement, '#990000');
			*/
			// restart the flash after the timeout
			flashShowTimer = window.setTimeout(function(event){
				var element = $(activeSnBtn);
				var childElement = element.firstChild;
				fadeElBgElFg(element, '#CCCCCC', childElement, '#990000');
				restoreElement('start-feature');
				$('range-advert').hide();
				$('subnav-view').className = '';
			}, flashShowDelay * 1000);
		});
	});
	
	// attach the functionality for range adverts
	$('subnav-view').playRangeAdvert = function(range){
		if(rangeAdvert != range){
			rangeAdvert = range;
			this.className = "range_ad_"+range;
			// update the advert using ajax to allow for possible server side coding
			new Ajax.Request('ajax-content.php', {
				parameters: { type: "range advert", id: range }, 
				onSuccess: function(transport){
					$('range-advert').hide();
					$('range-advert').update(transport.responseText);
					//$('range-advert').style.marginBottom: = '-100px';
					//$('range-advert').show();
					new Effect.Appear('range-advert', {
						duration: 0.5
					});
					/*
					new Effect.Morph('range-advert', {
						duration: 0.75,
						style: "margin-bottom: 24px",
						transition: Effect.Transitions.spring
					});
					*/
				}
			});
		} else {
			//new Effect.Appear('range-advert', {duration: 0.5});
			$('range-advert').show();
		}
	}
	
	
	// swap the range links with ajax content loaders
	var i = 1;
	if(ajaxLinking) $$('td.subnavbtn a').each(function(el){
    	el.href = "javascript:loadRange("+(i++)+")";
	});
	
	// swap the home link with a content loader
	if(ajaxLinking) $$('a.home').each(function(el){
		el.href = "javascript:loadHome()";
	});
}

function removeElement(elID){
	var el = $(elID);
	var cacheStr = el.innerHTML;
	if(elHTMLCache == '') elHTMLCache = cacheStr; // only store if empty!
	el.update('');
}

function restoreElement(elID){
	$(elID).update(elHTMLCache);
}

function initRange(){
	// set up global vars
	//range = 0; ??? ::TODO::
	activeProduct = "";
	
	// any loaded effects here?
	
	// decorate subnav tds with links and javascript enhancement
	$$('td.subnavbtn').each(function(snEl){
		snEl.observe('click', function(event){
			var tdEl = Event.findElement(event, "TD");
			// resolve the lnk number
			var theRange = tdEl.identify().substr(1);
			// load the range
			if(ajaxLinking==true){
				loadRange(theRange);
			} else {
				var aEl = tdEl.getElementsByTagName("A");
				location.href = aEl[0].href;
			}
		});
		snEl.observe('mouseover', function(event){
			var element = Event.findElement(event, "TD");
			var childElement = element.firstChild;
			var bgQueue = element.identify();
			var fgQueue = bgQueue+"_lnk";
			if(activeSnBtn != ""){
				if(queueEffectArray[bgQueue] != undefined) Effect.Queues.get(bgQueue).remove(queueEffectArray[bgQueue]);
				if(queueEffectArray[fgQueue] != undefined) Effect.Queues.get(fgQueue).remove(queueEffectArray[fgQueue]);
			}
			activeSnBtn = bgQueue;
			childElement.style.color = '#FFFFFF';
			element.style.backgroundColor = '#666666';
		});
		
		snEl.observe('mouseout', function(event){
			var element = Event.findElement(event, "TD");
			var childElement = element.firstChild;
			fadeElBgElFg(element, '#CCCCCC', childElement, '#990000');
		});
		
		// swap the range links with ajax content loaders
		// resolve the lnk number
		var theRange = snEl.identify().substr(1);
		var childElement = snEl.firstChild;
		if(ajaxLinking==true) childElement.href = "javascript:loadRange("+theRange+")";
	});
	
	// swap the home link with a content loader
	if(ajaxLinking) $$('a.home').each(function(el){
		el.href = "javascript:loadHome()";
	});
	
	// decorate product navigation with javascript
	$$('td.productbtn').each(function(prEl){
		// add glow effect		
		prEl.observe('mouseover', function(event){
			var element = Event.findElement(event, "TD");
			var childElement = element.firstChild;
			var bgQueue = element.identify();
			var fgQueue = bgQueue+"_lnk";
			if(queueEffectArray[bgQueue] != undefined) Effect.Queues.get(bgQueue).remove(queueEffectArray[bgQueue]);
			if(queueEffectArray[fgQueue] != undefined) Effect.Queues.get(fgQueue).remove(queueEffectArray[fgQueue]);
			// resolve the fade colour for the foreground
			if(childElement.className=="dark"){
				childElement.style.color = '#333333';						  
			} else {
				childElement.style.color = '#FFFFFF';
			}
			// the bg colour required is the previous sibling
			var colEl = element.previous();
			element.style.backgroundColor = colEl.style.backgroundColor;
		});
		
		// add fade effect
		prEl.observe('mouseout', function(event){
			var element = Event.findElement(event, "TD");
			var childElement = element.firstChild;
			var theProd = element.identify().substr(1);
			// if were not on the active product; then fade it out
			if(theProd != activeProduct) fadeElBgElFg(element, '#666666', childElement, '#FFFFFF');
		});
		
		// onclick - fade old active product if not same as current
		if(ajaxLinking==true) prEl.observe('click', function(event){
			var element = Event.findElement(event, "TD");
			var theProd = element.identify().substr(1);
			if(activeProduct != "" && (theProd != activeProduct)){
				var element = $('p'+activeProduct);
				var childElement = element.firstChild;
				fadeElBgElFg(element, '#666666', childElement, '#FFFFFF');
			}
		});
		
		// swap the link with content loader
		var theProd = prEl.identify().substr(1);
		var childElement = prEl.firstChild;
		if(ajaxLinking) childElement.href = "javascript:loadProduct("+theProd+")";		
	});
	
	// decorate product box tabs with javascript
	$$('td.prod-tile a').each(function(pbrEl){
		// add glow effect
		var btnId = pbrEl.identify().substr(1);
		var element = $(btnId);
		if(element){
			pbrEl.observe('mouseover', function(event){
				var childElement = $(btnId+"_lnk");
				var bgQueue = btnId;
				var fgQueue = btnId+"_lnk";
				if(queueEffectArray[bgQueue] != undefined) Effect.Queues.get(bgQueue).remove(queueEffectArray[bgQueue]);
				if(queueEffectArray[fgQueue] != undefined) Effect.Queues.get(fgQueue).remove(queueEffectArray[fgQueue]);
				// resolve the fade colour for the foreground
				if(childElement.className=="dark"){
					childElement.style.color = '#333333';						  
				} else {
					childElement.style.color = '#FFFFFF';
				}
				// the bg colour required is the previous sibling
				var colEl = element.previous();
				element.style.backgroundColor = colEl.style.backgroundColor;
			});
			
			// add fade effect
			pbrEl.observe('mouseout', function(event){
				var childElement = $(btnId+"_lnk");
				// if were not on the active product; then fade it out
				fadeElBgElFg(element, '#666666', childElement, '#FFFFFF');
			});
			
			// onclick - fade old active product if not same as current
			if(ajaxLinking==true) pbrEl.observe('click', function(event){
				var element = Event.findElement(event, "TD");
				var theProd = element.identify().substr(2);
				if(activeProduct != "" && (theProd != activeProduct)){
					var element = $('p'+activeProduct);
					var childElement = element.firstChild;
					fadeElBgElFg(element, '#666666', childElement, '#FFFFFF');
				}
			});
		}
	});
}

function initProduct(){
	initRange();
	activeProduct = product;
	// init the activeProduct btn
	var element = $('p'+activeProduct);
	var childElement = element.firstChild;
	if(childElement.className=="dark"){
		childElement.style.color = '#333333';						  
	} else {
		childElement.style.color = '#FFFFFF';
	}
	// the bg colour required is the previous sibling
	var colEl = element.previous();
	element.style.backgroundColor = colEl.style.backgroundColor;
}

function loadRange(i){
	// set the hash
	if(ajaxLinking == true)	location.hash = 'r-'+i;
	// load top banner
	new Ajax.Updater('top', 'banner-empty.php');
	// load menu items 
	range = i;
	// load html and init
	$('subnav-view').className = "";
	new Ajax.Updater('subnav-view', 'content-range.php', { parameters: { range: range }, method: 'get'});
	new Ajax.Updater('range-menu', 'content-range-menu.php', { parameters: { range: range }, method: 'get', onComplete: function(){ initRange(range); }});
}

function loadHome(){
	// set the hash
	if(ajaxLinking == true)	location.hash = 'h';
	// load html and init
	$('subnav-view').className = "nav0";
	new Ajax.Updater('top', 'banner-home.php');
	new Ajax.Updater('body', 'content-home.php', { method: 'get', onComplete:initHome });
}

function loadProduct(prodID){
	if(ajaxLinking == true)	location.hash = 'p-'+prodID;
	activeProduct = prodID;
	//console.log("loadProduct "+prodID);
	new Ajax.Updater('subnav-view', 'content-product.php', { parameters: { product: prodID }, method: 'get'});
}
function smGlow(element, colour){
	var elId = element.id;
	if(queueEffectArray[elId]!=undefined){
		Effect.Queues.get(elId).remove(queueEffectArray[elId]);
	}
	element.style.backgroundColor='#'+colour;
}

function smFade(element, colourFrom, colourTo){
	var elId = element.id;
	queueEffectArray[elId] = new Effect.Highlight(element, { startcolor: '#'+colourFrom, endcolor: '#'+colourTo, restorecolor: '#'+colourTo, duration: 0.5, queue: { scope: elId, position: 'end' }});
	return false;
}
