// JavaScript Document

window.addEvent("domready", function(){
	def.init();
});

// pl functions
def={
	init:function(){
		//if($('header')){ mm.init(); }
		//if($('dirSSlist')){ dirSS.init($('dirSSlist')); }
		//if($('workthumbs')){ thumbs.init(); }
		if($('nonflashintro')){ h5v.init(); }
		if($('intromovie')){ hm.init(); }
		if($('dropprojectlist')){ pl.init(); }
		if($('searchAll')){ sb.init(); }
	}
}

// home
hm={
	init:function(){
		mp.loadFlash("fyzzintro2.mp4");
	}
}

// html5 video
h5v={
	init:function(){
		if(!$('nonflashintro').hasClass('typeipad')){
			//$('nonflashintro').play();
		}
		//(function(){ h5v.showTime(); }).periodical(500);
		$('nonflashintro').addEvent('canplaythrough',function(an_event){
			//alert('loaded');
			
		});
	},
	showTime:function(){
		db.display($('nonflashintro').currentTime);
	}
}

// movie player
mp={
	loadFlash:function(movieName){
		try{
			var so = new SWFObject("assets/main/movieplayer.swf", "player", "960", "432", "9", "#000000");
			so.addVariable("movieName", movieName); //LOADS IN FLASH
			so.write("intromovie");
		}catch(err){
			//$('fyzzintrostill').removeClass('hideMe');
		}
		if($('fyzzintrostill')){
			//alert('no flash');
			$('fyzzintrostill').removeClass('hideMe');
			//var h5vstring = "<video id=\"nonflashintro\" autoplay=\"true\" controls=\"controls\" height=\"768\" width=\"432\"><source src=\"assets/main/fyzzintro.mp4\" type=\"video/mp4\"><source src=\"assets/main/fyzzintro.webm\" type=\"video/webm\"><source src=\"assets/main/fyzzintro.ogv\" type=\"video/ogg\"><img id=\"fyzzintrostill\" src=\"assets/main/fyzzintrostill.jpg\" width=\"768\" height=\"432\" /></video>";
			//$('intromovie').set('html',h5vstring);
		}
		
	},
	checkFlashLoaded:function(){
		if($('movieLink')){
			// flash didn't load
			cl.flashUpdateString = "<h2>Flash required</h2>";
			cl.flashUpdateString += "\n<div id=\"flashWarning\">";
			cl.flashUpdateString += "<p>You need to <a href=\"update.html\">update your flash player</a> to view these movies</p>";
			cl.flashUpdateString += "<p>Alternatively, you can view a quicktime of the movie you were trying to watch in a <a href=\""+cl.movieHref +"\" target=\"_blank\">new window</a></p>";
			cl.flashUpdateString += "</div>";
			$('pageContent').innerHTML = cl.flashUpdateString;
		}else{
			// flash loaded	
		}
	}
}

// project list
pl={
	init:function(){
		pl.liststatus = 'closed';
		$('dropprojectlist').getElement('a').addEvent('click',function(){
			pl.toggleList();
			return false;
		});
	},
	toggleList:function(){
		if(pl.liststatus == 'closed'){
			$('dropprojectlist').getElement('a').addClass('cursel');
			$('fullprojectlist').removeClass('hideMe');
			pl.buttonStatus = 'over';
			pl.listStatus = 'out';
			$('dropprojectlist').getElement('a').addEvent('mouseleave',function(){
				pl.buttonStatus = 'out';
				pl.checkMouseStatus();
			});
			$('dropprojectlist').getElement('a').addEvent('mouseenter',function(){
				pl.buttonStatus = 'over';
				pl.checkMouseStatus();
			});
			$('fullprojectlist').addEvent('mouseleave',function(){
				pl.listStatus = 'out';
				pl.checkMouseStatus();
			});
			$('fullprojectlist').addEvent('mouseenter',function(){
				pl.listStatus = 'over';
				pl.checkMouseStatus();
			});
			pl.liststatus = 'open';
		}else{
			if(pl.timer){
				clearInterval(pl.timer);
				pl.timer = null;
			}
			$('dropprojectlist').getElement('a').removeEvents('mouseleave');
			$('dropprojectlist').getElement('a').removeEvents('mouseenter');
			$('fullprojectlist').removeEvents('mouseleave');
			$('fullprojectlist').removeEvent('mouseenter');
			$('dropprojectlist').getElement('a').removeClass('cursel');
			$('fullprojectlist').addClass('hideMe');
			pl.liststatus = 'closed';
		}
	},
	countDown:function(){
		pl.counter--;
		//db.display(pl.counter);
		if(pl.counter == 0){
			pl.toggleList();
		}
	},
	checkMouseStatus:function(){
		if(pl.buttonStatus == 'out' && pl.listStatus == 'out'){
			pl.counter = 2;
			pl.timer = pl.countDown.periodical(300);
		}else{
			if(pl.timer){
				clearInterval(pl.timer);
				pl.timer = null;
			}
		}
	}
}

// search box
sb={
	init:function(){
		sb.searchAll = false;
		$('searchAll').addEvent('click',function(){
			if(sb.searchAll == false){
				$('searchAllBox').addClass('on');
				$('all').set('checked','checked');
				sb.searchAll = true;
				
			}else{
				$('searchAllBox').removeClass('on');
				sb.searchAll = false;
				$('all').erase('checked');
			}
			return false;
		});
		$('searchAll').addEvent('dblclick',function(){
			
			return false;
		});
	}
}

// debug output
db={
	init:function(){
		var outputHolder = new Element('div',{
			id:'output',
			styles:{
				position:'absolute',
				top:'100px',
				left:'100px',
				background:'#444',
				color:'#FFF',
				'z-index':'300',
				padding:'10px'
			}
		});
		outputHolder.inject(document.body);
	},
	display:function(string){
		if(!$('output')){
			db.init();
		}
		if(string || string == 0){
			$('output').set('html',string);
		}else{
			$('output').set('text','no string set');
		}
	},
	remove:function(){
		if($('output')){
			$('output').dispose();
		}
	}
}
