/**
 * ouvre les lien blank dans une nouvelle fenetre
 */
function openBlankLink() {

    var liens = $('a.blank');
        for (var i = 0 ; i < liens.length ; i++)  {
                liens[i].onclick = function()  {
                        window.open(this.href);
                        return false;
                };
        }
}


/**
 * Simple Popup Trigger
 * Simple popup de contenu
 */
function sPopupTrigger()
{
    var triggers = $(".spopup").each(function () {
        $(this).overlay({

        oneInstance: false,
        api: true,
        // some expose tweaks suitable for modal dialogs
        expose: {
            color: '#333',
            loadSpeed: 200,
            opacity: 0.9
        },

        closeOnClick: false
    });
    });
}

function flashPopup()
{
    if($("#flash_error").length)
    {
        $("#flash_error").overlay({
            top: "center",
            expose: {
                color: '#333',
                loadSpeed: 200,
                opacity: 0.9
            },
            api: true
        }).load();
    }

    if($("#flash_notice").length)
    {
        $("#flash_notice").overlay({
            top: "center",
            expose: {
                color: '#333',
                loadSpeed: 200,
                opacity: 0.9
            },
            api: true
        }).load();
    }
}

/**
 * Ajax Popup Trigger
 * Popup avec contenu Ajax
 */
function aPopupTrigger()
{
    $(".apopup").each(function() {
        aPopoupTriggerOne($(this));
    });
}

function aPopoupTriggerOne(trigger) {
    trigger.overlay({

        oneInstance: false,
        api: true,
        // some expose tweaks suitable for modal dialogs
        expose: {
            color: '#333',
            loadSpeed: 200,
            opacity: 0.9,
            zIndex: 99
        },

        closeOnClick: false,

        onBeforeLoad: function() {
//            var trigger = this.getTrigger();
            var overlay = this.getOverlay();

            if ($(trigger).hasClass('popup_520')) {
                $(overlay).addClass('popup_520');
            }
            /*
            else if($(trigger).hasClass('typeTwo')) {
                $(overlay).addClass('typeTwo');
            }*/
            $('#ajaxPopupContent',overlay).showLoader();

        },
        onLoad: function() {
            dest  = this.getOverlay();
            url   = this.getTrigger().attr('href');
            $('#ajaxPopupTitle',dest).html(this.getTrigger().attr('title'));
            jQuery.ajax({
                url : url,
                type : 'get',
                dataType   : 'html',
                cache : false,
                success    : function(data, textStatus){
                    $('#ajaxPopupContent',dest).html(data);
                    popHighlight();
                }
            });
        },
        onClose: function() {
            dest  = this.getOverlay();
            $('#ajaxPopupTitle',dest).html('');
            $('#ajaxPopupContent',dest).html('');
            $('.listingAjax').each(function () {
                loadAjax($(this), $(this).data('url'));
            });
        }

    });
}

function loadAjax(element, url) {
    $(element).data('url', url);

    var psearch = '';
    $('.keyword').each(function () {
      if (jQuery.trim($(this).val()) != '') {
        psearch += ( '&' + $(this).attr('id') + '=' + jQuery.trim($(this).val()) );
      }
    });

    //$(element).showLoader();

    $(element).load(url + psearch, function () {
        listingHighlight();
        $(".apopup", $(element)).each(function() {
            aPopoupTriggerOne($(this));
        });
        $(".yesno", $(element)).each(function() {
            yesnoPopupTriggerOne($(this));
        });
    });
}

function popHighlight() {
$('.keyword').each(function () {
  if (jQuery.trim($(this).val()) != '') {
    $('.popup .profile').highlight(($(this).val()));
  }
});
}
function listingHighlight() {
$('.keyword').each(function () {
  if (jQuery.trim($(this).val()) != '') {
    $('.listing-items').highlight(($(this).val()));
  }
});
}

function closeAll() {
    $(".apopup[rel]").each(function() {
        $(this).overlay().close();
    });
}

function yesnoPopupTrigger()
{
    $('.yesno').each(function () {
        yesnoPopupTriggerOne($(this));
    });
}

function yesnoPopupTriggerOne(trigger) {
    var onclick = null;
    if (jQuery.isFunction(onclick = $(trigger).attr('onclick'))) {
        $(trigger).unbind('click', onclick);
        $(trigger).removeAttr('onclick');
        $(trigger).data('cmd', onclick);
    }

    $(trigger).overlay({
        expose: {
            color: '#333',
            loadSpeed: 200,
            opacity: 0.9
        },
        onBeforeLoad: function() {
            var onclick = null;
            url   = this.getTrigger().attr('href');
            if (jQuery.isFunction(onclick = this.getTrigger().data('cmd'))) {
                $("#yesnoUrl").bind('click', onclick);
            }
            title   = this.getTrigger().attr('title');
            $("#yesnoUrl").attr('href', url);
            if(title != '')
                $("#yesnoLabel").html(title);
        }
    }).load();
}

function showLoader() {
  //$('#form-loader').show();
  //overlayAPI.getOverlay().html('<img src="/images/ajax-loader.gif" alt="chargement" />');
  //return true;
}

// extension de jquery
jQuery.fn.extend( {
    showLoader: function() {
        $(this).html('<img class="loader" src="/images/ajax-loader.gif" alt="chargement" />');
    }
});

function alignTitle(){
    if($('#family_bg').length > 0){
        var title_height = $('#family_bg .label').height();
        var family_bg_hight = $('#family_bg').height();
        var title_top_padding = family_bg_hight-title_height;
        
        $('#family_bg .label').css('padding-top', title_top_padding+'px');
        
    }
}

$(document).ready(function() {
  $(".toggle-rel").click(function () {
      if($(this).next().css("display")== 'none'){
        id = $(this).attr("rel");
        $(".actu-content").slideUp("slow");
        $("#"+id).slideToggle("slow");
        //#e1bc55
        $(".toggle-rel p strong").css('color', '#004468');
        $(this).children('p').children('strong').css('color', '#e1bc55');
        
    } else {
        $(this).next().slideUp("slow");
    }
    
  });

  alignTitle();

  if($('select.custom_select').length>0){
    $('select.custom_select').selectmenu({style:'dropdown'});
  }

  
});
