var last_menu_height = 70;
var lastUrl = '';
$(function()
{    
    $("body").hide().delay(1500).fadeIn();
    $("#leftbar").click(function(e)
    {
        $("#leftbar").animate({"backgroundColor":"#101821"}, 500);
    });
    
    //$(".background").height($(window).height() - 76);
    
    displaceMenu(70, false);
    
    $("#leftbar").css("opacity", 0.75);
    $("#leftbar-content a").hover(function()
    {
        $("#leftbar-content a.active").removeClass('active')
	$(this).addClass('active');
	if(lastUrl == './home.php')
	{
		$onCategorie = $(this);
	}
    });

    $("#leftbar-content a").click(function()
    {
	$onCategorie = $(this);
    });


    $("#leftbar-content ul").mouseleave(function()
    {
        $("#leftbar-content a.active").removeClass('active')
        $onCategorie.addClass('active');
    });
    
    $("#menu a").click(function(e)
    {
        e.preventDefault();
        
        loadContent($(this).attr('href'));
    });
    
    $("#backContainer").load(resize_background);
    $(window).resize(resize_background);
    setTimeout(loadHome, 450); 
});

function loadHome()
{
	loadContent('./home.php');
	$onCategorie = $("#leftbar-content a.active");
	$(".background").fadeIn(100);
}

var currentLoad = '';
function load(url,force)
{
	currentLoad = url;
	if(currentLoad == url || force == true)
	{
		loadContent(url);
	}
	lastLoad = url;
}

function resize_background()
{
    var $b = $("#backContainer");
    
    $b.css("height","100%");
    $b.css("height",($b.height()-76));
/*   
    var bgH = $b.height();
    var bgW = $b.width();
    
    var wH= $(window).height() - 76;
    var wW= $(window).width();
    
    var pi = bgW / bgH;
    var pe = wW / wH;
    
    if (pi > pe)
    {
        $b.height(wH);
    }
    else
    {
        $b.width(wW);
    }*/
    displaceMenu(last_menu_height);
}

function hideBar(callback)
{
    var $bar = $("#leftbar, #leftbar-content");
    $bar.animate({left:-$bar.width()}, 400);
    if (callback != undefined)
        setTimeout(callback, 400);    
}

function showBar(callback)
{
    $("#leftbar, #leftbar-content").animate({left:0}, 400);
    if (callback != undefined)
        setTimeout(callback, 400);    
}

function openMenu(callback)
{
    if(lastUrl != './home.php' && lastUrl != 'paints.php')
    {
	    var displacement = $(window).height() - 580;
	    
	    displacement = Math.max(70, Math.min(220, displacement));
	    displaceMenu(displacement, true, callback);
    }
    else
    {
        if (callback != undefined)
            setTimeout(callback, 400);
    }
}

function closeMenu()
{
    displaceMenu(70);
}

function displaceMenu(h, animation, callback)
{
    if (animation == undefined)
        animation = true;
    
    last_menu_height = h;
    
    var borderVal = $("#menu").outerHeight() - $("#menu").height();
    var mainSiteH = $(window).height() - (h + borderVal);
    
    if (animation)
    {
        $("#menu, .push").animate({height:h}, {duration:400, queue:false});
        $(".wrapper").animate({"margin-bottom": -h - borderVal}, {duration:400, queue:false});
        $(".wrapper .content").animate({"bottom":h + borderVal}, {duration:400, queue:false});
        $("#leftbar, #leftbar-content").animate({height:mainSiteH}, {duration:400, queue:false});
        if (callback != undefined)
            setTimeout(callback, 400);
    }
    else
    {
        $("#menu, .push").height(h);
        $(".wrapper").css({"margin-bottom": -h - borderVal});
        $(".wrapper .content").css("bottom", h + borderVal);
        $("#leftbar, #leftbar-content").height(mainSiteH);
    }
}

var debug_data;
function loadContent(url)
{
    if(url != lastUrl)
    {
	    lastUrl = url;
	    $(".wrapper .content").fadeOut(200);
	    $(".background").hide();
	    openMenu(function()
	    {
		$.get(url, function(data)
		{
		    $(".dynamic-loaded").remove(); 
		    var $data = $(data);
		    debug_data = $data;
		    if ($data.filter('div[rel^="part-"]').length == 0)
		        return;
		    
		    var next = function()
		    {
		        if ($data.filter('div[rel="part-head"]').length > 0)
		        {
		            $data.filter('div[rel="part-head"]').children("link").addClass("dynamic-loaded");
		            $data.filter('div[rel="part-head"]').children("link").appendTo('head');
		        }
		        if ($data.filter('div[rel="part-content"]').length > 0)
		        {
			    $(".wrapper .content").html('');
		            $data.filter('div[rel="part-content"]').children("*").appendTo('.wrapper .content');
			    $(".wrapper .content").hide().delay(250).fadeIn(200);
		        }
		        $data.filter('div[rel="part-head"]').children("jq").each(function ()
		        {
		            $.get($(this).attr('rel')); //Load & parse JS
		        });
		    }
		    
		    if ($data.filter('div[rel="part-leftbar"]').length == 0)
		        hideBar(next);
		    else
		    {
		        $("#leftbar").animate({"backgroundColor":$data.filter('div[rel="part-leftbar"]').css("background")}, 500);
			$("leftbar-content").remove(".dynamic-loaded");
			$data.filter('div[rel="part-leftbar"]').children("*").addClass("dynamic-loaded");
			$data.filter('div[rel="part-leftbar"]').children("*").appendTo('#leftbar-content');
			$(".logopaint").hide().delay(400).fadeIn(10);
			showBar(next);
		    }
		});
	    });
    }
}
function changeBackground(id)
{
	$("#backContainer").append('<img src="./imgs/cat_'+id+'.jpg" class="background" style="position:absolute "id="back'+id+'" />');
	$("#back"+id).hide().fadeIn(300);
	var t = setTimeout ( "$('#backContainer img').first().remove()", 1000 ); 
}

function changeMenu(color)
{
	$("#leftbar").animate({"backgroundColor":color}, 500);
}

function initialize() {
	var latlng = new google.maps.LatLng(48.879084, 2.350584);
	var myOptions = {
		zoom: 16,
		center: latlng,
		mapTypeId: google.maps.MapTypeId.ROADMAP
	};

	var map = new google.maps.Map(document.getElementById("map_canvas"),
	myOptions);

	var marker = new google.maps.Marker({
		position: latlng, 
		map: map,
		title:"Fifax"
	});
}

