$(document).ready(function() {


// Activer l'effet accordeon sur le menu de navigation des metiers

    if ($("#navigation_menu").length > 0){

        $("#navigation_menu div.artist_list_for_job").hide();
        $("#navigation_menu div.current_job").next(".artist_list_for_job").show();

        $("#navigation_menu div.job").click( function(){
            //alert("slide");
            $("#navigation_menu div.artist_list_for_job").hide();
            $(this).next(".artist_list_for_job").show();
        });


    }



// Activer l'affichage de la biographie de l'artiste

    if ($("#artist_biography").length > 0){
        
        $("#artist_biography").click(function () {
            afficher_bio();
            return (false);
        });
    }

// Activer l'affichage des clients de l'artiste

    if ($("#artist_clients").length > 0){

        $("#artist_clients").click(function () {
            afficher_clients();
            return (false);
        });
    }

// Activer l'affichage du contact de l'agence

    if ($("#agency_contact").length > 0){

        $("#agency_contact").click(function () {

           $("#main_content").fadeOut(200, function(){
               $("#main_content").html( $("#agency_contact_hidden").html() );
               $("#main_content").addClass("info_text");
               $("#main_contener").addClass("info_text");
           });
           $("#main_content").fadeIn();

           return (false);
        });
    }


// Rendre les images du portfolio redimensionnable avec la fenetre de navigateur

    redimensionner_images();

    $(window).bind("resize", function(){//Adjusts image when browser resized
        redimensionner_images();
    });


// Desactiver le clic droit sur toute la page (pour empecher recuperation facile d'images)

    $(document).bind("contextmenu",function(e){
        return false;
    });
    $("#main_contener").bind("mousedown",function(e){
        return false;
    });


// Activer le remplacement des polices

    FLIR.init( { path: 'facelift-1.2.2/' } );

 /*   FLIR.replace( 'div.job span' , new FLIRStyle({ cFont:'univers_condensed_bold' }) );*/
/*    FLIR.replace( 'div.artist a' , new FLIRStyle({ cFont:'univers_condensed' }) );*/


    /*FLIR.replace( 'div#artist_name h1' , new FLIRStyle({ cFont:'univers_light' }) );*/
    /*FLIR.replace( 'div.portfolio a' , new FLIRStyle({ cFont:'univers_light' }) );*/
    /*FLIR.replace( 'div.artist_info a' , new FLIRStyle({ cFont:'univers_light' }) );*/

    FLIR.replace( 'ul#navigation_secondaire li a' , new FLIRStyle({ cFont:'univers_condensed_bold' }) );

    FLIR.replace( 'a#lien_customsite' , new FLIRStyle({ cFont:'univers_condensed_bold' }) );
    FLIR.replace( 'a#lien_pdf' , new FLIRStyle({ cFont:'univers_condensed_bold' }) );

// Afficher le menu de navigation
/*
    if ($("#navigation_menu").length > 0){

        //$("#navigation_menu").hide();
        $("#navigation_menu").fadeIn(2000);

    }
*/


// Activer l'affichage des fleches de navigation dans le portfolio
// UNIQUEMENT DANS LE CAS DES PORTFOLIOS

    if ($("#dans_portfolio").length > 0) {

        $("#main_contener").mousemove(

            function(e){

                var x = e.pageX - this.offsetLeft;
                var y = e.pageY - this.offsetTop;

                var switch_limit = 150 + Math.floor( parseInt(contener_width)/2 );

                if ( x <= switch_limit ){
                    $("#next_image").hide();
                    $("#previous_image").show();
                }
                if ( x > switch_limit ){
                    $("#next_image").show();
                    $("#previous_image").hide();

                }

                //$('#debug').html(x +', '+ y + ' WIDTH: ' + contener_width + ' LIMIT: ' + switch_limit);
            }
        );


       $("#main_contener").hover(
            function(e){
            },
            function (e) {
               $("#next_image").hide();
               $("#previous_image").hide();
            }
       );

    }

});



var current_image_id;
// taille fixe pour le bloc info

var minimum_image_height = 300;
var maximum_image_height = 1000;

var contener_height;
var contener_width;

var viewport_height;
var viewport_width;

var height_offset_for_header = 74;
var height_offset_for_footer = 100; //  $("#artist_name").height(); /* 90 + rab de 5px */
var height_offset_for_info = 20;

function afficher_image (image_id){

    //alert (image_name);
    current_image_id = image_id;

    var image_max_height = images_heights[image_id];
    if (!image_max_height){
        image_max_height = maximum_image_height;
    }
    
    viewport_height = $(window).height();
    viewport_width = $(window).width();

    //height_offset_for_header = $("#header_contener").height();
    //height_offset_for_footer = $("#footer_contener").height();
    
    var image_height = parseInt(viewport_height)
                       - parseInt(height_offset_for_header)
                       - parseInt(height_offset_for_footer)
                       - parseInt(height_offset_for_info);

    if (image_height < minimum_image_height){
        image_height = minimum_image_height;
    }
    if (image_height > maximum_image_height){
        image_height = maximum_image_height;
    }

    if (image_height > image_max_height){
        image_height = image_max_height;
    }

    contener_height = parseInt(image_height) + parseInt(height_offset_for_info);
    contener_width = $("#main_contener").width();
    contener_half_width = Math.floor( parseInt(contener_width) / 2 );
    contener_half_height = Math.floor( parseInt(contener_height) / 2 ) - 20;
    
    // hauteur des div pour les fleches de navigation : moitie de la hauteur
    // plus un rab de 10px
    height_for_arrows = Math.floor( parseInt(contener_height) / 2 ) + 10;

    // supprimer classe eventuelle si on vient de bio ou clients
    $("#main_contener").removeClass("info_text");
    $("#main_content").removeClass("info_text");

    var image_name = images_names[image_id];
    var image_info = images_infos[image_id];
    var image_type = images_types[image_id];
    var image_orientation = images_orientations[image_id];

    var previous_image_id = images_prev[image_id];
    var next_image_id = images_next[image_id];

    var html_for_image = '';

 
    if (image_orientation == 'portrait_par_deux'){

        if (previous_image_id){

            var previous_image = images_names[previous_image_id];
            var previous_image_info = images_infos[previous_image_id];

            html_for_image += '<table cellspacing="0" cellpadding="0" border="0">';

            html_for_image += '<tr>';

            html_for_image += '<td id="left_image" align="right" valign="middle">';
            html_for_image += '<img src="' + previous_image + '" alt="Previous image" height="'+ image_height + 'px"/>';
            html_for_image += '</td>';

            html_for_image += '<td id="right_image" align="left" valign="middle">';
            html_for_image += '<img src="' + image_name + '" alt="Current image" height="'+ image_height + 'px"/>';
            html_for_image += '</td>';

            html_for_image += '</tr>';

            html_for_image += '<tr>';

            html_for_image += '<td id="left_caption">';
            html_for_image += '<span>' + previous_image_info + '</span>';
            html_for_image += '</td>';

            html_for_image += '<td id="right_caption">';
            html_for_image += '<span>' + image_info + '</span>';
            html_for_image += '</td>';

            html_for_image += '</tr>';

            html_for_image += '</table>';

        }

        if (next_image_id){

            var next_image_name = images_names[next_image_id];
            var next_image_info = images_infos[next_image_id];

            html_for_image += '<table cellspacing="0" cellpadding="0" border="0">';

            html_for_image += '<tr>';

            html_for_image += '<td id="left_image" align="right" valign="middle">';
            html_for_image += '<img src="' + image_name + '" alt="Previous image" height="'+ image_height + 'px"/>';
            html_for_image += '</td>';

            html_for_image += '<td id="right_image" align="left" valign="middle">';
            html_for_image += '<img src="' + next_image_name + '" alt="Current image" height="'+ image_height + 'px"/>';
            html_for_image += '</td>';

            html_for_image += '</tr>';

            html_for_image += '<tr>';

            html_for_image += '<td id="left_caption">';
            html_for_image += '<span>' + image_info + '</span>';
            html_for_image += '</td>';

            html_for_image += '<td id="right_caption">';
            html_for_image += '<span>' + next_image_info + '</span>';
            html_for_image += '</td>';

            html_for_image += '</tr>';

            html_for_image += '</table>';
            
        }

    }
    

    if (image_orientation == 'paysage' || image_orientation == 'portrait_seul'){

        var html_for_player = '<div id="main_player">'
                               + '<a href="http://www.adobe.com/go/getflashplayer"><img src="http://www.adobe.com/images/shared/download_buttons/get_flash_player.gif" alt="Get Adobe Flash player" /></a>'
                               + '</div>'
                               + '<div id="center_caption"><span>' + image_info + '</span></div>';

        if (image_type == 'flv'){

            $("#main_content").fadeOut(200, function(){             

                
                $("#main_content").html( html_for_player );
/*
                var flashvars = {    
                    videoURL: '../'+ image_name,
                    //thumbnail: "thumb.jpg",
                    autoHide: "1",
                    autoHideTime: "300",
                    initialVolume: "0.75",
                    bufferSize: "2",
                    controlsFadeTime: "0.2"
                    
                };
			
                var params = {
                    wmode:"direct",
                    allowfullscreen:  "true",
                    bgcolor: '#ffffff'
                };
                var attributes = {};
*/
                var player_width = parseInt(contener_width) - 40;
  /*
                swfobject.embedSWF("video_player/preview.swf", "main_player", player_width, image_height, "9.0.0", false, flashvars, params, attributes);
*/

                var mainswf = new SWFObject("video_player/preview.swf", "main_player", player_width, image_height, "9", "#ffffff");
                //var mainswf = new SWFObject("video_player/preview.swf", "main_player", "100%", "100%", "9", "#666666");

                mainswf.addParam("scale", "noscale");
                mainswf.addParam("wmode", "window");
                mainswf.addParam("allowFullScreen", "true");
                mainswf.addVariable("swfMovie", "../" + image_name);
                mainswf.write("main_player");

                


                 // Retailler la div contenante, pour eviter que la barre de nav du bas bouge
                $("#main_content").attr("style", "height:"+contener_height);
                
               // $("#main_content").attr("style", "height:100%");
               // $("#main_player").attr("style", "height:100%");

                // Placer le player sous les fleches de navigation
                //$("#main_player").css('zIndex','888');
                //$("#main_content").css('zIndex','777');

                // Afficher la div contenante
                $("#main_content").fadeIn('fast', function() {
                    if ($("#barre_artiste")){
                        $("#barre_artiste").fadeIn(3600);
                    }
                });
            
            });
          
        }


        if (image_type == 'swf'){

            
            $("#main_content").fadeOut(200, function(){

                $("#main_content").html( html_for_player );


                 var mainswf = new SWFObject(image_name, "main_player", contener_width, image_height, "9", "#ffffff");
                  //var mainswf = new SWFObject(image_name, "main_player", "100%", "100%", "9", "#ffffff");
                //var mainswf = new SWFObject("video_player/preview.swf", "main_player", "100%", "100%", "9", "#666666");

                // ORI
                //mainswf.addParam("scale", "noscale");
                
                mainswf.addParam("wmode", "window");
                mainswf.addParam("allowFullScreen", "true");
                mainswf.write("main_player");
     /*
                var flashvars = {};
                flashvars.bgcolor = "#ffffff";
                flashvars.bufferSize = "2";
                var params = {};
                var attributes = {};
                swfobject.embedSWF(image_name, "main_player", contener_width, image_height, "9.0.0", false, flashvars, params, attributes);
     */
                // Retailler la div contenante, pour eviter que la barre de nav du bas bouge
                $("#main_content").attr("style", "height:"+contener_height);

                
                $("#main_content").fadeIn('fast', function() {
                    if ($("#barre_artiste")){
                        $("#barre_artiste").fadeIn('slow');
                    }
                });

            });

        
        }


        if (image_type == 'flv' || image_type == 'swf'){

            // Retailler les zones pour les fleches de navigation

            var new_contener_width = $("#main_contener").width();
            var new_contener_half_width = Math.floor( parseInt(new_contener_width) / 2 );

            $("#next_image").width( new_contener_width );
            $("#previous_image").width( new_contener_half_width );
            //
            $("#next_image").height( height_for_arrows );
            $("#previous_image").height( height_for_arrows );
            //
            $("#next_image img").css('paddingTop', contener_half_height );
            $("#previous_image img").css('paddingTop', contener_half_height );

            // Reinitialiser les liens pour l'image suivante et l'image precedente
            initialiser_image_suivante(image_id);
            initialiser_image_precedente(image_id);

            return(false);

        }


        html_for_image += '<table cellspacing="0" cellpadding="0" border="0">';

        html_for_image += '<tr>';

        html_for_image += '<td id="center_image" align="center" valign="middle">';
        html_for_image += '<img src="' + image_name + '" alt="Current image" height="'+ image_height + 'px"/>';
        html_for_image += '</td>';

        html_for_image += '</tr>';

        html_for_image += '<tr>';

        html_for_image += '<td id="center_caption">';
        html_for_image += '<span>' + image_info + '</span>';
        html_for_image += '</td>';

        html_for_image += '</tr>';

        html_for_image += '</table>';

    }

    $("#main_content").fadeOut(200, function(){
        $("#main_content").html( html_for_image );
        //redimensionner_images();
        
    });
    $("#main_content").fadeIn('fast', function(){

        var new_contener_width = $("#main_contener").width();
        var new_contener_half_width = Math.floor( parseInt(new_contener_width) / 2 );

        /*
        $("#debug").html('IMAGE hauteur : '+ image_height
            + ' /// CONTENEUR IMAGE : ' + contener_width + ' x ' + contener_height + ' - largeur apres affichage : '+new_contener_width
            + ' /// FLECHE GAUCHE : '+ contener_half_width +'px - FLECHE DROITE : '+contener_width+'px '
            + ' /// FENETRE : ' + viewport_width + ' x ' + viewport_height
            + ' /// OFFSET : header ' + height_offset_for_header + ' footer : '+ height_offset_for_footer + ' info : ' + height_offset_for_info
        );
        */

        // Retailler la div contenante, pour eviter que la barre de nav du bas bouge
        $("#main_content").attr("style", "height:"+contener_height);

        // Retailler les zones pour les fleches de navigation
        $("#next_image").width( new_contener_width );
        $("#previous_image").width( new_contener_half_width );
        //
        $("#next_image").height( height_for_arrows );
        $("#previous_image").height( height_for_arrows );
        //
        $("#next_image img").css('paddingTop', contener_half_height );
        $("#previous_image img").css('paddingTop', contener_half_height );

        // Reinitialiser les liens pour l'image suivante et l'image precedente
        initialiser_image_suivante(image_id);
        initialiser_image_precedente(image_id);

        
        if ($("#barre_artiste")){
            $("#barre_artiste").fadeIn('slow');
        }

    });


    
    return (false);

}



function redimensionner_images() {

    //var image_width = 0;
    var image_id = current_image_id;

    var image_type = images_types[image_id];
    var image_orientation = images_orientations[image_id];
    var previous_image_id = images_prev[image_id];
    var next_image_id = images_next[image_id];

    var image_max_height = images_heights[image_id];
    if (!image_max_height){
        image_max_height = maximum_image_height;
    }

    viewport_height = $(window).height();
    viewport_width = $(window).width();
    
    //height_offset_for_header = $("#header_contener").height();
    //height_offset_for_footer = $("#footer_contener").height();
    
    var image_height = parseInt(viewport_height)
                       - parseInt(height_offset_for_header)
                       - parseInt(height_offset_for_footer)
                       - parseInt(height_offset_for_info);

    if (image_height < minimum_image_height){
        image_height = minimum_image_height
    }
    if (image_height > maximum_image_height){
        image_height = maximum_image_height
    }

    if (image_height > image_max_height){
        image_height = image_max_height;
    }
    
    contener_height = parseInt(image_height) + parseInt(height_offset_for_info);
    contener_width = $("#main_contener").width();
    contener_half_width = Math.floor( parseInt(contener_width) / 2 );
    contener_half_height = Math.floor( parseInt(contener_height) / 2 ) - 20;


    if (image_orientation == 'portrait_par_deux'){

        $("#left_image img").height(image_height);
        $("#right_image img").height(image_height);

    }


    if (image_orientation == 'paysage' && image_type == 'img'){

        $("#fulllenght_image img").height(image_height);

    }
    
    /*
    $("#debug").html('IMAGE hauteur : '+ image_height
            + ' /// CONTENEUR IMAGE : ' + contener_width + ' x ' + contener_height
            + ' /// FENETRE : ' + viewport_width + ' x ' + viewport_height
            + ' /// OFFSET : header ' + height_offset_for_header + ' footer : '+ height_offset_for_footer + ' info : ' + height_offset_for_info
        );
      */
    
    // Retailler la div contenante, pour eviter que la barre de nav du bas bouge
    $("#main_content").attr("style", "height:"+contener_height);

    // Retailler les zones pour les fleches de navigation
    $("#next_image").width( contener_width );
    $("#previous_image").width( contener_half_width );
    //
    $("#next_image").height( contener_height );
    $("#previous_image").height( contener_height );
    //
    $("#next_image img").css('paddingTop', contener_half_height );
    $("#previous_image img").css('paddingTop', contener_half_height );

}









function afficher_image_home (image_name, image_info, image_orientation, image_max_height){

    viewport_height = $(window).height();
    viewport_width = $(window).width();

    //height_offset_for_header = $("#header_contener").height();
    //height_offset_for_footer = $("#footer_contener").height();

    if (!image_max_height){
        image_max_height = maximum_image_height;
    }
    
    var image_height = parseInt(viewport_height)
                       - parseInt(height_offset_for_header)
                       - parseInt(height_offset_for_footer)
                       - parseInt(height_offset_for_info);

    if (image_height < minimum_image_height){
        image_height = minimum_image_height
    }
    if (image_height > maximum_image_height){
        image_height = maximum_image_height
    }
    if (image_height > image_max_height){
        image_height = image_max_height;
    }


    contener_height = parseInt(image_height) + parseInt(height_offset_for_info);
    contener_width = $("#main_contener").width();


    html_for_image = '';

    html_for_image += '<table cellspacing="0" cellpadding="0" border="0">';

    html_for_image += '<tr>';

    html_for_image += '<td id="center_image" align="center" valign="middle">';
    html_for_image += '<img src="' + image_name + '" alt="Current image" height="'+ image_height + 'px"/>';
    html_for_image += '</td>';

    html_for_image += '</tr>';

    html_for_image += '<tr>';

    html_for_image += '<td id="center_caption">';
    html_for_image += '<span>' + image_info + '</span>';
    html_for_image += '</td>';

    html_for_image += '</tr>';

    html_for_image += '</table>';


    $("#main_content").fadeOut(200, function(){
        $("#main_content").html( html_for_image );
        
    });
    $("#main_content").fadeIn('slow', function() {
        if ($("#barre_artiste")){
            $("#barre_artiste").fadeIn();
        }
    });

    

    return (false);

}



function initialiser_image_suivante (image_id){

    var image_id_suivant = images_next_for_nav[image_id];

    $("#next_image").unbind('click');

    $("#next_image").click(
        function (e) {
            if (image_id_suivant){
                afficher_image(image_id_suivant);
                //  alert('suivante de ' + image_id + ' = ' + image_id_suivant);
            } else {
                return (false);
            }
            
        }
    );

    

}

function initialiser_image_precedente (image_id){

    var image_id_prec = images_prev_for_nav[image_id];

    $("#previous_image").unbind('click');

    $("#previous_image").click(
        function (e) {
            if (image_id_prec){
                afficher_image(image_id_prec);
                //alert('suivante de ' + image_id);
            } else {
                return (false);
            }
            
        }
    );
    

}



function afficher_clients () {


    $("#main_content").fadeOut(200, function(){

       var html_for_clients = $("#artist_clients_hidden").html();
       //html_for_clients += '<span class="fulllenght_image_info image_info">&nbsp;</span>';

       $("#main_content").html( html_for_clients );
       $("#main_content").addClass("info_text");
       $("#main_contener").addClass("info_text");

       //$("#main_contener").height(contener_height);
       var height_text = parseInt(contener_height) - 20;
       $("#main_content").height(height_text);

       $("#main_content").fadeIn();
    });
           

}



function afficher_bio() {

    $("#main_content").fadeOut(200, function(){

        $("#main_content").html( $("#artist_biography_hidden").html() );
        $("#main_content").addClass("info_text");
        $("#main_contener").addClass("info_text");

        //$("#main_contener").height(contener_height);
        var height_text = parseInt(contener_height) - 20;
        $("#main_content").height(height_text);

        $("#main_content").fadeIn();
    });


}
