
/************************************************************** 
 * Clear Default Text: functions for clearing and replacing default text in
 * <input> elements.
 * by Ross Shannon, http://www.yourhtmlsource.com/
 **************************************************************/

function init() {
    var formInputs = document.getElementsByTagName('input');
    for (var i = 0; i < formInputs.length; i++) {
        var theInput = formInputs[i];
        
        if (theInput.type == 'text' && (theInput.className.match(/\bcleardefault\b/) || theInput.id.match(/\SearchForm_SearchForm_Search\b/))) {  
            /* Add event handlers */          
            addEvent(theInput, 'focus', clearDefaultText, false);
            addEvent(theInput, 'blur', replaceDefaultText, false);
            
            /* Save the current value */
            if (theInput.value !== '') {
                theInput.defaultText = theInput.value;
            }
        }
    }
}
DOMAssistant.DOMReady(init); 

function clearDefaultText(e) {
    var target = window.event ? window.event.srcElement : e ? e.target : null;
    if (!target) return;
    
    if (target.value == target.defaultText) {
        target.value = '';
    }
}

function replaceDefaultText(e) {
    var target = window.event ? window.event.srcElement : e ? e.target : null;
    if (!target) return;
    
    if (target.value == '' && target.defaultText) {
        target.value = target.defaultText;
    }
}

/************************************************************** 
 * Cross-browser event handling, by Scott Andrew
 **************************************************************/
function addEvent(element, eventType, lamdaFunction, useCapture) {
    if (element.addEventListener) {
        element.addEventListener(eventType, lamdaFunction, useCapture);
        return true;
    } else if (element.attachEvent) {
        var r = element.attachEvent('on' + eventType, lamdaFunction);
        return r;
    } else {
        return false;
    }
}



/************************************************************** 
 * MENU
 * 
 * 
 **************************************************************/

function StartMenu() {

	// Check we're working with a DOM compliant browser
	if (document.getElementById && document.createElement)
	{
		var strLocation = window.location;
		
		var MenuItem = $$("mainnav");

		//Get second level navs
		var MenuSecondItem = $("#mainnav .dropnav");
		MenuSecondItem.each( function() {
			//Hide all second levels
			$$(this).addClass('hide');
		});

        // unhide select one
		var MenuItemSelected = $("#section");
		MenuItemSelected.removeClass("hide");

		//add onclicks to  elements
		var MenuSecondLink = $("#mainnav .dropdown");
		MenuSecondLink.each( function() {
			var MenuSecondLink2 = $$(this).firstChild;
			// REQUIRED FOR IE6 //
            $$(MenuSecondLink2).onclick = function(){return OpenMenu(this);}
            $$(MenuSecondLink2).onkeypress = function(){return OpenMenu(this);}
			// END REQUIRED FOR IE6 //
/* 
			//Doesn't work on ie6, so no longer used
			$$(MenuSecondLink2).setAttributes({
				onclick : "return OpenMenu(this); return false;",
				onkeypress : "return OpenMenu(this); return false;"
			});
*/
		});



	}
}
DOMAssistant.DOMReady(StartMenu);


function OpenMenu(thisMenu) {

	//First, add hide to all sub menus
	var MenuSecondItem = $("#mainnav .dropnav");
	MenuSecondItem.each( function() {
		//Hide all second levels
		$$(this).addClass('hide');
	});
	
	
	//second, remove hide from chosen class
	$$(thisMenu).next().removeClass('hide');

return false;
}


/************************************************************** 
 * Image Cross Fade Redux
 * Version 1.0
 * Last revision: 02.15.2006
 * steve@slayeroffice.com
 * Rewrite of old code found here: http://slayeroffice.com/code/imageCrossFade/index.html
 **************************************************************/

var d=document, imgs = new Array(), zInterval = null, current=0, pause=false;

function so_init() {
	if(!d.getElementById || !d.createElement)return;

	/*
	// DON'T FORGET TO GRAB THIS FILE AND PLACE IT ON YOUR SERVER IN THE SAME DIRECTORY AS THE JAVASCRIPT!
	// http://slayeroffice.com/code/imageCrossFade/xfade2.css
	css = d.createElement("link");
	css.setAttribute("href","xfade2.css");
	css.setAttribute("rel","stylesheet");
	css.setAttribute("type","text/css");
	d.getElementsByTagName("head")[0].appendChild(css); */
	
	//Replaces css rubbish
	$("#currentproject li").each( function() {
			$$(this).setStyle("display", "none");
		});

	imgs = d.getElementById("currentproject").getElementsByTagName("li");
	for(i=1;i<imgs.length;i++) imgs[i].xOpacity = 0;
	imgs[0].style.display = "block";
	imgs[0].xOpacity = .99;
	
	setTimeout(so_xfade,4000);
}
DOMAssistant.DOMReady(so_init); 

function so_xfade() {

	cOpacity = imgs[current].xOpacity;
	nIndex = imgs[current+1]?current+1:0;

	nOpacity = imgs[nIndex].xOpacity;
	
	cOpacity-=.05; 
	nOpacity+=.05;
	
	imgs[nIndex].style.display = "block";
	imgs[current].xOpacity = cOpacity;
	imgs[nIndex].xOpacity = nOpacity;
	
	setOpacity(imgs[current]); 
	setOpacity(imgs[nIndex]);
	
	if(cOpacity<=0) {
		imgs[current].style.display = "none";
		current = nIndex;
		setTimeout(so_xfade,4000);
	} else {
		setTimeout(so_xfade,50);
}
	
	function setOpacity(obj) {
		if(obj.xOpacity>.99) {
			obj.xOpacity = .99;
			return;
		}
		obj.style.opacity = obj.xOpacity;
		obj.style.MozOpacity = obj.xOpacity;
		obj.style.filter = "alpha(opacity=" + (obj.xOpacity*100) + ")";
	}
	
}



/************************************************************** 
 * HOME PAGE - Products javascript
 **************************************************************/
function homeProductsInit() { // Calls JS to add events to all product links
	$("#products a").each( function() {
		//alert("bob " + $$(this));
		$$(this).addEvent("mouseover", function (){
			homeProducts($$(this).parentNode.id);
		}); 
		$$(this).addEvent("mouseout", function (){
			homeProducts();
		}); 
	});

/*
	// adding temp img element to page
		objLightboxImage = document.createElement("img");
		objLightboxImage.setAttribute('id','temp-image');
		objLightboxImage.setAttribute('width','1');
		objLightboxImage.setAttribute('height','1');
		objLightboxImage.setAttribute('class','unviewable');
		document.body.appendChild(objLightboxImage);

	var NewImage = "gcsl/images/home-products.jpg";

	//Get Image
	var imgContainer = $("#products ul");

	TheImageTemp = new Image();
		TheImageTemp.onload=function(){
			$$('temp-image').src = NewImage;
    		var TheImage = document.createElement('img');
    		TheImage.onload = function (evt) {
        		imgContainer.setStyle("background-image", "url("+this.src+")");
	    	};
			TheImage.src = NewImage;
			TheImageTemp.onload=function(){};	//	clear onLoad, IE behaves irratically with animated gifs otherwise 
		};
	TheImageTemp.src = NewImage;
*/

	window.onload = function() {
		$("#products ul").setStyle("background-image", "url(gcsl/images/home-products.jpg)");
	}
}


function homeProducts(imageName) { // Makes the actual change when moused over/out
	if (!imageName || imageName == "") {
		$("#products ul").setAttributes({
			className : ""
		}); 

		//$("#products ul").removeClass('products-scaf').removeClass('products-form').removeClass('products-site').removeClass('products-mate').removeClass('products-timb').removeClass('products-hire').removeClass('products-fenc').removeClass('products-spec').removeClass('products-labo');
		//var CSSstyle = "url(gcsl/images/home-products-default.jpg) no-repeat";
		//$("#products ul").setStyle("background", CSSstyle);
	} else {
		$("#products ul").addClass(imageName);
		//var CSSstyle = "url(gcsl/images/home-"+imageName+".jpg) no-repeat";
		//$("#products ul").setStyle("background", CSSstyle);
	}
}

/************************************************************** 
 * EMPLOYMENT
 **************************************************************/
function employmentInit() { // Calls JS to add events to all positions
		$("#joblist div").each( function() {
			//alert("bob " + $$(this));
			$$(this).addEvent("mouseover", function (){
				employmentHover($$(this));
			}); 
			$$(this).addEvent("mouseout", function (){
				employmentHover($$(this));
			}); 
			$$(this).addEvent("click", function (){
				employmentToggle($$(this));
			}); 
			$$(this).addClass("closed"); 
		});
}


function employmentToggle(elem) { // Makes the actual change when moused over/out
	//alert('bob: ' + elem);
	if (!elem) { return false;}
	if ($(elem).hasClass("open") == true) {
		$(elem).removeClass("open");
		$(elem).addClass("closed");
	} else {
		$(elem).addClass("open");
		$(elem).removeClass("closed");
	}
}

function employmentHover(elem) { // adds class to highlight current moused over
	//alert('bob: ' + elem);
	if (!elem) { return false;}
	if ($(elem).hasClass("show-me") == true) {
		$(elem).removeClass("show-me");
	} else {
		$(elem).addClass("show-me");
	}
}
