function pageWidth() {
	return window.innerWidth != null ? window.innerWidth : document.documentElement && document.documentElement.clientWidth ? document.documentElement.clientWidth : document.body != null ? document.body.clientWidth : null;} function pageHeight() {return  window.innerHeight != null? window.innerHeight : document.documentElement && document.documentElement.clientHeight ? document.documentElement.clientHeight : document.body != null ? document.body.clientHeight : null;} function posLeft() {return typeof window.pageXOffset != 'undefined' ? window.pageXOffset :document.documentElement && document.documentElement.scrollLeft ? document.documentElement.scrollLeft : document.body.scrollLeft ? document.body.scrollLeft : 0;} function posTop() {return typeof window.pageYOffset != 'undefined' ?  window.pageYOffset : document.documentElement && document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop ? document.body.scrollTop : 0;} function posRight() {return posLeft()+pageWidth();} function posBottom() {return posTop()+pageHeight();
}
function pageHeight() {
	return window.innerHeight != null ? window.innerHeight : document.documentElement && document.documentElement.clientHeight ? document.documentElement.clientHeight : document.body != null ? document.body.clientHeight : null;} function pageHeight() {return  window.innerHeight != null? window.innerHeight : document.documentElement && document.documentElement.clientHeight ? document.documentElement.clientHeight : document.body != null ? document.body.clientHeight : null;} function posLeft() {return typeof window.pageXOffset != 'undefined' ? window.pageXOffset :document.documentElement && document.documentElement.scrollLeft ? document.documentElement.scrollLeft : document.body.scrollLeft ? document.body.scrollLeft : 0;} function posTop() {return typeof window.pageYOffset != 'undefined' ?  window.pageYOffset : document.documentElement && document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop ? document.body.scrollTop : 0;} function posRight() {return posLeft()+pageHeight();} function posBottom() {return posTop()+pageHeight();
}

var current;
var prev;
var next;
var index;
var total;
var alpha = 0;
var type;
var imageURL;
var targetWidth;
var targetHeight;
function zoomIMG(id){
	var xmlHttp;
	try{
		xmlHttp=new XMLHttpRequest(); // Firefox, Opera 8.0+, Safari
	}catch(e){
		try{
			xmlHttp=new ActiveXObject("Msxml2.XMLHTTP"); // Internet Explorer
		}catch(e){
			try{
				xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
			}catch(e){
				return true;
			}
		}
	}
	xmlHttp.onreadystatechange = function(){
		if(xmlHttp.readyState==4){
		    var current = id;
			var ans = xmlHttp.responseText;
			
			//document.title = ans;
			ans = ans.split('|');
			var width = ans[0];
			var height = ans[1];
			imageURL  = '/images/'+ans[2];
			prev = ans[3];
			next = ans[4];
			var imageName = ans[5];
			var imageDesc = ans[6];
			index = ans[7];
			total  = ans[8];						
			type = ans[9];
			document.getElementById('pageNum').innerHTML = index + '/' + total;
			var myWidth = pageWidth()-100;
			var myHeight = pageHeight()-100;

   			if(width > myWidth){
   			    var scale = (myWidth)/width;
   			    width *= scale;
   			    height *= scale;
   			}
   			
			if(height > myHeight){
   			    var scale = (myHeight)/height ;
   			    width *= scale;
   			    height *= scale;
   			}
            targetWidth = width;
            targetHeight = height;
   			fadeIn();
		}
	}
	xmlHttp.open("GET","/ajax/images/"+id,true);
	xmlHttp.send(null);	
}

function loadIMG(){
    var el = document.getElementById("photopreview");
    while(el.hasChildNodes()){
        el.removeChild(el.firstChild);
    }
    if(type == "Images"){
        var child = document.createElement("img");
        child.src = imageURL;
        child.style.width = targetWidth+"px";
        child.style.height = targetHeight+"px";
        child.onload = imageLoaded;
        el.appendChild(child);
    }else if(type == "Objects"){
        var code_str = "";
		code_str += " <object type=\"application/x-shockwave-flash\"\n";
		code_str += "data=\"/images/objects/rotaryviewer.swf?url="+imageURL+"\" \n";
		code_str += "width=\""+targetWidth+"\" height=\""+targetHeight+"\">\n";
		code_str += "<param name=\"movie\" value=\"/images/objects/rotaryviewer.swf?url="+imageURL+"\" />\n";
		code_str += "<param name=\"wmode\" value=\"transparent\" />\n";
		code_str += "</object>\n";
		el.innerHTML = code_str;
		imageLoaded();
    }else if(type == "Animations"){    
        var code_str = "";
		code_str += " <object type=\"application/x-shockwave-flash\"\n";
		code_str += "data=\""+imageURL+"\" \n";
		code_str += "width=\""+targetWidth+"\" height=\""+targetHeight+"\">\n";
		code_str += "<param name=\"movie\" value=\""+imageURL+"\" />\n";
		code_str += "</object>\n";
		el.innerHTML = code_str;
		imageLoaded();
    }else if(type == "Videos"){   
        var code_str = "";
		code_str += " <object type=\"application/x-shockwave-flash\"\n";
		code_str += "data=\"/images/movies/player.swf?url="+imageURL+"\" \n";
		code_str += "width=\""+targetWidth+"\" height=\""+targetHeight+"\">\n";
		code_str += "<param name=\"movie\" value=\"/images/movies/player.swf?url="+imageURL+"\" />\n";
		code_str += "</object>\n";
		el.innerHTML = code_str;
		imageLoaded();
    }
}

var size = 0;
function imageLoaded(){
    sizeIn();    
}

function sizeIn(){ 	   		  	
photopreview
  	var photoBox = document.getElementById('photoBox');
  	photoBox.style.display = 'block';
  	//photoBox.style.width = size/100*targetWidth+"px";
  	photoBox.style.width = targetWidth+"px";
  	photoBox.style.height = size/100*(targetHeight)+"px" ;
  	
  	var photopreview = document.getElementById('photopreview');
  	photopreview.style.display = 'block';
  	//photopreview.style.width = size/100*targetWidth+"px";
  	photopreview.style.width = targetWidth+"px";
  	photopreview.style.height = size/100*(targetHeight)+"px" ;
  	
	if(size < 100){
		size += 20;
		setTimeout(sizeIn,10);
	}
}
function sizeOut(){ 	   		  	
  	var photoBox = document.getElementById('photoBox');
  	photoBox.style.display = 'block';
  	//photoBox.style.width = size/100*targetWidth+"px";
  	photoBox.style.width = targetWidth+"px";
  	photoBox.style.height = size/100*(targetHeight)+"px" ;
  	
  	var photopreview = document.getElementById('photopreview');
  	photopreview.style.display = 'block';
  	//photopreview.style.width = size/100*targetWidth+"px";
  	photopreview.style.width = targetWidth+"px";
  	photopreview.style.height = size/100*(targetHeight)+"px" ;
	if(size > 0){
		size -= 10;
		setTimeout(sizeOut,5);
	}else{
		sizeOutDone();
	}
}

function sizeOutDone(){
    var el = document.getElementById("photopreview");
    while(el.hasChildNodes()){
        el.removeChild(el.firstChild);
    }
	if(nextAction>0){
		zoomIMG(nextAction);
	}else{
		fadeOut();
	}
}
function hideIMG(){
	nextAction = 0;
	sizeOut();
}
function nextIMG(){
	nextAction = next;
	sizeOut();
}
function prevIMG(){
	nextAction = prev;
	sizeOut();
}

function fadeIn(){
    var photofade = document.getElementById('photofade');
    photofade.style.display = 'block';
    photofade.style.opacity = alpha/100*0.9;
    photofade.style.filter="alpha(opacity="+alpha*0.9+")";
    var photoWindow = document.getElementById('photoWindow');
  	photoWindow.style.display = 'block';
  	photoWindow.style.opacity = alpha/100;
  	photoWindow.style.filter="alpha(opacity="+alpha+")";
    if(alpha < 100){
		alpha+=10;
		setTimeout(fadeIn,5);
	}else{
		fadeInDone();
	}
}
function fadeOut(){
    var photofade = document.getElementById('photofade');
    photofade.style.display = 'block';
    photofade.style.opacity = alpha/100*0.9;
    photofade.style.filter="alpha(opacity="+alpha*0.9+")";
    var photoWindow = document.getElementById('photoWindow');
  	photoWindow.style.display = 'block';
  	photoWindow.style.opacity = alpha/100;
  	photoWindow.style.filter="alpha(opacity="+alpha+")";
   if(alpha > 0){
		alpha-=10;
		setTimeout(fadeOut,5);
	}else{
		fadeOutDone();
	}
}
function fadeOutDone(){
    var photofade = document.getElementById('photofade');
	photofade.style.display = 'none';
    var photoWindow = document.getElementById('photoWindow');
	photoWindow.style.display = 'none';
    var el = document.getElementById("photopreview");
    while(el.hasChildNodes()){
        el.removeChild(el.firstChild);
    }
}

function fadeInDone(){
   	loadIMG();	
}
