var featured = new Array();
var http = getHTTPObject();
function init() {
	if(!document.getElementById) {return false;}
	try {
		document.execCommand('BackgroundImageCache', false, true);
	} catch(e) {}
	if(document.getElementsByTagName("body")[0].className == "home") {
		featuredEffects();
		showFeatured();	
	}
	if(document.getElementsByTagName("body")[0].className == "portfolio") {
		makeHovers();
	}
	if(document.getElementsByTagName("body")[0].className == "article") {
		moveComments();
		hoverFields("commentformarea");
		hoverButtons();
	}
	if((document.getElementsByTagName("body")[0].className == "about") || (document.getElementsByTagName("body")[0].className == "contact")) {
		emailCloak();
	}
	if(document.getElementsByTagName("body")[0].className == "contact") {
		hoverFields("contactform");
		hoverButtons();
	}
	determineStyle();
}

function featuredEffects() {
	featured = document.getElementById("works").getElementsByTagName("li");
	for(var q = 0; q < featured.length; q++) {
		featured[q].getElementsByTagName("a")[0].style.backgroundPosition = "0 0";
		featured[q].onmouseover = function() {
			for(var w = 0; w < featured.length; w++) {featured[w].getElementsByTagName("a")[0].style.backgroundPosition = "0 100%";}
			this.getElementsByTagName("a")[0].style.backgroundPosition = "0 50%";
		}
		featured[q].onmouseout = function() {for(var w = 0; w < featured.length; w++) {featured[w].getElementsByTagName("a")[0].style.backgroundPosition = "0 50%";}}
	}
	var featuredZone = document.getElementById("works");
	featuredZone.onmouseout = function() {for(var w = 0; w < featured.length; w++) {featured[w].getElementsByTagName("a")[0].style.backgroundPosition = "0 0";}}
}
function getHTTPObject() { 
	var xmlhttp; 
	/*@cc_on 
	@if (@_jscript_version >= 5) 
	try { 
		xmlhttp = new ActiveXObject("Msxml2.XMLHTTP"); 
	} catch (e) { 
		try { 
			xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); 
		} catch (E) {
			xmlhttp = false; 
		} 
	} 
	@else 
	xmlhttp = false; 
	@end @*/ 
	if (!xmlhttp && typeof XMLHttpRequest != 'undefined') { 
		try { 
			xmlhttp = new XMLHttpRequest();
				xmlhttp.overrideMimeType("text/xml");
		} catch (e) { 
			xmlhttp = false; 
		} 
	} 
	return xmlhttp; 
}
function showFeatured() {
	http.open("GET", "/ui/data/featured.xml", true);
	http.onreadystatechange = doFeatured;
	http.send(null);
}
function doFeatured() {
	if (http.readyState == 4) {
		if (http.responseText.indexOf('invalid') == -1) {
			var xmlDocument = http.responseXML;
			var items = xmlDocument.getElementsByTagName("workitem");
			var uniques = new Array(6);
			for(var q = 0; q < 6; q++) {uniques[q] = 999;}
			var randomNo = 999;
			for(var q = 0; q < 6; q++) {
				while((randomNo == uniques[0]) || (randomNo == uniques[1]) || (randomNo == uniques[2]) || (randomNo == uniques[3]) || (randomNo == uniques[4]) || (randomNo == uniques[5])) {
					randomNo = Math.floor(Math.random()*items.length);
				}
				uniques[q] = randomNo;
			}
			for(var q = 0; q < 6; q++) {
				var myLink = featured[q].getElementsByTagName("a")[0];
				myLink.href = items[uniques[q]].getAttribute("href");
				myLink.title = items[uniques[q]].getAttribute("title");
				myLink.style.background = "url(" + items[uniques[q]].getAttribute("href") + "thumb.jpg) no-repeat 0 0";
				var myText = document.createTextNode(myLink.title);
				var myTextContainer = document.createElement("i");
				myTextContainer.appendChild(myText);
				myLink.replaceChild(myTextContainer, myLink.getElementsByTagName("i")[0]);
			}
		}
	}
}
function makeHovers() {
	var list = document.getElementById("portfoliolist");
	for (var q = 0; q < list.childNodes.length; q++) {
		if(list.childNodes[q].nodeName == "LI") {
			var link1 = list.childNodes[q].getElementsByTagName("a")[0];
			var link2 = list.childNodes[q].getElementsByTagName("a")[1];
			link1.onmouseover = function() {this.parentNode.getElementsByTagName("a")[1].className = "hover";}
			link1.onmouseout = function() {this.parentNode.getElementsByTagName("a")[1].className = "";}
			link2.onmouseover = function() {this.parentNode.parentNode.getElementsByTagName("a")[0].className += " hover";}
			link2.onmouseout = function() {this.parentNode.parentNode.getElementsByTagName("a")[0].className = this.parentNode.parentNode.getElementsByTagName("a")[0].className.replace(/ hover/g, "");}
		}
	}
}
function slideShow(no, sno) {
	for(var q = 0; q < sno.parentNode.parentNode.childNodes.length; q++) {if(sno.parentNode.parentNode.childNodes[q].className) {sno.parentNode.parentNode.childNodes[q].className = sno.parentNode.parentNode.childNodes[q].className.replace(/ active/g, "");}}
	sno.parentNode.className += " active";
	var obj = document.getElementById("photo").getElementsByTagName("ul")[0];
	if(obj.style.marginLeft != 0) {var x = parseInt(obj.style.marginLeft.replace(/px/g, "")); x = -x;}
	else {var x = obj.style.marginLeft;}
	--no;
	var newx = no*284;
	slideIt(x, newx);
}
function slideIt(x, newx) { //inspired by Jeremy Keith - www.adactio.com
	if (newx > x) {x += Math.ceil((newx-x)/2);}
	else if (newx < x) {x += Math.floor((newx-x)/2);}
	else if (newx == x) {return true;}
	var obj = document.getElementById("photo").getElementsByTagName("ul")[0].style.marginLeft = -x + 'px';
	var time = setTimeout('slideIt('+x+', '+newx+')', 75);
}
function moveComments() {
	var form = document.getElementById("commentformarea");
	var browserWidth = getBrowserWidth();
	if(browserWidth <= 925) {
		form.style.top = "auto";
		form.style.left = "auto";
		return;
	}
	if(browserWidth > 925) {
		var curPos = findPosY(form);
		if (window.innerHeight) {pos = window.pageYOffset;}
		else if (document.documentElement && document.documentElement.scrollTop) {pos = document.documentElement.scrollTop;}
		else if (document.body) {pos = document.body.scrollTop;}
		if(pos > curPos) {
			curPos = pos;
			form.style.top = ++curPos + 'px';
		}
		if(pos < curPos) {
			if(pos > 454) {
				curPos = pos;
				form.style.top = ++curPos + 'px';
			}
			else if(pos <= 454) {
				curPos = pos;
				form.style.top = 454 + 'px';
			}
		}
		var temp = setTimeout('moveComments()',50);
	}
}
function findPosY(obj) { //inspired by Peter-Paul Koch - www.quirksmode.org
	var curtop = 0;
	if (obj.offsetParent) {
		while (obj.offsetParent) {
			curtop += obj.offsetTop
			obj = obj.offsetParent;
		}
	}
	else if (obj.y) {curtop += obj.y;}
	return curtop;
}
function hoverFields(what) {
	var elements = document.getElementById(what).getElementsByTagName("input");
	for(var q = 0; q < elements.length; q++) {
		if(elements[q].parentNode.nodeName == "DIV") {
			elements[q].onfocus = function() {this.parentNode.style.backgroundPosition = "0 50%";}
			elements[q].onblur = function() {this.parentNode.style.backgroundPosition = "0 0";}
		}
	}
	var elements = document.getElementById(what).getElementsByTagName("textarea")[0];
	elements.onfocus = function() {this.parentNode.style.backgroundPosition = "0 50%";}
	elements.onblur = function() {this.parentNode.style.backgroundPosition = "0 0";}
}
function hoverButtons() {
	var buttons = document.getElementsByTagName("input");
	for(var q = 0; q < buttons.length; q++) {
		if(buttons[q].type == "image") {
			buttons[q].onmouseover = function() {this.src = this.src.replace(/_off/g, "_over");}
			buttons[q].onmouseout = function() {this.src = this.src.replace(/_over/g, "_off");}
		}
	}
}
function emailCloak() {
	if (document.getElementById) {
		var alltags = document.all? document.all : document.getElementsByTagName("*");
		for (i=0; i < alltags.length; i++) {
		  if (alltags[i].className == "emailCloak") {
			var oldText = alltags[i].firstChild;
			var emailAddress = alltags[i].firstChild.nodeValue;
			var user = emailAddress.substring(0, emailAddress.indexOf("("));
			var website = emailAddress.substring(emailAddress.indexOf(")")+1, emailAddress.length);
			var newText = user+"@"+website;
			var a = document.createElement("a");
			a.href = "mailto:"+newText;
			var address = document.createTextNode(newText);
			a.appendChild(address);
			alltags[i].replaceChild(a,oldText);
		  }
		}
	}
}
function getBrowserWidth() { //from ParticleTree - www.particletree.com
	if (window.innerWidth) {return window.innerWidth;}	
	else if (document.documentElement && document.documentElement.clientWidth != 0) {return document.documentElement.clientWidth;}
	else if (document.body) {return document.body.clientWidth;}		
	return 0;
}
function determineStyle() { //inspired by A List Apart - www.alistapart.com
	var browserWidth = getBrowserWidth();
	if(browserWidth <= 990) {
		for(var q = 0; (a = document.getElementsByTagName("link")[q]); q++) {
			a.disabled = true;
			if(a.getAttribute("title") == "narrow") a.disabled = false;
		}
	}
	if(browserWidth > 990) {
		for(var q = 0; (a = document.getElementsByTagName("link")[q]); q++) {
			a.disabled = true;
			if(a.getAttribute("title") == "wide") a.disabled = false;
		}
	}
}
function doResize() {
	determineStyle();
	if(document.getElementsByTagName("body")[0].className == "article") {
		var form = document.getElementById("commentformarea");
		var browserWidth = getBrowserWidth();
		if(browserWidth <= 990) {
			form.parentNode.removeChild(form);
			document.getElementsByTagName("body")[0].appendChild(form);
		}
		if(browserWidth > 990) {
			form.parentNode.removeChild(form);
			document.getElementById("infos").appendChild(form);
		}
		moveComments();
	}
}

window.onload = init;
window.onresize = doResize;
