$(document).ready(function() {
    dbClickOnDoubleList();
    
    $(".sf_admin_list_td_logo").each(function(i){
        var logo = $.trim( $(this).html() );
        if ($.trim(logo))
        {
            $(this).parent().attr('rel', "/uploads/logo/"+logo);
            $(this).parent().addClass("screenshot");
        }
        $(this).remove();
        $('.sf_admin_list_th_logo').remove();
    });


    $(".add_elem_to_list").die();
    $(".add_elem_to_list").live('click', function()
    {
        var id = $(this).attr("id").split('-')[1];
        var stringToRegExpOn = $("#"+id).attr("name");
        var RegExpPattern =new RegExp("^([^\[.]*)([\[])([a-z_\-]*)(\])(.*)","gi");
        if(RegExpPattern.exec(stringToRegExpOn)) {
            $.post(UTILS_NEW_FORM+'?model='+RegExp.$3)
        }
    });

    /* Panel for filter */
    var $a = $('<a href="#" id="trigger2" class="trigger right">Filter</a>');
    $(".sf_admin_list").css('padding-right', '0px');
    $('.sf_admin_filter').addClass('panel').addClass('right');
    $a.prependTo($('#sf_admin_bar'));
    $('.sf_admin_filter').slidePanel({
        triggerName: '#trigger2',
        triggerTopPos: '60px',
        panelOpacity:       1,
        panelTopPos: '60px'
    });

    /* Stats Promo*/
    $('.plus_stats').click(function(){
        $(this).parent().find('ul').show();
        $(this).hide();
        $(this).parent().find('.moins_stats').show();
    })
    $('.moins_stats').click(function(){
        $(this).parent().find('ul').hide();
        $(this).hide();
        $(this).parent().find('.plus_stats').show();
    })
});

function dbClickOnDoubleList()
{
    $(".double_list_select").each(function(i){
        var id = $(this).attr("id");
        $('#'+id+'> option').live("dblclick", function(){
            sfDoubleList.move(id, id.substr(13) );
            return false;
        });
    });

    $(".double_list_select-selected").each(function(i){
        var id = $(this).attr("id");
        $('#'+id+'> option').live("dblclick", function(){
            sfDoubleList.move(id, 'unassociated_'+id );
            return false;
        });
    });
}

function in_array (needle, haystack, argStrict) {
    // Checks if the given value exists in the array
    //
    // version: 911.718
    // discuss at: http://phpjs.org/functions/in_array
    // +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   improved by: vlado houba
    // +   input by: Billy
    // +   bugfixed by: Brett Zamir (http://brett-zamir.me)
    // *     example 1: in_array('van', ['Kevin', 'van', 'Zonneveld']);
    // *     returns 1: true
    // *     example 2: in_array('vlado', {0: 'Kevin', vlado: 'van', 1: 'Zonneveld'});
    // *     returns 2: false
    // *     example 3: in_array(1, ['1', '2', '3']);
    // *     returns 3: true
    // *     example 3: in_array(1, ['1', '2', '3'], false);
    // *     returns 3: true
    // *     example 4: in_array(1, ['1', '2', '3'], true);
    // *     returns 4: false
    var key = '', strict = !!argStrict;

    if (strict) {
        for (key in haystack) {
            if (haystack[key] === needle) {
                return true;
            }
        }
    } else {
        for (key in haystack) {
            if (haystack[key] == needle) {
                return true;
            }
        }
    }

    return false;
}
