// require dhtml.js


// =========================================================
// global variables
// =========================================================
pathPrfx = "";

// directory
function abs2Rel( pPathFile ){
	
	if( pPathFile.substring(0,1) == "/" ){
		return pathPrfx + pPathFile.substring( 1, pPathFile.length );
	}	
	return pPathFile;
}
	
// =========================================================
// onload library
// =========================================================

onLoadFunctionStr = "";
window.onload = onLoadHandler;

function registerOnLoad( pFuncStr ) {
	onLoadFunctionStr += pFuncStr + ";";
}

function onLoadHandler() {
	eval(onLoadFunctionStr);
}

// =========================================================
// stupid Netscape 4 resize fix
// =========================================================
function MM_reloadPage(init) {  
	if (init==true) 
		with (navigator) {
			if ((appName=="Netscape")&&(parseInt(appVersion)==4)) {
				document.MM_pgW=innerWidth; 
				document.MM_pgH=innerHeight; 
				onresize=MM_reloadPage; 
			}
		}
	else if (innerWidth!=document.MM_pgW || innerHeight!=document.MM_pgH) 
		location.reload();
}
MM_reloadPage(true);

// =========================================================
// date library
// =========================================================
// Returns day dd month yyyy
function getDateStr()
{	dayStr = new Array("Sun","Mon","Tue","Wed","Thu","Fri","Sat");
	monthStr = new Array("January","February","March","April","May","June","July","August","September","October","November","December");
	
	zDay = dayStr[this.getDay()];
	zDate = this.getDate();
	zMonth = monthStr[this.getMonth()];
	zYear = this.getYear();
	if (zYear < 2000) zYear += 1900;
	outStr = zDay+" "+zDate+" "+zMonth+" "+zYear;
	return outStr;
}

//register method
Date.prototype.getDateStr=getDateStr;

function getLastMod()
{ 
	vDate = new Date(); // set today
	timeLapsed = Date.parse(document.lastModified);
	if (timeLapsed == 0 || timeLapsed == null) // Opera3
	{	
	}
	else
	{	vDate.setTime(timeLapsed);
	}
	ouStr = vDate.getDateStr();
	return outStr;
}

// =========================================================
// taxonomy library
// =========================================================
menuItem = new Array();
tagLookup = new Array();

function taxoCreateLookup(){
	for( i = 0 ; i < menuNum ; i++ ) {
		tagLookup[menuItem[i].tag] = i;
	}
}

function createDefPagePath(){
	var pathPrfx,i;
	
	for( pathPrfx='',i=0 ; i < taxonomy.length-1 ; i++ )
		 pathPrfx += '../';
	return pathPrfx;
}

function getMenu( tag, prop ){
	return menuItem[tagLookup[tag]][prop];
}

function getMenuByIdx( idx, prop ){
	return menuItem[idx][prop];
}

function getMenuIdx( tag ){
	return tagLookup[tag];
}


function taxoObj(tag, title, url, children){	
	this.tag = tag; 
	this.title = title; 
	this.url = url;
	this.children = children;
}

// =========================================================
// rollover library
// =========================================================

// =========================================================
// css library
// =========================================================
function loadCss( pFile ){
  document.write('<link href=' );
  document.write('"'+pFile+'" ');
  document.write('type="text/css" rel="stylesheet">'); 
}  

// =========================================================
// tooltip library
// =========================================================
function tipOver(tipId){	
	zWrite("toolTipContent", gText(tipId+"Txt") );
	zMove("toolTip", zMouseX, zMouseY+5 ); //gLeft(tipId),gTop(tipId)+20);	
	zShow("toolTip");
}

function tipOut(pObj,tipVar){
	zHide("toolTip");	
}

// =========================================================
// cookies library
// =========================================================
isCookie=false;

function checkCookie() {
  setBiscuit("cook", "1", 0);
	if( parseInt(getCookie("cook"))==1 ){
    isCookie=true;
	}	
  delCookie("cook")	; 
}  
  
function getCookie (name) {
	var result = null;
	var vCookie = " " + document.cookie + ";";
	var searchStr = " " + name + "=";
	var cookieStart = vCookie.indexOf(searchStr)
	var cookieEnd;
	if (cookieStart != -1) {
		cookieStart += searchStr.length; // skip past cookie name
		cookieEnd = vCookie.indexOf(";", cookieStart);
		result = unescape(vCookie.substring(cookieStart, cookieEnd));
	}
	return result;
}

function setCookie(name, value, expires, path, domain, secure) {
	var expStr = ((expires == null) ? "" : ("; expires=" + expires.toUTCString()))
	var pathStr = ((path == null) ? "" : ("; path=" + path))
	var domainStr = ((domain == null) ? "" : ("; domain=" + domain))
	var secureStr = ((secure == true) ? "; secure" : "")
	document.cookie = name + "=" + escape(value) + expStr + pathStr + domainStr + secureStr;
}

function setBiscuit(name, value, lifetime){
	// lifetime in hour, set 0 to session only
	
	var vNow = new Date();
	var vHours = 3600000*lifetime; // in ms
	if( vHours == 0 )
		setCookie( name, value, null, "/" );
	else{
		expDate.setTime( vNow.getTime() + vHours );
		setCookie( name, value, expDate, "/" ); // everybody in root can read
	}
}

function delCookie(name){
	var expDate = new Date();
	expDate.setTime( 62179200000 );
	setCookie( name, "", expDate, "/" );	
}

// =========================================================
// query String library
// =========================================================
function getQueryStr(varname) {
  var query = window.location.search.substring(1);
  var vars = query.split("&");
  for (var i=0;i<vars.length;i++) {
    var pair = vars[i].split("=");
    if (pair[0] == varname ) {
      return pair[1];
    }
  } 
}

// =========================================================
// iframe library
// =========================================================
function load2Iframe( pIFrameName, pUrl )
{	
	if( frames[pIFrameName] && frames[pIFrameName].location )
		frames[pIFrameName].location.href = pUrl;
	else if( dom ){
		obj = getById(pIFrameName);
		if( obj ){
			obj.src = pUrl;
		}
	}	
}

var prevHeight = 0;
function flexiIframe(pIFrameName) {
	var winWidth = gWinWidth();
	if( gBrowser.isGecko )
		winWidth -= 16;

  var iframeWin = window.frames[pIFrameName];
	var iframeObj = getById(pIFrameName);
  if ( iframeObj && iframeWin ) {
    var iframeDoc = iframeWin.document;
		var h1 = iframeDoc.body.scrollHeight;
		var h2 = iframeDoc.body.offsetHeight;
		if( h1 && h2 ){
			var newHeight = (h1 > h2)?h1:h2;	
			newHeight += 30;
			if( newHeight < screen_Height ) newHeight = screen_Height;
			
			if (prevHeight < newHeight){
				zSize(pIFrameName, winWidth, newHeight);
		    //iframeObj.style.height = "auto"; // resize 
	    	//iframeObj.style.height = newHeight + 30 + "px";		
				prevHeight = newHeight;
			}
  	}
	}
}

// =========================================================
// iFrameStage library
// =========================================================
var stageParam = new Array();
stageParam[0] = "xml file";
stageParam[1] = "tag";

function stageLoaded(){	
}

function stageResize(){	
	flexiIframe("iFrameStage");
}

function stageClose(){	
	zHide( "iFrameStage" );
}

function getStageParam(){
	return stageParam;
}

function stageShow( pXml )
{	showInStage( pXml, null, 0 );
}

function showInStage( pXml, pTag, pIdx )
{	
	if( !gBrowser.isIE6up && !gBrowser.isFirefox && !gBrowser.isNS7up )
		return true;

	stageParam[0] = pXml; // send absolute path to stage -> /portfolio/photography/... 
	stageParam[1] = pTag; // artwork's tag
	stageParam[2] = pIdx; // artwork's index

	load2Iframe( "iFrameStage", abs2Rel( "/rsrc/stage/artstage.html" ) );
	zMove( "iFrameStage", 0, gPageYOffset() );	
	zShow( "iFrameStage" );
	return false;
}

// =========================================================
// layout fixed/fluid library
// =========================================================
layout = "fixed"; // default

function initFluid(){
	chkFluid();
	if( layout == "fluid" ){ loadCss( pathPrfx+"css/fluid.css" ); }
}

function chkFluid(){
	if( isCookie ){
		if( getCookie("fluid") == "1" ){
			layout = "fluid";
		}
	}
}

function toggleFluid(){
	if( layout == "fixed" )
		setBiscuit("fluid", "1", 0);
	else
		setBiscuit("fluid", "0", 0);
	window.location.reload();
	return false;
}

function buildFluidLink( prefix, postfix ){
	if( isCookie ){
		if( layout == "fixed" )
			document.write('<a href="#" title="fluid/liquid layout" onclick="return toggleFluid()">fluid</a>' + postfix); 
		else
			document.write('<a href="#" title="fixed width layout" onclick="return toggleFluid()">fixed</a>' + postfix); 
	}	
}

// =========================================================
// flash detect library
// =========================================================

flashVersion = 0;
latestVersion = 7;
downloadUrl = "http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version="+latestVersion+",0,0,0";

function isFlash(version){
	if( version <= flashVersion )
		return true;
}	

function flashDetect(){
	agt=navigator.userAgent.toLowerCase();
	
	// Netscape & Opera detection
	if (navigator.plugins && navigator.plugins["Shockwave Flash"]) {
		
	    var words = navigator.plugins["Shockwave Flash"].description.split(" ");

	    for (var i = 0; i < words.length; ++i)
	    {	if (isNaN(parseInt(words[i])))
				continue;
			flashVersion = parseInt(words[i]);
		}
	// IE detection using fucking activex
	} else if (agt && (agt.indexOf("msie") >= 0) && parseInt(navigator.appVersion) >= 4 && agt.indexOf("win")!=-1 ) {
		
		xdw('<scr');
		zdw('ipt language=vbscript>');
		zdw('on error resume next');
		zdw('for ver = ' + latestVersion + ' to 1 step -1');
		zdw('isShock = IsObject(CreateObject("ShockwaveFlash.ShockwaveFlash." & ver ))');
		zdw('if isShock then');
		zdw('flashVersion = CStr(ver)');
		zdw('exit for');
		zdw('end if');
		zdw('next');
		xdw('</scr');
		xdw('ipt>');
	}	
}

function flashString( pId, pVersion, pSwf, pW, pH, pImg ){
	var fString = "";
	if (flashVersion>=pVersion){
		fString += '<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="'+downloadUrl+'" id="'+pId+'" width="'+pW+'" height="'+pH+'">'
		fString += '<param name="src" value="' + pSwf + '" />'
		fString += '<param name="quality" value="high" /><param name="wmode" value="transparent" />';
		fString += '<embed type="application/x-shockwave-flash" name="'+pId+'" id="'+pId+'" ';
		fString += 'width="'+pW+'" height="'+pH+'" src="' + pSwf + '" pluginspage="' + downloadUrl + '"></embed>';
		fString += '</object>';
	}else{
		if( pImg ){
			fString += '<img src="'+pImg+'" width="'+pW+'" height="'+pH+'" />';
		}
		fString += '<div class="warning">You need Flash '+ pVersion +' plugin to view this content. ';
		fString += 'You either do not have it or do not have neccessary version. You may download it <a href="' + downloadUrl + '">here</a>.</div>';
	}
	return fString;
}

function flashWrite( pId, pVersion, pSwf, pW, pH, pImg ){
	if (flashVersion>=pVersion){
		xdw('<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="'+downloadUrl+'" id="'+pId+'" width="'+pW+'" height="'+pH+'">');
		xdw('<param name="src" value="' + pSwf + '" />');
		xdw('<param name="quality" value="high" /><param name="wmode" value="transparent" />');
			xdw( '<embed type="application/x-shockwave-flash" name="'+pId+'" id="'+pId+'" ' );
			xdw( 'width="'+pW+'" height="'+pH+'" src="' + pSwf + '" pluginspage="' + downloadUrl + '"></embed>' );
		xdw('</object>');
	}else{
		if( pImg ){
			xdw('<img src="'+pImg+'" width="'+pW+'" height="'+pH+'" />');
		}
		xdw('<div class="warning">You need Flash '+ pVersion +' plugin to view this content.' );
		xdw('You either do not have it or do not have neccessary version. You may download it <a href="' + downloadUrl + '">here</a>.</div>');
	}
}

function flashCheck( pVersion ){
	if ( flashVersion < pVersion ){
		xdw('<div class="warning">If you see empty space above, you may not have Flash '+ pVersion +' plugin nedeed to view the content. ' );
		xdw('<a href="' + downloadUrl + '">Download the plugin</a>.</div>');
	}
}
