/*

	flash killin' javascript by jsilvestris
	                            http://x-industries.com

*/


var dom=document.getElementById;
var iex=document.all;
var ns4=document.layers;
var nextZ=1;
document.speed=10;

getElement=function(name,nest){
	/* squidfingers */
	var nest=nest?'document.'+nest+'.':'';
	var el=dom?document.getElementById(name):iex?document.all[name]:ns4?eval(nest+'document.'+name):false;
	return el;
}

function scaleText(){
	var newTextSize=document.body.clientWidth/75;
	if((newTextSize)>=12){document.body.style.fontSize=(newTextSize)+"px";}
	else{document.body.style.fontSize="9pt";}
	document.needsSave=true;
	checkForSave();
	return true;
}

/* yep, that's asynchronous javascript, all right. */

function newXMLHttpRequest(){
	var errors;
	if(window.XMLHttpRequest){
		try{return new XMLHttpRequest();}
		catch(e){errors=true;}
	}else if(window.ActiveXObject){
		try{return new ActiveXObject("Msxml2.XMLHTTP");}
		catch(e){
			try{return new ActiveXObject("Microsoft.XMLHTTP");}
			catch(e){errors=true;}
		}
	}
	if(errors)return false;
}

var req;
function checkForSave(){
	if(document.needsSave){
		req=false;
		req=newXMLHttpRequest();
		if(req){
			req.onreadystatechange=processSave;
			req.open("GET", document.path+'inc/javascriptvars.php?javascript_var1='+document.body.style.fontSize, true);
			req.send("");
		}
		document.needsSave=false;
	}
}
function processSave(){
	if(req.readyState==4){
		if(req.status==200){
			//getElement("javascriptErrors").innerHTML=(req.responseText);
		}else{
			if(!document.alertMade){
				getElement("javascriptErrors").innerHTML="There was a problem saving your window size preference, please ignore flickering fonts sizes in the navigation between page loads. "+req.statusText;
				document.alertMade=true;
			}
		}
	}
}

var req2;
function sendEmail(){
	req2=false;
	req2=newXMLHttpRequest();
	if(req2){
		var errors=false;
		if(document.forms["contact"].email.value){var emailaddress=document.forms["contact"].email.value;}
		else errors=true;
		if(document.forms["contact"].message.value){var message=document.forms["contact"].message.value;}
		else errors=true;
		if(!errors){
			var querystring="ajax=true&email="+emailaddress+"&message="+message;
			req2.onreadystatechange=processEmail;
			req2.open("POST", document.path+'inc/sendemail.php?'+querystring, true);
			req2.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
			req2.send(querystring+"");
			getElement("contactform").innerHTML="<h3>Processing...</h3>";
		}else{alert("Please make sure you supply your email address and a message before sending your email.");}
	}
}
function processEmail(){
	if(req2.readyState==4){
		if(req2.status==200){
			getElement("contactform").innerHTML=(req2.responseText);
		}else{
			alert("Oops, we couldn't send your email! Try again, perhaps? Error: "+req2.statusText);
		}
	}
}

zeroAlpha=function(el){getElement(el).style.opacity=0;getElement(el).style.filter='alpha(opacity=0)';}

fadeShowHide=function(el){
	var obj=getElement(el);
	if(obj.style.opacity){obj.currOpacity=obj.style.opacity*10;}else obj.currOpacity=0;
	if(obj.currOpacity!=obj.trgt){
		var newVal=obj.trgt+(obj.currOpacity-obj.trgt)/1.4;
		if(newVal>9.99)newVal=10;
		if(newVal<0.01)newVal=0;
		obj.style.opacity=newVal/10;
		obj.style.filter='alpha(opacity='+parseInt(newVal*10)+')';
		obj.style.display="block";
		if(/animate/.test(el)&&obj.currOpacity<1&&obj.trgt==0)obj.style.display="none";
		if(obj.trgt!=obj.currOpacity){setTimeout("fadeShowHide('"+el+"')",document.speed);}
	}
}

objTween=function(el,attr,unit){
	/*v4*/
	var e=getElement(el);
	var es=e.style;
	var cur=parseInt(es[attr]);
	var target=parseInt(e["target_"+attr]);
	var rate=100;
	if(cur!=target){
		var newVal=parseInt((target+(parseInt(es[attr])-target)/2)+.5);
		if(newVal+1>=target&&newVal-1<=target)newVal=target;
		//confirm this works for negative values?
		es[attr]=newVal+unit;
		setTimeout("objTween('"+el+"','"+attr+"','"+unit+"');",rate);
		if(el=="contactform")window.scrollTo(0,document.height);
		// "objTween=function(el,attr,unit,extraFunction)" && "setTimeout(extraFunction,0);" ?
		// ...or just account for screen position and scroll?
	}
}

bgTween=function(el,attr,unit){
	var e=getElement(el);
	var es=e.style
	var cur=parseInt(e.cur);
	var target=parseInt(e["target_"+attr]);
	var rate=260;
	if(cur!=target){
		var newVal=parseInt(target+(parseInt(e.cur)-target)/2);
		if(newVal+1>=target&&newVal-1<=target)newVal=target;
		//confirm this works for negative values?
		es[attr]=newVal+unit+" 0px";
		e.cur=newVal;
		setTimeout("bgTween('"+el+"','"+attr+"','"+unit+"');",rate);
	}
}

function scrollimg(){
	var imgscroll=getElement("imgscroll");
	if(!imgscroll.target_backgroundPosition){
		imgscroll.target_backgroundPosition=0;
		imgscroll.cur=0;
	}
	imgscroll.target_backgroundPosition=imgscroll.cur-174;
	bgTween("imgscroll","backgroundPosition","px");
	setTimeout("scrollimg();",6000);
}

function introAnimation(){
	for(strcount=0;getElement("str"+strcount);strcount++){
		var str=getElement("str"+strcount);
		str.trgt=10;
		str.style.display="none";
	}
	var intro=getElement("intro");
	intro.style.display="none";
	intro.trgt=10;
	var launchform=getElement("launchform");
	launchform.style.display="none";
	launchform.trgt=10;
	var cform=getElement("contactform");
	cform.style.visibility="hidden";
	var animate=getElement("animate");
	animate.style.display="block";
	animate.style.opacity=10;
	var nav=getElement("nav");
	nav.style.display="none";
	nav.trgt=10;
	// this is a job for strcount!
	for(strcount=0;getElement("str"+strcount);strcount++){
		setTimeout('fadeShowHide("str'+strcount+'")',strcount*1000);
	}
	setTimeout('getElement("animate").trgt=0;fadeShowHide("animate")',(strcount*1000)+(strcount*300));
	setTimeout('showPage();',(strcount*1000)+(strcount*400));
}

function showPage(){
	getElement("contactform").style.visibility="visible";
	getElement("nav").style.visibility="visible";
	fadeShowHide("nav");
	fadeShowHide("intro");
	fadeShowHide("launchform");
}

function waitForLoad(){
	if(getElement("doneLoading")){
		doneLoading();
	}else setTimeout('waitForLoad();',100);
}
function doneLoading(){
	var cform=getElement("contactform");
	cform.innerHTML='<form id="contact" name="contact" method="POST"><fieldset><legend>Contact Form</legend><label>To:</label><input type="text" id="inquire" readonly="readonly" value="" /><label for="email">From:</label><input type="text" name="email" /><div style="display:none"><label for="no">Leave Blank:</label><textarea name="spambot"></textarea></div><label for="message">Message:</label><textarea name="message"></textarea><br clear="all" /><div id="buttons"><button type="submit" id="submitButton">Send Email</button></div></fieldset></form>';
	cform.onsubmit=function(){
		sendEmail();
		return false;
	}
	/* adjust everything here. */
	getElement('inquire').value="inquire"+"@"+"x-industries.com";
	cform.style.width='0%';
	cform.style.visibility="visible";
	document.contactformheight=cform.offsetHeight;
	if(iex)document.contactformheight-=80;
	cform.style.height='0px';
	document.contactform=false;
	var formLaunchers=getElement("right").getElementsByTagName("a");
	for(t=0;t<formLaunchers.length;t++){
		if(/about\/contact.php/.test(formLaunchers[t].href))formLaunchers[t].href='javascript:contactform();';
	}
	getElement("launchform").href='javascript:contactform();';
	scaleText();
	getElement("content").style.visibility="visible";
	getElement("content").trgt=10;
	fadeShowHide("content");
	if(getElement('intro')){introAnimation();}
	else{getElement("nav").style.visibility="visible";}
	/* get that scroll going */
	if(getElement("imgscroll"))setTimeout("scrollimg();",4200);
	/* mouseover on the logo */
	getElement("logo").onmouseover=function(){
		getElement("right").style.backgroundImage="url("+document.path+"inc/img/whitent-hover.gif)";
	}
	getElement("logo").onmouseout=function(){
		getElement("right").style.backgroundImage="url("+document.path+"inc/img/whitent.gif)";
	}
	/* navigation stuff */
	var navItems=getElement("nav").getElementsByTagName('a');
	for(i=0;i<navItems.length;i++){
		navItems[i].id="nav"+i;
		if(navItems[i].className!="on"){
			navItems[i].style.paddingLeft="0px";
			navItems[i].og_paddingLeft="0";
			navItems[i].onmouseover=function(){
				this.target_paddingLeft="10px";
				objTween(this.id,"paddingLeft","px");
			}
			navItems[i].onmouseout=function(){
				this.target_paddingLeft=this.og_paddingLeft;
				setTimeout('objTween("'+this.id+'","paddingLeft","px");',200);
			}
		}
	}
	window.onresize=function(){scaleText();}
}

function contactform(){
	if(document.contactform==true){
		document.contactform=false;
		getElement('contactform').target_width='0%';
		getElement('contactform').target_height="0";
	}else{
		document.contactform=true;
		getElement('contactform').style.width='0%';
		getElement('contactform').target_width='100%';
		getElement('contactform').target_height=document.contactformheight;
	}
	objTween('contactform','width','%');
	objTween('contactform','height','px');
}

waitForLoad();