﻿function initSlideShow(header, content, byIndex, accordion, firstShowed)
{
    if(accordion)
    {
        $(content +":visible").each(function(i, item)
        {
            $(item).css("display","none");
        });
    }
    else if(firstShowed)
    {
        var firstDo = false;
        $(content +":visible").each(function(i, item)
        {
            if(firstDo)
                $(item).css("display","none");
            else
            {
                firstDo = true;
                $(item).parent().find("p.1 img").addClass("open").attr('src', "/Content/Images/Common/Various/fleche-violette-haut-fond-gris.gif");
            }
        });
    }

    $(header).click(function() 
    {
        
        var imgFleche = $(this).find("img");
        if ( imgFleche.length > 0)
        {
            if (imgFleche.hasClass("open"))
            {
                imgFleche.attr('src', "/Content/Images/Common/Various/fleche-violette-bas-fond-gris.gif");
                imgFleche.removeClass("open");
            }
            else
            {
                imgFleche.attr('src', "/Content/Images/Common/Various/fleche-violette-haut-fond-gris.gif");
                imgFleche.addClass("open");
            }
       }
        
        var container;
        if(byIndex)
        {
            var classTab = $(this).attr("class").split(" ");
            var itemIndex = classTab[classTab.length-1];
            container = $(this).parent().find(content + "." + itemIndex);
        }
        else
            container = $(this).parent().parent().parent().find(content);

        if (container.is(":visible"))
            container.slideUp();
        else
        {
            if(accordion)
                $(content +":visible").slideUp();

            container.show("slow");
        }

        return false;
    });
}

function colorSelector(conteneur)
{
    var listColor = $(conteneur);
    listColor.find("label").css("display","none");

    listColor.find(".cbColor").each(function(i, item)
    {
        var currentItem = $(item);

        var value = currentItem.val();
        var checked = currentItem.attr("checked");
        var srcChecked = "/Content/Images/Common/ColorSelector/choix-couleur-" + value + "-checked.gif";
        var srcNotChecked = "/Content/Images/Common/ColorSelector/choix-couleur-" + value + ".gif";
        
        currentItem.css("display", "none");
        var src = checked ? srcChecked : srcNotChecked;
        var img = $('<img alt="'+ value + '" src="' + src + '" />');
        
        if (!currentItem.attr("disabled"))
        {
            img.click(function()
            {
                checked = !checked;
                currentItem.click();
                currentItem.change();
                this.src = checked ? srcChecked : srcNotChecked; 
            });
        }
        currentItem.before(img);
	});
}


/* Modifie les checkbox pour un selector donné */
function initCustomCheckBox(selector)
{
    $(selector).each(function(i, item)
    {
        item = $(item);

        item.checkize(
        {
            checked:"/Content/Images/Forms/Common/checkbox-checked.png",
            unchecked:"/Content/Images/Forms/Common/checkbox-not-checked.png"
        });
    });
}

/* Modifie les checkbox pour un selector donné */
function initBigCustomCheckBox(selector)
{
    $(selector).each(function(i, item)
    {
        item = $(item);

        item.checkize(
        {
            checked:"/Content/Images/Forms/Common/checkbox-big-checked.png",
            unchecked:"/Content/Images/Forms/Common/checkbox-big-not-checked.png"
        });
    });
}

/*********************************/

/* Generation d'un menu à onglet */

function generateTabbedMenu(selector, container, groupClass)
{
    var menu = $(selector);
    menu.filter(":first").addClass("actif");
    
    $(container + " " + groupClass).not(":first").each(function(i, item)
    {
        item = $(item);
        item.css("display","none");
    });

    menu.each(function(i, item)
    {
        item = $(item);
        item.click(function()
        {
            var idToShow = $(this).attr("class").split(" ")[1];
            menu.removeClass("actif");
            $(this).addClass("actif");

            $(container + " " + groupClass).filter(":visible").css("display","none");
            $(container + " " + groupClass + "."+idToShow).show();

            return false;
        }).focus(function() {
            $(this + "a").blur();
        });
    });
}

/*********************************/

/************** DEBUT POP UP **************/

function initPopUp()
{
    $("body form").before("<div id='PopUp'></div>");
    var popUp = $("#PopUp");

    if($.browser.msie && $.browser.version < 7)
    {
        popUp.dialog({
            autoOpen: false,
            bgiframe: true,
			modal: true,
			overlay: {
				backgroundColor: "#fff",
				opacity: 0.7
			}
		});
    }
    else
    {
        popUp.dialog({
            autoOpen: false,
			modal: true,
			overlay: {
				backgroundColor: "#fff",
				opacity: 0.7
			}
		});
    }
}

function showPopUp(url,parameters,ajaxType, callBack)
{
    if($("#PopUp").length == 0)
            initPopUp();
            
    if(ajaxType == "POST")
    {
        $.ajax({ type:ajaxType, url : url, data: parameters, dataType: "html", cache:false,
        success: function(result, message)
        {
            
            if ($("#PopUp").dialog("isOpen"))
                $("#PopUp").dialog("close");
                
            $("#PopUp").empty();
            $("#PopUp").append(result);
            $("#PopUp").dialog("open");
            
            /* Ajoute l'evenement click au bouton pour fermer la popup */
            $(".btFermer").bind("click", btClosePopUP);
            
            /* TEMP supprimer la barre de titre de la popup */
            $(".ui-dialog-titlebar").remove();
            if(callBack != null)
                callBack();
        }});
    }
    else
    {
        alert(url);
        $.ajax( 
        { 
            
            async: false, type : ajaxType, url : url, dataType : "text", 
            success: function(data, message)
            {
               
                $("#PopUp").empty();
                $("#PopUp").append(data);
            }
        });
    }
}

var btClosePopUP = function() {
    $("#PopUp").dialog("close");
    return false;
}


//function ShowLoadingPopup(callback)
//{
//    
//    if($("#PopUp").length == 0)
//        initPopUp();
//    
//    //On retire la barre de titre
//    $(".ui-dialog-titlebar").remove();
//    
//    var newcallback = function()
//    {
//        $("#PopUp").dialog("open");
//    
//        if (callback != null)
//            callback();
//        
//        
//    };
//    
//    $.ajax(
//    {
//            cache:false, 
//            url:"/Content/Templates/PopupLoading.tpl",
//            success: function(html)
//            {
//                //var tempDate = new Date();
//                $("#PopUp").html(html);
//                newcallback();
//            }
//     });
//}

/*************** FIN POP UP ***************/

