var dontHide = false;
var busy = false;
var contacted = false;

$(document).ready(function () {
	
	if (foundApp)
	{
		loadApp(foundAppId);
	}
	
	// Get the query
    if ($.address.value() != '') {
		$.ajax({
			type: "GET",
			url: "ajax.php",
			data: "function=getAppId&app="+$.address.value(),
			cache: false,
			dataType: "html",
			success: function(html) {
				if (html == "0")
				{
					clickMenu($("#header_apps_app_"+$.address.value()));
				}
				else {
					clickMenu($("#header_apps_app_"+html));
				}
			}
		});
    }
	
	$("div[class=header_logo]").mouseover(function () {
		var offset = $(this).offset();
		var leftOffset = offset.left + ($(this).width()/2);
		
		var text = "Appified homepage";
		
		showBubble(offset.top-60, leftOffset, text, true);
	});

	$("div[class=header_logo]").mouseout(function () {
		if (!busy)
			hideBubble();
	});
	
	$("div[class=header_logo]").click(function () {
		loadHome();
	});
	
	$("div[class=header_apps_app]").mouseover(function () {
		var menuId = $(this).find('input[name=appId]').val();
		
		if (menuId != currentMenu)
		{
			if (!busy)
			{
				busy = true;
				$(this).animate({ opacity: 1}, 100, function () {
					var offset = $(this).offset();
					var leftOffset = offset.left + ($(this).width()/2);
					
					var text = $(this).find('div').html();
					
					showBubble(offset.top, leftOffset, text, true);
				});
				busy = false;
			}
		}
	});
	
	$("div[class=header_apps_appOver]").mouseover(function () {
		var menuId = $(this).find('input[name=appId]').val();
		
		if (menuId != currentMenu)
		{
			if (!busy)
			{
				$(this).animate({ opacity: 1}, 100, function () {
					var offset = $(this).offset();
					var leftOffset = offset.left + ($(this).width()/2);
					
					var text = $(this).find('div').html();
					
					showBubble(offset.top, leftOffset, text, true);
				});
				busy = false;
			}
		}
	});
	
	$("div[class=header_apps_appOver]").mouseout(function () {
		var menuId = $(this).find('input[name=appId]').val();
		
		if (menuId != currentMenu)
		{
			if (!busy)
			{
				$(this).animate({opacity: 0.7}, 100);
			
				hideBubble();
				busy = false;
			}
		}
	});
	
	$("div[class=header_apps_app]").click(function () {
		clickMenu($(this));
	});
	
	$("div[class=header_apps_appOver]").click(function () {
		clickMenu($(this));
	});
	
	$("div[class=header_apps_app]").mouseout(function () {
		var menuId = $(this).find('input[name=appId]').val();
		
		if (menuId != currentMenu)
		{
			if (!busy)
			{
				$(this).animate({opacity: 0.7}, 100);
			
				hideBubble();
				busy = false;
			}
		}
	});
	
	$("#contactButton").click(function () {
		if (contacted == false)
		{
			contacted = true;
			
			var name	= $("#contact_name").val();
			var email	= $("#contact_email").val(); 
			var subject	= $("#contact_subject").val();
			var message	= $("#contact_message").val().replace(/\n/g,'<br />');
			
			$("#contact_submit").css({"background-image":"none", "cursor":"default"});
			$(this).find('img').attr("width","16");
			$(this).find('img').attr("height","16");
			$(this).find('img').attr("src","web/images/loaderSmall-2.gif");
			
			$.ajax({
				type: "GET",
				url: "ajax.php",
				data: "function=contact&name="+name+"&email="+email+"&subject="+subject+"&message="+message,
				cache: false,
				dataType: "html",
				success: function(html) {
					if (html == "1")
					{
						alert("Thank you!\nYour message has now been send. \n\nWe will reply as soon as possible.")
					}
					else {
						alert("Your message could not be send. \n\nError:"+html);
					}
				}
			});
		}
	});
});

function clickMenu (object)
{
	var menuId = object.find('input[name=appId]').val();
	var urlName = object.find('input[name=urlName]').val();

	object.attr('class', 'header_apps_appOver');
	object.css({ "margin-top":"0px", opacity: 1});
	
	if (currentMenu != null && menuId != currentMenu)
	{
		$('#header_apps_app_'+currentMenu).animate({ "margin-top" : $('#header_apps_app_'+currentMenu).height()+10 }, 100, function (){
			$('#header_apps_app_'+currentMenu).attr('class', 'header_apps_app');
			$('#header_apps_app_'+currentMenu).hide();
			$('#header_apps_app_'+currentMenu).css({ "margin-top" : 0, opacity: 0.7 });
			$('#header_apps_app_'+currentMenu).fadeIn('fast', function () {
				currentMenu = menuId;
				clickMenu (object);
			});
		});
	}
	else {
		object.css({ "margin-top":"0px", opacity: 1});
		currentMenu = menuId;
		
		hideBubble();
		
		loadApp(currentMenu, urlName);
	}
}

function openAppLink (appID)
{
	$.ajax({
		type: "GET",
		url: "ajax.php",
		data: "function=getAppId&app="+appID,
		cache: false,
		dataType: "html",
		success: function(html) {
			if (html == "0")
			{
				clickMenu($("#header_apps_app_"+appID));
			}
			else {
				clickMenu($("#header_apps_app_"+html));
			}
		}
	});
}

function loadApp (appId, name)
{
	$("#mainPage").fadeOut('fast', function () {
		$("#loader").fadeIn('fast');
		
		$.ajax({
			type: "GET",
			url: "ajax.php",
			data: "function=getInfo&app="+appId,
			cache: false,
			dataType: "html",
			success: function(html) {
			
				if (name)
				{
					$.address.value(name);
				}
			
				$("#mainPage").html(html);
				
				$("#loader").fadeOut('fast', function () {
					$("#mainPage").fadeIn('fast');
				});
			}
		});
	});
}

function loadHome ()
{
	$("#mainPage").fadeOut('fast', function () {
		$("#loader").fadeIn('fast');
		
		$('#header_apps_app_'+currentMenu).animate({ "margin-top" : $('#header_apps_app_'+currentMenu).height()+10 }, 100, function (){
			$('#header_apps_app_'+currentMenu).attr('class', 'header_apps_app');
			$('#header_apps_app_'+currentMenu).hide();
			$('#header_apps_app_'+currentMenu).css({ "margin-top" : 0, opacity: 0.7 });
			$('#header_apps_app_'+currentMenu).fadeIn('fast', function () {
				currentMenu = null;
			});
		});
		
		$.ajax({
			type: "GET",
			url: "ajax.php",
			data: "function=getHome",
			cache: false,
			dataType: "html",
			success: function(html) {
				$.address.value('');
			
				$("#mainPage").html(html);
				
				$("#loader").fadeOut('fast', function () {
					$("#mainPage").fadeIn('fast');
				});
			}
		});
	});
}

function showBubble (top, left, text, animate, width)
{
	busy = true;
	dontHide = true;
	
	if (!width)
		width = $("#bubble").width();
	
	left = left - (width/2);
	
	if ($("#bubble").is(":visible"))
	{
		$("#bubble").fadeOut(100, function (){
			if (animate == true)
				top = top - 10;
			
			$("#bubble").css({ "top": top, "left": left});
			$("#bubble_text").html(text);
			
			$("#bubble").css({ "display":""});
			
			if (animate)
				$("#bubble").animate({ "top": top+10 }, 100, function () {
					busy = false;
				});
			
			dontHide = false;
		});
	}
	else {
		if (animate == true)
			top = top - 10;
		
		$("#bubble").css({ "top": top, "left": left});
		$("#bubble_text").html(text);
		
		$("#bubble").css({ "display":""});
		
		if (animate)
			$("#bubble").animate({ "top":top+10}, 100, function () {
				busy = false;
			});
		
		dontHide = false;
	}
}

function hideBubble ()
{
	busy = true;
	if (dontHide == false)
	{
		$("#bubble").fadeOut('fast');
	}
	busy = false;
}
