window.onload = initAll;
window.onresize = resize;

// find position of an element
function findPos(obj) {
	var curleft = curtop = 0;
	if (obj.offsetParent) {
		curleft = obj.offsetLeft
		curtop = obj.offsetTop
		while (obj = obj.offsetParent) {
			curleft += obj.offsetLeft
			curtop += obj.offsetTop
		}
	}
	return [curleft,curtop];
}

//Returns ENTIRE window height and width INCLUDING scrolled area
function winSizeAll(x){
	if( window.innerHeight && window.scrollMaxY ) // Firefox 
	{
		pageWidth = window.innerWidth + window.scrollMaxX - 5; //document.body.scrollWidth; 
		pageHeight = window.innerHeight + window.scrollMaxY;
	}
	else if( document.body.scrollHeight > document.body.offsetHeight ) // all but Explorer Mac
	{
		pageWidth = document.body.scrollWidth;
		pageHeight = document.body.scrollHeight;
	}
	else // works in Explorer 6 Strict, Mozilla (not FF) and Safari
	{ 
		pageWidth = document.body.offsetWidth + document.body.offsetLeft; 
		pageHeight = document.body.offsetHeight + document.body.offsetTop; 
	}
  if (x=='w'){ return pageWidth; } else if (x == 'h'){  return pageHeight; }
}


// ### builds paths
function buildPath(el,type){
	var name = el.name.split(".");     // name - same for content file and image
	var fname = name[0];
	var imExt = "." + name[1];
	var title = el.title.split(" ");
	//build file path from image title
	function path(){
        if (!title || title.length < 1){return}
        var p="/portfolio/";
        for (t=0;t<title.length;t++){
            var dir = title[t];
            p+= dir + "/";
        }
        return (p);
    }
	var imgPath = path() + fname + imExt;
	var prevPath = path() + fname + "-pre" + imExt;
	var page = path() + fname + ".html";
	// returns path are any part thereof based on 2nd argument
	if(type=="name") return fname;
	if(type=="ext") return imExt;
	if(type=="base") return path();
	if(type=="img") return imgPath;
	if(type=="prev") return prevPath;
	if(type=="page") return page;
}

// #### Layer Window functions
// Turns window and overlay on. sets overlay transparency. gets ajax file.
function showWindow(){
	var content = this.href;
	var title = this.title;
	var wSizes = getWinSize(this.rel);
	if (title.length > 0 && winTitle){
		winTitle.innerHTML = title;
		winTitle.style.display='block';
	} else if (winTitle){
		winTitle.style.display='none';
	}
	layer.style.display="block";
	overlay.style.display='block';
	if (this.rel.indexOf("iframe") > -1){
        makeIframe(content);
		var winFrame = document.getElementById('winFrame');
		// changes size of iframe is content is passed in via rel attribute
		if (wSizes && wSizes.length > 0){
			var h = (wSizes[1] * 1)  + 15;
			var w = (wSizes[2] * 1);
        	winFrame.style.height=h +"px"; // 2nd position in array is passed height
       	 	winFrame.style.width=w +"px"; // 3rd position in array is passed width
    	} 
    } else {
        getNewFile(content,'updateArea');
    }
	setTimeout("getHeight()",500);
	return false;
}

/* pass contents of rel attribute, looks for "size" at the front then turns contents into array */
function getWinSize(x){
	if(x.substring(0,4) == "size"){
		el = x.split(" ");
		return el;
	} return;
}

// building a table with and iFrame and populate layerWin with it.    
function makeIframe(url){
    var content = '<table id="winTable"><tr><td>\
			<div id="winTitle"></div>\
			<div id="winClose"><a href="#" onclick="closeWindow()" class="closeWindow">Close Window</a></div>\
			<iframe frameborder="0" id="winFrame" src="' + url + '"></iframe></td></tr></table>'
    layer.innerHTML=content;
    return false;
}

// Get height of Layer Window and pass to setHeight
function getHeight(x){
  	var winH = layer.offsetHeight;
	var winT = layer.offsetTop;
	var yPos;
		if (self.pageYOffset) {
			yPos = self.pageYOffset;;
		} else if (document.documentElement && document.documentElement.scrollTop){
			yPos = document.documentElement.scrollTop; 
		} else if (document.body) {
			yPos = document.body.scrollTop;
		} 
	var h = winH + winT + yPos;
	setHeight (h,x);
  	return;
}
/*

function setHeight(h,x){
    var pHeight = (document.getElementById('pageBtm') != null) ? document.getElementById('pageBtm').offsetTop : 0;
	overlay.style.width=winSizeAll('w') + "px";
	var max1 = Math.max(h,pHeight);
	var max2 = Math.max(max1,overlay.offsetHeight);
	var max3 = Math.max(max2,winSizeAll('h'));
	//if (BrowserDetect.browser == "Explorer"  &&  BrowserDetect.version < 7){
		overlay.style.height=max3 + "px";
   // }
   if(!x) {document.onclick = getXY;}
}
*/
function setHeight(h,x){
	//overlay.style.width=winSizeAll('w') + "px";
	var pHeight = (document.getElementById('pageBtm') != null) ? document.getElementById('pageBtm').offsetTop : 0;

	var max1 = Math.max(h,pHeight);
	var max2 = Math.max(max1,overlay.offsetHeight);
	var max3 = Math.max(max2,winSizeAll('h'));
	if (h > pHeight){
		overlay.style.height=h + "px";
	} else if (BrowserDetect.browser == "Explorer"  &&  BrowserDetect.version < 7){
		overlay.style.height=document.documentElement.clientHeight + "px";
		overlay.style.width=document.documentElement.clientWidth + "px";
	}

	
    if(!x) {document.onclick = getXY;}
}

// close window onclick if not within #innerWin area
function clickClose(x,y){
    var top = layer.offsetTop;
    var bot = top + layer.offsetHeight;
    var left = innerWin.offsetLeft;
    var right = left + innerWin.offsetWidth;
    if (y > top && y < bot && x > left && x < right) {
        return false;
    } else { 
        closeWindow();
        return false;
    }
}

//find cursor position onclick
function getXY(e) {
  x = (window.Event) ? e.pageX : event.clientX;
  y = (window.Event) ? e.pageY : event.clientY;
  clickClose(x,y);
}


// Turns window off.
function closeWindow(){
	overlay.style.display='none';
	if (layer.className.indexOf("block") > -1){
		layer.className = layer.className.replace("block","none");
		}
	if (overlay.className.indexOf("block") > -1){
		overlay.className = layer.className.replace("block","none");
		}
    document.onclick = function(){};
    layer.innerHTML=layerMarkUp;
	layer.style.display="none";
	return false;
}
// #### end Layer Window functions

var overlay; var layer; var layerMarkUp; var winTitle; var innerWin; var updateArea; var allLinks;

function initVars(){
    // GLOBAL VARIABLES AND EVENTS -- MUST BE LAUNCHED AFTER THE PAGE ELEMENTS LOAD
		
    //ids for layer window
    overlay = document.getElementById('overlay');
    layer = document.getElementById('layerWin');
    layerMarkUp = layer.innerHTML;
    winTitle = document.getElementById('winTitle');	
    innerWin = document.getElementById('innerWin');
    updateArea = document.getElementById('updateArea');
	
	allLinks = document.getElementsByTagName("a");
}


// initializes selectors and sets up their events
function initSelectors(){
	for (i=0;i<allLinks.length;i++){
		if (allLinks[i].className.indexOf("openWindow") > -1){
			allLinks[i].onclick=showWindow;
		}
		if (allLinks[i].className=='closeWindow'){
			allLinks[i].onclick=closeWindow;
		}
	}
}

function initAll(){
	initVars(); // set global vars
	initSelectors(); // start events
	if (window.Event) {
		document.captureEvents(Event.CLICK);
	}
}

function resize(){
	setTimeout('getHeight("1")', 500);
}
