var menu = new menu;
var image = new image_popup();

window.onload = function(e)
{
	menu.prepare(e);
	image.onload();
}
window.onscroll = image.scroll;
window.onresize = image.resize;

function menu()
{
	this.current = "";
	this.currentTimer = "";
	
	this.prepare = prepare;
	this.buttonMouseover = buttonMouseover;
	this.menuMouseover = menuMouseover;
	this.mouseout = mouseout;
	this.delayhide = delayhide;
	
	function prepare(e)
	{
		var x = 1;
		while(document.getElementById("menu_item_" + x))
		{
			document.getElementById("menu_item_" + x).thisx = x;
			document.getElementById("menu_item_" + x).onmouseover = menu.buttonMouseover;
			document.getElementById("menu_item_" + x).onmouseout = menu.mouseout;
			if(menut = document.getElementById("menu_sub_" + x))
			{
				menut.thisx = x;
				menut.onmouseover = menu.menuMouseover;
				if(e)
					menut.onmouseout = menu.mouseout;
			}
			x++;
		}
	}
	function buttonMouseover(e)
	{
		var e = !e ? window.event : e;
		
		if(menu.current != this.thisx && menu.current)
		{
			menu.delayhide();
			menu.current = this.thisx;
		}
		// get the total left offset
		var targ = this;
		var offsetLeft = 0;
		while(targ.offsetParent)
		{
			offsetLeft += targ.offsetLeft
			targ = targ.offsetParent;
		}
		
		var menut = document.getElementById("menu_sub_" + this.thisx);
		menut.style.display = "block";
		menut.style.left = (offsetLeft + 15) + "px";
		// Clear timer if exists
		if(menu.currentTimer)
		{
			window.clearTimeout(menu.currentTimer);
			menu.currentTimer = "";
		}
	}
	function menuMouseover(e)
	{
		// Clear timer if exists
		if(menu.currentTimer)
		{
			window.clearTimeout(menu.currentTimer);
			menu.currentTimer = "";
		}
	}
	function mouseout(e)
	{
		// Create a timer to clear the menu
		menu.current = this.thisx;
		menu.currentTimer = window.setTimeout("menu.delayhide()",250);
	}
	function delayhide()
	{
		if(menu.current > 0)
		{
			var menut = document.getElementById("menu_sub_" + menu.current);
			menut.style.display = "none";
		}
	}
}

/* Image popup */


function image_popup()
{
	// Declare functions
	this.onload = image_onload;
	
	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;
	
	// Start with functions
	function image_onload()
	{
		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;
	}
	
	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 images = document.createElement("IMG");
		images.src = src;
		images.onload = this.loaded;
		this.container.appendChild(images);
		
		if(images.clientHeight > 50)
		{
			image.loaded();
		}
		
		this.activeImg = images;
	}
	function image_loaded()
	{
		if(image.activeLoaded == 1)
			return image.place_image();
		
		//alert(image.activeImg.clientWidth);
		// 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";
		
		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 - 70) < this.activeImg.oldHeight || (width - 50) < this.activeImg.oldWidth)
		{
			overflowHeight = (height - 70) / this.activeImg.oldHeight;
			overflowWidth  = (width - 50) / this.activeImg.oldWidth;
			
			procent = overflowHeight < overflowWidth ? overflowHeight : overflowWidth;
			
			if(procent < 1)
			{
				this.activeImg.style.height = Math.round(this.activeImg.oldHeight * procent) + "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";
	}
	
	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);
	}
}
