$(function () {
	
	


	

$( ".widgetcontainer" ).draggable({ delay: 100 });

        $(".widgetcontainer").draggable({
                stop: function (event, ui) {
					
                        var currentPos = $(this).position();
                        $.cookie('positiont' + $(this).index(), currentPos.top, { path: '/' });
                        $.cookie('positionl' + $(this).index(), currentPos.left, { path: '/' });
						 $(this).css({
                                position: 'absolute'
                        });
						  $(this).css({
                        top: $.cookie('positiont' + $(this).index()) + 'px'
                });
                $(this).css({
                        left: $.cookie('positionl' + $(this).index()) + 'px'
                });
                }
				 
        });





        $(".widgetcontainer").each(function () {
                if ($.cookie('positiont' + $(this).index()) != null) {
                        $(this).css({
                                position: 'absolute'
                        });
                }
                $(this).css({
                        top: $.cookie('positiont' + $(this).index()) + 'px'
                });
                $(this).css({
                        left: $.cookie('positionl' + $(this).index()) + 'px'
                });
        });

        $(".boxes").each(function () {



                var mydiv = $(this).attr('id');


                var mycookie = mydiv.replace(/box/g, 'boxhead');


                if ($.cookie(mycookie) == "close") {


                        if ($('#' + mydiv).hasClass("close")) {
                                $('#' + mycookie).removeClass("highlight");
                                $('#' + mydiv).removeClass("close");
                        } else {
                                $('#' + mycookie).addClass("highlight");
                                $('#' + mydiv).addClass("close");
                        }
                }





        });



        $('.box_head').click(function () {
                idelem = $(this).attr('id');
                idelemdiv = idelem.replace('boxhead_', 'box_');
                $('#' + idelemdiv).slideToggle("normal");
                $(this).toggleClass("highlight");


                if ($.cookie(($(this).attr('id'))) == null) {
                        $.cookie(($(this).attr('id')), "close",{ path: '/' });
                } else if ($.cookie(($(this).attr('id'))) == "open") {
                        $.cookie(($(this).attr('id')), "close",{ path: '/' });
                } else {
                        $.cookie(($(this).attr('id')), "open",{ path: '/' });
                }




        });


        $('.box_head').hover(

        function () {
                $(this).css('cursor', 'pointer');
        }, function () {
                $(this).css('cursor', 'default');
        });


        $('#reset').click(function () {

                function get_cookies_array() {

                        var cookies = {};

                        if (document.cookie && document.cookie != '') {

                                var split = document.cookie.split(';');

                                for (var i = 0; i < split.length; i++) {

                                        var name_value = split[i].split("=");

                                        name_value[0] = name_value[0].replace(/^ /, '');

                                        cookies[decodeURIComponent(name_value[0])] = decodeURIComponent(name_value[1]);

                                }

                        }



                        return cookies;

                }

                var cookies = get_cookies_array();
                for (var name in cookies) {

                        $.cookie(name, null,{ path: '/' });

                };
                location.reload();

        });



});
