$(function() {
	//Account Menu
	InitAccountMenu();
	
	$("ul.Navigation li").each(
		function(index) {
			$(this).data('index', index + 1);
			$(this).mouseenter(function() { openNavPanel($(this).data('index')); }).mouseleave(function() { closeNavPanel($(this).data('index')); });
		}
	);
	
	$("div.DepartmentMenuPanel").each(
		function(index) {
			$(this).data('index', index + 1);
			//$(this).css("background-image", "url(/images/MainMenu/NavPanel_0" + $(this).data('index') +".gif)");
			$(this).mouseenter(function() { openNavPanel($(this).data('index')); }).mouseleave(function() { closeNavPanel($(this).data('index')); });
		}
	);
	
	$.ajaxSetup({ cache: false });
	$.getJSON('/Lab/json.php?department=42', function(data) {
		$('#NavPanel1 .AdsPanel').html(createPromoBox(data)).click(function() { Go(data.url); });
	});
	$.getJSON('/Lab/json.php?department=44', function(data) {
		$('#NavPanel2 .AdsPanel').html(createPromoBox(data)).click(function() { Go(data.url); });
	});
	$.getJSON('/Lab/json.php?department=43', function(data) {
		$('#NavPanel3 .AdsPanel').html(createPromoBox(data)).click(function() { Go(data.url); });
	});
	$.getJSON('/Lab/json.php?department=50', function(data) {
		$('#NavPanel4 .AdsPanel').html(createPromoBox(data)).click(function() { Go(data.url); });
	});
	$.getJSON('/Lab/json.php?department=2', function(data) {
		$('#NavPanel5 .AdsPanel').html(createPromoBox(data)).click(function() { Go(data.url); });
	});
	$.getJSON('/Lab/json.php?department=271', function(data) {
		$('#NavPanel6 .AdsPanel').html(createPromoBox(data)).click(function() { Go(data.url); });
	});
	$.getJSON('/Lab/json.php?department=48', function(data) {
		$('#NavPanel7 .AdsPanel').html(createPromoBox(data)).click(function() { Go(data.url); });
	});
	$.getJSON('/Lab/json.php?department=45', function(data) {
		$('#NavPanel8 .AdsPanel').html(createPromoBox(data)).click(function() { Go(data.url); });
	});
});

function createPromoBox(product)
{
	var html = "";
	html += '<img src="' + product.picture + '" />';
	html += '<a href="' + product.url + '">';
	html += '<span class="Brand">' + product.brand + '</span><br />';
	html += '<span class="Title">' + product.category + " " + product.name + '</span>';
	html += '</a><br />';
	html += '<span class="Price">' + product.price + '&euro;</span>';
	
	return html;
}

function openNavPanel(id)
{
	var ctx = $("#MainMenu");
	$("#NavPanel" + id, ctx).show();
	$("#Nav" + id, ctx).css("background-image", "url(/images/MainMenu/Nav_0" + id +".gif)");
}

function closeNavPanel(id)
{
	var ctx = $("#MainMenu");
	$("#NavPanel" + id, ctx).hide();
	$("#Nav" + id, ctx).css("background-image", "url(/images/MainMenu/Nav_00.gif)");
}

function Go(url) {
    window.location.href = url;
}

function InitAccountMenu() {
	$('ul.menuCompte ul').hide();
	
	$('ul.menuCompte li').mouseenter(
		function() {
			var checkElement = $("ul", this);
			checkElement.slideDown(500);
		}
	).mouseleave(
		function() {
			var checkElement = $("ul", this);
			checkElement.slideUp(200);
		}
	);
}
