$(function(){
    $('a[href="#"]').live('click',function(e){
        e.preventDefault();
    });

    $('.listing-project:last').addClass('last');

    tabs_z = 20;
    $('.tabs ul li').each(function(){
        $(this).css('z-index',tabs_z);
        tabs_z--;
    });
   
    $('#btn-more-info').click(function(){
        $(this).toggleClass('opened');
        if($('.more-caract:hidden').length != 0 && !$('.more-caract').is(':animated')){
            $('.more-caract').slideDown(500);
        }else{
            $('.more-caract').slideUp(200);
        }
        
    });
    
    $('table tr:even td').addClass('td-even');
    
    $('a.btn-print').live('click', function(e){
        e.preventDefault();
        window.print();
    });
    
    //####################################################################
    //##  System de Tabs  ################################################
    
    $('.tabs ul li a').click(function(e){
        //e.preventDefault();
        
        if($(this).attr('href') == 'tab-1' && !$('.photos').data('initGalleria')){
            initGalleria();
        }
        
        $('.tab-wrapper:visible').hide();
        $('.tabs ul li.active, .tab-wrapper.active').removeClass('active');
        
        $(this).parent().addClass('active');
        $('#'+$(this).attr('href')).show().addClass('active');
        
        // Initiation des position de pin (page "project") dans le dom pour le hover en zoom
        if($('.plan-project-wrapper').length > 0){
            $('.plan-pin').each(function(){
                position = $(this).position();
                $(this).data('pos_left',position.left);
                $(this).data('pos_top',position.top);
            });
        }
    });

    $(".submit_general").click(function(){
        

    });

    var w_height = $(window).height();
    var w_width = $(window).width();

    $('#send_to_friend_form, #demande_info_form, #iframe-content-wrapper').bind('validate', function(){
        var iFrames = window.parent.$('#popup-iframe');
        popInfo = window.parent.$('.b_ctr').data();
        //console.log('oriHeight : '+popInfo.oriHeight);

        popup_element = window.parent.$('.b_ctr');
        newIframeHeight = iFrames.contents().find('body').height();
        //console.log('newIframeHeight '+newIframeHeight);

        iFrames[0].style.height = newIframeHeight + 'px';
        popup_element.css({height: newIframeHeight + 32});

        popElementPos = popup_element.position();
        pos_y = popElementPos.top;

        //console.log('old pos :'+popElementPos.top);

        //### Determine si l'
        if(newIframeHeight > popInfo.oriHeight){
            posDif = (newIframeHeight - popInfo.oriHeight)/2;
            popup_element.css({top: pos_y - posDif});
            //new_pos = pos_y - posDif;
        }else{
            posDif = (popInfo.oriHeight - newIframeHeight)/2;
            popup_element.css({top: pos_y + posDif});
            //new_pos = pos_y + posDif;
        }
        //console.log('new pos : '+new_pos);
        popInfo.oriHeight = newIframeHeight;

        //console.log('posDif '+posDif);
    });

    //console.log(ori_popup_height);
    
    //####################################################################
    //##  Popups  ########################################################

    
    $('.popup-iframe-trigger').click(function(){
        if($(this).data('popup_size_width') != undefined && $(this).data('popup_size_height') != undefined){

            popup_data = $(this).data();
            //console.log(popup_data.popup_size_height);
            width = popup_data.popup_size_width;
            height = popup_data.popup_size_height;
        }else{
            width = 668;
            height = 430;
        }
        $.wt_popup_iframe($(this).data('target-href'), $(this), {
            targetHeight: height,
            targetWidth: width,
            backgroundColor: "#fff",
            title: $(this).data('popup_title')
        });
    });
    
    $('.popup-image-trigger').click(function(){
        $.wt_lightbox('image', {
            target: $(this).data('target_src'),
            title: $(this).data('popup_title')
        });
    });
    
    $('#btn-zoom-map').click(function(){
        $.wt_lightbox('gm', {
            target: '<div id="popup-map"></div>',
            width: 800,
            height: 600,
            gm_id: 'popup-map',
            title: $(this).data('popup_title')
        });
    });

    //##  Print du popup des plans  ######################################
    $('a.btn-print-popup').live('click', function(e){
        e.preventDefault();

        target_src = $('.b_ctr img.main-img').attr('src');
        //console.log($('.b_ctr img.main-img'));
        $('.b_ctr_inner').html('<iframe id="popup-iframe" name="popup_iframe" src="iframe.html" style="height: '+$('.b_ctr_inner').outerHeight()+'px; width: '+$('.b_ctr_inner').outerWidth()+'px;"></iframe>');

        $('#popup-iframe').load(function(){
            $('#popup-iframe').contents().find('body #iframe-content-wrapper').append('<img src="'+target_src+'">');
            top.popup_iframe.printWin();
            $('.b_close').delay(1000).trigger('click');
        });

    });

});

$(window).load(function(){
    //$('#rc').css('min-height',$('#lc').height());
    
    

    

    
    
    //####################################################################
    //##  Plans d'unites  ################################################
    
    $('.plan-project-wrapper').hover(function(){
        $('#plan-project-wrapper-overlay').css({height: $(this).outerHeight()+'px', width: $(this).outerWidth()+'px'}).stop(true, true).animate({opacity: 0.2}, 200);
    }, function(){
        $('#plan-project-wrapper-overlay').animate({opacity: 0}, 200);
    });
    
    if($('.plan-project-wrapper').is(':visible')){
        $('.plan-pin').each(function(){
            position = $(this).position();
            $(this).data('pos_left',position.left);
            $(this).data('pos_top',position.top);
        });
    }
   
    
    $('.plan-pin').hover(function(){
        position = $(this).position();
        if(!$(this).is(':animated')){
            $(this).stop(true, true).animate({height: '24px', width: '24px', top: position.top - 2,  left: position.left - 2}, 100);
        }
    }, function(){
        $(this).stop(true, true).animate({height: '20px', width: '20px', top: $(this).data('pos_top'),  left: $(this).data('pos_left')}, 200);
    });
    
    
    //##  Tooltips  ######################################################
    
    $('.tooltip-plans').hover(function(){
     
    });
    
     
    $('.tooltip-plans').each(function(){
        $(this).wtTooltip({
            tooltipDataIdContainer: "target_content",
            //tooltipTargetContent: $(this).data('target_content'),
            tooltipMaxWidth: '300',
            tooltipFade: true
        });
    });
    
    
});


//#####################################################################################################
//##  Popup AJAX  #####################################################################################
    
$.wt_popup_ajax = function(content, obj, opt){
    
    var $b_black = $("#back-overlay");

    //var $b_loading = $("#b_loading");

    var w_height = $(window).height();
    var w_width = $(window).width();

    var d_height = $(document).height();
    var d_width = $(document).width();

    var pos_x = 0;
    var pos_y = 0;

    var box_width = 0;
    var box_height = 0;

    $b_black.css({
        "width": d_width,
        "height": d_height
    });

    pos_x = w_width / 2;
    pos_y = (w_height / 2) + $(window).scrollTop();

    $('body').append('<div class="b_ctr_content b_ctr"></div>');
    $b_ctr_content = $('.b_ctr_content');
    
    //$(opt.targetDiv).clone().appendTo(b_ctr_content);
    
    $('.b_ctr_content').append('<div class="b_ctr_inner"></div>');
    
    $b_ctr_inner = $('.b_ctr_content .b_ctr_inner');
    $b_ctr_inner.html(content);
    
    $b_ctr_inner.find('.tooltip-plans').each(function(){
        $(this).wtTooltip({
            tooltipDataIdContainer: "target_content",
            //tooltipTargetContent: $(this).data('target_content'),
            tooltipMaxWidth: '300',
            tooltipFade: true
        });
    });

    box_height = opt.targetHeight+"px";
    box_width = opt.targetWidth+"px";

    if(box_width != '' && box_height != ''){
        $b_ctr_inner.css({
            "min-height": box_height,
            "min-width": box_width,
            'display': 'block'
        });
    }
    
    $b_ctr_content.css({
        'opacity': '0',
        "min-height": box_height,
        "min-width": box_width,
        padding: '5px',
        backgroundColor: opt.backgroundColor,
        'display': 'block',
        "top": ((pos_y - ($b_ctr_content.outerHeight()/2))+60),
        "left": pos_x - ($b_ctr_content.outerWidth()/2)
    });

    /*b_ctr_content.css({
        "top": ((pos_y - (b_ctr_content.outerHeight()/2))+60),
        "left": pos_x - (b_ctr_content.outerWidth()/2)
    })*/
    
    $b_ctr_content.prepend('<div class="popup-top-bar clearfix"><strong>'+obj.data('popup_title')+'</strong><ul class="list-hori list-menu clearfix"><li><a href="#" class="btn-print-popup">Imprimer</a></li><li><a href="#" class="b_close" title="Fermer la fenêtre">Fermer</a></li></ul></div>');
    $b_black.stop(true,true).fadeTo(600, 0.75, function(){
        /*var l_pos_x =  w_width / 2;
        var l_pos_y = (w_height / 2) + $(window).scrollTop();
        var l_height = $b_loading.outerHeight() / 2;
        var l_width = $b_loading.outerWidth() / 2;*/
    });
    
    b_ctr_offset = $b_ctr_content.offset();
    
    $b_ctr_content.animate({opacity: 1, top: b_ctr_offset.top-60}, 800, function(){
        //$b_loading.hide();
    });
        
    // ############################################################################

    $(".b_close, #back-overlay").click(function(e){
        e.preventDefault();
        e.stopPropagation();
        
        $b_ctr_content.animate({opacity: 0}, 300, function(){

            $b_ctr_content.find(".b_close").remove();
            $b_ctr_content.remove();

            $b_black.fadeOut(200);
        });
    });
};


//#####################################################################################################
//##  Popup iFrame  ###################################################################################

$.wt_popup_iframe = function(url, obj, opt){
    
    var $b_black = $("#back-overlay");

    //var $b_loading = $("#b_loading");

    var w_height = $(window).height();
    var w_width = $(window).width();

    var d_height = $(document).height();
    var d_width = $(document).width();

    var pos_x = 0;
    var pos_y = 0;

    var box_width = 0;
    var box_height = 0;

    $b_black.css({
        "width": d_width,
        "height": d_height
    });

    pos_x = w_width / 2;
    pos_y = (w_height / 2) + $(window).scrollTop();

    $('body').append('<div class="b_ctr_content b_ctr"></div>');
    $b_ctr_content = $('.b_ctr_content');
    
    //$(opt.targetDiv).clone().appendTo(b_ctr_content);
    
    
    $b_ctr_content.append('<div class="b_ctr_inner"></div>');
    
    $b_ctr_inner = $('.b_ctr_content .b_ctr_inner');
    //$b_ctr_inner.html(content);

    $b_ctr_content.data({
        windowH: w_height,
        windowW: w_width,
        oriHeight: opt.targetHeight,
        oriWidth: opt.targetWidth
    });

    //console.log($b_ctr_inner.data());

    box_height = opt.targetHeight+"px";
    box_width = opt.targetWidth+"px";

    if(box_width != '' && box_height != ''){
        $b_ctr_inner.css({
            "height": box_height,
            "width": box_width,
            'display': 'block'
        });
    }
    
    $b_ctr_content.css({
        'opacity': '0',
        //"height": box_height,
        "width": box_width,
        padding: '5px',
        backgroundColor: opt.backgroundColor,
        'display': 'block',
        "top": ((pos_y - ($b_ctr_content.outerHeight()/2))+60),
        "left": pos_x - ($b_ctr_content.outerWidth()/2)
    });

    $b_ctr_inner.append('<iframe id="popup-iframe" name="popup-iframe" frameborder="0" src="'+url+'" style="height: '+$('.b_ctr_inner').outerHeight()+'px; width: '+$('.b_ctr_inner').outerWidth()+'px;"></iframe>')

    /*b_ctr_content.css({
        "top": ((pos_y - (b_ctr_content.outerHeight()/2))+60),
        "left": pos_x - (b_ctr_content.outerWidth()/2)
    })*/
    
    $b_ctr_content.prepend('<div class="popup-top-bar clearfix"><strong>'+opt.title+'</strong><ul class="list-hori list-menu clearfix"><li><a href="#" class="b_close" title="Fermer la fenêtre">Fermer</a></li></ul></div>');
    $b_black.stop(true,true).fadeTo(600, 0.75, function(){
        /*var l_pos_x =  w_width / 2;
        var l_pos_y = (w_height / 2) + $(window).scrollTop();
        var l_height = $b_loading.outerHeight() / 2;
        var l_width = $b_loading.outerWidth() / 2;*/
    });
    
    b_ctr_offset = $b_ctr_content.offset();
    
    $b_ctr_content.animate({opacity: 1, top: b_ctr_offset.top-60}, 800, function(){
    });
        
    // ############################################################################

    $(".b_close").click(function(e){
        e.preventDefault();
        e.stopPropagation();
        
        $b_ctr_content.animate({opacity: 0}, 300, function(){

            $b_ctr_content.find(".b_close").remove();
            $b_ctr_content.remove();

            $b_black.fadeOut(200);
        });
    });
};


//#####################################################################################
//##  Lightbox WT  ####################################################################

$.wt_lightbox = function(type, opt)
{
    //  *******************************************************
    //  Possible options :
    //  *******************************************************
    //  target - Target content to be displayed in the ligthbox
    //  width - Lightbox width
    //  height - Lightbox height
    //  gm_id - Google maps container div "id"
    
    
    var b_black = $("#back-overlay");

    var w_height = $(window).height();
    var w_width = $(window).width();

    var d_height = $(document).height();
    var d_width = $(document).width();

    var pos_x = 0;
    var pos_y = 0;

    var box_width = 0;
    var box_height = 0;

    b_black.css({
        "width": d_width,
        "height": d_height
    });

    pos_x = w_width / 2;
    pos_y = (w_height / 2) + $(window).scrollTop();

    // Si on veux afficher une image ##############################################

    if(type == "image"){
        $('body').append('<div class="b_ctr_image b_ctr"><div class="b_ctr_inner"></div></div>')

        var b_ctr_image = $(".b_ctr_image");
        var b_ctr_inner = $('.b_ctr_inner');

        b_ctr_inner.html('<img id="curr_zoom_image" class="main-img" src="'+opt.target+'" />');

        //b_ctr_image.append('<a href="#" class="b_close" title="Fermer la fenêtre">Fermer la fenêtre</a>');
        b_ctr_image.prepend('<div class="popup-top-bar clearfix"><strong>'+opt.title+'</strong><ul class="list-hori list-menu clearfix"><li><a href="#" class="btn-print-popup">Imprimer</a></li><li><a href="#" class="b_close" title="Fermer la fenêtre">Fermer</a></li></ul></div>');

        $("#curr_zoom_image").one("load",function(){

            box_width = b_ctr_image.width();
            box_height = b_ctr_image.height() - 5;
            // Setup du container de la photo
            b_ctr_image.css({
                'opacity': '0',
                "height": box_height,
                "width": box_width,
                "top": pos_y - (box_height/2),
                "left": pos_x - (box_width/2),
                'display': 'block'
            });

            // Setup de la toile de fond
            b_black.stop(true,true).fadeTo(400, 0.75, function(){   }).fadeIn(300);

            b_ctr_image.animate({opacity: 1}, 500, function(){   });
        })
        .each(function(){
            if(this.complete) $(this).trigger("load"); 
        });

    // Si on veux afficher du content  ############################################

    }else if(type == "content"){

        $('body').append('<div class="b_ctr_content b_ctr"></div>');
        b_ctr_content = $('.b_ctr_content');

        target_div = opt.target;
        $(opt.target).clone().appendTo(b_ctr_content);

        box_height = opt.height;
        box_width = opt.width;

        if(opt.width != '' && opt.height != ''){
            b_ctr_content.find(target_div).css({
                "min-height": box_height,
                "min-width": box_width,
                'display': 'block'
            });
        }

        b_ctr_content.css({
            'opacity': '0',
            "height": box_height,
            "width": box_width,
            padding: '15px',
            backgroundColor: '#fff',
            'display': 'block',
            "top": pos_y - (b_ctr_content.outerHeight()/2),
            "left": pos_x - (b_ctr_content.outerWidth()/2)
        });
        
        b_ctr_content.prepend('<div class="popup-top-bar clearfix"><strong>'+opt.title+'</strong><ul class="list-hori list-menu clearfix"><li><a href="#" class="b_close" title="Fermer la fenêtre">Fermer</a></li></ul></div>');
        b_black.stop(true,true).fadeTo(400, 0.75, function(){    });
        
        b_ctr_content.animate({opacity: 1}, 500, function(){     });
        
    //##  GOOGLE MAPS  ##########################################################    
    }else if(type == "gm"){
        
        $('body').append('<div class="b_ctr_content b_ctr"></div>');
        b_ctr_content = $('.b_ctr_content');
        
        b_ctr_content.append(opt.target);
        
        box_height = opt.height;
        box_width = opt.width;

        if(opt.width != '' && opt.height != ''){
            b_ctr_content.find('#'+opt.gm_id).css({
                "height": box_height-30, // -30 compense pour le padding haut et bas
                "width": box_width,
                'display': 'block'
            });
        }

        b_ctr_content.css({
            'opacity': '0',
            "height": box_height,
            "width": box_width,
            padding: '5px',
            backgroundColor: '#fff',
            'display': 'block',
            "top": pos_y - (b_ctr_content.outerHeight()/2),
            "left": pos_x - (b_ctr_content.outerWidth()/2)
        });
        
        b_ctr_content.prepend('<div class="popup-top-bar clearfix"><strong>'+opt.title+'</strong><ul class="list-hori list-menu clearfix"><li><a href="#" class="b_close" title="Fermer la fenêtre">Fermer</a></li></ul></div>');
        
        b_black.stop(true,true).fadeTo(400, 0.75, function(){    });
        b_ctr_content.animate({opacity: 1}, 500, function(){     });
        
        initialize_gm(opt.gm_id, true);
    }
    // ############################################################################

    $(".b_close, #back-overlay").live('click',function(){
        var popup_element;
        if (type == "image"){popup_element = b_ctr_image}else{popup_element = b_ctr_content}
        popup_element.stop(true,false).animate({opacity:0}, 300, function(){

            popup_element.find(".b_close").remove();
            
            if (type == "image"){  // Lorsque l'on ferme un popup image
                popup_element.remove();
            }else if(type == 'content'){  // Lorsque l'on ferme un popup content
                $('.popup_errors_wrapper').hide().find(".errors_list").html("");
                $('div.nj_form_lblfld label').css('color','#000');
                popup_element.remove();
            }else if(type == 'gm'){
                popup_element.remove();
            }

            b_black.fadeOut(200);
        });
    });
};



