/**
 * @author Krzysztof Kotowicz koto at webworkers dot pl
 * @package b2boss
 *
 * Copyright (c) 2007, Krzysztof Kotowicz
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
jQuery.fn.slideMenu = function(opt) {
    jQuery('h3 a', this).click(function() {
        jQuery(this).parent().next().slideToggle('normal');
        return false;
    });

    jQuery('ul.navlist', this).not(':has(#current)').hide();

    if (jQuery('#current').size() == 0)
        jQuery('h3 a:first', this).parent().next().show();

    jQuery('h3 a#current', this).click();

};

jQuery.loadOnce = {
    css : function() {
        jQuery.each(arguments, function(i,file) {

            if (jQuery.inArray(file, jQuery.loadOnce.cssloaded) != -1) {
                return;
            } else {
                if (document.createStyleSheet) {
                    document.createStyleSheet(file);
                } else {
                    jQuery("<link>")
                        .attr({type:"text/css", rel:"stylesheet", href: file})
                        .appendTo('head');
                }
                jQuery.loadOnce.cssloaded[jQuery.loadOnce.cssloaded.length] = file;
            }
        });
    },

    cssloaded : [],

    js : function(files, cb) {
        if (typeof(files) !="object" || !(files.length))
            files = new Array(files);
        jQuery.each(files, function(i,file) {
            if (jQuery.inArray(file, jQuery.loadOnce.jsloaded) != -1) {
                return;
            } else {
                jQuery.getScript(file, cb);
                /*
                jQuery("<script>")
                      .attr({type:"text/javascript", src:file})
                      .appendTo("head");*/
                jQuery.loadOnce.jsloaded[jQuery.loadOnce.jsloaded.length] = file;
            }
        });
    },

    jsloaded: []
};

jQuery.fn.remember = function(def) {
    var $ = jQuery;
    return this.each(function() {
        var name,cookie;
        if ($(this).is(':input')) {
            //if this item has been cookied, restore it
            name = $(this).attr('name');
            cookie = $.cookie(name);
            if((def === undefined || $(this).val() == def) && cookie) {
                $(this).val(cookie);
            }

            //assign a change function to the item to cookie it
            $(this).bind('change.remember', function() {
                $.cookie(name, $(this).val());
            });
        }
    });
};

jQuery(function() {
    jQuery('.navleft').slideMenu();

    // enable options remembering for package elements
    jQuery(':input[name^=element_options]').remember(-1);
});