var resizeTimer = null;
var navTimer = null;
var myStrip = null;
var slide;
var isIE6 = /msie|MSIE 6/.test(navigator.userAgent);
var mousey;
var navOpen;
var ctop,htop,animating;
$(document).ready(function(e){
	
	//initNavigation();
	
	//projects page
	var projects = $('#projects .row').text();
	
	if(projects != null && projects != ""){
		stretchProjects();
	}
	
	//project overview page
	var project = $('#proj_container').text();
	
	if(project != null && project != ""){
		slide = "project";
		iStripInit(slide);
		if(!isIE6){
			initRollover();
		}
	}
	
	//publications page
	var publication = $('.slideshow').text();
	
	if(publication != null && publication != ""){
		slide = "publication";
		iStripInit(slide);
	}
	
	//bg full screen
	var bgimg = $('#bg_img').html();
	
	if(bgimg != null && bgimg != ""){
		resizeBg();
	}
	
	//flash video
	var flash_video = $('#flash_video').text();
	
	if(flash_video != null && flash_video != ""){
		var v = $('span.video').text();
		var h = $('span.height').text();
		var w = $('span.width').text();
		var i = $('#flash_video img').attr("src");
		if(i == "" || i == null || i == undefined){
			i = "http://"+location.hostname+"/layout/img/default_video.gif";
		}
		loadVideo(v,h,w,i);
	}
	
	var flash_audio = $('#flash_audio').text();
	
	if(flash_audio != null && flash_audio != ""){
		var t = $('span.audio').text();
		var d = $('span.duration').text();
		loadAudio(t,d);
	}
	
	var flash_hp = $('#flash_hp');
	if (flash_hp.length > 0) {
		var fhp = flash_hp;
		var ctop = $('#header').height();
		fhp.height($(window).height() - ctop);
		loadHP();
		$(window).resize(function(){
		  fhp.height($(window).height() - ctop);
		});
	}
	

	
	var flash_slide = $('#flash_slideshow').text();
	
	if(flash_slide != null && flash_slide != ""){
		var xml = "http://"+location.hostname+$('span.xml').text();
		//console.log(xml);
		loadSlideshow(xml);
	}
	
	//transcript
	var tr = $('a.transcript').text();
	if(tr != null && tr != ""){
		$('span#transcript').hide();
		initTranscript();
	}
	
	//press page
	var frm = $('form#login').text();
	if(frm != null && frm != ""){
		rewriteLinks();
	}
	
	$(window).bind('resize', function(){
		if((project != null && project != "") || (publication != null && publication != "")){
			if (resizeTimer) clearTimeout(resizeTimer); 
			resizeTimer = setTimeout(update, 100);
		}
		if(bgimg != null && bgimg != ""){
			resizeBg();
		}
	});
	
	var sbx = $('#query');
	if (sbx.length > 0) {
		sbx.focus(function(){
			if(sbx.val() == 'Enter keyword') {
				sbx.val('');
			} else {
				sbx.select();
			}
		});
		sbx.blur(function(){
			if (sbx.val() == '') {
				sbx.val('Enter keyword');
			}
		
		});
	}
	
	
});
/*function initNavigation(){
	ctop = $('#header a#logo img').height();
	htop = $('#header').height() - ctop;
	var hp = $('#nav ul li a.active span').text();
	if(hp != "Home"){
		$('#header').css({"position":"absolute","top":-htop,"z-index":"100"});
		$('#canvas').css({"top":ctop});
	}else{
		$('#header').css({"position":"absolute","z-index":"100"});
		$('#canvas').css({"top":ctop});
		closeHpNav();
	}
	
	$('#header').hover(
		//rollover
		function(){
			if(!animating){
				animating = true;
				$(this).animate({top:"0"},250,
					function(){
						animating = false;
						navOpen = true;
						if(navTimer) clearTimeout(navTimer);
						navTimer = setTimeout(hideNav, 1500);
						$(document).bind("mousemove",function(){
							getMousePos();
						});
				});
			}
		},
		//rollout
		function(){
			if(!animating){
				animating = true;
				closeNav($(this));
			}
		}
	);
}
function hideNav(){
	if(mousey > ctop+htop && navOpen == true){
		closeNav($('#header'));
	}
}
function closeNav(nav){
	nav.animate({top:-htop},150,
			function(){
				animating = false;
				navOpen = false;
				if(navTimer) clearTimeout(navTimer);
				$(document).unbind("mousemove");
			}
	);
}
function closeHpNav(){
	//getMousePos();
	//if(mousey > ctop+htop){
		$('#header').fadeTo(2000,1).animate({top:-htop},250,
			function(){
				animating = false;
				navOpen = false;
				if(navTimer) clearTimeout(navTimer);
				$(document).unbind("mousemove");
			}
		);
	//}	
}*/
function getMousePos() {
	document.onmousemove = function(e){
		if (!e) e = window.event;
		if (e){ 
			if (e.pageY){
				mousey = e.pageY;
			}else if (e.clientY){
				mousey = e.clientY + document.body.scrollTop;
			}  
		}
	};
}
function initRollover(){
	$('.assett h1 a').each(function(){
		var link = $(this).attr('href');
		var w = $(this).parent().parent().width();
		$(this).parent().parent().append('<a href="'+link+'" class="rover" style="width:'+w+'px"></a>');
	});
	$('.assett').each(function(){
		$(this).mouseover(function(){
			$(this).find('a.rover').css('opacity','.1');
			$(this).find('a.rover').css('filter','progid:DXImageTransform.Microsoft.Alpha(Opacity=10)');
		}),$(this).mouseout(function(){
			$(this).find('a.rover').css('opacity','0');
		});
	});
}
function loadVideo(flv,h,w,i){
	var today = new Date();
	var mill = today.getTime();
	var video = new SWFObject("/layout/flash/main.swf?"+mill, 'main', w, h, '9', '#000000');
	video.addParam("allowFullScreen","true");
	video.addParam("scale", "noScale");
	video.addParam("salign", "tl");
	video.addVariable("videourl", flv);
	video.addVariable("w", w);
	video.addVariable("h", h);
	video.addVariable("img", i);
	video.write("flash_video");
}
function loadAudio(mp3,time){
	var today = new Date();
	var mill = today.getTime();
	var video = new SWFObject("/layout/flash/audio.swf?"+mill, 'audio', '460', '24', '9', '#e2ded3');
	video.addParam("allowFullScreen","true");
	video.addParam("scale", "EXACT_FIT");
	video.addParam("salign", "tl");
	video.addVariable("track", mp3);
	video.addVariable("duration", time);
	video.write("flash_audio");
}
function loadHP(){
	var today = new Date();
	var mill = today.getTime();
	var video = new SWFObject("/layout/flash/flock.swf?"+mill, 'hp', '100%', '100%', '9', '#000E1B');
	video.addParam("allowFullScreen","false");
	video.addParam("scale", "100%");
	video.addParam("salign", "tl");
	video.write("flash_hp");
}
function loadSlideshow(xml){
	var today = new Date();
	var mill = today.getTime();
	var video = new SWFObject("/layout/flash/slideshow.swf?"+mill, 'audio', '600', '492', '9', '#292929');
	video.addParam("allowFullScreen","true");
	video.addParam("scale", "EXACT_FIT");
	video.addParam("salign", "tl");
	video.addVariable("xmlToLoad", xml);
	video.write("flash_slideshow");
}
function initTranscript(){
	$('a.transcript').toggle(function(){
		$('span#transcript').show("fast");
	},function(){
		$('span#transcript').hide("fast");
	});
}
function resizeBg(){
	var win_width = $(window).width();
	var win_height = $(window).height() - $('a#logo').height();
	var i_width = $('#bg_img img').width();
	var i_height = $('#bg_img img').height();
	var hor_ratio = win_width / i_width;
	var ver_ratio = win_height / i_height;
	var aspect_ratiowh = i_width / i_height;
	var aspect_ratiohw = i_height / i_width;
	var scale = win_height * aspect_ratiowh;
	if (scale >= win_width) {
		new_height = win_height;
		new_width = scale;
	} else {
		scale = win_width * aspect_ratiohw;
		new_height = scale;
		new_width = win_width;
	}
	if (isIE6){
		$('#bg_img img').css({height: new_height+"px", width: new_width+"px"});
		$('#bg_img').css({height: win_height+"px", width: win_width+"px"});
	} else {
		$('#bg_img img').css({height: new_height, width: new_width});
		$('#bg_img').css({height: win_height, width: win_width});
	}
	$('#bg_img').css({visibility:"visible", display:"block", overflow:"hidden"});
}
function stretchProjects(){
	$('.project div').each(function(){
		var rowh = $(this).parent().parent().height();
		$(this).parent().height(rowh);
		
		//var ph = rowh
		var pmt = parseFloat($(this).css("marginTop"));
		var ptb = parseFloat($(this).css("paddingTop")) + parseFloat($(this).css("paddingBottom"));
		
		$(this).height(rowh - pmt - ptb);
		//alert(ph+" "+pmt+" "+ptb);
	});
}
function rewriteLinks(){
	$('table a').each(function(){
		var c = $(this).html();
		var cl = $(this).attr("class");
		$(this).replaceWith("<p class='"+cl+"'>"+c+"</p>");
	});
}
function positionExtraNav() {
	var nav = $('ul.extranav');
	nav.css('left',($(window).width()-47)+'px');
}

function iStripInit(slide){
	if(slide == "project"){
		$('#project').each(function(){
			myStrip = new iStrip(this);
			myStrip.setWidth();
		});
	}else{
		$('.slider').each(function(){
			myStrip = new iStrip(this);
			myStrip.setWidth();
		});
	}
}
function iStrip(container) {
	this.me = $(container);
	if(slide == "project"){
		this.wrap = $($('#proj_container'));
	}else{
		this.wrap = $($('.slideshow'));
		this.wrap.width($('#canvas').width());
		var tw = parseInt(this.wrap.width());
		this.wrap.width(tw - 295);
		$('.w960').width(tw);
	}
	this.slngth = this.wrap.width();
	this.imgs = new Array();
	this.gap = null;
	this.lngth = 0;
	this.cpos = 0;
	this.max = 0;
	this.maxp = null;
	this.btn = new Array();
	this.captions = new Array();
	this.caption = null;
	this.act = [true,true];
	this.init();
	return this;
}
iStrip.prototype.init = function() {
	var sobj = this;
	if(slide == "project"){
		this.me.find('.assett').each(function(){
			sobj.addImg($($(this)));
		});
	}else{
		this.me.find('img').each(function(){
			sobj.addImg($($(this)));
		});
		this.lngth += this.lngth;
	}
	this.me.css('width',this.lngth);
	this.max = -((this.lngth-6) - this.slngth);
	this.maxp = 0;
	var l = this.imgs.length;
	for(this.maxp=0;this.maxp<l;this.maxp++) {
		if (this.max >= this.imgs[this.maxp][2]) {
			break;
		}
	}
	if (this.lngth > this.slngth) {
		if(slide == "project"){
			$('#canvas').after('<ul class="extranav"><li><a href="#" class="next_o"><span>Next</span></a></li><li><a href="#" class="prev_o notactive"><span>Previous</span></a></li></ul>');
			//position
			positionExtraNav();
			$(window).resize(function(){
				positionExtraNav();
			});
			$('ul.subnav').prepend('<li><a class="prev_o" href="#"><span>Previous</span></a></li><li><a class="next_o" href="#"><span>Next</span></a></li>');
			this.btn[0] = $('a.prev_o');
			this.btn[1] = $('a.next_o');
		}else{
			$('.slideshow').prepend('<p class="controls clearfix"><a class="prev" href="#"><span>Previous</span></a><a class="next" href="#"><span>Next</span></a><span class="title"></span></p>');
			this.btn[0] = $('a.prev');
			this.btn[1] = $('a.next');
		}
		
		this.btn[0].click(function(me){sobj.move(-1);return false;});
		this.btn[1].click(function(me){sobj.move(1);return false;});
		this.caption = $('p.controls').find('span.title');
		this.ckB();
	} 
}
iStrip.prototype.addImg = function(elm) {
	var w = parseInt(elm.width());
	if (this.gap == null) {
		this.gap = parseInt(elm.css('margin-right'));
	}
	this.imgs.push([elm,-w,-this.lngth]);
	this.lngth +=  w + this.gap;
	this.captions.push(elm.attr('rel'));	
}
iStrip.prototype.getW = function() {
	return this.lngth+this.gap;
}
iStrip.prototype.move = function(dir) {
	if (dir == 1) {
		if (this.cpos < this.maxp && this.act[1]) {
			if (this.cpos < this.imgs.length-1) {
				this.cpos++;
				var npos = this.imgs[this.cpos][2];
				if (-(npos) > (this.lngth-this.gap-this.slngth)) {
					npos = -(this.lngth-this.gap-this.slngth);	
				}
				this.me.animate({marginLeft:npos},'slow');
			}
		}
	} else {
		if (this.cpos > 0 && this.act[0]) {
			this.cpos--;
			var npos = this.imgs[this.cpos][2];
			this.me.animate({marginLeft:npos},'slow');
		}
	}	
	this.ckB();
}
iStrip.prototype.ckB = function() {
	if(slide != "project"){
		this.caption.html(this.captions[this.cpos]);
	}
	if (this.cpos == 0) {
		this.btn[0].addClass('notactive');
		this.act[0] = false;
		this.act[1] = true;
		this.btn[1].removeClass('notactive');
	} else if (this.cpos == this.maxp) {
		this.btn[1].addClass('notactive');
		this.act[1] = false;
		this.act[0] = true;
		this.btn[0].removeClass('notactive');
	} else {
		this.btn[0].removeClass('notactive');
		this.btn[1].removeClass('notactive');
		this.act[0] = true;
		this.act[1] = true;
	}
}
iStrip.prototype.setWidth = function(){
	if(slide == "project"){
		this.slngth = $('#proj_container').width();
	}else{
		this.wrap = $($('.slideshow'));
		this.wrap.width($('#canvas').width());
		var tw = parseInt(this.wrap.width());
		if(tw >= 960){
			$('.w960').width(tw);
		}else{
			tw = 960;
			$('.w960').width(960);
		}
		var siden = $('#sidenav ul').text();
		//check for side navigation
		if(siden != null && siden != ""){
			this.wrap.width(tw - 295);
			if (isIE6) {
				this.wrap.width(tw - 355);
			}
		}else{
			this.wrap.width(tw - 63);
		}
		
		this.slngth = this.wrap.width();
	}
	this.max = -((this.lngth-6) - this.slngth);
}
function update(){
	myStrip.setWidth();
}