var resetTopMS = 0;
var resetEnabled = 1;
var currentTop = 'default';
var currentAnchor = '_none';


function getAnchor()
{
	var anchor='home';
	try
	{
		anchor = $(window.location).attr("hash").substring(1);
	}
	catch(e)
	{
		/* couldn' get anchor */
	}		
	
	return anchor;
}


$().ready(function()
{
	// load first page
	//currentAnchor = getAnchor();
	//$("#content").load('bridge?p='+currentAnchor);
	loadContents();
	menu_fade_hover(currentAnchor);
	
	// bind handlers
	$('.menu_link_active').click(function(e){
		setTimeout(function(){loadContents();}, 50);
	});		
});

function loadContents()
{
	var anchor = getAnchor();
	if(anchor != currentAnchor)
	{
		menu_fade_dehover(currentAnchor);
		$.ajax({
			url : 'bridge?p='+anchor,
			success : function(data){
				$("#content").stop(true, true).fadeOut(350, function (){			
					setTimeout(function(){$("#content").html(data).fadeIn(600, function (){});}, 100);			
				});
			}
		});
		currentAnchor = anchor;		
		menu_fade_hover(currentAnchor);
	}
}

function menu_fade_hover(id)
{
	$("#menu_frame_"+id).animate(
	{
		marginLeft: -172
	}, 180);
}
function menu_fade_dehover(id)
{
	$("#menu_frame_"+id).animate(
	{
		marginLeft: 0
	}, 180);
}
function topcontent_fade(nr, id)
{						
	if(currentTop == id)
		return;
	if(document.getElementById("topcontent_"+currentTop))
	{
		$("#topcontent_"+currentTop).stop().fadeTo(500,0.01,
			function(){ 
				$(this).css('opacity', 1.0); 
			}
		);
	}
	if(document.getElementById("topcontent_img_"+id))
	{
		// set the image uri
		if($("#topcontent_img_"+id).attr('src')	== '')
		{
			$("#topcontent_img_"+id).fadeOut(0).attr('src', 'img/topcontent/'+id+'.png').bind("load", function(){ $(this).fadeIn(300)});;
		}
	}	
	currentTop = id;
	if(document.getElementById("topcontent_"+id))
	{
		$("#topcontent_"+id).stop().css('opacity', 0.01).fadeTo(500,1);	
	}
	
	$("#topcontent_slider").stop().animate(
	{
		marginLeft: -1*nr*880
	}, 500);
}
function menu_bind_mouse(id, nr, img)
{	
	$("#menu_frame_"+id).bind({
		mouseenter: function(event)
		{
			resetEnabled = 0;
			topcontent_fade(nr, id);
		},
		mouseleave: function(event)
		{
			topcontent_fade(nr, id);
		
			
			resetTopMS = new Date().getTime();
			resetEnabled = 1;
			setTimeout(function(){
				resetTop()
			}, 5000);
		}
	});
	$("#menu_frame_"+id).bind({
		mouseenter: function(event)
		{
			menu_fade_hover(id);
		},
		mouseleave: function(event)
		{
			if (currentAnchor == null || id != currentAnchor)
				menu_fade_dehover(id);
		}
	});
	$("#topcontent_"+id).bind({
		mouseenter: function(event)
		{
			resetEnabled = 0;
		},
		mouseleave: function(event)
		{
			resetTopMS = new Date().getTime();
			resetEnabled = 1;
			setTimeout(function(){
				resetTop()
			}, 5000);
		}
	});
}
function resetTop()
{
	if(resetEnabled == 0)
		return;
	
	if(resetTopMS+4800 < new Date().getTime())
	{
		topcontent_fade(0, 'default');
	}
}
$(function(){
	menu_bind_mouse('administration', 1);
	menu_bind_mouse('engineering', 2);
	menu_bind_mouse('hosting', 3);
	menu_bind_mouse('webdevelopment', 4);
});
