
var loadingImage = 'assets/img/imageloading.gif';		
var closeimg = 'assets/img/close.gif';		
function getPageScroll(){

	var yScroll;

	if (self.pageYOffset) {
		yScroll = self.pageYOffset;
	} else if (document.documentElement && document.documentElement.scrollTop){	
		yScroll = document.documentElement.scrollTop;
	} else if (document.body) {
		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){ 
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { 
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	
	var windowWidth, windowHeight;
	if (self.innerHeight) {	
		windowWidth = self.innerWidth;
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { 
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { 
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}	
	
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else { 
		pageHeight = yScroll;
	}

	if(xScroll < windowWidth){	
		pageWidth = windowWidth;
	} else {
		pageWidth = xScroll;
	}


	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight) 
	return arrayPageSize;
}

function pause(millis) {
	var now = new Date();
	var exitTime = now.getTime() + millis;
	while (true) {
		now = new Date();
		if (now.getTime() > exitTime)
			return;
	}
}


function showImage(objLink)
{
	// prep objects
	var imageBack = document.getElementById('image-back');
	var imageForm = document.getElementById('image-form');
	var imageMain = document.getElementById('image-main');
	var imageLoad = document.getElementById('image-load');
	var imageBottom = document.getElementById('image-bottom');

	
	var arrayPageSize = getPageSize();
	var arrayPageScroll = getPageScroll();

	if (imageLoad) {
		imageLoad.style.top = (arrayPageScroll[1] + ((arrayPageSize[3] - 35 - imageLoad.height) / 2) + 'px');
		imageLoad.style.left = (((arrayPageSize[0] - 20 - imageLoad.width) / 2) + 'px');
		imageLoad.style.display = 'block';
	}

	imageBack.style.height = (arrayPageSize[1] + 'px');
	imageBack.style.display = 'block';

	var image = new Image();

	image.onload=function(){
		imageMain.src = objLink.href;
		
        var height=imageMain.height;
		if(image.height > 0)
		   height=image.height;
		var width=image.width;
		if(image.width > 0)
		   width=image.width;
		/*
		if(height < 400){	
		   var bl=width/height;
		   height=400;
		   width=Math.ceil(400*bl);	   
		}
		*/
		if(height > arrayPageSize[3]){
		var bl=width/height;
		   height=arrayPageSize[3];
		   width=Math.ceil(arrayPageSize[3]*bl);
		}
	    image.height=height;
		image.width=width;
		imageMain.height=height;
		imageMain.width=width;
		
		var top = arrayPageScroll[1] + ((arrayPageSize[3] - 35 - image.height) / 2);
		var left = ((arrayPageSize[0] - 20 - image.width) / 2);
		
		imageForm.style.top = (top < 0) ? "0px" : top + "px";
		imageForm.style.left = (left < 0) ? "0px" : left + "px";


		imageBottom.style.width = image.width + 'px';	
		if (navigator.appVersion.indexOf("MSIE")!=-1){
			pause(100);
		} 

		if (imageLoad) {	
		   imageLoad.style.display = 'none'; 
	    }	
		imageForm.style.display = 'block';
		arrayPageSize = getPageSize();
		imageBack.style.height = (arrayPageSize[1] + 'px');
		return false;
	}

	image.src = objLink.href;
	
}

//close image
function closeImage()
{
	imageBack = document.getElementById('image-back');
	imageForm = document.getElementById('image-form');

	imageBack.style.display = 'none';
	imageForm.style.display = 'none';
}

function initImage()
{
    if (!document.getElementsByTagName){ return; }
	var anchors = document.getElementsByTagName("a");
	for (var i=0; i<anchors.length; i++){
		var anchor = anchors[i];
		if (anchor.getAttribute("href") && (anchor.getAttribute("rel") == "showImage")){
			anchor.onclick = function () {showImage(this); return false;}
		}
	}
	
	var imageBack = document.createElement("div");
	imageBack.setAttribute('id','image-back');
	imageBack.onclick = function () {closeImage(); return false;}
	imageBack.style.display = 'none';
	imageBack.style.position = 'absolute';
	imageBack.style.top = '0';
	imageBack.style.left = '0';
	imageBack.style.zIndex = '90';
 	imageBack.style.width = '100%';
	document.body.insertBefore(imageBack, document.body.firstChild);
	
	var imageLoading = new Image();	
	imageLoading.onload=function(){
		var imageLoadingLink = document.createElement("a");
		imageLoadingLink.setAttribute('href','#');
		imageLoadingLink.onclick = function () {closeImage(); return false;}
		imageBack.appendChild(imageLoadingLink);
		
		var imageLoadingImage = document.createElement("img");
		imageLoadingImage.src = loadingImage;
		imageLoadingImage.setAttribute('id','image-load');
		imageLoadingImage.style.position = 'absolute';
		imageLoadingImage.style.zIndex = '150';
		imageLoadingLink.appendChild(imageLoadingImage);

		imageLoading.onload=function(){};	
		return false;
	}
	imageLoading.src = loadingImage;
	
	var imageForm = document.createElement("div");
	imageForm.setAttribute('id','image-form');
	imageForm.style.display = 'none';
	imageForm.style.position = 'absolute';
	imageForm.style.zIndex = '100';	
	document.body.insertBefore(imageForm, imageBack.nextSibling);
	
	var closeButtonLink = document.createElement("a");
	closeButtonLink.setAttribute('href','#');
	closeButtonLink.setAttribute('title','Click to close');
	closeButtonLink.onclick = function () {closeImage(); return false;}
	imageForm.appendChild(closeButtonLink);

	var loadCloseButton = new Image();
	loadCloseButton.onload=function(){
		var closeButton = document.createElement("img");
		closeButton.src = closeimg;
		closeButton.setAttribute('id','close-button');
		closeButton.style.position = 'absolute';
		closeButton.style.zIndex = '200';
		closeButtonLink.appendChild(closeButton);
		return false;
	}
	loadCloseButton.src = closeimg;
	
	var image = document.createElement("img");
	image.setAttribute('id','image-main');
	closeButtonLink.appendChild(image);
	
	var imageBottom = document.createElement("div");
	imageBottom.setAttribute('id','image-bottom');
	imageForm.appendChild(imageBottom);
}

function addEvent(func)
{	
	var oldonload = window.onload;
	if (typeof window.onload != 'function'){
    	window.onload = func;
	} else {
		window.onload = function(){
		oldonload();
		func();
		}
	}

}
addEvent(initImage);	