var window1;
var images = new Array();
var loader = new Image();
loader.src = 'http://freezerburned.org/images/loading.gif';
var next = new Image();
next.src = 'next.gif';
var prev = new Image();
prev.src = 'prev.gif';
var prevlink = document.createElement('a');
var nextlink = document.createElement('a');

var overlay;
var menuw = document.createElement('div');
var popups = document.getElementsByName('popup');
var popup;
var i;

var cimage;
var arrayPageSize;
var arrayPageScroll;
//get window size
function getPageScroll(){

	var yScroll;

	if (self.pageYOffset) {
		yScroll = self.pageYOffset;
	} else if (document.documentElement && document.documentElement.scrollTop){	 // Explorer 6 Strict
		yScroll = document.documentElement.scrollTop;
	} else if (document.body) {// all other Explorers
		yScroll = document.body.scrollTop;
	}

	arrayPageScroll = new Array('',yScroll) 
	return arrayPageScroll;
}
function getPageSize(){
	
	var xScroll, yScroll;
	
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = document.body.scrollWidth;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	
	var windowWidth, windowHeight;
	if (self.innerHeight) {	// all except Explorer
		windowWidth = self.innerWidth;
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}	
	
	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		var pageHeight = windowHeight;
	} else { 
		var pageHeight = yScroll;
	}

	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){	
	var	pageWidth = windowWidth;
	} else {
	var	pageWidth = xScroll;
	}

	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight) 
	return arrayPageSize;
}
/////////////////////////////////////////

function build_window(){
	arrayPageSize = getPageSize();
	arrayPageScroll = getPageScroll();
}

function windowRes(){
	window1.style.top = (arrayPageScroll[1] + ((arrayPageSize[3] - 35 - popup.height) / 2) + 'px');
	window1.style.left = (((arrayPageSize[0] - 20 - popup.width) / 2) + 'px');
	window1.appendChild(menuw);
}

function portinit(){
	loader.style.position = 'absolute';
	loader.style.zindex = '200';
	loader.setAttribute('background-color','white');
	build_window();
	overlayf();
	var popups = document.getElementsByName('popup');
	for(i = 0; i < popups.length; i++){
		images[i] = new Array();
		images[i][0] = popups[i].href;
		images[i][1] = popups[i].title;
		popups[i].href="javascript: PopupPic("+i+");";
		popups[i].onClick = "this.blur();";
	}
		menuw.id = 'menuw';
		nextlink.href = 'javascript: remove(); PopupPic('+(images.length-1)+');';
		nextlink.innerHTML ="<img src='http://freezerburned.org/images/next.gif'/>";
		nextlink.id='next';
	
		prevlink.href = 'javascript: remove(); PopupPic(1);';
		prevlink.innerHTML = "<img src='http://freezerburned.org/images/prev.gif'/>";
		prevlink.id = 'prev';
		
		var closelink = document.createElement('a');
		closelink.href = 'javascript: remove();';
		closelink.innerHTML = "<img src='http://freezerburned.org/images/close.gif'/>";
		menuw.innerHTML = "<div id='caption'></div>";
		menuw.appendChild(nextlink);
		menuw.appendChild(closelink);
		menuw.appendChild(prevlink);
}

function overlayf(){
	if(!document.getElementById('TB_overlay')){
		document.getElementsByTagName('body')[0].innerHTML += "<div id='TB_overlay' onClick='remove()'></div>";
	}
	overlay = document.getElementById('TB_overlay');
	overlay.style.height= arrayPageSize[1] +"px";
  	overlay.style.width= arrayPageSize[0] +"px";
	overlay.style.display = 'none';
}

function PopupPic(n){
	build_window();
	cimage = n;
	
	var total = images.length -1;
	var p;
	var f;
	//setup next/ previos buttons

	f = (total < n+1) ? 0: n+1;
	p = (n-1 <0) ? total-1: n-1;
	p = (p < 0) ? 0: p;
		
	nextlink.href = "javascript: remove(); PopupPic(" + f + ");";
	prevlink.href = "javascript: remove(); PopupPic(" + p + ");";
	
	popup = new Image();
	popup.src = images[n][0];
	window1 = document.createElement('div');
	window1.style.width = "0px";
	window1.id = 'windowl';
	overlayf();
	overlay.style.display = 'inline';
	
	document.body.appendChild(loader);
	loader.style.top = (arrayPageScroll[1] + ((arrayPageSize[3] - 35 - loader.height) / 2) + 'px');
	loader.style.left = (((arrayPageSize[0] - 20 - loader.width) /2) + 'px');
	
	window1.style.top = (arrayPageScroll[1] + ((arrayPageSize[3] - 35 - popup.height) / 2) + 'px');
	window1.style.left = (((arrayPageSize[0] - 20 - popup.width) / 2) + 'px');
	
	document.body.appendChild(window1);
	
	//detect if image is loaded
	if (!popup.complete || typeof popup.naturalWidth != "undefined" && popup.naturalWidth == 0) {
		popup.onload = function(){
			setTimeout(applyWindow, 500);	
		}
		
	}else{
		setTimeout(applyWindow, 500);	
	}
		

}

function applyWindow(){
window1.style.width = popup.width + "px";
		window1.id = 'window1';

		document.body.removeChild(loader);
		window1.appendChild(popup);
		windowRes();
		document.body.appendChild(window1)
		document.getElementById('caption').innerHTML = images[cimage][1];
}
function remove(){
	cimage = null;
	overlay.style.display = 'none';
	document.body.removeChild(window1);
}

function resize_window(){
	build_window();
	windowRes();
	overlayf();
	if(cimage != null){
		overlay.style.display = 'inline';
	}
}