// -----------------------------------------------------------------------------------
//
//	Navigationbox v1.02
//	by View Strategic PLC - http://www.view.uk.com
//	Last Modification: 07/05/2009
//
// -----------------------------------------------------------------------------------
/*

    Table of Contents
    -----------------
    Configuration

    Navigationbox Class Declaration
    - initialize()
    - updateNavigationList()
    - end() 
    - switchHover()
    - killHover()
    - resetActive()
    
    Function Calls
    - document.observe()
   
*/
// -----------------------------------------------------------------------------------

//
//  Configuration
//

var subNavOn = false;		// bool to check if the second level nav is showing. that way we dont keep trying to switch it off  when it already off.

NavigationboxOptions = Object.extend({

}, window.NavigationboxOptions || {});

// -----------------------------------------------------------------------------------

var Navigationbox = Class.create();

Navigationbox.prototype = {

	// initialize()
    // Constructor runs on completion of the DOM loading. Calls updateNavigationList and then
    // the function inserts html at the bottom of the page which is used to display the shadow 
    // overlay and the image container.
    //
    initialize: function() {    
		try{		  
			this.updateNavigationList();
			//Set hit areas to close the navbox
			$('header').observe('mouseover', (function() { this.end(); }).bind(this));
			$('main-content').observe('mouseover', (function() { this.end(); }).bind(this));
			$('primary-nav-close-target').observe('mouseover', (function() { this.end(); }).bind(this));
		}catch(err){
			// do nothing. we dont need to do this for anything other that IE anyway
		}		 
    },

    //
    // updateNavigationList()
    // Loops through anchor tags looking for 'navigationbox' references and applies onmouseover
    // events to appropriate links.
    //
    updateNavigationList: function() {   
        this.updateNavigationList = Prototype.emptyFunction;
			document.observe('mouseover', (function(event){
	        var target = event.findElement('a[rel^=navigationbox]');
			var targethome = event.findElement('a[rel^=navigationhome]');

			if (target){
				this.switchHover(target);
				event.stop();
				subNavOn = true;
			}
			if (targethome){
				 this.switchHover(targethome);
				 this.killHover(targethome);
				subNavOn = false;
			}
        }).bind(this));
    },
    
    //
    //  end()
	//	Closes navigationbox and sets everything back again
    //
    end: function() {

		if (subNavOn)
		{
			//alert(subNavOn);
			this.killHover();
			this.resetActive() ;
			subNavOn = false;
		}

	},


    //
    //  switchHover()
	//	de-activate all class="sethover" and class="active" on nav, and set this menu to class="sethover"
    //
	switchHover: function(anchor){
		this.killHover();
		//alert(anchor.parentNode.className);
		if (anchor.parentNode.className.indexOf('sethover') < 0)
		{
				//alert('boo' + anchor.parentNode.className.indexOf('last'));			
			if (anchor.parentNode.className.indexOf('last') > -1)
			{
				anchor.parentNode.className+=" lastsethover";
				
			}
			else{
			
				anchor.parentNode.className+=" sethover";
				//alert('boo');
			}
			// Required for IE to grow the iframe when content is longer than standard.
			if (Prototype.Browser.IE){
				if (navigator.appVersion.substr(22,3) >"5.5") {
					var ieULs = anchor.parentNode.getElementsByTagName('ul');
					var ieIframes = anchor.parentNode.getElementsByTagName('iframe');	
					if (ieULs.length > 0 && ieIframes.length > 0){
						ieIframes[0].style.height = ieULs[0].offsetHeight+"px";
					}
				}else{
					var ieSelects = document.getElementById("main-content").getElementsByTagName('select');
					if (ieSelects.length > 0){
						for (j=0; j<ieSelects.length; j++) {
							ieSelects[j].style.visibility="hidden" 
						}
					}
				}
			}
		}
	},

    //
    //  killHover()
	//	de-activate all class="sethover" and class="active" on nav
    //
	 killHover: function(){
		var sfEls = document.getElementById("primary-navigation").getElementsByTagName("li");
		for (var i=0; i<sfEls.length; i++) {
			//alert(sfEls[i].className);	
			if (sfEls[i].className != null)
			{
				sfEls[i].className=sfEls[i].className.replace(new RegExp("hide\\b"), "");
				sfEls[i].className=sfEls[i].className.replace(new RegExp("lastsethover\\b"), "");
				sfEls[i].className=sfEls[i].className.replace(new RegExp("sethover\\b"), "");
				sfEls[i].className=sfEls[i].className.replace(new RegExp("active\\b"), " hideactive");
			}
		}
	},

    //
    //  resetActive()
	//	de-activate all class="sethover"  on nav, and set this menu to class="sethover"
    //
	 resetActive: function(){
		var sfEls = document.getElementById("primary-navigation").getElementsByTagName("li");
		for (var i=0; i<sfEls.length; i++) {
			//alert(sfEls[i].className);	
			if (sfEls[i].className != null)
			{
				sfEls[i].className=sfEls[i].className.replace(new RegExp(" hideactive\\b"), " active");
			}
		}


		if (navigator.appVersion.substr(22,3) <"6.0") {
			var ieSelects = document.getElementById("main-content").getElementsByTagName('select');
			if (ieSelects.length > 0){
				for (j=0; j<ieSelects.length; j++) {
					ieSelects[j].style.visibility="visible" 
				}
			}
		}



	},

	setHover: function(nav) {
		try
		{
			var ieULs = nav.getElementsByTagName('ul');
			var ieIframes = nav.getElementsByTagName('iframe');	
			if (navigator.appVersion.substr(22,3) >"5.5" && navigator.appVersion.substr(22,3) <"7.0" && Prototype.Browser.IE) {
					// IE script to cover <select> elements with <iframe>s
				if (ieIframes.length < 1){
					for (j=0; j<ieULs.length; j++) {
						var ieMat=document.createElement('iframe');
						if(document.location.protocol == "https:")
							ieMat.src="//0";
						else if(window.opera != "undefined")
							ieMat.src="";
						else
							ieMat.src="javascript:false";
						ieMat.scrolling="no";
						ieMat.frameBorder="0";
						ieMat.style.width="940px";
						ieMat.style.height="285px";
						ieMat.style.zIndex="-1000000";
						ieMat.style.position="absolute";
						ieMat.style.filter="progid:DXImageTransform.Microsoft.Alpha(style=0,opacity=0)";
						ieULs[j].insertBefore(ieMat, ieULs[j].childNodes[0]);
						ieULs[j].style.zIndex="10001";
						ieULs[j].style.position="absolute";
					}
				}
			}
		}
		catch(err)
		{
			// do nothing. we dont need to do this for anything other that IE anyway
		}
	}
}


document.observe('dom:loaded', function () { new Navigationbox(); try{Navigationbox.prototype.setHover(document.getElementById('primary-navigation').childNodes[0])	}catch(err){}	 });
//document.addEvent('domloaded', function ()) { new Navigationbox(); });


	 
