/*----------------------------------------------------------------------------//
// Javascript pour le site client  -------------------------------------------//
//----------------------------------------------------------------------------//
// Client : Casacubana
//----------------------------------------------------------------------------*/

/*----------------------------------------------------------------------------*/
// Declarations jQuery
/*----------------------------------------------------------------------------*/
$(document).ready(function () {

    $("a.LienBouton").each(function () {
        centrerTexte($(this));
    });

    $("table.TableauListeProduits tr:last td.DescriptionProduit").css("background-image", "none");

    // Appels de fonctions ----------------------------------------------------
    equalHeight($("body.Accueil .BlocPromo div"));
    equalHeight($("#Corps.DeuxColonnes .ColonneGauche, #Corps.DeuxColonnes .ColonneDroite"));
    equalHeight($(".ProduitListe li"), { nbPerRow: 2 });
    equalHeight($(".WrapContenu .ColonneGauche, .WrapContenu .ColonneGauche"));

    // Input Label Focus ------------------------------------------------------
    if ($("input.LabelFocus").length > 0) {
        $("input.LabelFocus").each(function () {

            $(this)
				.focus(function () {
				    if ($(this).val().length == 0 || $(this).val() == $(this).attr("title")) {
				        $(this).val("").css("color", "#3b1603");
				    }
				})
				.blur(function () {
				    if ($(this).val().length == 0 || $(this).val() == $(this).attr("title")) {
				        $(this).val($(this).attr("title")).css("color", "#94918d");
				    }
				});
        });
    }

    /////////////////////////////////////////////////////////////
    // Tabs
    $('div.TabsGroup').each(function () {
        if ($('ul.TabsMenu li', this).length > 1) {
            //$('div.TabsContent', this).hide();
            $('ul.TabsMenu li a')
			.click(function () {
			    var $this = $(this);
			    var $parent = $this.parents('div.TabsGroup');
			    $parent
					.find('div.TabsContent')
						.removeClass("On")
					.end()
					.find('ul.TabsMenu li a')
						.removeClass("On")
				;
			    $($this.attr('rel')).addClass("On");
			    $this.addClass("On");
			});
        }
        else {
            $(this)
				.find('ul.TabsMenu li')
					.css("border", 0);
        }
        if ($(this).find('div.TabsContent.On').length == 0) {
            //Select the first one
            $(this)
				.find('ul.TabsMenu li:first a')
					.addClass('On')
				.end()
				.find('div.TabsContent:first')
					.addClass('On')
			;
        }
    });

    /////////////////////////////////////////////////////////////
    // Forgot Password
    $('a#ForgotPassword').live('click', function () {
        $.colorbox({ width: '300', height: 'auto', inline: true, transition: "none", href: '#ForgotPasswordBox', opacity: '0.7' });
        $('#colorbox').appendTo('form');
    });

    // Unpaid Invoices popup
    $('a.UnpaidInvoices').colorbox({ iframe: true, width: 650, height: 500, transition: "none" });

	// onEnter
	kafe.form.onEnter("input.Login", function(el) { eval($("a.LinkLogin").attr("href")); });
	kafe.form.onEnter("input.ForgotPasswordText", function(el) { eval($("a.LinkSendPassword").attr("href")); });
	kafe.form.onEnter("input.TxtRecherche", function(el) { if($(el).val().length > 0) { eval($("a.BtnRecherche").attr("href")); } });
	kafe.form.onEnter("#RechercheAvancee input", function() { eval($("#RechercheAvancee .Btn").attr("href")); });

    /////////////////////////////////////////////////////////////
    // AutoComplete Select Client
    BindChosenEvent();

    /////////////////////////////////////////////////////////////
    // Image popup
    $(".ZoomImage").colorbox({ opacity: '0.7', transition: "none" });

});
function BindChosenEvent() {
    $("select.SelectClientDropDownList").chosen();
}

/* - Fonction : Equalcols height
   ------------------------------------*/
function equalHeight() {
    function _doIt() {
        var $z = $(arguments[0]);
        $z.height(Math.max.apply(Math, $z.map(function() { return $(this).height(); }).get()));
    }

    var $o = $(arguments[0]);
    var options = arguments[1] || {};
    var nbPerRow = options.nbPerRow;

    if (nbPerRow) {
        var max = Math.ceil($o.length / nbPerRow);
        for (var i = 0; i < max; ++i) {
            _doIt($($o.splice(0, nbPerRow)));
        }
    } else {
        _doIt($o);
    }
}

// Fonction pour centrer le texte d'un bouton éditable -------------------------
function centrerTexte(btn){
	hauteurBouton = ($(btn).height());
	texte = $(btn).children("span");
	hauteurTexteBouton = ($(texte).height());
	difference = Math.round((hauteurBouton - hauteurTexteBouton) / 2);
	$(texte).css("padding-top",difference);	
}


