﻿/**************************************************
         Correction des png transparents
**************************************************/         
$(document).ready(function(){ 
        $(document).correctPNG(); 
    }); 

jQuery.fn.correctPNG = function(settings) 
{
    if (isNecessaryToCorrectPNG())
    {       
        for(var i=0; i<document.images.length; i++)
        {   
            var img = document.images[i];
            var correctPNG = true;
        
            var parentImg = img;
            
            while (parentImg.parentNode != null && parentImg.parentNode.id != "MutContent" && correctPNG)
            {                    
                if (parentImg.className.search("NoCorrectPNG") != -1)
                {
                    correctPNG = false;
                }
                
                parentImg = parentImg.parentNode;
            }
            
            if (correctPNG)
            {
                var imgName = img.src.toUpperCase();
               
                if (imgName.substring(imgName.length-3, imgName.length) == "PNG")
                {                
                    var imgID = (img.id) ? "id='" + img.id + "' " : ""
                    var imgClass = (img.className) ? "class='" + img.className + "' " : ""
                    var imgTitle = (img.title) ? "title='" + img.title + "' " : "title='" + img.alt + "' "
                    var imgStyle = "display:inline-block;" + img.style.cssText 
                    if (img.align == "left") imgStyle = "float:left;" + imgStyle
                    if (img.align == "right") imgStyle = "float:right;" + imgStyle
                    if (img.parentElement.href) imgStyle = "cursor:hand;" + imgStyle
                    var strNewHTML = "<span " + imgID + imgClass + imgTitle
                        + " style=\"" + "width:" + img.width + "px; height:" + img.height + "px;" + imgStyle + ";"
                        + "filter:progid:DXImageTransform.Microsoft.AlphaImageLoader"
                        + "(src=\'" + img.src + "\', sizingMethod='image');\"></span>" 
                    img.outerHTML = strNewHTML
                    
                    i = i-1
                }
            }
       }            
    }
}

function Correct_Unique_Png(img)
{
    if (isNecessaryToCorrectPNG())
    {
        var imgID = (img.id) ? "id='" + img.id + "' " : ""
        var imgClass = (img.className) ? "class='" + img.className + "' " : ""
        var imgTitle = (img.title) ? "title='" + img.title + "' " : "title='" + img.alt + "' "
        var imgStyle = "display:inline-block;" + img.style.cssText 
        if (img.align == "left") imgStyle = "float:left;" + imgStyle
        if (img.align == "right") imgStyle = "float:right;" + imgStyle
        if (img.parentElement.href) imgStyle = "cursor:hand;" + imgStyle
        var strNewHTML = "<span " + imgID + imgClass + imgTitle
            + " style=\"" + "width:" + img.width + "px; height:" + img.height + "px;" + imgStyle + ";"
            + "filter:progid:DXImageTransform.Microsoft.AlphaImageLoader"
            + "(src=\'" + img.src + "\', sizingMethod='image');\"></span>" 
        img.outerHTML = strNewHTML
    }
}

function isNecessaryToCorrectPNG()
{
    var X_nameNavigateur = 'FF'; // Par default Firefox
    var X_versionNavigateur = 0; // Par default pas de version
	
	var X_tabNav = navigator.appVersion.split(';');
	
	for(var i = 0; i < X_tabNav.length; i++)
	{
		// Si IE
		if (X_tabNav[i])
		{
		    if(X_tabNav[i].indexOf('MSIE') != -1)
		    {   
    			X_nameNavigateur = 'IE';
    			
    			var X_version = X_tabNav[i].replace('MSIE','');
    			X_versionNavigateur = X_version.replace(' ','');
		    }
		}
	}
	
	if (X_nameNavigateur == 'IE' && parseInt(X_versionNavigateur) < 7) 
	{
	    return true;
	}
	else
	{
	    return false;
	}
}

// Fonction qui centre verticalement tous les éléments portant la classe CSS "ToVerticalAlignByJavascript"
function CenterElementsByJavascript()
{
    $(".ToVerticalAlignByJavascript").each(function ()
    {
        var tailleParent = ($(this).parent()).height();
        var tailleToCenter = ($(this)).height();
        
        var marginTop = (tailleParent / 2) - (tailleToCenter / 2);        
        ($(this)).attr("style", "margin-top:" + marginTop + "px");
    });
}

$(document).ready(function()
{
    CenterElementsByJavascript();
});


/* MENU */
$(document).ready(function(){ 
        $(document).Menu(); 
    }); 

jQuery.fn.Menu = function(settings) 
{
    $("#Menu .Noeud").addClass("HideMenu");

    $($($('#Menu a[href='+location.pathname+']')).parents("li")).removeClass("HideMenu")
    $($($('#Menu a[href='+document.location+']')).parents("li")).removeClass("HideMenu")
    
    $($($('#Menu a[href='+location.pathname+']')).parents("li")).addClass("Active")
    $($($('#Menu a[href='+document.location+']')).parents("li")).addClass("Active")
}

/********************** Switcheur de photos ******************************/
function ChangeSrc(IdImage, Src)
{
    $( "#"+ IdImage).attr("src" , Src);
}
  
function SwitcheurPhoto(photo, picto)
{
    var srcPhoto = $( "#"+ photo).attr( "src" );
    $( "#" + photo).attr( "src" , $( "#"+ picto).attr("src"));
    $( "#"+ picto).attr( "src" , srcPhoto);
}

/********************** Ajout favoris ******************************/
function AddBookmark(url, title)
{
    title = title.replace("\"", "'");

    if (window.sidebar) 
    { 
        // Mozilla Firefox Bookmark
        window.sidebar.addPanel(title, url, "");
    } 
    else if( window.external ) 
    { 
        // IE Favorite
        window.external.AddFavorite( url, title);
    }
    else 
    {   
        // for Safari, Konq etc - browsers who do not support bookmarking scripts (that i could find anyway)
        alert("Désolé, nous ne sommes pas capables d''ajouter un lien vers La Cuisine d''Olivia avec ce navigateur.");
    }
}


/********************** ONGLETS ******************************/
$(document).ready(function()
{
    // On masque tous les contenus  des onglets
    $(".ContenuOnglet").addClass("hide");
    
    // On active le 1er onglet
    var listeOnglets = $(".MenuOnglets li");
    if (listeOnglets.length > 0) { $(listeOnglets[0]).addClass("current"); }
    
    // On affiche le contenu de l'onglet activé
    $("#" + $(".current a").attr("rel")).removeClass("hide");
    
    // On affecte la fonction click aux liens du menu des onglets
    $(".MenuOnglets a").click(function()
        {
            if (!($(this).parent().hasClass("current")))
            {
                $(".current").removeClass("current");
                $(this).parent().addClass("current");
                $(".ContenuOnglet").addClass("hide");
                var contenuAffiche = $(this).attr("rel");
                $("#" + contenuAffiche).removeClass("hide");
            }
        }
    );
}); 


/********************** CATEGORIES ******************************/
$(document).ready(function()
{
    $(".DivToExpand").each(function ()
    {
        if (($(this).children(".Recette").length) > 0)
        {
            ($(this).children(".Recette").addClass("HideRecette"));           
            
            ($(this).children("a.ExpandCategorie").addClass("Plus"));
            
            // Function click sur le lien pour afficher ou masquer les recettes cachées
            ($(this).children("a.ExpandCategorie").click(function()
            {
                if (($(this)).hasClass("Plus"))
                {
                    ($(this).parent().children(".Recette").removeClass("HideRecette"));
                    
                    ($(this)).html("Replier les recettes");
                    ($(this)).attr("title", "Replier les recettes");
                    ($(this)).removeClass("Plus");
                    ($(this)).addClass("Moins");
                }
                else
                {
                    ($(this).parent().children(".Recette").addClass("HideRecette"));
                    
                    ($(this)).html("Voir toutes les recettes");
                    ($(this)).attr("title", "Voir toutes les recettes");
                    ($(this)).removeClass("Moins");
                    ($(this)).addClass("Plus");
                }
            }));
        }
        else
        {
            // aucune recette masquée => on masque le lien "Voir toutes les recettes"
            ($(this).children(".ExpandCategorie").addClass("Hide"));
        }
    });
});

    
