// JavaScript Document

function fadein (div_id) { 
	document.getElementById(div_id).style.display="block";
	document.getElementById(div_id).style.opacity="0";
	document.getElementById(div_id).style.MozOpacity="0";
	document.getElementById(div_id).style.filter="alpha(opacity=0)";
	this.timer=0;
	this.speed=5;
	for (i=0; i<=100; i++) {
		setTimeout('upopacity(\''+i+'\',\''+div_id+'\')', this.timer*this.speed);	
		this.timer++;
	}
}
function fadeout(div_id) {
	this.timerfadeout=0;
	this.speedfadeout=5;
	for (i=0; i<=100; i++) {
		setTimeout('downopacity(\''+i+'\',\''+div_id+'\')', this.timerfadeout*this.speedfadeout);	
		this.timerfadeout++;
		if (i==100) {
			document.getElementById(div_id).style.display="none";
		}
	}
}
function upopacity (opacityvar,div_id) {
	var objectvar = document.getElementById(div_id);
	objectvar.style.opacity=opacityvar/100;
	objectvar.style.MozOpacity=opacityvar/100;
	objectvar.style.filter="alpha(opacity="+opacityvar+")";
}

function downopacity(opacityvar,div_id) {
	document.getElementById(div_id).style.opacity=1-opacityvar/100;
	document.getElementById(div_id).style.MozOpacity=1-opacityvar/100;
	document.getElementById(div_id).style.filter="alpha(opacity="+1-opacityvar+")";
}

function loadajax (pageurl, cssdoc, jsdoc, div_id, newtitle, fadeboolean) {
	if (window.XMLHttpRequest) {
		pagerequest = new XMLHttpRequest();
	} else {
		pagerequest = new ActiveXObject ("Microsoft.XMLHTTP");
	}
	timestamp = "?t="+new Date().getDate()+":"+new Date().getMonth()+":"+new Date().getTime();
	pagerequest.open("GET", pageurl+timestamp, true);
	pagerequest.send(null);
	pagerequest.onreadystatechange=function () {
		if (pagerequest.readyState==4 && (pagerequest.status==0 || pagerequest.status==200)) {		
			if (cssdoc!="none" || cssdoc!="None") {
				var csslink="";
				if (cssdoc.indexOf(".css")!=-1){
					csslink=document.createElement("link")
					csslink.setAttribute("rel", "stylesheet");
					csslink.setAttribute("type", "text/css");
					csslink.setAttribute("href", "css/"+cssdoc);
					document.getElementsByTagName("head").item(0).appendChild(csslink);
				}
			}
			if (jsdoc!="none" || jsdoc!="None") {
				var jslink=""
				if (jsdoc.indexOf(".js")!=-1){
					jslink=document.createElement('script')
					jslink.setAttribute("type","text/javascript");
					jslink.setAttribute("src", "scripts/"+jsdoc);
					document.getElementsByTagName("head").item(0).appendChild(jslink);
				}
			}
			document.getElementById(div_id).innerHTML = "";
			document.getElementById(div_id).innerHTML=pagerequest.responseText;
			window.location.hash=pageurl.split('.')[0];
			
			if (newtitle!="none") {
				document.title=newtitle;
			}
					
			if (fadeboolean=="YES" || fadeboolean=="Yes" || fadeboolean=="yes") {
				document.getElementById(div_id).style.visibility="visible";
				document.getElementById(div_id).style.opacity="0";
				document.getElementById(div_id).style.MozOpacity="0";
				document.getElementById(div_id).style.filter="alpha(opacity=0)";
				var timer=0;
				var speed=2;
				setTimeout('fadein(\''+div_id+'\')',200);
			} else {
				document.getElementById(div_id).style.opacity="0";
				document.getElementById(div_id).style.MozOpacity="0";
				document.getElementById(div_id).style.filter="alpha(opacity=0)";
			}
		}
	}
}
function loadxmldata(xmlurl) {
	if (window.XMLHttpRequest) {
		xmlrequest = new XMLHttpRequest();
	} else {
		xmlrequest = new ActiveXObject("Microsoft.XMLHTTP");
	}
	xmlrequest.open("GET", xmlurl, false);
	xmlrequest.send(null);
	this.xmldata=xmlrequest.responseXML;
	return this.xmldata;
}
function movediv (div_id, marginstart, marginend, duration, type) {
	if (arguments.length!=5) { return; } else {
		this.div_id=div_id;
		this.margin=marginstart;
		this.marginend=marginend;
		this.duration=duration;
		this.type=type;
		this.startmargintop=Number(this.margin.split(' ')[0].replace('px',''));
		this.startmarginright=Number(this.margin.split(' ')[1].replace('px',''));
		this.startmarginbottom=Number(this.margin.split(' ')[2].replace('px',''));
		this.startmarginleft=Number(this.margin.split(' ')[3].replace('px',''));	
		this.endmargintop=Number(this.marginend.split(' ')[0].replace('px',''));
		this.endmarginright=Number(this.marginend.split(' ')[1].replace('px',''));
		this.endmarginbottom=Number(this.marginend.split(' ')[2].replace('px',''));
		this.endmarginleft=Number(this.marginend.split(' ')[3].replace('px',''))
		
		this.margintopconstant=(this.endmargintop-this.startmargintop)/100;
		this.marginrightconstant=(this.endmarginright-this.startmarginright)/100;
		this.marginbottomconstant=(this.endmarginbottom-this.startmarginbottom)/100;
		this.marginleftconstant=(this.endmarginleft-this.startmarginleft)/100;
		
		this.timer=0;
		this.speed=this.duration/100;
		for (this.i=0; this.i<100; this.i++) {
			this.newmargintop=String(this.startmargintop+(this.margintopconstant*this.i))+"px";
			this.newmarginright=String(this.startmarginright+(this.marginrightconstant*this.i))+"px";
			this.newmarginbottom=String(this.startmarginbottom+(this.marginbottomconstant*this.i))+"px";
			this.newmarginleft=String(this.startmarginleft+(this.marginleftconstant*this.i))+"px";
			this.newmargin=this.newmargintop+" "+this.newmarginright+" "+this.newmarginbottom+" "+this.newmarginleft;
			var self=this;
			setTimeout('movemargin(\''+this.div_id+'\',\''+this.newmargin+'\')', this.timer*this.speed);
			this.timer++;
		}	
	}	
}
function movemargin (element,margins) {
	document.getElementById(element).style.margin=margins;
	document.getElementById(element).style.position="absolute";
}

//Checks hash values to follow along the back and bookmarking functions of browsers.//
var currenthash='';
var indexloaded=true;

function hashtimer () {
	this.newtemphash=window.location.hash;
	if (this.newtemphash!=currenthash) {
		currenthash=this.newtemphash;
		indexloaded=false;
		hashchecker();
	}
}

function hashchecker () {
	if (window.location.hash=="") {
		if (indexloaded==true) {
			return;
		} else {
			document.getElementById('displaycontainer').innerHTML="";
			loadajax('cover.html','cover.css','cover.js','displaycontainer','Wordsalad','yes');
			indexloaded=true;
		}
	} else {
		this.hashvalue=String(window.location.hash).replace('#','').split('.')[0];
		this.htmlvalue=this.hashvalue+'.html';
		this.cssvalue=this.hashvalue+'.css';
		this.jsvalue=this.hashvalue+'.js';
		this.div_id="displaycontainer";
		if (this.hashvalue=="cdartwork") {
			this.titlevalue="Wordsalad - Album Cover Artwork";
		}
		if (this.hashvalue=='television') {
			this.titlevalue='Wordsalad - Television';
		}
		if (this.hashvalue=='exhibition') {
			this.titlevalue='Wordsalad - Exhibition and Outdoors';
		}
		if (this.hashvalue=='publication') {
			this.titlevalue='Wordsalad - Publications & Prints';
		}
		if (this.hashvalue=='corporate') {
			this.titlevalue='Wordsalad - Corporate Design';
		}
		if (this.hashvalue=='contactform') {
			this.titlevalue='Wordsalad - Contact Form';
		}
		if (this.hashvalue=='cover') {
			this.titlevalue='Wordsalad';
		}
		currenthash=window.location.hash;
		loadajax(this.htmlvalue,this.cssvalue,this.jsvalue,this.div_id,this.titlevalue,'yes');
		document.getElementById('displaycontainer').style.backgroundColor="#FFFFFF";
		indexloaded=false;
	}
}

function validateurl (urlvar) {
	var $urlstring=(window.location.pathname);
	var $urlsplit=$urlstring.split ("/");
	var $elementnumber=$urlsplit.length - 1;
	
	if ($urlsplit[$elementnumber]==urlvar) {
		location.href="index.html#"+urlvar;
	};
}



