var InternetExploder = (navigator.userAgent.toLowerCase().indexOf('msie') != -1);
var Safari = (navigator.userAgent.toLowerCase().indexOf('safari') != -1);
var SafariTwo = Safari && (navigator.userAgent.toLowerCase().indexOf('applewebkit/4') != -1);
var SafariThree = Safari && (navigator.userAgent.toLowerCase().indexOf('version/3') != -1);
var Opera = (navigator.userAgent.toLowerCase().indexOf('opera') != -1);
var scale;
var zoomduration = 0.5;
var fadeduration = 0.75;
var concretewidth = 820; // width of .concrete class
var clicked = false;
var initduration = 0.75;

function pageCommonInit()
{
	initPreload();
	if(GetCookie("index") != null)
	{
		DeleteCookie("index");
		if(ge("copy")) Effect.Appear("copy", { duration: initduration });
		if(ge("copy1")) Effect.Appear("copy1", { duration: initduration });
		if(ge("menu")) Effect.Appear("menu", { duration: initduration });
		if(ge("submenu")) Effect.Appear("submenu", { duration: initduration });
		if(ge("back")) Effect.Appear("back", { duration: initduration });
	}
	else
	{
		if(ge("copy")) ge("copy").style.display = "block";
		if(ge("copy1")) ge("copy1").style.display = "block";
		if(ge("menu")) ge("menu").style.display = "block";
		if(ge("submenu")) ge("submenu").style.display = "block";
		if(ge("back")) ge("back").style.display = "block";
	}
}

//function hl(i, name)	{ i.src = 'images/' + name + '_hl.png'; };
//function unhl(i, name)	{ i.src = 'images/' + name + '.png'; };
function ge(s)			{ return(document.getElementById(s)); }

function hlpip(el, pipid)
{
	el.src = "images/pip_hl.png";
	showportfoliocopy(pipid);

	if(copytimerhandle)
	{
		window.clearTimeout(copytimerhandle);
		copytimerhandle = null;
	}
}

function unhlpip(el, pipid)
{
	if(pipid != portfolioactive)
		el.src = "images/pip.png";
	
	copytimerhandle = window.setTimeout("showcopytimerevent()", 500);
}

function initpips()
{
	var i;
	var halfway = portfoliocount;
	var br = true;
	var line = 0;
	var top = 0;

	if(ge("pip1") == null)
		return;
	
	if(portfoliocount > 5)
		halfway = portfoliocount / 2;
	else
		top = 17;
		
	for(i = 1; i <= portfoliocount; i++)
	{
		var left = (i - line - 1) * 17;
		var pip = ge("pip" + i);
		var pipdiv = ge("pipdiv" + i);
		if(i == portfolioactive)
			pip.src = "images/pip_hl.png";
		else
			pip.src = "images/pip.png";
			
		pipdiv.style.display = "block";
		pipdiv.style.top = top + "px";
		pipdiv.style.left = left + "px";
		if(i >= halfway && br)
		{
			top = 17;
			line = i;
			br = false;
		}
	}
}

function resetpips(n)
{
	var i;

	for(i = 1; i <= portfoliocount; i++)
	{
		var pip = ge("pip" + i);
		if(i == n)
			pip.src = 'images/pip_hl.png';
		else
			pip.src = 'images/pip.png';
	}			
}

var copytimerhandle = null;

function showcopytimerevent()
{
	copytimerhandle = null;
	showportfoliocopy(portfolioactive);
}

function gfe(s)
{
	// IE and Netscape refer to the movie object differently.
	// This function returns the appropriate syntax depending on the browser.
  	if (navigator.appName.indexOf ("Microsoft") !=-1)
	{
    	return window[s];
	}
	else
	{
    	return document[s];
  	}
}

function movieIsLoaded(theMovie)
{
	// First make sure the movie's defined.
  	if (typeof(theMovie) != "undefined")
	{
    	// If it is, check how much of it is loaded.
   		return theMovie.PercentLoaded() == 100;
  	}
	else
	{
    	// If the movie isn't defined, it's not loaded.
    	return false;
	}
}

var prevpcopy = null;

function showportfoliocopy(n)
{
	var el = ge("pcopy" + n);
	if(el == null)
		el = ge("pcopy1");
	if(el == null)
		return;

	if(prevpcopy)
		prevpcopy.style.visibility = "hidden";

	el.style.visibility = "visible";
	prevpcopy = el;
}

function stopVideo()
{
	var movie = gfe("videoplayer");

	playerIsVisible = false;
	if(movieIsLoaded(movie))
		movie.playerStop();
	else
		window.setTimeout("stopVideo()", 50);
}

function startVideo(n)
{
	var movie = gfe("videoplayer");

	if(movieIsLoaded(movie))
	{
		videofile = videobasename + n + '.flv';
		movie.playerStop();
		movie.playerSetpath(videofile);
		movie.playerPlay();
		playerIsVisible = true;
	}
	else
	{
		window.setTimeout("startVideo(" + n + ")", 50);
	}
}

var playerIsVisible = false;

function generatevideogallery()
{
	var n = 1;
	ge("portfolio").style.display = "none";
	ge("still").style.display = "none";
	ge("faux").style.display = "block";
	var img = ge("photo1");
	img.src = photobasename + n + '.jpg';
	ge("stilllink").href = "javascript:play(" + n + ");";
	ge("stilllink2").href = "javascript:play(" + n + ");";
	imgloadtimeoutcounter = 20;
	trackimageload();
	resetpips(1);
	showportfoliocopy(1);
	portfolioactive = 1;
}

function portfolio(n)
{
	if(portfolioisvideo)
	{
		if(portfolioactive == n)
			play(n);
		else
		{
			if(playerIsVisible)
				stopVideo();
			else
			{
				ge("photo2").src = photobasename + portfolioactive + '.jpg';
				ge("still").style.display = "none";
				ge("stillfader").style.display = "block";
			}

			ge("portfolio").style.display = "none";
//			ge("still").style.display = "block";

			ge("faux").style.display = "block";

			var img = ge("photo1");
			img.src = photobasename + n + '.jpg';
			ge("stilllink").href = "javascript: play(" + n + ");";
			ge("stilllink2").href = "javascript: play(" + n + ");";
			Effect.Fade("stillfader", fadeduration);
			Effect.Appear("still", fadeduration);
		}
	}
	else
	{
//		ge("portfolio" + portfolioactive).style.visibility = "hidden";
//		ge("portfolio" + n).style.visibility = "visible";

		if(n != portfolioactive)
//		{
			Effect.Fade("portfolio" + portfolioactive, 1);
			Effect.Appear("portfolio" + n, 1);
//		}
	}
	
	portfolioactive = n;
	initpips();
	showportfoliocopy(n);
}

function play(n)
{
	ge("portfolio").style.display = "block";
	ge("still").style.display = "none";
	ge("faux").style.display = "none";
	window.setTimeout("startVideo(" + n + ");", SafariTwo ? 200 : (Opera ? 2000 : 20));
}

function playerStopped(n)
{
	ge("faux").style.display = "block";
	Effect.Fade("portfolio");
	Effect.Appear("still", fadeduration);
	playerIsVisible = false;
}

var imgloadtimeoutcounter = 0;

function preloadPhotoGallery()
{
	var i;
	for(i = 1; i <= portfoliocount; i++)
		addPreload('zoom' + i, photobasename + 'zoom' + i + '.jpg');
}

function generatephotogallery(s)
{
	var i;
	for(i = 1; i <= portfoliocount; i++)
	{
		var div = document.createElement("div");
		div.setAttribute('id', 'portfolio' + i);
		div.setAttribute('class', 'pdisplay');

		div.style.display = "none";
		div.style.position = "absolute";

		var a = document.createElement("a");
		a.setAttribute('class', 'imglink');
		a.setAttribute('href', 'javascript:zoom()');

		var img = document.createElement("img");
		img.setAttribute('id', 'photo' + i);
		img.setAttribute('src', photobasename + i + '.jpg');
		img.setAttribute('class', 'photo');
		img.style.border = "none";

		a.appendChild(img);
		div.appendChild(a);
		
		var div2 = document.createElement("div");
		
		if(! s)
			div2.setAttribute('class', 'floatright');

		div2.style.position = "relative";
		div2.style.paddingTop = "3px";
		
		a = document.createElement("a");
		a.setAttribute('class', 'imglink');
		a.setAttribute('href', 'javascript:zoom()');
//		a.style.float = 'right';
//		a.style.marginTop = "3px";
//		a.style.marginLeft = "auto";
		a.style.right = "0";

		img = document.createElement("img");
		img.setAttribute('id', 'enlarge' + i);
		img.setAttribute('src', 'images/largerview.png');
		img.setAttribute('class', 'photo');
		img.style.border = "none";

		a.appendChild(img);
		div2.appendChild(a);
		div.appendChild(div2);
		
		ge("portfolio").appendChild(div);
	}

	imgloadtimeoutcounter = 20;	
	trackimageload();
	
	window.setTimeout("preloadPhotoGallery()", 2700);
}

function generatephotogalleryfourup(width, height, spacing)
{
	var i;
	var left = 0;
	for(i = 1; i <= portfoliocount; i++)
	{
		var div = document.createElement("div");
		div.setAttribute('id', 'portfolio' + i);
		div.setAttribute('class', 'fourup');

		div.style.display = "block";
		div.style.position = "absolute";
		div.style.left = left + "px";
		div.style.height = height + "px";
		div.style.width = width + "px";
		
		var a = document.createElement("a");
		a.setAttribute('class', 'imglink');
		a.setAttribute('href', 'javascript:focus(' + i + ')');

		var img = document.createElement("img");
		img.setAttribute('id', 'photo' + i);
		img.setAttribute('src', photobasename + i + '.jpg');
		img.setAttribute('class', 'photo');
		img.style.border = "none";

		a.appendChild(img);
		div.appendChild(a);
		
		ge("portfolio").appendChild(div);
		
		left += width[i - 1] + spacing;
		
		var div2 = document.createElement("div");
	
		div2.setAttribute('class', 'floatright');

		div2.style.position = "relative";
		div2.style.paddingTop = "3px";
	
		a = document.createElement("a");
		a.setAttribute('class', 'imglink');
		a.setAttribute('href', 'javascript:focus(' + i + ')');
//		a.style.float = 'right';
//		a.style.marginTop = "3px";
//		a.style.marginLeft = "auto";
		a.style.right = "0";

		img = document.createElement("img");
		img.setAttribute('id', 'enlarge' + i);
		img.setAttribute('src', 'images/largerview.png');
		img.setAttribute('class', 'photo');
		img.style.border = "none";

		a.appendChild(img);
		div2.appendChild(a);
		div.appendChild(div2);
	}

	imgloadtimeoutcounter = 20;	
	trackimageload();
	
	window.setTimeout("preloadPhotoGallery()", 2700);
}

function zoooom(n)
{
	var div = ge("zoom" + n);
	var img = ge("zoomphoto" + n);
	if(ge("zoom" + n) == null)
	{
		div = document.createElement("div");
		div.setAttribute('id', 'zoom' + n);
		div.setAttribute('class', 'pdisplay');
		div.style.display = "none";
		div.style.position = "absolute";
		div.style.zIndex = "1000000";
		div.style.left = ge('portfolio' + n).style.left;

		var img = document.createElement("img");
		img.setAttribute('id', 'zoomphoto' + n);
		img.setAttribute('class', 'photo');
		img.src = photobasename + 'zoom' + n + '.jpg';
		img.style.border = "none";
		img.style.zIndex = "1000000";

		var a = document.createElement("a");
		a.setAttribute('class', 'imglink');
		a.setAttribute('href', 'javascript:focus(' + n + ')');

		a.appendChild(img)
		div.appendChild(a);
		ge("portfolio").appendChild(div);
	}
	
	if(img.complete == false)
	{
		window.setTimeout("zoooom(" + n + ")", 20);
		return;
	}

	var imgwidth = img.width;
	var photo = ge("photo" + n);
	div.style.width = photo.width + "px";
	div.style.height = photo.height + "px";
	div.style.display = "block";
	img.style.width = photo.width + "px";
	img.style.height = photo.height + "px";

	var scale;
	var win = windowdimensions();
	var width;
	if(photo.width > photo.height)
	{
		win.width -= 30;
		width = win.width > 1000 ? 1000 : win.width;
		scale = width / photo.width;
	}
	else
	{
		scale = 1000 / photo.height;
	}
	
	scale *= 100;

	var x = 0;
	var center = concretewidth / 2;
	var left = unpx(ge('portfolio' + n).style.left);
//	if(left > center)
//		x = -(left - center + (imgwidth / 2));
//	else
//		x = center - left - (imgwidth / 2);
	
	if(currentzoom == portfolioactive || currentzoom == -1)
	{
		new Effect.Parallel(
			[ new Effect.Scale(div, scale), 
			  new Effect.Scale(img, scale),
			  new Effect.Move(div, { x: -left, mode: 'relative' }),
			  new Effect.Fade('portfolio1'),
			  new Effect.Fade('portfolio2'),
			  new Effect.Fade('portfolio3'),
			  new Effect.Fade('portfolio4')],
			{ duration: zoomduration, afterFinish: function() { clicked = false; } });
	}
	else
	{
		new Effect.Parallel(
			[ new Effect.Scale(div, scale), 
			  new Effect.Scale(img, scale),
			  new Effect.Move(div, { x: -left, mode: 'relative' })],
			{ duration: zoomduration, afterFinish: function() { clicked = false; } });
	}
}

var unzoomportfolio;
	
function unzoooom(n)
{
	unzoomportfolio = n;
	var n1, n2, n3;
	if(n == 1) { n1 = 2; n2 = 3; n3 = 4; }
	if(n == 2) { n1 = 1; n2 = 3; n3 = 4; }
	if(n == 3) { n1 = 1; n2 = 2; n3 = 4; }
	if(n == 4) { n1 = 1; n2 = 2; n3 = 3; }
	var div = ge("zoom" + n);
	var img = ge("zoomphoto" + n);
	var photo = ge("photo" + n);
	var imgwidth = img.width;

	var scale;
	var win = windowdimensions();
	var width;
	if(photo.width > photo.height)
	{
		win.width -= 30;
		width = win.width > 1000 ? 1000 : win.width;
		scale = photo.width / width;
	}
	else
	{
		scale = photo.height / 1000;
	}
	
	scale *= 100;

	var x = 0;
	var center = concretewidth / 2;
	var left = unpx(ge('portfolio' + n).style.left);
//	if(left > center)
//		x = -(left - center + (imgwidth / 2));
//	else
//		x = center - left - (imgwidth / 2);
	
	if(currentzoom == portfolioactive)
	{
		new Effect.Parallel(
			[ new Effect.Scale(div, scale), 
			  new Effect.Scale(img, scale),
			  new Effect.Move(div, { x: left, mode: 'relative' }),
			  new Effect.Appear('portfolio1'),
			  new Effect.Appear('portfolio2'),
			  new Effect.Appear('portfolio3'),
			  new Effect.Appear('portfolio4')],
			{ duration: zoomduration, afterFinish: hidezoom2 });
	}
	else
	{
		new Effect.Parallel(
			[ new Effect.Scale(div, scale), 
			  new Effect.Scale(img, scale),
			  new Effect.Move(div, { x: left, mode: 'relative' })],
			{ duration: zoomduration, afterFinish: hidezoom2 });
	}
}

function hidezoom2()
{
	var n = unzoomportfolio;
	
	ge("portfolio").removeChild(ge("zoom" + n));
	clicked = false;
//	ge("zoom" + n).style.display = "none";
//	ge("pcopy" + n).style.display = "block";
//	ge("pips").style.display = "block";
}

function zoom()
{
	if(clicked == true) return;
	clicked = true;
	var n = portfolioactive;
	var div = ge("zoom" + n);
	var img = ge("zoomphoto" + n);
	if(ge("zoom" + n) == null)
	{
		div = document.createElement("div");
		div.setAttribute('id', 'zoom' + n);
		div.setAttribute('class', 'pdisplay');
		div.style.display = "none";
		div.style.position = "absolute";
		div.style.zIndex = "1000000";

		var img = document.createElement("img");
		img.setAttribute('id', 'zoomphoto' + n);
		img.setAttribute('class', 'photo');
		img.src = photobasename + 'zoom' + n + '.jpg';
		img.style.border = "none";
		img.style.zIndex = "1000000";

		var a = document.createElement("a");
		a.setAttribute('class', 'imglink');
		a.setAttribute('href', 'javascript:unzoom()');

		a.appendChild(img)
		div.appendChild(a);
		ge("portfolio").appendChild(div);
	}
	
	if(img.complete == false)
	{
		window.setTimeout("zoom()", 20);
		clicked = false;
		return;
	}

	var photo = ge("photo" + n);
	div.style.width = photo.width + "px";
	div.style.height = photo.height + "px";
	div.style.display = "block";
	img.style.width = photo.width + "px";
	img.style.height = photo.height + "px";

	var scale;
	var win = windowdimensions();
	var width;
	if(photo.width > photo.height)
	{
		win.width -= 30;
		width = win.width > 1000 ? 1000 : win.width;
		scale = width / photo.width;
	}
	else
	{
		scale = 1000 / photo.height;
	}
	
	scale *= 100;

	if(width > concretewidth)
	{
		new Effect.Parallel(
			[ new Effect.Scale(div, scale), 
			  new Effect.Scale(img, scale),
			  new Effect.Move(div, { x: -((width - concretewidth) / 2), mode: 'relative' }),
			  new Effect.Fade("pips"),
			  new Effect.Fade("pcopy" + n)],
			{ duration: zoomduration, afterFinish: function() { clicked = false; } });
	}
	else
	{
		new Effect.Parallel(
			[ new Effect.Scale(div, scale), 
			  new Effect.Scale(img, scale),
			  new Effect.Fade("pips"),
			  new Effect.Fade("pcopy" + n)],
			{ duration: zoomduration, afterFinish: function() { clicked = false; } });
	}
}

function unzoom()
{
	if(clicked == true) return;
	clicked = true;
	var n = portfolioactive;

	var div = ge("zoom" + n);
	var img = ge("zoomphoto" + n);
	var photo = ge("photo" + n);

	var scale;
	var win = windowdimensions();
	var width;
	if(photo.width > photo.height)
	{
		win.width -= 30;
		width = win.width > 1000 ? 1000 : win.width;
		scale = photo.width / width;
	}
	else
	{
		scale = photo.height / 1000;
	}
	
	scale *= 100;

	if(width > concretewidth)
	{
		new Effect.Parallel(
			[ new Effect.Scale(div, scale), 
			  new Effect.Scale(img, scale),
			  new Effect.Move(div, { x: ((width - concretewidth) / 2), mode: 'relative' }),
			  new Effect.Appear("pips"),
			  new Effect.Appear("pcopy" + n)],
			{ duration: zoomduration, afterFinish: hidezoom });
	}
	else
	{
		new Effect.Parallel(
			[ new Effect.Scale(div, scale), 
			  new Effect.Scale(img, scale),
			  new Effect.Appear("pips"),
			  new Effect.Appear("pcopy" + n)],
			{ duration: zoomduration, afterFinish: hidezoom });
	}
}

function hidezoom()
{
	var n = portfolioactive;
	
	ge("portfolio").removeChild(ge("zoom" + n));
	clicked = false;
//	ge("zoom" + n).style.display = "none";
//	ge("pcopy" + n).style.display = "block";
//	ge("pips").style.display = "block";
}

function trackimageload()
{
	imgloadtimeoutcounter -= 1;
	var img = ge("photo1");
	if(typeof(img.complete) != "undefined" && img.complete == false && imgloadtimeoutcounter > 0)
	{
		window.setTimeout("trackimageload()", 250);
		return;
	}
	
	if(img)
	{
		if(portfolioisvideo)
			Effect.Appear("still", fadeduration);
		else
			portfolio(1);
	}
}

function windowdimensions()
{
	var dim = new Object(), d=document;

	if(typeof window.innerWidth!='undefined')
	{
 		dim.width = window.innerWidth;
 		dim.height = window.innerHeight;
	}
	else
	{
 		if(d.documentElement && 
			typeof d.documentElement.clientWidth!='undefined' && 
			d.documentElement.clientWidth!==0)
		{
  			dim.width = d.documentElement.clientWidth;
			dim.height = d.documentElement.clientHeight;
		}
		else
		{
			if(d.body && typeof d.body.clientWidth!='undefined')
			{
   				dim.width = d.body.clientWidth;
   				dim.height = d.body.clientHeight;
			}
		}
	}
	
	return(dim);
}

function unpx(s)
{
	return(s.substr(0, s.length - 2));
}

var thishost = "";

function getCookieValue(offset)
{
	var endstr = document.cookie.indexOf(";", offset);
	if(endstr == -1)
		endstr = document.cookie.length;
	var value = document.cookie.substring(offset,endstr);
	return(unescape(value));
}

function GetCookie(name)
{
	var arg = name + "=";
	var alen = arg.length;
	var clen = document.cookie.length;
	var i = 0;
	
	while(i < clen)
	{
		var j = i + alen;
		if(document.cookie.substring(i,j) == arg)
			return(getCookieValue(j));
		
		i = document.cookie.indexOf(" ", i) + 1;
		if(i == 0)
			break;
	}
	
	return(null);
}

function SetCookie(name,value)
{
	SetCookieInDomain(thishost,name,value);
}

function SetCookieInDomain(domain,name,value)
{
//	alert("SetCookieInDomain('" + domain + "','" + name + "','" + value + "')");
	var today = new Date();
	var expires = new Date(today.getTime() + 365 * 24 * 60 * 60 * 1000);

	var cookie = name + "=" + escape(value) + "; path=/; expires=" + expires.toGMTString();
	document.cookie = cookie;
}

function DeleteCookie(name)
{
	DeleteCookieInDomain(thishost,name);
}

function DeleteCookieInDomain(domain,name)
{
	document.cookie = name + "=; expires=Fri, 13-Apr-1970 00:00:00 GMT; path=/;"
}






