//### Cookie class
// http://livepipe.net/projects/prototype_tidbits/
// http://www.lalit.org/lab/jsoncookies
var Cookie = function()
{
    var config = {
        prefix: "",
        path: "/",
        expires: 0
    };


    return {
        set_config: function(val)
        {
            $.extend(config, val);
        },

        set: function(name, val, expires)
        {
            switch (typeof val)
            {
                case "undefined":
                case "function":
                case "unknown":
                    return false;
            }

            // expires...
            var d = new Date();
            d.setTime(d.getTime() + (expires || config.expires) * 1000);

            // set cookie
            document.cookie = config.prefix + name + "=" + escape(val) + "; expires=" + d.toGMTString() + "; path=" + config.path;
        },

        get: function(name)
        {
            var nameEQ = name + "=";

            var cookies = document.cookie.split(";");

            for (var i = 0; i < cookies.length; i++)
            {
                var c = cookies[i];

                while (c.charAt(0) == ' ')
                {
                    c = c.substring(1, c.length);
                }

                if (c.indexOf(nameEQ) == 0)
                {
                    return c.substring(nameEQ.length, c.length);
                }
            }

            return null;
        },

        unset: function(name)
        {
            Cookie.set(name, "", -1);
        }
    };
}();



//### init function
$(function()
{
    // hide sub-menus
    //$(".navleft ul ul").hide();

    // open selected menu
    var menu = Cookie.get("menu");
    if (menu)
    {
        $("#"+menu).show();
    }

    // show hidden elements
    $("#switcher, #print, #quicklinks").show();


    // set selected style
    var cookie = Cookie.get("style");
    if (cookie)
    {
        $("link[rel*=stylesheet][title]").attr("disabled", true);
        $("link[rel*=stylesheet][title="+cookie+"]").attr("disabled", false);
    }


    // sub-menu links
    $(".navleft > ul > li > a").click(function()
    {
        var ul = $(this).next("ul");

        if (!ul.length)
        {
            Cookie.unset("menu");

            return true;
        }

        // save opened menu
        Cookie.set("menu", ul.attr("id"), 365);

        // hide/show - only if there's no link
        if ($(this).attr("href") == "#")
        {
            $(".navleft ul ul").hide();

            ul.toggle();

            return false;
        }

        return true;
    });


    // style switcher links
    $("#switcher a").click(function()
    {
        var a = $(this);
        var style = a.attr("class");

        $("link[rel*=stylesheet][title]").attr("disabled", true);

        switch (style)
        {
            case "default":
            case "back2default":
                Cookie.unset("style");
                break;

            default:
                $("link[rel*=stylesheet][title="+style+"]").attr("disabled", false);
                Cookie.set("style", style, 365);
                break;
        }

        return false;
    });


    // quicklinks
    $("#quicklinks select").change(function()
    {
        var url = $(this).val();

        if (url)
        {
            document.location.href = url;
        }
    });


    // print link
    $("#print a").click(function()
    {
        window.print();

        return false;
    });


    // thumbnails
    $("a.thumb").click(function()
    {
        var thumb = window.open(this.href, "thumb", "width=465,height=500,left=100,top=200,location=no,scrollbars=yes");
        thumb.focus();

        return !thumb;
    });
});
