/*globals $:false, document:false, gettext:false */

(function($, undefined) {
    document.title = document.title.replace(/HalmaStar/, "Hᴀʟᴍᴀ✶");
    var oRe = /<script\b[^>]*>([\s\S]*?)<\/script>/gm;
    
    function tabify() {
        $('.tabs').each(function() {
            var sId = $(this).attr('id');
            var iSelected = sId?
                parseInt($.cookie('selected_for_' + sId), 10) || 0: 0;
            
            var $tabs = $(this).tabs({
               selected: iSelected
            }).bind('tabsselect', function(event, ui) {
                if (sId) {
                    $.cookie('selected_for_' + sId, ui.index);
                }
            });
        });
    }
    
    function remove_action_hash() {
        $('form').each(function() {
            var sAction = $(this).attr('action');
            sAction = sAction.replace(/#.*$/, "");
            $(this).attr('action', sAction);
        })
    }
    
    $(document).ready(function() {
        tabify();
        remove_action_hash();
        $('a.popup').live('click', function() {
            var sTitle = $(this).attr('title');
            $('<div>', {'id': 'dialog_popup'}).dialog({
                autoOpen: true,
                title: sTitle,
                width: 700,
                modal: true,
                position: ['center', 30],
                close: function(event, ui) {
                    $('#dialog_popup').remove();
                }
            }).load($(this).attr('href') + ' #content>*', function(responseText, textStatus, XMLHttpRequest) {
                tabify();
                remove_action_hash();
                var sScripts = '';
                responseText.replace(
                    oRe,
                    function($0, $1) {
                        sScripts += $1;
                        return $0;
                    }
                );
                eval(sScripts);
            });
            return false;
        });
    });
}(jQuery));

