var image = new image_popup();

window.onload = function(e)
{
	image.init();
}

window.onscroll = image.scroll;
window.onresize = image.resize;

/* Image popup */


function image_popup()
{
	// Declare functions
	this.init = image_init;
	
	this.load = image_load;
	this.loaded = image_loaded;
	this.close = image_close;
	
	this.place_overlay = image_place_overlay;
	this.place_image = image_place_image;
	
	// Declare events
	this.scroll = image_scroll;
	this.resize = image_resize;
	
	// Declare variables
	this.active = 0;
	this.activeImg = 0;
	this.activeSrc = "";
	this.activeTitle = "aa";
	this.activeLoaded = 0;
	
	function image_init()
	{
		var links = document.getElementsByTagName("a");
		for(var x in links)
		{
			if(links[x].rel == "image.popup")
			{
				links[x]._image = links[x].href;
				links[x].href = "javascript:image.load('"+links[x].title+"', '"+links[x]._image+"')";
			}
		}
		
		this.overlay 	= document.getElementById("img_overlay");
		this.placehold 	= document.getElementById("img_placehold");
		this.container 	= document.getElementById("img_container");
		this.title 		= document.getElementById("img_title");
		
		this.overlay.onmousedown = this.close;
		this.placehold.onmousedown = this.close;
		
		this.ready = 1;
	}
	
	function image_load(title, src)
	{
		// Save some info
		this.activeTitle = title;
		this.activeSrc = src;
		this.active = 1;
		
		// Set the scroll right
		this.scroll();
		
		// Place overlay
		this.overlay.style.display = "block";
		this.place_overlay();
		
		// Create image
		var image = document.createElement("IMG");
		image.onload = this.loaded;
		image.src = src;
		this.container.appendChild(image);
		
		this.activeImg = image;
	}
	function image_loaded()
	{
		// Image is loaded
		image.activeLoaded = 1;
		
		if (image.activeCheck)
			window.clearTimeout(image.activeCheck);
		
		image.title.innerHTML = image.activeTitle;
		
		// Save old height
		this.oldWidth = this.clientWidth;
		this.oldHeight = this.clientHeight;
		
		// Resize image
		window.setTimeout("image.place_image();", 5);
	}
	function image_close()
	{
		if(!image.active)
			return;
		
		// Remove overlay and placeholder
		image.overlay.style.display = "none";
		image.placehold.style.visibility = "hidden";
		image.placehold.style.height = "0px";
		image.placehold.style.width = "0px";
		image.container.style.marginLeft = "0px";
		image.container.style.marginTop = "0px";
		
		if(image.activeLoaded)
			image.container.removeChild(image.activeImg);
			
		if (image.activeCheck)
			window.clearTimeout(image.activeCheck);
		
		image.activeLoaded = 0;
		image.activeImg = 0;
		image.active = 0;
	}
	
	
	function image_place_overlay()
	{
		var height = document.body.parentNode.clientHeight;
		var width = document.body.clientWidth;
		
		this.overlay.style.height = height+"px";
		this.overlay.style.width  = width+"px";
		this.overlay.style.lineHeight = height+"px";
		
		this.placehold.style.height = height+"px";
		this.placehold.style.width  = width+"px";
	}
	function image_place_image()
	{
		var height = document.body.parentNode.clientHeight;
		var width = document.body.clientWidth;
		
		if((height - 150) < this.activeImg.oldHeight || (width - 150) < this.activeImg.oldWidth)
		{
			overflowHeight = this.activeImg.oldHeight - (height - 150);
			overflowWidth  = this.activeImg.oldWidth - (width - 150);
			
			// Resize from width
			if((this.activeImg.oldWidth - overflowWidth) < 100)
				overflowWidth = this.activeImg.oldWidth - 100;
				
			var procent = 1 - (overflowWidth / this.activeImg.oldWidth);
			this.activeImg.style.height = Math.round(this.activeImg.oldHeight * procent) + "px";
			this.activeImg.style.width = (this.activeImg.oldWidth - overflowWidth) + "px";

			// Resize from height
			if((this.activeImg.oldHeight - overflowHeight) < 100)
				overflowHeight = this.activeImg.oldHeight - 100;
				
			var procent = 1 - (overflowHeight / this.activeImg.oldHeight);
			this.activeImg.style.height = (this.activeImg.oldHeight - overflowHeight) + "px";
			this.activeImg.style.width = Math.round(this.activeImg.oldWidth * procent) + "px";
		}
		
		image.container.style.marginLeft = 0 - Math.round(image.container.clientWidth / 2) + "px";
		image.container.style.marginTop = 0 - Math.round(image.container.clientHeight / 2) + "px";
		
		image.placehold.style.visibility = "visible";
	}
	
	function image_scroll()
	{
		if(!image.active)
			return;
		
		var y;
		if (self.pageYOffset)
			y = self.pageYOffset;
		else if (document.documentElement && document.documentElement.scrollTop)
			y = document.documentElement.scrollTop;
		else if (document.body)
			y = document.body.scrollTop;
		
		image.overlay.style.top = y + "px";
		image.placehold.style.top = y + "px";
	}
	function image_resize()
	{
		if(!image.active)
			return;
		
		// Resize overlay
		image.place_overlay();
		
		// Resize image
		if(image.activeLoaded)
		{
			image.place_image();
		}
		if (image.onresizetimer)
			window.clearTimeout(image.onresizetimer);
		image.onresizetimer = window.setTimeout("image.resize();", 150);
	}
}
