﻿function popWindow(windowUrl, sWndName, sWidth, sHeight, sResizable) {
	var param = 'menubar=0, toolbar=0,status=0,scrollbars=' +sResizable+ ',resizable=' +sResizable+ ',width=' + sWidth.toString() + ',height='+ sHeight.toString();
	var newWnd = window.open(windowUrl,sWndName,param);
	newWnd.window.moveTo((screen.width/2)-(parseInt(sWidth)/2), (screen.height/2)-(parseInt(sHeight)/2));
	newWnd.focus();
}

$(document).ready(function() {

	$("#menu-bar li").hover(function() {
		var $offsetTopBar = $("#top-bar").offset();
		var $offsetLI     = $(this).offset();
		var newLeft       = $offsetTopBar.left - $offsetLI.left;
		var $itemOver     = $(this);

		$(this).data('timeoutShowSousMenu', setTimeout(function() {
			$(".sousMenu", $itemOver).css("left", newLeft + "px").show();
		}, 75));
	}, function() {
		var $itemOver = $(this);
		clearTimeout( $itemOver.data('timeoutShowSousMenu') );
		$("#menu-bar .sousMenu").hide();
	});

	$("#menu-bar li > a").click(function() {
		$.ajax(BaseUrl + 'preference/dernierMenu.ajax.php', {
			async: false,
			data: { id: $(this).attr('id') }
		});
	});
	$("#menu-bar li div a").click(function() {
		$li = $(this).closest('li');
		menuId = $("a:first", $li).attr('id');
		$.ajax(BaseUrl + 'preference/dernierMenu.ajax.php', { async: false, data: { id: menuId }});
	});

	if ($.browser.mozilla)
	{
		$("input.bouton").css('padding-top', '4px').css('padding-bottom', '4px');
	}

	$("img.btnFavori").live('click', function() {
		var idProduit = parseInt($(this).attr('id').replace('favori_', ''));
		var action    = $(this).attr('src').indexOf('favorite-on') == -1 ? 'ajouter' : 'enlever';
		var $img      = $(this);

		$.get(BaseUrl + 'preference/addRemoveFavori.ajax.php', { produit: idProduit, action: action }, function (data) {
			if (data.substr(0, 2) != "OK")
			{
				alert("Something went wrong, please try again.\n\nIl y a eu un problème, veuillez recommencer.");
				window.location = window.location;
				return false;
			}

			if ($img.attr('src').indexOf('favorite-on') == -1)
			{
				var newImg = $img.attr('src').replace('favorite-off', 'favorite-on');
			}
			else
			{
				var newImg = $img.attr('src').replace('favorite-on', 'favorite-off');
			}

			newTitle = data.substr(2);
			$img.attr('src', newImg).attr('title', newTitle);

		});
	}).css('cursor', 'pointer');

	$("#menu-bar-recherche input[name=keyword]").click(function() {
		if ($(this).data("clicked") == undefined)
		{
			$(this).val("").css("color", "black");
		}
		$(this).data("clicked", true);
	});

	$("#menu-bar .sousMenu img.tag").click(function() {
		var newImg;
		var action;
		var $img = $(this);

		var url    = $img.closest('a').attr('href');
		var texte  = $(this).attr('data-texte');
		var table  = $(this).attr('data-table');
		var colNom = $(this).attr('data-colNom');
		var colId  = $(this).attr('data-colId');
		var id     = $(this).attr('data-id');

		if ( $img.attr('src').indexOf('add') != -1 )
		{
			newImg = $img.attr('src').replace('add', 'del');
			action = 'ajouter';
		}
		else
		{
			newImg = $img.attr('src').replace('del', 'add');
			action = 'enlever';
		}

		var args = { action: action, url: url, texte: texte, table: table, colNom: colNom, colId: colId, id: id };

		$.getJSON(BaseUrl + 'preference/addRemoveRaccourci.ajax.php', args, function(data) {
			if (data.response != "OK")
			{
				alert("Something went wrong, please try again.\n\nIl y a eu un problème, veuillez recommencer.");
				window.location = window.location;
				return false;
			}

			$img.attr('src', newImg).attr('title', data.imgTitle);
			$("#raccourcis").html( data.htmlRaccourcis );

		});
		return false;
	});

	if ($("#replace-left-sidebar").length == 1)
	{
		var $contenu = $("#contenu");
		var $chemin = $(".Chemin").detach();
		var $leftSidebar = $("#replace-left-sidebar").detach();
		$("script", $contenu).remove(); // sinon le javascript est évalué 2 fois

		var table = '<table cellpadding="0" cellspacing="0" border="0">'
			+ '<tr><td width="179" valign="top" id="totototo"></td>'
			+ '<td valign="top" width="761" id="contenu2">'
			+ $contenu.html()
			+ '</td></tr></table>';

		$contenu.html( table );
		$leftSidebar.appendTo("#totototo");
		$chemin.prependTo("#contenu");
	}

	$("form.overrideDefaultSubmit input, form.overrideDefaultSubmit select").live("keypress", function (e) {
		if ((e.which && e.which == 13) || (e.keyCode && e.keyCode == 13))
		{
			var $form = $(this).closest("form");
			$("input[type=submit].default", $form).click();
			return false;
		}
		else
		{
			return true;
		}
	});
});

function initTableDnD(selector, url)
{
	$(selector).tableDnD({
		dragHandle: "dragHandle",
		onDrop: function(table, row) {
			$.get(url, $.tableDnD.serialize(), function(data) {
				if (data != "OK")
				{
					alert("Something went wrong, please try again.\n\nIl y a eu un problème, veuillez recommencer.");
					window.location = window.location;
					return false;
				}
			});
		}
	});
}
