$(document).ready(function() {

    //highlight active menuitem
    var path = location.pathname.substring(1);
    if (path) {
        $('#nav a[href$="' + path + '"]').parent('li').attr('class', 'active');
    }

    //$('select.beautify').selectmenu({width: 210,maxHeight: 400, style: 'dropdwon'});
    $('a[rel*=facebox]').facebox();
    $('button').hover(function() {
       $(this).addClass('widget_hover');
    }, function() {
        $(this).removeClass('widget_hover');
    });
    $("input").hint();

    $("#run_search").keyup(function() {
        var s = $(this).val();
        if(s.length > 1) {
            $.ajax({
                type: "POST",
                url: "/search/ajax",
                data: "search="+s,
                success: function(response){
                    $('#search_result').remove();
                    $('#container').prepend('<div id="search_result">'+response+'</div>');
                    $('#search_result ul li:even').addClass('even');
                }
            });
        } else if (s.length == 0) {
            $('#search_result').remove();
        }
    });

    $('.confirm').click(function(e) {
        return confirm($(this).attr('title'));
        
        $("#dialog").html($(this).attr('title')).dialog({
            title: 'Bekräfta',
            bgiframe: true,
            modal: true,
            width: 300,
            buttons: {
                'Ok': function() {
                    window.location.href = $(this).attr('href');
                },
                'Avbryt': function() {
                    $(this).dialog('close');
                }
            }
        });
    });

    $('.refresh').click(function() {
        window.location.reload();
    });

});

//a custom format option callback for beautify selectbox
var selectFormatting = function(text){
    var newText = text;
    //array of find replaces
    var findreps = [
        {find:/^([^\-]+) \- /g, rep: '<span class="ui-selectmenu-item-header">$1</span>'},
        {find:/([^\|><]+) \| /g, rep: '<span class="ui-selectmenu-item-content">$1</span>'},
        {find:/([^\|><\(\)]+) (\()/g, rep: '<span class="ui-selectmenu-item-content">$1</span>$2'},
        {find:/([^\|><\(\)]+)$/g, rep: '<span class="ui-selectmenu-item-content">$1</span>'},
        {find:/(\([^\|><]+\))$/g, rep: '<span class="ui-selectmenu-item-footer">$1</span>'}
    ];

    for(var i in findreps){
        newText = newText.replace(findreps[i].find, findreps[i].rep);
    }
    return newText;
}


