var image = new image_popup();

window.onload = function(e)
{
	image.init();
	if(typeof(promotion) != "undefined")
		promotion.load();
}

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");
		
		var close = this.container.getElementsByTagName("b");
		close[0].onmousedown = this.close;
		//this.placehold.onmousedown = this.close;
		
		this.ready = 1;
	}
	
	function image_load(title, src)
	{
		// Save some info
		image.activeTitle = title;
		image.activeSrc = src;
		image.active = 1;
		
		// Set the scroll right
		image.scroll();
		
		// Place overlay
		image.overlay.style.display = "block";
		image.place_overlay();
		
		// Create image
		var images = document.createElement("IMG");
		images.src = src;
		images.onload = image.loaded;
		
		image.contains = document.createElement("DIV");
		image.container.appendChild(image.contains);
		image.contains.appendChild(images);
		
		image.activeImg = images;
		
		if(images.clientHeight > 50)
		{
			image.loaded();
		}
	}
	function image_loaded()
	{
		if(image.activeLoaded == 1)
			return image.place_image();
		
		// Image is loaded
		image.activeLoaded = 1;
		
		image.title.innerHTML = image.activeTitle;
		
		// Save old height
		image.activeImg.oldWidth = image.activeImg.clientWidth;
		image.activeImg.oldHeight = image.activeImg.clientHeight;
		
		// Resize image
		image.place_image();
		
		// Now make it visible
		image.placehold.style.visibility = "visible";
	}
	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";
		image.container.style.top = "0px";
		image.container.style.left = "0px";
		
		if(image.activeLoaded)
			image.container.removeChild(image.contains);
			
		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;
		
		overflowHeight = (height - 100) / image.activeImg.oldHeight;
		overflowWidth  = (width - 50) / image.activeImg.oldWidth;
		
		procent = overflowHeight < overflowWidth ? overflowHeight : overflowWidth;
		
		imwidth = image.activeImg.oldWidth;
		imheight = image.activeImg.oldHeight;
		
		if(overflowHeight < 1)
		{
			imheight = Math.round(imheight * overflowHeight);
			image.container.style.top = "25px";
			image.container.style.marginTop = "0px";
		}
		else
		{
			image.container.style.top = ((height - imheight) / 2) + "px";
		}
		
		if(overflowWidth < 1)
		{
			imwidth = Math.round(imwidth * overflowWidth);
			image.container.style.left = "15px";
		}
		else
		{
			image.container.style.left = ((width - imwidth) / 2) + "px";
		}
		
		image.contains.style.height = imheight + "px";
		image.contains.style.width = imwidth + "px";
		
		if(procent < 1)
		{
			image.contains.style.overflow = "scroll";
		}
		else
		{
			image.contains.style.overflow = "visible";
		}
		
		if(image.activeImg.oldWidth != image.contains.clientWidth && overflowHeight < 1 && overflowWidth >= 1)
			image.contains.style.width = image.activeImg.oldWidth + (image.activeImg.oldWidth - image.contains.clientWidth) + "px";
		

	}
	
	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);
	}
}
