﻿$(function() {
    // check for order cookies
    for (var i = 0; i < 3; i++) {
        var ckie = $.cookie("col" + i);

        if (ckie && ckie != '') {
            var list = ckie.split(',');
            for (var x = 0; x < list.length; x++) {
                $('#' + list[x]).appendTo('#col' + i);
            }
        }
    }
    $('#btnReset').click(function() {
        $('#p_2').appendTo('#col1');
        $('#p_3').appendTo('#col1');
        $('#p_4').appendTo('#col1');

        $('#p_5').appendTo('#col2');
        $('#p_6').appendTo('#col2');
        $('#p_7').appendTo('#col2');
        var i = 0;
        $('.column').each(function() {
            $.cookie("col" + (i++), $(this).sortable('toArray'), { expires: 365 });
        });
        

    });

    $(".column").sortable({
        connectWith: '.column'
    });

    $(".portlet").addClass("ui-widget ui-widget-content ui-helper-clearfix ui-corner-all")
			.find(".portlet-header")
				.addClass("wpTitle ui-corner-all")
				.prepend('<span class="ui-icon ui-icon-openH"></span>')
				.end()
			.find(".portlet-content");

    $(".portlet-header .ui-icon").click(function() {
    $(this).toggleClass("ui-icon-openH").toggleClass("ui-icon-closeH");
        $(this).parents(".portlet:first").find(".portlet-content").toggle();
    });

    $(".column").disableSelection();

    $(".column").bind('sortupdate', function() {
        var i = 0;
        $('.column').each(function() {
            $.cookie("col" + (i++), $(this).sortable('toArray'), { expires: 365 });
        });
    });
});

