/* Standard Stuff */
var flashMovie = false;
var INFOTEXT = "infotext";
var OLDNUM = "numdiv0";
var OLDPAGETYPE, CURRENTGALLERY, OLDGALLERY, PICLINKID;
if(pagSitename == "interiors"){
	var pagAjaxappension = "&interiors=1";
} else {
	var pagAjaxappension = "";
}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++) {
    //alert(a[i]);
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}}
}

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

var OLDELIST = "elist1";
function swapelist(newlist){
	if (newlist != OLDELIST){
		$(OLDELIST).hide();
		$(newlist).show();
		OLDELIST = newlist;
	}
}


function newpic(lineid,picname,picwidth,picheight,lgpicwidth,lgpicheight) {
	//new Effect.SlideDown('thumbscroller');
	var dimensions = $('body').getDimensions();
	//alert("Width: "+dimensions.width+"\nHeight: "+dimensions.height);
	
	var xt = picname.substr(-4);
	if (f_clientHeight() > 750 && picwidth < 500) {
		picname = picname.substr(0, picname.length - 7) + xt;
		picwidth = lgpicwidth;
		picheight = lgpicheight;
	}
	//alert("picname: "+picname+"\npicwidth: "+picwidth+"\npicheight: "+picheight);
	//var img = new Image();
    //img.src = '/images/' + picname;
	//loaded($('mainpic'));
	
	var newnum = 'numdiv'+lineid;
	
	if(OLDNUM!='numdiv0' && OLDNUM != newnum) { new Effect.Morph(OLDNUM,{ style:'margin-top:8px;height:8px;background-color:#666;', duration:0.5 }); }

	if (OLDNUM != newnum) {
		var morphPicWrap = function() {
			swapmainpic(picname, '/images/');
			new Effect.Morph('mainpicwrap',{ style:'width:'+picwidth+'px;height:'+picheight+'px;', duration:0.5,afterFinish: function() { 
				document.getElementById('mainpic').style.width = picwidth + 'px';
				document.getElementById('mainpic').style.height = picheight + 'px';
				new Effect.Appear('mainpic',{duration:0.5});
				new Effect.Morph(newnum,{ style:'margin-top:0px;height:17px;background-color:#716C00;', duration:0.3});
			}});
		}
		alert($('mainpic').getStyle("display"));
		if ($('mainpic').getStyle("display") != "none")
			new Effect.Fade('mainpic', {duration:0.5, afterFinish: morphPicWrap});
		else morphPicWrap();
	}
	OLDNUM=newnum;
}

/* Window height calculation */
function f_clientHeight() {
	return f_filterResults (
		window.innerHeight ? window.innerHeight : 0,
		document.documentElement ? document.documentElement.clientHeight : 0,
		document.body ? document.body.clientHeight : 0
	);
}

function f_clientWidth() {
	return f_filterResults (
		window.innerWidth ? window.innerWidth : 0,
		document.documentElement ? document.documentElement.clientWidth : 0,
		document.body ? document.body.clientWidth : 0
	);
}

function f_filterResults(n_win, n_docel, n_body) {
	var n_result = n_win ? n_win : 0;
	if (n_docel && (!n_result || (n_result > n_docel)))
		n_result = n_docel;
	return n_body && (!n_result || (n_result > n_body)) ? n_body : n_result;
}
    
function swapmainpic(picname, root) {
	root = (root)?root:"";
    document.getElementById('mainpic').style.backgroundImage = 'url(' + root + picname + ')';
}

var oldmenuid = 0;
//console.log(imageID);
function menuswap(menuid) {
	var offid = menuid+'_off';
	var onid = menuid+'_on';
	var onmenu = 'mn_' + menuid;
	var imgdir = "/display/zone/";
	
	switch(menuid) {
		case "approach":
			MM_preloadImages(imageID.philosophy.large, imageID.sustainability.large, imageID.process.large);
			break;
		case "services":
			if(pagSitename == "interiors"){
				MM_preloadImages(imageID.architecture.large, imageID.service_2.large, imageID.service_3.large, imageID.service_4.large);
			} else{
				MM_preloadImages(imageID.architecture.large, imageID.planning.large, imageID.asset_management.large, imageID.environmental_graphics.large);
			}
			break;
		case "portfolio":
			MM_preloadImages(imageID.client_list.large);
			break;
		case "people":
			MM_preloadImages(imageID.culture.large, imageID.careers.large);
			break;
		case "contact":
			MM_preloadImages(imageID.general_info.large, imageID.directory.large);
			break;
	}
	
	if(oldmenuid != 0){
		var oldoffid = oldmenuid+'_off';
		var oldonid = oldmenuid+'_on';
		var offmenu = 'mn_' + oldmenuid;
		new Effect.Fade(offmenu,{duration:0.2,afterFinish: function() {
			new Effect.Morph(oldoffid,{ style:'left:0px;', duration:0.5 });
			new Effect.Morph(oldonid,{ style:'left:-108px;', duration:0.5 });
			new Effect.Morph(offid,{ style:'left:108px;', duration:0.5 });
			new Effect.Morph(onid,{ style:'left:0px;', duration:0.5,afterFinish: function() {
				new Effect.Appear(onmenu,{duration:0.4});
				new Effect.BlindDown(onmenu,{duration:0.4});
			}});
		}});
	} else {
		new Effect.Morph(offid,{ style:'left:108px;', duration:0.5 });
		new Effect.Morph(onid,{ style:'left:0px;', duration:0.5,afterFinish: function() {
			new Effect.Appear(onmenu,{duration:0.4});
			new Effect.BlindDown(onmenu,{duration:0.4});
		}});
	}
	oldmenuid=menuid;
}

function menuoff(menuid) {
	var offid = menuid+'_off';
	var onid = menuid+'_on';
	var offmenu = 'mn_' + menuid;	
	new Effect.Fade(offmenu,{duration:0.2,afterFinish: function() {
		new Effect.Morph(offid,{ style:'left:0px;', duration:0.5 });
		new Effect.Morph(onid,{ style:'left:-108px;', duration:0.5});
	}});
	
	oldmenuid = 0;
}

	
function loadpage(contentfile, newPageType) {
	//alert("old: " + OLDPAGETYPE + " new: " + newPageType);
	var dimensions = $('body').getDimensions();
	var leftwidth = Math.round(.45 * dimensions.width);
	
	if (newPageType == 'h') {
		new Effect.Morph('trcol', {style: 'height: 1px', delay: 0.5, duration: 1.0});
	} else {
		new Effect.Morph('trcol', {style: 'height: 200px', delay: 0.5, duration: 1.0});
	}
	if ($('mainpicfooterwrap').getStyle('height') == '1px' && newPageType != 'h') {
		new Effect.Parallel([
			new Effect.Morph('mainpicfooterwrap', {sync: true, style: 'height: 20px'}),
			new Effect.Morph('pagetable', {sync: true, style: 'height: ' + (parseInt($('pagetable').getStyle('height')) - 19) + 'px'})
		], {duration: 0.3});
	}
	
	if (newPageType == 'i' || newPageType == 'h') {
		OLDGALLERY = null;
	} else
		CURRENTGALLERY = contentfile;
	
	if (f_clientHeight() < 750 && newPageType != 'h')
		new Effect.Fade('copyright');
	
	/* GALLERY PAGE TRANSITIONS */
	if (newPageType == 't') {
		if (OLDPAGETYPE == newPageType) {
			//alert(CURRENTGALLERY + " " + OLDGALLERY);
			if (CURRENTGALLERY != OLDGALLERY)
				OLDGALLERY.hide(function() { CURRENTGALLERY.show(); });
			
		} else if (OLDPAGETYPE == 'i') {
		
			CURRENTGALLERY.show();
			new Effect.Morph('piccaptionwrap', {style: 'margin-top: -20px', duration:0.3});
			
			new Effect.Fade('mainpic',{duration:0.5,afterFinish: function() {
				new Effect.Morph('mainpicwrap',{ style: "width: " + (leftwidth-1) + "px; height: 1px; background-color: #eee", duration:0.5});
			}});
			
		} else if (OLDPAGETYPE == 'h') {
		
			if (flashMovie) {
				new Effect.Parallel([
				new Effect.Morph('mainpicwrap', {sync: true, style:'width: ' + (leftwidth-1) + 'px;'}),
				new Effect.Morph('pagetable', {sync: true, style: 'height: ' + (parseInt($('pagetable').getStyle('height')) + parseInt($('intropic').getStyle('height'))) + 'px'}),
				new Effect.Morph('intropicwrap', {sync: true, style: 'height: 1px'})
					], {duration: 0.6, afterFinish: function() {
						$('intropicwrap').setStyle({visibility: 'hidden'});
						flashMovie.Rewind();
						CURRENTGALLERY.show();
					}}
				);
			}
			
		}
		
		OLDGALLERY = CURRENTGALLERY;
	}
	
	/* INFO PAGE TRANSITIONS */
	if (newPageType == 'i') {
		OLDELIST = "elist1";
		if (OLDPAGETYPE == newPageType) {
			new Effect.Morph('piccaptionwrap', {style: 'margin-top: -20px', duration: 0.5});
			new Effect.Fade('mainpic', {duration:0.4});
			new Effect.Opacity(INFOTEXT, {from: 1.0, to: 0, duration:0.4, afterFinish: function() {
				new Ajax.Request('/content/' + contentfile + '.php?directload=1'+ pagAjaxappension, {onComplete: function(transport) {
					newPicZone(contentfile, transport.responseText);
				}});
			}});
			
		} else if (OLDPAGETYPE == 't') {
		
			CURRENTGALLERY.hide(function() {
				new Effect.Morph('mainpicwrap', {style: 'background-color: #ccc', duration:0.5});
				new Ajax.Request('/content/' + contentfile + '.php?directload=1'+ pagAjaxappension, {onComplete: function(transport) {
					newPicZone(contentfile, transport.responseText);
				}});
			});
			
		} else if (OLDPAGETYPE == 'h') {
		
			$('mainpicwrap').setStyle({backgroundColor: '#ccc'});
			new Ajax.Request('/content/' + contentfile + '.php?directload=1'+ pagAjaxappension, {onComplete: function(transport) {
				newPicZone(contentfile, transport.responseText);
			}});
			if (flashMovie) {
				new Effect.Parallel([
					new Effect.Morph('pagetable', {sync: true, style: 'height: ' + (parseInt($('pagetable').getStyle('height')) + parseInt($('intropic').getStyle('height'))) + 'px'}),
					new Effect.Morph('intropicwrap', {sync: true, style: 'height: 1px'})
					], {duration: 1.0, afterFinish: function() {
						$('intropicwrap').setStyle({visibility: 'hidden'});
						flashMovie.Rewind();
				}});
			}
			
		}
	}
	
	/* HOMEPAGE TRANSITIONS */
	if (newPageType == 'h' && OLDPAGETYPE != 'h') {
		menuoff(oldmenuid);
		new Effect.Appear('copyright');
		
		new Effect.Morph('mainpicfooterwrap', {style: 'height: 1px', duration: 0.3});
		
		var style = {
			paddingTop: "0px",
			height: "0px"
		}
		
		swapText($(INFOTEXT), "", style);
		if (OLDPAGETYPE == 'i') {
		
			new Effect.Morph('piccaptionwrap', {style: 'margin-top: -20px', duration:0.3});
			new Effect.Opacity(INFOTEXT, {from: 1.0, to: 0, duration:0.4});
			new Effect.Fade('mainpic',{duration:0.5, afterFinish: function() {
				new Effect.Morph('mainpicwrap',{ style:'width: ' + (leftwidth-1) + 'px;', duration:0.5});
				new Effect.Morph('mainpicwrap', { style: 'height: 1px; background-color: #eee', duration:0.5});
				playFlash();
			}});
			
		}
		
		else if (OLDPAGETYPE == 't')
			CURRENTGALLERY.hide(function() { playFlash(); });
			
	} else if (newPageType == 'h' && OLDPAGETYPE == 'h') menuswap(0);
	
	OLDPAGETYPE = newPageType;
}

function newPicZone(contentfile, text) {
	var dimensions = $('body').getDimensions();
	var picname = imageID[contentfile];
	//alert("Width: "+dimensions.width+"\nHeight: "+dimensions.height);
	if (f_clientHeight() > 750) {
		picname = picname.large;
		picwidth = 523;
		picheight = 475;
	}
	else {
		picname = picname.small;
		picwidth = 440;
		picheight = 325;
	}
	//alert("picname: "+picname+"\npicwidth: "+picwidth+"\npicheight: "+picheight);
	
	swapmainpic(picname/* , '/display/zone/' */);
	new Effect.Morph('mainpicwrap',{ style:'width:'+picwidth+'px;height:'+picheight+'px;', duration:0.5,afterFinish: function() {
		//alert(dimensions.width+" "+f_clientWidth());
		var rightwidth = dimensions.width - parseInt($('mainpicwrap').getStyle('width'));
		
		style = getCenterPaddingStyle();
		if (contentfile != 'directory') {
			if (f_clientHeight() < 750)
				style.paddingTop = '50px';
			else
				style.paddingTop = '100px';
		} else {
			if (f_clientHeight() < 750)
				style.paddingTop = '20px';
			else
				style.paddingTop = '40px';
		}
		document.getElementById('mainpic').style.width = picwidth + 'px';
		document.getElementById('mainpic').style.height = picheight + 'px';
		new Ajax.Updater('piccaption','/content/' + contentfile + '.php?directload=1&fetchcaption=1'+ pagAjaxappension, {asynchronous:true, evalScripts:true, onComplete: function() {
			new Effect.Appear('mainpic',{duration:0.5});
			//new Effect.Opacity(INFOTEXT, {from: 0, to: 1.0, duration:0.4});
			var opacity = $(INFOTEXT).getOpacity();
			if (opacity > 0) $(INFOTEXT).setOpacity(0);
			
			swapText($(INFOTEXT), text, style, function() {
				new Effect.Opacity(INFOTEXT, {from: 0, to: 1.0, duration:0.3});
			});
			new Effect.Morph('piccaptionwrap', {style: 'margin-top: 0px', duration: 0.5});
		}});
	}});
}


function resizeAccordions() {
	$('pagetable').setStyle({
		height: document.body.clientHeight - 11 - parseInt($("thumbscrollerwrap").getStyle("height")) - parseInt($('intropicwrap').getStyle('height')) + "px"
	});
	
	if (OLDPAGETYPE == 'i') {
		$(INFOTEXT).setStyle(getCenterPaddingStyle());
	}
	/*
	if (OLDPAGETYPE == 'i') {
		if (isie && engine < 8) {
			var contentWidth = 322;
			var padding = Math.floor((parseInt($("trcol").getStyle("width")) - contentWidth) / 2);
			//alert(contentWidth);
			//alert(padding);
			$(INFOTEXT).setStyle({
				width: (padding + contentWidth) + "px",
				paddingLeft: (padding) + "px"
			});
		} else {
			var padding = Math.floor((parseInt($("trcol").getStyle("width")) - parseInt($(INFOTEXT).getStyle("width"))) / 2);
			$(INFOTEXT).setStyle({paddingLeft: padding + "px"});
		}
	}
	*/
}

function getCenterPaddingStyle() {
	var style;
	if (isie && engine < 8) {
		var contentWidth = 322;
		var padding = Math.floor((parseInt($("trcol").getStyle("width")) - contentWidth) / 2);
		style = {
			width: (padding + contentWidth) + "px",
			paddingLeft: (padding) + "px"
		}
	} else {
		var padding = Math.floor((parseInt($("trcol").getStyle("width")) - parseInt($(INFOTEXT).getStyle("width"))) / 2);
		style = { paddingLeft: padding + "px" };
	}
	//alert(contentWidth);
	//alert(padding);
	return style;
}

function projectpic(picnum, picname, lgpicname, picwidth, picheight, lgpicwidth, lgpicheight) {
	var gallery, textcontainer;
	var dimensions = $('body').getDimensions();
	//alert("Width: "+dimensions.width+"\nHeight: "+dimensions.height);
	
	if (f_clientHeight() > 750 && picwidth < 500) {
		picname = lgpicname;
		picwidth = lgpicwidth;
		picheight = lgpicheight;
	}
	
	var rightwidth = dimensions.width - picwidth;
	var gallerytextwidth = 424;
	var newleft = (rightwidth/2) - (gallerytextwidth/2) + "px";
	
	var directory = picname.substr(0, picname.lastIndexOf("/") + 1);
	picname = picname.substr(picname.lastIndexOf("/") + 1);
	//alert("picname: "+picname+"\npicwidth: "+picwidth+"\npicheight: "+picheight+"\ndirectory: "+directory);
	//alert("oldpic: " + LASTPROJECTPIC + "\nnew: "+picnum);
	
	if (LASTPROJECTPIC != picnum) {
		if (LASTPROJECTPIC !== null) { new Effect.Morph("projectpic" + LASTPROJECTPIC, { style:'margin-top:8px; height:8px; background-color:#666;', duration:0.5 }); }
		
		new Effect.Fade('mainpic', {duration:0.5,afterFinish: function() {
			swapmainpic(picname, directory);
			var newStyle = getCenterPaddingStyle();
			newStyle.paddingTop = "100px";
			//console.log(newStyle);
			new Effect.Morph(INFOTEXT, {style: newStyle, duration: 0.5});
			if (isie)
				new Effect.Morph('mainpicfooterwrap', {style: {width: picwidth+'px'}, duration: 0.5});
			new Effect.Morph('mainpicwrap',{ style:'width:'+picwidth+'px; height:'+picheight+'px; background-color: #ccc', duration:0.5, afterFinish: function() {
				document.getElementById('mainpic').style.width = picwidth + 'px';
				document.getElementById('mainpic').style.height = picheight + 'px';
				new Effect.Appear('mainpic',{duration:0.5});
				new Effect.Morph("projectpic" + picnum, {style:'margin-top:0px; height:17px; background-color:#716C00;', duration:0.3});
			}});
		}});
		LASTPROJECTPIC = picnum;
	}
}

function playFlash(mydelay) {
	if (flashMovie) {
		if (mydelay == undefined)
			mydelay = 0;
		$('intropicwrap').setStyle({visibility: 'visible'});
		$('intropic').setStyle({height: homeDivHeight()+'px'});
		flashMovie.Play();
		new Effect.Parallel([
			new Effect.Morph('intropicwrap', {sync: true, style: 'height: '+homeDivHeight()+'px'}),
			new Effect.Morph('pagetable', {sync: true, style: 'height: '+(parseInt($('pagetable').getStyle('height')) - homeDivHeight())+'px'})
			], {delay: mydelay, duration: 1.6}
		);
	}
}

function getFlashMovieObject(movieName) {
	if (window.document[movieName])
		return window.document[movieName] || false;
	
	if (navigator.appName.indexOf("Microsoft Internet") == -1) {
		if (document.embeds && document.embeds[movieName])
			return document.embeds[movieName] || false; 
	} else // if (navigator.appName.indexOf("Microsoft Internet")!=-1)
		return document.getElementById(movieName) || false;
}

function homeDivHeight() {
	var height = document.body.clientHeight - 11 - 251 - 30;
	if (height > 420)
		height = 420;
	else if (height < 300)
		height = 300;
	return height;
}