/*
Simple Menu code for Infected Mushroom HTML site feature
Author: Liran Oz
*/
var menuContainer = 'htmlMenuDiv';
var transparentImage = 'images/menuTrans.gif';
var imgMaskClass = 'htmlImageMask';
var menuLoadingImg = 'images/menuLoading.gif';

//preload loading gif
var menuLoadingPC = new Image();
menuLoadingPC.src = menuLoadingImg;

//ie6 detection from http://mindfultechnology.wordpress.com/2008/02/11/detect-ie6/
var is_ie6 = (
	window.external &&
	typeof window.XMLHttpRequest == "undefined"
);

menuObj.prototype.img;
menuObj.prototype.unhide;
menuObj.prototype.menuContainer = menuContainer;

function menuObj(objName, parentObj, imgUrl, imageMap)
{
	this.img = new Image();	
	this.parentObj = parentObj;
	var objPtr = this;
	var objName = objName;
	var imgUrl = imgUrl;
	var imageMap = imageMap;
	var imgLoaded = false;
	var preLoaded = false;
	var display = false;
	
	this.showImage = function()
	{
		display = true;		

		if (!imgLoaded) {		//return and be called again only after image is loaded, in the meanwhile display loading or something
			this.showPreload();
			return;
		}
		//show image

		var obj = document.getElementById(this.menuContainer);
		if (!obj)
			return;
	
		//show the image as background
		if (!is_ie6)
			obj.style.backgroundImage = 'url(' + imgUrl + ')';
		else {
			obj.style.backgroundImage = '';
			obj.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src=" + imgUrl + ", sizingMethod='scale')"; 
		}
			
		//apply a masking image (thank you IE 6)
		this.clearMaskImage(obj);		//clear any previous mask if any
		var imgMask = document.createElement('img');
		imgMask.setAttribute('className', 'htmlImageMask');
		imgMask.setAttribute('class', 'htmlImageMask');
		imgMask.setAttribute('src', transparentImage);
		//apply imageMap to the image
		imgMask.setAttribute('usemap', '#' + imageMap, 0);
		
		obj.appendChild(imgMask);		
	}
	
	this.showPreload = function()
	{
		var obj = document.getElementById(this.menuContainer);		
		if (obj) {
			this.clearMaskImage(obj);		//will clear any image and disable image map
			obj.style.backgroundImage = 'url(' + menuLoadingImg + ')';
		}	
	}
	
	this.clearMaskImage = function(obj)
	{
		/*var imgs = obj.getElementsByTagName('img');
		for (i=0; i<imgs.length; i++) {
			imgs[i].parentNode.removeChild(imgs[i]);
		}*/
		obj.innerHTML = '';		
	}
	
	this.goBack = function()
	{
		if (!this.parentObj)
			return;
		this.parentObj.showImage();
	}
	
	this.preCache = function()
	{
		this.img.onload = this.unhide;
		this.img.src = imgUrl;
		preLoaded = true;	
	}
	
	this.unhide = function()
	{
		imgLoaded = true;		
		if (display) 
			objPtr.showImage();
	}
	
	//start precaching on object creation
	this.preCache();
}

var rootObj = null;
var infoObj = null;
var mediaObj = null;
var shopObj = null;
var talkObj = null;

function createHTMLMenu()
{
	rootObj = new menuObj('rootObj', null, 'images/htmlMenu/rootObj.png', 'rootObj');
	rootObj.showImage();
	infoObj = new menuObj('infoObj', rootObj, 'images/htmlMenu/infoObj.png', 'infoObj');
	mediaObj = new menuObj('mediaObj', rootObj, 'images/htmlMenu/mediaObj.png', 'mediaObj');
	shopObj = new menuObj('shopObj', rootObj, 'images/htmlMenu/shopObj.png', 'shopObj');
	talkObj = new menuObj('talkObj', rootObj, 'images/htmlMenu/talkObj.png', 'talkObj');
}

