$(document).ready(function() {
    $('#fondNoir').click(function() {
        fermeturePopup();
    });
});

function rollOver(element)
{
    id = element.attr("id");//On récupère l'id du lien cliqué
    src = $('#'+id+' img').attr('src');//On récupère du coup l'image correspondante (/images/fr/toto.png)
    lien = src.split('/');//On decoupe pour n'avoir que l'image
    newLien = '/'+lien[1]+'/'+lien[2]+'/';//On reconstruit le lien
    nomImage = lien[3].split('.');//On enlève l'extension
    newLien += nomImage[0]+'Hover.'+nomImage[1];//On ajoute "Hover" au nom de l'image puis l'extension de nouveau
    $('#'+id+' img').attr('src', newLien);//On remplace le src de l'image avec la nouvelle image
}
function rollOverImg(element)
{
    src = element.attr('src');//On récupère du coup l'image correspondante (/images/fr/toto.png)
    lien = src.split('/');//On decoupe pour n'avoir que l'image
    newLien = '/'+lien[1]+'/'+lien[2]+'/';//On reconstruit le lien
    nomImage = lien[3].split('.');//On enlève l'extension
    newLien += nomImage[0]+'Hover.'+nomImage[1];//On ajoute "Hover" au nom de l'image puis l'extension de nouveau
    element.attr('src', newLien);//On remplace le src de l'image avec la nouvelle image
}

function rollOut(element)
{
    id = element.attr("id");
    src = $('#'+id+' img').attr('src');
    lien = src.split('/');
    newLien = '/'+lien[1]+'/'+lien[2]+'/';
    nomImage = lien[3].split('.');
    newLien += nomImage[0].substr(0, nomImage[0].length-5)+'.'+nomImage[1];//On supprime le mot "Hover" du lien en réduisant la chaine
    $('#'+id+' img').attr('src', newLien);
}

function rollOutImg(element)
{
    src = element.attr('src');
    lien = src.split('/');
    newLien = '/'+lien[1]+'/'+lien[2]+'/';
    nomImage = lien[3].split('.');
    newLien += nomImage[0].substr(0, nomImage[0].length-5)+'.'+nomImage[1];//On supprime le mot "Hover" du lien en réduisant la chaine
    element.attr('src', newLien);
}
function affichePopup(popup)
{
    var win = $(window);
    afficheFondNoir();
    var l = win.scrollLeft(), w = win.width();
    var largeur = popup.css('width');
    popup.css(
    {
            top: '120px',
            left:  l + (w / 2),
            marginLeft: - largeur.substring(0,largeur.length-2)/2
    }
    ).show();
}
function afficheFondNoir()
{
    var arrPageSizes=___getPageSize();
    var overlay = $('#fondNoir');
    
    overlay.css(
    {
        opacity:0.8,
        width:arrPageSizes[0],
        height:arrPageSizes[1]
    }
    ).fadeIn();
}
function cacheFondNoir()
{
    $('#fondNoir').hide();
}
function ouverturePopup(classe)
{
    var popup = $('#popup');
    popup.addClass(classe);
    affichePopup(popup);
}
function fermeturePopup()
{
    var popup = $('#popup');
    cacheFondNoir();
    popup.removeClass();
    popup.removeAttr('style');
    popup.hide();
}


// Gets the dimensions of the user's window for many different browsers--
// Returns an array of pageWidth, pageHeight, windowWidth, and windowHeight
// By Lokesh Dhakar of http://www.huddletogether.com
function ___getPageSize()
{
    var xScroll, yScroll;

    if (window.innerHeight && window.scrollMaxY)
    {
        xScroll = document.body.scrollWidth;
        yScroll = window.innerHeight + window.scrollMaxY;
    }
    else if (document.body.scrollHeight > document.body.offsetHeight)
    // all but Explorer Mac
    {
        xScroll = document.body.scrollWidth;
        yScroll = document.body.scrollHeight;
    }
    else
    // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
    {
        xScroll = document.body.offsetWidth;
        yScroll = document.body.offsetHeight;
    }

    var windowWidth, windowHeight;

    if (self.innerHeight)
    // all except Explorer
    {
        windowWidth = self.innerWidth;
        windowHeight = self.innerHeight;
    }
    else if (document.documentElement && document.documentElement.clientHeight)
    // Explorer 6 Strict Mode
    {
        windowWidth = document.documentElement.clientWidth;
        windowHeight = document.documentElement.clientHeight;
    }
    else if (document.body)
    // other Explorers
    {
        windowWidth = document.body.clientWidth;
        windowHeight = document.body.clientHeight;
    }

    // for small pages with total height less then height of the viewport
    if (yScroll < windowHeight)
        pageHeight = windowHeight;
    else
        pageHeight = yScroll;

    // for small pages with total width less then width of the viewport
    if (xScroll < windowWidth)
        pageWidth = windowWidth;
    else
        pageWidth = xScroll;

    arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight)
    return arrayPageSize;
}

function uploadCVEnd(sError, sPath, sMessage) {
    if(sError == 'OK') {
            $("#uploadStatusCV").text(sMessage+' ('+sPath+')');
            $("#candidature_cv").attr('value', sPath);
    } else {
            $("#uploadStatusCV").text(sError);
    }
    $("#loadingUpload").hide();
}

function uploadMotivationEnd(sError, sPath, sMessage) {
    if(sError == 'OK') {
            $("#uploadStatusMotivation").text(sMessage+' ('+sPath+')');
            $("#candidature_motivation").attr('value', sPath);
    } else {
            $("#uploadStatusMotivation").text(sError);
    }
    $("#loadingUpload").hide();
}
