window.onload = function(){
	//HomeBanner Functions
	if(document.getElementById('homebanner')){
		loadBannerLinks(-1);
		jQuery(function($) {
		$("#homebanner").hover(
			function () {$("#homeslideshowcaption").fadeIn("slow");}, 
			function () {$("#homeslideshowcaption").fadeOut("slow");}
		);
		$("#homebanner").click(
			function () {$("#homeslideshowcaption").fadeOut("slow");}
		);
		});
	}
	
	// Add Movie to Feature Movie Page
	if ((document.getElementById("videocontainer") && window.location!="http://dev.matterco.com/category/features") || (document.getElementById("videocontainer") && window.location!="http://matterco.com/category/features")) {
		document.getElementById("videocontainer").innerHTML = '<object classid="clsid:02BF25D5-8C17-4B23-BC80-D3488ABDDC6B" width="320" height="255" codebase="http://www.apple.com/qtactivex/qtplugin.cab"><param name="bgcolor" value="#000000"> <param name="src" value="http://matterco.com/wp-content/uploads/Matter_Open_House.mov"></param><param name="autoplay" value="true"></param><param name="controller" value="true"></param><param name="loop" value="false"></param><param name="cache" value="true"></param><embed src="http://matterco.com/wp-content/uploads/Matter_Open_House.mov" width="320" height="255" autoplay="true" controller="true" loop="false" cache="true" pluginspage="http://www.apple.com/quicktime/download/"></embed></object>';
		document.getElementsByTagName("img")[0].focus();
	} else if(document.getElementById("videocontainer")) {
		document.getElementById("videotext").style.display = "none";
		document.getElementById("videocontainer").style.display = "none";
	}
	
	// Toggle Contact Form in Footer
	if(document.getElementById('opencontact')){
		openContact('opencontact');
	}
	if(document.getElementById('openinline')){
		openContact('openinline');
	}
	if(document.getElementById('openform')){
		openContact('openform');
	}
	
	if(document.getElementById('closeformlink')){
		document.getElementById('closeformlink').onclick = function(){
			if(window.parent.document.getElementById('openform').innerHTML=='Close the contact form'){
				fireEvent(window.parent.document.getElementById("openform"),'click');
			}else{
				fireEvent(window.parent.document.getElementById("openform"),'click');
			}
		}
	}
	
	if(document.getElementById('sub_banner')){
		document.getElementById('sub_banner').onclick = function(){
			if(document.getElementById('sub_banner').getAttribute('title')=='Click to Open'){ 
				document.getElementById('sub_banner').getElementsByTagName('div')[0].setAttribute('style','background-position:-980px top;');
				document.getElementById('sub_banner').setAttribute('title','Click to Close');
			}else{
				document.getElementById('sub_banner').getElementsByTagName('div')[0].setAttribute('style','');
				document.getElementById('sub_banner').setAttribute('title','Click to Open');
			}
		}
	}
}

//Home Banner
var activeBanner;
var activeText;
var homeBanner= new Array();
homeBanner[0] = new Array("home0", "Innovation is Dead.", "hometext0");
homeBanner[1] = new Array("home1", "Do-gooder Design.", "hometext1");
homeBanner[2] = new Array("home2", "Beautiful Strategies.", "hometext2");
//homeBanner[3] = new Array("home3", "Dora.", "hometext3");
//homeBanner[4] = new Array("home4", "Barney.", "hometext4");

function loadBannerLinks(active){
	var content = new Array();
	content[0] 	= homeBanner[0];
	content[1] 	= homeBanner[1];
	content[2] 	= homeBanner[2];
	//content[3] 	= homeBanner[3];
	//content[4] 	= homeBanner[4];
	
	var span0 	= document.getElementById("homebanner").getElementsByTagName("span")[0].getElementsByTagName("em")[0];
	var span1 	= document.getElementById("homebanner").getElementsByTagName("span")[1].getElementsByTagName("em")[0];
	
	//Page Load => Get Random Banner
	if(active==-1){ 
		active = Math.floor(Math.random()*content.length);
		new Effect.Parallel([
			new Effect.Appear(content[active][0],{sync: true}),
			new Effect.Appear(content[active][2],{sync: true})
		]);
	}
	activeBanner 	= content[active][0];
	activeText 		= content[active][2];
			
	//Links
	content.splice(active,1);
	span0.innerHTML = (active == 0) ? (content[content.length-1][1]) : (content[active-1][1]);
	if(active == content.length){
		span1.innerHTML = content[0][1];
	}else{
		span1.innerHTML = content[active][1];
	}
}

function bannerLinks(which){
	for(j=0;j<homeBanner.length;j++){
		if(document.getElementById(which).getElementsByTagName("em")[0].innerHTML == homeBanner[j][1]){
			new Effect.Parallel([
 				new Effect.Fade(activeBanner,{sync: true}),
				new Effect.Fade(activeText,{sync: true})		
			]);
			new Effect.Parallel([
				new Effect.Appear(homeBanner[j][0],{sync: true}),
				new Effect.Appear(homeBanner[j][2],{sync: true})	
			]);
			loadBannerLinks(j);
			break;
		}
	}
}

function openContact(id){
	var openLink 	= document.getElementById(id);
	var oInline		= document.getElementById("openinline");
	var oContact	= document.getElementById("opencontact");
	var oForm		= document.getElementById("openform");
	var formname	= window.frames[0].document.getElementById('wpcf_your_name');
	var formemail	= window.frames[0].document.getElementById('wpcf_email');
	var formsubject	= window.frames[0].document.getElementById('wpcf_subject');
	var formmessage	= window.frames[0].document.getElementById('wpcf_msg');
	var cform		= window.frames[0].document.getElementById('c_form_2');
	var cbutton		= window.frames[0].document.getElementById('contactsubmit');
	var ciframe		= window.frames[0].document.getElementById('contactiframe');
			
		openLink.onclick = function(){
			if(document.getElementById("banner_title_project") || document.getElementById("feature-banner") || document.getElementById("project_title")){
				window.scrollTo(0,50000);
			}
			
			if (openLink.innerHTML == "Open the contact form") {
				new Effect.Parallel([
					new Effect.Move('contactdiv', {
						x: 0,
						y: -415,
						mode: 'absolute',
						transition: Effect.Transitions.sinoidal
					}), new Effect.Morph('contactdiv', {
						style: 'width:640px;',
						duration: 0.7
					}),	new Effect.Morph('contactiframe', {
						style: 'width:640px;',
						duration: 0.7
					}), new Effect.Morph(formname, {
						style: 'width:244px;',
						duration: 1
					}), new Effect.Morph(formemail, {
						style: 'width:244px;',
						duration: 1
					}), new Effect.Morph(formsubject, {
						style: 'width:244px;',
						duration: 1
					}), new Effect.Morph(formmessage, {
						style: 'width:244px;',
						duration: 1
					}), new Effect.Morph(cbutton, {
						style: 'width:98px;',
						duration: 1
					})
				]);

				openLink.innerHTML = "Close the contact form";
				
				if(oInline){
					oInline.innerHTML = "Close the contact form";
				}
				if(oContact){
					oContact.innerHTML = "Close the contact form";
				}
				if(oForm){
					oForm.innerHTML = "Close the contact form";
				}
			}
			else {
				new Effect.Parallel([
						new Effect.Move('contactdiv', {
						x: 640,
						y: -415,
						mode: 'absolute',
						transition: Effect.Transitions.sinoidal
					}),new Effect.Morph('contactdiv', {
						style: 'width:210px;',
						duration: 0.6
					}),	new Effect.Morph('contactiframe', {
						style: 'width:210px;',
						duration: 0.6
					}), new Effect.Morph(formname, {
						style: 'width:10px;',
						duration: 0.6
					}), new Effect.Morph(formemail, {
						style: 'width:10px;',
						duration: 0.6
					}), new Effect.Morph(formsubject, {
						style: 'width:10px;',
						duration: 0.6
					}), new Effect.Morph(formmessage, {
						style: 'width:10px;',
						duration: 0.6
					}), new Effect.Morph(cbutton, {
						style: 'width:1px;',
						duration: 0.6
					})
				]);
				
				openLink.innerHTML = "Open the contact form";
				
				if(oInline){
					oInline.innerHTML = "Open the contact form";
				}
				if(oContact){
					oContact.innerHTML = "Open the contact form";
				}
				if(oForm){
					oForm.innerHTML = "Open the contact form";
				}
			}
			return false;
		}
}

function fireEvent(obj,evt){
	var fireOnThis = obj;
	if( document.createEvent ) {
		var evObj = document.createEvent('MouseEvents');
		evObj.initEvent( evt, true, false );
		fireOnThis.dispatchEvent(evObj);
	} else if( document.createEventObject ) {
		fireOnThis.fireEvent('on'+evt);
	}
}

