/*	

	Dusted Design Partners Limited

*/

/* BROWSER SNIFFER */
var detect = navigator.userAgent.toLowerCase();
var OS,browser,version,total,thestring;

if (checkIt('konqueror'))
{
        browser = "Konqueror";
        OS = "Linux";
}
else if (checkIt('safari')) browser = "Safari"
else if (checkIt('omniweb')) browser = "OmniWeb"
else if (checkIt('opera')) browser = "Opera"
else if (checkIt('webtv')) browser = "WebTV";
else if (checkIt('icab')) browser = "iCab"
else if (checkIt('msie')) browser = "Internet Explorer"
else if (!checkIt('compatible'))
{
        browser = "Mozilla"
        version = detect.charAt(8);
}
else browser = "An unknown browser";

if (!version) version = detect.charAt(place + thestring.length);

if (!OS)
{
        if (checkIt('linux')) OS = "Linux";
        else if (checkIt('x11')) OS = "Unix";
        else if (checkIt('mac')) OS = "Mac"
        else if (checkIt('win')) OS = "Windows"
        else OS = "an unknown operating system";
}

function checkIt(string)
{
        place = detect.indexOf(string) + 1;
        thestring = string;
        return place;
}


function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      if (oldonload) {
        oldonload();
      }
      func();
    }
  }
}


/* TAB MANAGER */

var tabArray = new Array("01", "02", "03", "04", "05");
var tabCount = tabArray.length;
	
function showLayer(whichLayer) {
	if (document.getElementById)
	{
	// this is the way the standards work
	for (i=0; i<tabCount; i++) {
		document.getElementById("tab-"+tabArray[i]).className = "tab-off";
		document.getElementById("s"+tabArray[i]).className = "slide-hidden";
	}
	document.getElementById("tab-"+whichLayer).className = "tab-live";
	document.getElementById("s"+whichLayer).className = "slide-container";
	}
	else if (document.all)
	{
	// this is the way old msie versions work
	for (i=0; i<tabCount; i++) {
		var whatTab = tabArray[i];
		document.all["tab-"+whatTab].className = "tab-off";
		document.all["s"+whatTab].className = "slide-hidden";
	}
	document.all["tab-"+whichLayer].className = "tab-live";
	document.all["s"+whichLayer].className = "slide-container";
	}
	else if (document.layers)
	{
	// this is the way nn4 works
	for (i=0; i<tabCount; i++) {
		var whatTab = tabArray[i];
		document.layers["tab-"+whatTab].className = "tab-off";
		document.layers["s"+whatTab].className = "slide-hidden";
	}
	document.layers["tab-"+whichLayer].className = "tab-live";
	document.layers["s"+whichLayer].className = "slide-container";
	}
}
function initLayers() {
	showLayer("01");
}
function showFields(whichLayer) {
	if (document.getElementById)
	{
	// this is the way the standards work
		document.getElementById(whichLayer).className = "formrow";
	}
	else if (document.all)
	{
	// this is the way old msie versions work
		document.all[whichLayer].className = "formrow";
	}
	else if (document.layers)
	{
	// this is the way nn4 works
		document.layers[whichLayer].className = "formrow";
	}
}
function hideFields(whichLayer) {
	if (document.getElementById)
	{
	// this is the way the standards work
		document.getElementById(whichLayer).className = "hidden";
	}
	else if (document.all)
	{
	// this is the way old msie versions work
		document.all[whichLayer].className = "hidden";
	}
	else if (document.layers)
	{
	// this is the way nn4 works
		document.layers[whichLayer].className = "hidden";
	}
}
/* FOOTER MANAGER */
function getWindowHeight() {
	var windowHeight = 0;
	if (typeof(window.innerHeight) == 'number') {
		windowHeight = window.innerHeight;
	}
	else {
		if (document.documentElement && document.documentElement.clientHeight) {
			windowHeight = document.documentElement.clientHeight;
		}
		else {
			if (document.body && document.body.clientHeight) {
				windowHeight = document.body.clientHeight;
			}
		}
	}
	return windowHeight;
}
function setFooter() {
	if (browser != "Internet Explorer" || OS !="Mac" && OS !="an unknown operating system") {
		if (document.getElementById) {
			var windowHeight = getWindowHeight();
			if (windowHeight > 0) {
				var contentHeight = document.getElementById('container').offsetHeight;
				var footerElement = document.getElementById('footer');
				var footerHeight  = footerElement.offsetHeight+1;
				if (windowHeight - (contentHeight + footerHeight) >= 0) {
					footerElement.style.top = (windowHeight - (contentHeight + footerHeight)) + 'px';
				}
				else {
					footerElement.style.top = '0px';
				}
			}
		}
	}
}
		

function toggle(id){
	if (document.getElementById) {
		ulElement = document.getElementById(id);
		if (ulElement) {
			if (ulElement.className == 'closed') {
				ulElement.className = "open";
			} else {
				ulElement.className = "closed";
			}
			if (OS !="an unknown operating system") {
				setFooter();
			}
		}
	}
}
function JSopen(id){
	if (document.getElementById) {
		ulElement = document.getElementById(id);
		if (ulElement) {
			ulElement.className = "open";
			if (OS !="an unknown operating system") {
				setFooter();
			}
		}
	}
}
function JSclose(id){
	if (document.getElementById) {
		ulElement = document.getElementById(id);
		if (ulElement) {
			ulElement.className = "closed";
			if (OS !="an unknown operating system") {
				setFooter();
			}
		}
	}
}

sfHover = function() {
	if (browser != "Internet Explorer" || OS !="Mac" && OS !="an unknown operating system") {
		if (document.getElementById("content-nomargin")) {
			var sfEls = document.getElementById("content-nomargin").getElementsByTagName("div");
			for (var i=0; i<sfEls.length; i++) {
				sfEls[i].onmouseover=function() {
					var dest = this.getAttribute('id');
					if (browser == "Internet Explorer" && OS =="Windows") {
						this.className+=" sfhover";
					}
				}
				sfEls[i].onmouseout=function() {
					if (browser == "Internet Explorer" && OS =="Windows") {
						this.className=this.className.replace(new RegExp(" sfhover\\b"), "");
					}
				}
				sfEls[i].onclick=function() {
					var dest = this.getAttribute('id');
					document.location=dest+".php";
				}
			}
		}
	}
}
tabLink = function() {
	if (document.getElementById("home-tabs")) {
		var tabEls = document.getElementById("home-tabs").getElementsByTagName("a");
		for (var i=0; i<tabEls.length; i++) {
			tabEls[i].onclick=function() {
				var dest = this.getAttribute('href');
				refOffset = dest.indexOf('#') + 2;
  				refEnd = dest.length;
  				newDest = dest.substring(refOffset, refEnd);
				showLayer(newDest);
				return false;
			}
		}
	}
}
formLink = function() {
	if (document.getElementById("submit")) {
		var formLinks = document.getElementById("submit");
		var offState = formLinks.getAttribute('src');
		formLinks.onmouseover=function() {
			if (document.getElementById("client-login")) {
					formLinks.setAttribute('src', 'http://www.dusteddesign.com/images/forms/login_over.png');
			} else {
					formLinks.setAttribute('src', 'http://www.dusteddesign.com/images/forms/submit_over.png');
		
			}
		}
		formLinks.onmouseout=function() {
			formLinks.setAttribute('src', offState);
		}
	}
}
toggleLink = function() {
	if (document.getElementById("sector-list")) {
		var uberExpander = document.getElementById("expand-all");
		var tabEls = document.getElementById("sector-list").getElementsByTagName("a");
		for (var i=0; i<tabEls.length; i++) {
			if (tabEls[i].getAttribute("rel")) {
				var relIndex = tabEls[i].getAttribute("rel");
				var relSplit = relIndex.split("|");
				if (relSplit[0] == "external") {
					var whatTab = tabEls[i].parentNode;
					whatTab.className = "popup";
					tabEls[i].onclick=function() {
						var relIndex = this.getAttribute("rel");
						var relSplit = relIndex.split("|");
						var windowName = relSplit[1];
						windowAttrib = "width=" + relSplit[2] + ",height=" + relSplit[3];
						window.open(this.href,windowName,windowAttrib);
						return false;
					}
				}
			} else {
				var group = tabEls[i].getAttribute('href');
				var isExpander = group.indexOf("#");
				
				if (isExpander != -1) {
					var whatTab = tabEls[i].parentNode.getAttribute('id');
					toggle(whatTab);
				}
				tabEls[i].onclick=function() {
					var group = this.getAttribute('href');
					var thisExpander = group.indexOf("#");
					if (thisExpander != -1) {
						refOffset = group.indexOf('#') + 1;
						refEnd = group.length;
						newGroup = group.substring(refOffset, refEnd);
						toggle(newGroup);
						return false;
					}
				}
			}
		}
		uberExpander.onclick=function() {
			for (var i=0; i<tabEls.length; i++) {
				var group = tabEls[i].getAttribute('href');
				var isExpander = group.indexOf("#");
				if (isExpander != -1) {
					var whatTab = tabEls[i].parentNode.getAttribute('id');
					if (uberExpander.innerHTML == "Expand the whole list") {
						JSopen(whatTab);
					} else {
						JSclose(whatTab);
					}
				}
			}
			if (uberExpander.innerHTML == "Expand the whole list") {
				uberExpander.innerHTML = "Close up the whole list";
			} else {
				uberExpander.innerHTML = "Expand the whole list";
			}
			return false;
		}
	}
}
toggleField = function() {
	if (document.getElementById("nature")) {
		var natureSel = document.getElementById("nature");
		natureSel.onchange=function() {
			if (this.value == "placement" || this.value == "employment") {
				showFields("portfolio-field");
				showFields("cv-upload");
			} else {
				hideFields("portfolio-field");
				hideFields("uni-field");
				hideFields("cv-upload");
			}
			if ( this.value == "placement") {
				showFields("uni-field");
			}
			if ( this.value == "employment") {
				hideFields("uni-field");
			}
			setFooter();
		}
	}
	
	if (document.getElementById("employment-status")) {
		var employSel = document.getElementById("employment-status");
		employSel.onchange=function() {
			if (this.value == "Working") {
				showFields("working-fields");
				hideFields("looking-fields");
			}
			if (this.value == "Looking") {
				showFields("looking-fields");
				hideFields("working-fields");
			}
			if (this.value == "0") {
				hideFields("looking-fields");
				hideFields("working-fields");
			}
			setFooter();
		}
	}
}
printLink = function() {
	if (document.getElementById("footer-print")) {
		var printBut = document.getElementById("footer-print").getElementsByTagName("a");
		for (var i=0; i<printBut.length; i++) {
			printBut[i].onclick=function() {
				if (window.print) {
					window.print();
				} else {
					alert("Unfortunately your browser doesn't support printing via Javascript.\nTo print this page please select \"File > Print...\" in your browser's menu.");
				}
				return false;
			}
		}
	}
}

  // this function is needed to work around 
  // a bug in IE related to element attributes
function hasClass(obj) {
	var result = false;
	if (obj.getAttributeNode("class") != null) {
		result = obj.getAttributeNode("class").value;
	}
	return result;
}   

function stripe() {
	var even = false;
	var evenColor = arguments[1] ? arguments[1] : "#fff";
	var oddColor = arguments[2] ? arguments[2] : "#eee";
	var statList = document.getElementById("stats");
	
	if (! statList) { return; }
	
	var lis = statList.getElementsByTagName("li");
	
	for (var h = 0; h < lis.length; h++) {
		var myli = lis[h]
		if (! hasClass(myli) && ! myli.style.backgroundColor) {
			myli.style.backgroundColor = even ? evenColor : oddColor;
		}
		even =  ! even;
	}
}

mooFx = function(){
	if (document.getElementById("client-login")) {
		myHeight = new fx.Height('client-login', {duration: 400});
		if (!document.getElementById("client-login-show")) {
			myHeight.hide();
		}
		if (document.getElementById("login-toggle")) {
			var toggleBut = document.getElementById("login-toggle").getElementsByTagName("a");
			for (var i=0; i<toggleBut.length; i++) {
				toggleBut[i].onclick=function() {
					myHeight.toggle();
					return false;
				}
			}
		}
	}
}
showLogin = function(){
	if (document.getElementById("client-login")) {
		myHeight.toggle();
	}
}

jobAdClose = function(){
	if (document.getElementById("ad-close")) {
		var closeBut = document.getElementById("ad-close");
		var adBut = document.getElementById("job-ad");
		closeBut.onclick=function() {
			adBut.style.display = "none";
			return false;
		}
	}
}

safariSearch = function() {
	if (browser == "Safari" && document.getElementById("s")) {
		var s = document.getElementById("s");
		s.type = "search";
		s.setAttribute("results", "10");
		s.setAttribute("autosave", "dusted");
	}
}

function correctPNG() // correctly handle PNG transparency in Win IE 5.5 or higher.
   {
   for(var i=0; i<document.images.length; i++)
      {
	  var img = document.images[i]
	  var imgName = img.src.toUpperCase()
	  if (imgName.substring(imgName.length-3, imgName.length) == "PNG")
	     {
		 var imgID = (img.id) ? "id='" + img.id + "' " : ""
		 var imgClass = (img.className) ? "class='" + img.className + "' " : ""
		 var imgTitle = (img.title) ? "title='" + img.title + "' " : "title='" + img.alt + "' "
		 var imgStyle = "display:inline-block;" + img.style.cssText 
		 if (img.align == "left") imgStyle = "float:left;" + imgStyle
		 if (img.align == "right") imgStyle = "float:right;" + imgStyle
		 if (img.parentElement.href) imgStyle = "cursor:hand;" + imgStyle		
		 var strNewHTML = "<span " + imgID + imgClass + imgTitle
		 + " style=\"" + "width:" + img.width + "px; height:" + img.height + "px;" + imgStyle + ";"
	     + "filter:progid:DXImageTransform.Microsoft.AlphaImageLoader"
		 + "(src=\'" + img.src + "\', sizingMethod='scale');\"></span>" 
		 img.outerHTML = strNewHTML
		 i = i-1
	     }
      }
   }
if (window.attachEvent) window.attachEvent("onload", correctPNG);


window.onload = function() {
	mooFx();setFooter();sfHover();tabLink();toggleLink();toggleField();printLink();formLink();stripe('stats');jobAdClose();safariSearch();
}
window.onresize = function() {
	setFooter();
}