﻿var IMG_MAX_WIDTH = 480;
var IMG_MAX_HEIGHT = 360;

var isDiaporamaHomeOn = 1;
var currentDiaporamaHome = 'vig_0';
var vitesseDiaporamaHome = 5000;
var firstDiaporamaHome = 1;

var vitesseModuleImageLien = 3000;

$(document).ready(function()
{
    $('.snav ul li a').pngFix();
    $('.snav ul li label').pngFix();
    $('.first-box-home').pngFix();
    $('.bt-bloc').pngFix();
    $('.hide').pngFix();
    $('.hidePresse').pngFix();
    $('.small-boxes').pngFix();
    $('.hideSP').pngFix();
    $('.highlight span').wrapAll(document.createElement("p"));
    //Navigation enrichie
    $('.Group').click(function()
    {
        if(!$(this).hasClass("current") && !$(this).hasClass("nogo"))
        {
            $('.Group').removeClass("current");
            $(this).addClass("current");
            
            afficheNav(this, "long");
        }
    });

    $('div#nav .current').each(function()
    {
        afficheNav(this, "short");
    });

    //LightBox
    $(".thickbox").colorbox({
        opacity:0.7,
        title:function()
        {
            return $(this).children("img").attr("alt");
        }
    });
    
    $(".alertBox").colorbox({
        opacity:0.7,
        width:"578px", 
        height:"213px",
        iframe:true
    });
    
    $(".alertBox2").colorbox({
        opacity:0.7,
        width:"578px", 
        height:"285px",
        close:$.trim($("#labelFermer").html()),
        title:function()
        {
            return $(this).children("img").attr("alt");
        },
        iframe:true
    });

    //ToolTip
    var fadeINT = 0;
    if($.browser.msie && parseFloat($.browser.version) < 9)
        fadeINT = 0;
    else
        fadeINT = 200;
        
    $(".showToolTip").tooltip({
        showURL: false,
        track: true,
        fixPNG: true,
        delay: 0,
        opacity: 0.9,
        extraClass: "tooltip",
        fade: fadeINT,
        top: -80,
        left: -119
    });
    initPartageToolTip();
    //$(".tooltip").pngFix();
    
    //Module Image Lien
    initModuleImageLien();
});

function initPartageToolTip()
{
    var fadeINT = 0;
    if($.browser.msie && parseFloat($.browser.version) < 9)
        fadeINT = 0;
    else
        fadeINT = 200;
    $(".partage li a").tooltip({
        showURL: false,
        track: true,
        fixPNG: true,
        delay: 0,
        opacity: 0.9,
        extraClass: "tooltipPartage",
        fade: fadeINT,
        top: -50,
        left: -86
    });
}

function afficheNav(elem, vitesse)
{
    $('.snav').css("display","none");
    $('#s'+$(elem).attr("id")).css("display","block");
    
    if(vitesse != "short")
    {
        var currheight = $('#s'+$(elem).attr("id")).height();
        $('#s'+$(elem).attr("id")).css("height","0px");
        $('#s'+$(elem).attr("id")).animate({
            height:currheight
        },300);
    }
}

function initMediathequeHome()
{
    $('.HomeMediaLien').hover(function()
    {
        $('.HomeMediaLien').removeClass("current");
        $(this).addClass("current");
    },function()
    {
        $(this).removeClass("current");
    });
    $('.HomeMediaLien').click(function()
    {
        var currentID = $(this).attr('id');
        setMediathequeHome(currentID);
    });
    setMediathequeHome('homeVideos')
}

function setMediathequeHome(currentID)
{
    $('.HomeMediaLien').parent().removeClass("active");
    $('#' + currentID).parent().addClass("active");

    $('.media-box .see-more').css("display","none");
    $('#' + currentID + 'More').css("display","inline");

    $('.homeMedia').css("display","none");
    $('#' + currentID + 'Box').css("display","block");
}

function initMediathequeSection(id_type, id_media_start)
{
    var fadeINT = 0;
    if($.browser.msie && parseFloat($.browser.version) < 9)
        fadeINT = 0;
    else
        fadeINT = 200;
    $(".mediatheque_vignette").tooltip({
        showURL: false,
        fixPNG: true,
        showBody: " || ",
        track: true,
        delay: 0,
        opacity: 0.9,
        extraClass: "tooltip",
        fade: fadeINT,
        top: -80,
        left: -119,
        url:"toto"
    });
    
    $(".mediathequeOF").fadeTo(1,0);
    $('.mediatheque_vignette').click(function()
    {
        var currentID = $(this).attr('id');
        setMediathequeSection(currentID, id_type);
    });
    $('.mediatheque_vignette').hover(function()
    {
        $(this).addClass("hover");
    },function()
    {
        $(this).removeClass("hover");
    });
    $("#mediatheque_img").load(function()
    {
        var iWidth = parseInt($("#mediatheque_img").width());
        var iHeight = parseInt($("#mediatheque_img").height());
        var curRapport = iWidth / iHeight;
        var genRapport = IMG_MAX_WIDTH / IMG_MAX_HEIGHT;
        
        if(curRapport > genRapport)
        {
            if(iWidth > IMG_MAX_WIDTH)
            {
                iWidth = IMG_MAX_WIDTH;
                iHeight = iWidth / curRapport;
            }
        }
        else
        {
            if(iHeight > IMG_MAX_HEIGHT)
            {
                iHeight = IMG_MAX_HEIGHT;
                iWidth = iHeight * curRapport;
            }
        }
        $("#mediatheque_img").width(iWidth + "px");
        $("#mediatheque_img").height(iHeight + "px");
        $(".mediathequeOF").height((iHeight + 40) + "px");
        $(".mediathequeOF").fadeTo('fast',1);
    }); 
    $(".thickbox_zoom").colorbox({
        opacity:0.7,
        href:function()
        {
            return $('#mediatheque_img').attr("src");
        },
        title:function()
        {
            return $('#mediatheque_date').html() + " - " + $('#mediatheque_titre').html() + " - " + $('#mediatheque_copyright').html();
        }
    }); 

    if(id_media_start == '0')
        setMediathequeSection($("#ul_cadrephoto li:first .mediatheque_vignette").attr("id"),id_type);
    else
        setMediathequeSection('media_'+id_media_start,id_type);
}

function setMediathequeSection(currentID, id_type)
{
    $('.mediatheque_vignette').removeClass("current");
    $("#" + currentID).addClass("current");
        
    var titre = $.trim($("#" + currentID + "_titre").html());
    var chemin = $.trim($("#" + currentID + "_chemin").html());
    var audioimage = $.trim($("#" + currentID + "_audioimage").html());
    var date = $.trim($("#" + currentID + "_date").html());
    var copyright = $.trim($("#" + currentID + "_copyright").html());
    var texte = $.trim($("#" + currentID + "_texte").html());
    var id_media = $.trim($("#" + currentID + "_id").html());

    $("#mediatheque_titre").html(titre);
    $("#mediatheque_date").html(date);
    $("#mediatheque_copyright").html(copyright);
    $("#mediatheque_texte").html(texte);
    $("#partage_mail").attr("href","frames/envoi_ami.aspx?section=117&type="+id_type+"&id_media="+id_media);
        
    if(id_type == "1")
    {
        if($("#mediatheque_img").attr("src") != chemin)
        {
            $(".mediathequeOF").fadeTo('fast',0,function()
            {
                $("#mediatheque_img").css("width","");
                $("#mediatheque_img").css("height","");
                $("#mediatheque_img").attr("src", chemin);
                $("#mediatheque_img").attr("alt", titre);
            });   
        }
    }
    else if(id_type == "4")
    {
        if(audioimage != "")
        {
            $(".lightbox-zoom").css("display","block");
            $("#mediatheque_audio_img").attr("src", audioimage);
            $("#mediatheque_audio_img").attr("alt", titre);
        }
        else
            $(".lightbox-zoom").css("display","none");
            
        $("#mediatheque_audio").html("<object type=\"application/x-shockwave-flash\" data=\"flash/dewplayer.swf\" width=\"200\" height=\"20\" id=\"dewplayer\" name=\"dewplayer\"><param name=\"bgcolor\" value=\"#FFFFFF\" /><param name=\"wmode\" value=\"transparent\" /><param name=\"movie\" value=\"flash/dewplayer.swf\" /><param name=\"flashvars\" value=\"mp3="+chemin+"&amp;showtime=1\" /></object>");
    }
    else if(id_type == "8")
    {
        var langue = "";
        if($("#mediatheque_video").hasClass("EN"))
            langue = "En";
        $("#mediatheque_video").html("<object type=\"application/x-shockwave-flash\" data=\"flash/video.swf\" width=\"480\" height=\"428\"><param name=\"bgcolor\" value=\"#FFFFFF\" /><param name=\"wmode\" value=\"transparent\" /><param name=\"movie\" value=\"flash/video"+langue+".swf\" /><param name=\"flashvars\" value=\"video=../"+chemin+"\" /></object>");
    }
}

function initDiaporamaHome(newVitesse)
{
    if(!isNaN(newVitesse))
        vitesseDiaporamaHome = parseInt(newVitesse);
        
    $('.HomeVignette').hover(function()
    {
        isDiaporamaHomeOn = 0;
        if(!$(this).hasClass("current"))
        {
            var currentID = $(this).attr("id");
            setDiaporamaHome(currentID);
        }
    },
    function()
    {
        isDiaporamaHomeOn = 1;
    });
    timeDiaporamaHome();
}

function timeDiaporamaHome()
{
    if(isDiaporamaHomeOn == 1)
    {
        var tempID = parseInt(currentDiaporamaHome.replace('vig_',''));
        tempID++;
        if(tempID > 3)
            tempID = 1

        setDiaporamaHome('vig_'+ tempID);
    }
    setTimeout('timeDiaporamaHome()', vitesseDiaporamaHome);
}

function setDiaporamaHome(currentID)
{
    $('.HomeVignette').removeClass("current");
    $("#"+currentID).addClass("current");
    currentDiaporamaHome = currentID;
    var titre = $.trim($("#titre_" + currentID).html());
    var chapeau = $.trim($("#chapeau_" + currentID).html());
    var lien = $.trim($("#lien_" + currentID).html());
    var target = $.trim($("#target_" + currentID).html());
    var imagechemin = $.trim($("#image_" + currentID).html());
    var imagetitre = $.trim($("#imagealt_" + currentID).html());
    $('#HomeTitre').html("<a href='"+lien+"' target='"+target+"'>"+titre+"</a>");
    $('#HomeChapeau').html("<a href='"+lien+"' target='"+target+"'>"+chapeau+"</a>");
    $('#HomeImage').attr("alt",imagetitre);

    if(firstDiaporamaHome != 1)
    {
        $('#HomeImage').fadeTo(250,0.3,function()
        {
            $('#HomeImage').attr("src",imagechemin);
            $('#HomeImage').fadeTo(250,1);
        });
    }
    else
    {
        firstDiaporamaHome = 0;
        $('#HomeImage').attr("src",imagechemin);
    }
}

function initModuleImageLien()
{
    $('.ImageLien').each(function()
    {
        setImageModuleImageLien(this, 0);
    });
}
function setImageModuleImageLien(elem, idCurrent)
{
    $(elem).children('img').css('display','none');
    $(elem).children('img').eq(idCurrent).css("display","block");
    
    idCurrent++;
    if(idCurrent >= $(elem).children('img').length)
        idCurrent = 0;
        
    setTimeout(function(){setImageModuleImageLien(elem, idCurrent)}, vitesseModuleImageLien);
}
