var menuDest = 0;
var submenuDest = 0;
var menuWorker = null;
var submenuWorker = null;
var submenuAlpha = null;

function menuClick(index, url) { menuCoreClick(index, url, "content"); }
function menuCoreClick(index, url, cont)
{
	if (menuWorker!=null)
	{
		clearInterval(menuWorker);
		menuWorker = null;
	}
	menuDest = index*31;
	menuWorker = setInterval("moveDiv(\"menu\", false)", 1);
	if (submenuAlpha!=null)
	{
		clearInterval(submenuAlpha);
		submenuAlpha = null;
	}
	submenuAlpha = setInterval("hideSub("+index+")", 1);
	document.getElementById("submenucont"+index).style.display = 'block';
	load("mods/"+url, cont);
}

function submenuClick(index, id, url) { submenuCoreClick(index, id, url, "content"); }
function submenuCoreClick(index, id, url, cont)
{
	if (submenuWorker!=null)
	{
		clearInterval(submenuWorker);
		submenuWorker = null;
	}
	submenuDest = index*31;
	submenuWorker = setInterval("moveDiv(\"submenu"+id+"\", true)", 1);
	load("mods/"+url, cont);
}

function moveDiv(id, sub)
{
	var elem = document.getElementById(id)
	var bott = getBottom(elem);
	if (!sub&&bott<menuDest)
	{
		elem.style.bottom = (bott+1)+"px";
	}
	else if (!sub&&bott>menuDest)
	{
		elem.style.bottom = (bott-1)+"px";
	}
	else if (sub&&bott<submenuDest)
	{
		elem.style.bottom = (bott+1)+"px";
	}
	else if (sub&&bott>submenuDest)
	{
		elem.style.bottom = (bott-1)+"px";
	}
	else
	{
		if (sub)
		{
			clearInterval(submenuWorker);
			submenuWorker = null;
		}
		else
		{
			clearInterval(menuWorker);
			menuWorker = null;
		}
	}
}

function hideSub(index)
{
	var touched = false;
	for (i=0;i<4;i++)
	{
		var elem = document.getElementById("submenucont"+i);
		var opac = getOpacity(elem);
		if (opac>0.0)
		{
			touched = true;
			elem.style.opacity = Math.max(opac-0.02, 0.0);
			elem.style.filter = "alpha(opacity="+Math.max(opac-0.025, 0.0)*100+")";
		}
		else
		{
			elem.style.display = 'none';
			document.getElementById("submenu"+i).style.bottom = "0px";
		}
	}
	if (!touched)
	{
		clearInterval(submenuAlpha);
		submenuAlpha = setInterval("showSub("+index+")", 1);
	}
}

function showSub(index)
{
	var elem = document.getElementById("submenucont"+index);
	elem.style.display = 'block';
	var opac = getOpacity(elem);
	if (opac<1.0)
	{
		elem.style.opacity = Math.min(opac+0.02, 1.0);
		elem.style.filter = "alpha(opacity="+Math.min(opac+0.025, 1.0)*100+")";
	}	
	else
	{
		clearInterval(submenuAlpha);
		submenuAlpha = null;
	}
}

var curr = null;
var imgLoader = null;
var images = new Array(
	"img/top_gradient.png",
	"img/bar_bottom.png",
	"img/bar_mid.png",
	"img/bar_top.png",
	"img/content.png",
	"img/content_bottom.png",
	"img/content_top.png",
	"img/logo.png",
	"img/menu_bar.png",
	
	"img/menu_profile.png",
	"img/menu_contact.png",
	"img/menu_down.png",
	"img/menu_works.png",
	"img/submenu_0_about.png",
	"img/submenu_0_latest.png",
	"img/submenu_0_services.png",
	"img/submenu_0_team.png",
	"img/submenu_1_id.png",
	"img/submenu_1_logos.png",
	"img/submenu_1_misc.png",
	"img/submenu_1_prints.png",
	"img/submenu_1_web.png",
	"img/submenu_2_misc.png",
	"img/submenu_2_wall.png",
	"img/submenu_3_form.png",
	"img/submenu_3_numbers.png",
	"img/loading.gif"
);
var imgIndex = 0;

function preload()
{	
	if (imgIndex==images.length)
	{
		clearInterval(imgLoader);
		imgLoader = null;
		fixPng();
		document.getElementById('preloader').style.display = 'none';
		document.getElementById('all').style.display = 'block';
		menuCoreClick(0, 'auto/works.latest.html?', null);
		return;
	}
	else
	{
		document.getElementById('preloader').style.display = 'block';
		if (curr==null||curr.complete)
		{
			if (curr!=null)
			{
				imgIndex++;
				document.getElementById('pre_done').style.width = (1+parseInt((imgIndex/images.length)*100))+'px';
			}
			try
			{
				curr = new Image();
				curr.src = images[imgIndex];
			}
			catch (e) {}
		}
		if (imgLoader==null)
			imgLoader = setInterval(preload, 50);
	}
}

var loadIdQueue;
var loadSrcQueue;
var loadImages;
var loadWorker = null;
var putContent;

function initLoading()
{
	if (loadWorker != null)
	{
		clearInterval(loadWorker);
		loadWorker = null;
	}
	loadIdQueue = new Array();
	loadSrcQueue = new Array();
	loadImages = new Array();
	putContent = '';
}

function add2Load(id, img, w, h)
{
	var imgz = new Image();
	imgz.src = img;
	
	if (!imgz.complete)
	{
		loadIdQueue.push(id);
		loadSrcQueue.push(img);
		loadImages.push(imgz);
		putContent += '<div class="workLoading" id="work_id'+id+'" style="width: '+w+'px; height: '+h+'px;"></div>';
	}
	else
	{
		putContent += '<div class="work" id="work_id'+id+'" style="width: '+w+'px; height: '+h+'px;"><img src="'+img+'" /></div>';
	}
}

function runLoader()
{
	var cont = document.getElementById('content');
	cont.innerHTML = putContent;
	loadWorker = setInterval(doLoad, 100);
}

function doLoad()
{
	if (loadIdQueue.length==0)
	{
		clearInterval(loadWorker);
		loadWorker = null;
	}
	for (i in loadImages)
	{
		if (loadImages[i]!=null&&loadImages[i].complete)
		{
			try
			{
				var id = loadIdQueue.splice(i, 1);
				var src = loadSrcQueue.splice(i, 1);
				loadImages.splice(i, 1);
				var e = document.getElementById('work_id'+id);
				e.className = 'work';
				e.innerHTML = '<img src="'+src+'" />';
			}
			catch (exc) { }
			break;
		}
	}
}

function sendMsg()
{
	try
	{
		var n = document.getElementById('f_name').value;
		var e = document.getElementById('f_email').value;
		var t = document.getElementById('f_topic').value;
		var m = document.getElementById('f_msg').value;
		document.getElementById('sending').style.display = 'block';
		load("mods/sendMsg.php?name="+escape(n)+"&email="+escape(e)+"&topic="+escape(t)+"&msg="+escape(m), null);
	}
	catch (exc) { }
}

function okMsg()
{
	try
	{
		document.getElementById('sending').style.display = 'none';
		alert('Message has been sent. Thank you.');
		document.getElementById('f_name').value = '';
		document.getElementById('f_email').value = '';
		document.getElementById('f_topic').value = '';
		document.getElementById('f_msg').value = '';
	}
	catch (exc) { }
}

function errMsg()
{
	try
	{
		document.getElementById('sending').style.display = 'none';
		alert('Server error. Please try again or mail me at info@insanefacilities.com');
	}
	catch (exc) { }
}

function getBottom(elem)
{
	try
	{
		return parseInt(document.defaultView.getComputedStyle(elem,"").getPropertyValue("bottom").replace("px", ""));
	}
	catch (exc)
	{
		return parseInt(elem.currentStyle.bottom.replace("px", ""));
	}
	return 0;
}

function getOpacity(elem)
{
	try
	{
		return parseFloat(document.defaultView.getComputedStyle(elem,"").getPropertyValue("opacity"));
	}
	catch (exc)
	{
		return parseFloat(elem.currentStyle.filter.replace("alpha(opacity=", "").replace(")", ""))/100;
	}
	return 0.0;
}

function fixPng()
{
	try
	{
		if (navigator.appName!="Microsoft Internet Explorer")
			return;
		var items = document.getElementsByTagName('div');
		for (i in items)
		{
			var poElement = items[i];
			if (poElement.className!='menuitem'&&poElement.className!='menutop'&&poElement.className!='menubottom'&&poElement.className!='menuitemtext')
				continue;
			var cImage = poElement.currentStyle.backgroundImage.substring(poElement.currentStyle.backgroundImage.indexOf('"') + 1, poElement.currentStyle.backgroundImage.lastIndexOf('"'));
			poElement.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + cImage + "', sizingMethod='scale')";
			poElement.style.backgroundImage = "none";
		}
	}
	catch (e) { }
}
