function oldIE(){
	return (navigator.appName == 'Microsoft Internet Explorer' && parseFloat(navigator.appVersion.substr(22,3)) < 7);
	// Use regex match() to find 'MSIE x.x' at some point.
}

function toggle(div_id){
	if(document.getElementById(div_id)){
		var el = document.getElementById(div_id);
		if(el.style.display == 'none'){
			el.style.display = 'block';
		}else{
			el.style.display = 'none';
		}
	}
}

function fog_size(popUpDivVar) {
	var viewPortArray=viewPortSize();
	if(typeof window.innerWidth != 'undefined'){
		viewportheight = window.innerHeight;
	}else{
		viewportheight = document.documentElement.clientHeight;
	}

	if((viewportheight > document.body.parentNode.scrollHeight) && (viewportheight > document.body.parentNode.clientHeight)){
		fog_height = viewportheight;
	}else{
		if (document.body.parentNode.clientHeight > document.body.parentNode.scrollHeight) {
			fog_height = document.body.parentNode.clientHeight;
		}else{
			fog_height = document.body.parentNode.scrollHeight;
		}
	}
	var fog = document.getElementById('fog');
	fog.style.height = viewPortArray[3] + 'px';
	fog.style.width = viewPortArray[2] + 'px';

	popShim('fog');
}

function window_pos(popUpDivVar){
	if(document.getElementById(popUpDivVar)){
		var viewPortArray=viewPortSize();

		var popUpDiv = document.getElementById(popUpDivVar);
		var window_width=viewPortArray[0];
		left_pos=Math.max(min_left,((window_width-parseInt(popUpDiv.style.width))/2));
		popUpDiv.style.left = left_pos + 'px';

		var popUpDiv = document.getElementById(popUpDivVar);
		var window_height=viewPortArray[1];
		top_pos=Math.max(scroller_height + min_top,(((window_height-scroller_height-player_height)-parseInt(popUpDiv.style.height))/2)+scroller_height);
		if(popUpDivVar=='popUpDivScroller'){
			popUpDiv.style.top = '10px';
			popUpDiv.style.position = document.getElementById('popUpDiv').style.position;
		}else if(popUpDivVar=='popUpDivPlayer'){
			popUpDiv.style.top = Math.max((window_height - player_height), parseInt(document.getElementById('popUpDiv').style.top) + parseInt(document.getElementById('popUpDiv').style.height) + 10) +'px';
			popUpDiv.style.position = document.getElementById('popUpDiv').style.position;
		}else{
			popUpDiv.style.top = top_pos + 'px';
			popUpDiv.style.position = (parseInt(popUpDiv.style.height) > (window_height - scroller_height - player_height -2 * 10) || parseInt(popUpDiv.style.width) > (window_width -2 * 10) || oldIE())?'absolute':'fixed';
		}

		if(popUpDiv.style.position=='absolute'){
			if(popUpDivVar!='popUpDivPlayer') popUpDiv.style.top = parseInt(popUpDiv.style.top) + getScrollHeight() + 'px';
			popUpDiv.style.left = parseInt(popUpDiv.style.left) + getScrollWidth() + 'px';
		}

	}	
}

function popShim(windowname){ // Ensures the IE6 'Shim' tracks the 'windowname' div.
	if(document.getElementById(windowname + 'Shim')){
		shimID=document.getElementById(windowname + 'Shim');
		windowID=document.getElementById(windowname);
		shimID.style.width=windowID.style.width;
		shimID.style.height=windowID.style.height;
		shimID.style.left=windowID.style.left;
		shimID.style.top=windowID.style.top;
	}
}

function window_size(popUpDivVar){
	var viewPortArray=viewPortSize();
	var window_width=viewPortArray[0];
	var window_height=viewPortArray[1];
	
	var clip_width=Math.min(window_width-10-10-20, max_width, nativeWidth);
	var clip_height=Math.min(window_height-scroller_height-player_height-10-10-20, max_height, nativeHeight);

	var popUpDiv = document.getElementById(popUpDivVar);
	var mainImage = document.getElementById('mainimage');

	var scale = Math.max(Math.min(clip_height / nativeHeight, clip_width / nativeWidth), Math.min(min_height / nativeHeight, 1), Math.min(min_width / nativeWidth, 1));


	mainImage.width = nativeWidth * scale;;   
	mainImage.height = nativeHeight * scale;
	
	popUpDiv.style.width = (parseInt(mainImage.width)+20) + 'px';
	popUpDiv.style.height = (parseInt(mainImage.height)+20) + 'px';
}

/*************************************************************************/

var isLoading=false;
var firstView = true;

var scroller_height = 0;
var scroller_top = 10;
var player_height = 0;
var player_btm = 10
var nativeWidth=0;
var nativeHeight=0;
var min_left=10;
var min_top=10;
var min_width=250;
var min_height=250;
var max_width=1000;
var max_height=1000;


function popup(windowname){
	if (firstView) { 
		scroller_height = parseInt(document.getElementById(windowname + 'Scroller').style.height);
		if(scroller_height>0) scroller_height += scroller_top;
		player_height = parseInt(document.getElementById(windowname + 'Player').style.height);
		if(player_height>0) player_height += player_btm;
	}
	fog_size(windowname);
	window_size(windowname);
	window_pos(windowname);
	toggle('fog');
	toggle('fogShim');
	toggle(windowname);
	window_pos(windowname + 'Scroller');
	toggle(windowname + 'Scroller');
	window_pos(windowname + 'Player');
	toggle(windowname + 'Player');
	if (firstView) loadScroller();
	firstView = false;
}

window.onresize = function (){
	windowname='popUpDiv';
	if(document.getElementById(windowname) && document.getElementById(windowname).style.display == 'block'){
		fog_size(windowname);
		window_size(windowname);
		window_pos(windowname);
		window_pos(windowname + 'Scroller');
		window_pos(windowname + 'Player');
	}
}
	
function newImage(){
	isLoading=false;
	document.getElementById('mainimage').removeAttribute("width"); 
	document.getElementById('mainimage').removeAttribute("height");	
	nativeWidth = parseInt(document.getElementById('mainimage').width); //global
	nativeHeight = parseInt(document.getElementById('mainimage').height); //global
	document.getElementById('loading').style.display="none"; 
	windowname='popUpDiv';
	window_size(windowname);
	window_pos(windowname);
	window_pos(windowname + 'Scroller');
	window_pos(windowname + 'Player');
}
