/********************************************************/
/* controls */

jQuery.fn.splitList = function(cols) {
    var length = this.children("li").length;
    for (var x = 0; x < cols - 1; x++) {
        var newList = this.before("<ul></ul>").prev();
        var colLength = columnLength(x, length, cols);
        newList.append(this.children("li:lt(" + colLength + ")"));
    }

    function columnLength(col, itemCount, cols) {
        var maxLength = Math.ceil(itemCount / cols);
        var extraCols = (itemCount % cols);
        if (extraCols === 0)
            return maxLength;
        if (col <= extraCols)
            return maxLength;
        return maxLength - 1;
    }
};

jQuery.fn.btnClass = function()
{
    $("input[type=button]", this).addClass("btn");
};

jQuery.fn.expandableList = function(show)
{
    return this.each(function()
    {
        var list = $(this);
        if (list.children("li").length > show)
        {
            list.after("<a class=\"expandable\">Show more</a>");
            list.children("li:gt(" + (show - 1) + ")").css("display", "none");
            list.next("a.expandable").click(function()
            {
                list.children("li").css("display", "");
                $(this).css("display", "none");
            });
        }
    });
};

jQuery.fn.watermark = function()
{
    this.click(function()
    {
        if (this.value == this.title)
            this.value = "";
    });
    this.blur(function()
    {
        if (this.value === "")
        {
            this.value = this.title;
        }
    });

    return this;
};

/********************************************************/
/* DOM extension methods */

jQuery.fn.extend({
    borderHeight: function()
    {
        var height = 0;
        height += parseInt(borderTop, 10);
        height += parseInt(this.css("border-bottom-width"), 10);
        height += parseInt(this.css("padding-top"), 10);
        height += parseInt(this.css("padding-bottom"), 10);
        return height;
    }
});

jQuery.fn.value = function()
{
    return this.attr("value") != this.attr("title") ?
        this.attr("value") : "";
};

/********************************************************/
/* serializing and deserializing things */

jQuery.fn.extend({
    serializeChildren: function(selector, options)
    {
        var settings = jQuery.extend({
            attributes: []
        }, options);

        var elements = {};
        this.children(selector).each(function()
        {
            var parentId = $(this).parent().attr("id");
            if (!elements[parentId])
                elements[parentId] = [];

            var child = { id: this.id };
            if (settings.attributes)
            {
                for (var x = 0; x < settings.attributes.length; x++)
                {
                    var attribute = settings.attributes[x];
                    child[attribute] = $(this).attr(attribute);
                }
            }

            elements[parentId].push(child);
        });
        return $.toJSON(elements);
    },
    restoreChildren: function(serialized, options)
    {
        if (!serialized) return;
        var settings = jQuery.extend({
            itemRestored: null
        }, options);

        var elements = $.parseJSON(serialized);
        for (var parent in elements)
        {
            var children = elements[parent];
            for (var x = 0; x < children.length; x++)
            {
                var child = children[x];
                var element = $("#" + child.id);
                element.appendTo("#" + parent);
                for (var attribute in child)
                    element.attr(attribute, child[attribute]);

                if (settings.itemRestored)
                    settings.itemRestored(element);
            }
        }
    }
});

jQuery.cookieJSON = function(name, value, options)
{
    if (typeof (value) != "undefined")
    {
        jQuery.cookie(name, $.toJSON(value), options);
    }
    else
    {
        value = jQuery.cookie(name);
        if (value)
            return $.parseJSON(value);
        return undefined;
    }
};
