$(document).ready(function () {
    dropboxCookie();
    toogleCookie();
});

function dropboxCookie(){
    // verifica se cookies existem
    if ($.cookie('coluna1_2') != null) {
        //alert($.cookie('coluna1'));
        // formata string do cookie
        var coluna1 = $.cookie('coluna1_2').replace(/drop-esquerda\[\]=/g, '');
        var coluna1 = coluna1.split('&');
        //alert(coluna1);
        var div_id = '';
        for (var x = 0; x <= coluna1.length; x++) {
            div_id = coluna1[x];
            $('#drop-esquerda').append($('#'+div_id));
        }
    }
    if ($.cookie('coluna2_2') != null) {
        var coluna2 = $.cookie('coluna2_2').replace(/drop-centro\[\]=/g, '');
        var coluna2 = coluna2.split('&');
        //alert(coluna2);
        var div_id = '';
        for (var x = 0; x <= coluna2.length; x++) {
            div_id = coluna2[x];
            $('#drop-centro').append($('#'+div_id));
        }
    }
    if ($.cookie('coluna3_2') != null) {
        var coluna3 = $.cookie('coluna3_2').replace(/drop-direita\[\]=/g, '');
        var coluna3 = coluna3.split('&');
        //alert(coluna3);
        var div_id = '';
        for (var x = 0; x <= coluna3.length; x++) {
            div_id = coluna3[x];
            $('#drop-direita').append($('#'+div_id));
        }
    }

    $('div.recebeDrag').Sortable({
        accept           : 'itemDrag',
        helperclass      : 'dragAjuda',
        activeclass      : 'dragAtivo',
        hoverclass       : 'dragHover',
        handle           : 'h2',
        opacity          : 0.7,
        onChange         : function(){
            serialEsq = $.SortSerialize('drop-esquerda');
            serialCen = $.SortSerialize('drop-centro');
            serialDir = $.SortSerialize('drop-direita');
            $('#ser-e').val(serialEsq.hash);
            $('#ser-c').val(serialCen.hash);
            $('#ser-d').val(serialDir.hash);
            // salva cookie com posicionamento
            $.cookie('coluna1_2', serialEsq.hash, {expires: 7});
            $.cookie('coluna2_2', serialCen.hash, {expires: 7});
            $.cookie('coluna3_2', serialDir.hash, {expires: 7});
            //alert($.cookie('coluna1_2'));
        },
        onStart : function(){ $.iAutoscroller.start(this, document.getElementsByTagName('body')); },
        onStop : function() { $.iAutoscroller.stop(); }
    });
}

function myToogle(){
    $(".toggle_container").hide();
    $("h2.trigger").toggle(function(){
        $(this).addClass("activea");
        $(this).addClass("active");
    }, function () {
        $(this).removeClass("active");
        $(this).removeClass("activea");
    });
    $("h2.trigger").click( function(){ $(this).next(".toggle_container").slideToggle("slow,"); });
}
function toogleCookie(){
    $("li.widget h2").addClass("active");
    var i = $('li.widget h2');
    var l = i.length;
    var panel_2 = $("li.widget ul");
    for (c=0;c<=l;c++){
        //alert(i.eq(c).attr('id'))
        //var cvalue = $.cookie('panel_2' + panel_2);
        var cvalue = $.cookie('panel_2' + i.eq(c).attr('id'));
        if ( cvalue == 'closed' + i.eq(c).attr('id') ) {
            $(panel_2).eq(c).css({display:"none"});
            $(panel_2).eq(c).prev().removeClass('active').addClass('inactive');
            $('div', i.eq(c)).removeClass('t');
            $('div', i.eq(c)).addClass('t-active');
       };
    };
    $("li.widget h2.active").toggle(
          function () {
                //var num = $("li.widget h2").index(this);
                var num = $(this).attr('id');
                var cookieName = 'panel_2' + num;
                var cookieValue = 'closed' + num;
                $(this).next("ul").slideUp(500);
                $(this).removeClass('active');
                $('div', this).removeClass('t');
                $('div', this).addClass('t-active');
                $.cookie(cookieName, cookieValue, { path: '/', expires: 10 });
          },
          function () {
                var num = $(this).attr('id');
                //var num = $("li.widget h2").index(this);
                var cookieName = 'panel_2' + num;
                $(this).next("ul").slideDown(500);
                $(this).addClass("active");
                $('div', this).removeClass('t-active');
                $('div', this).addClass('t');
                $.cookie(cookieName, null, { path: '/', expires: 10 });
          }
        );
    $("li.widget h2.inactive").toggle(
          function () {
                //var num = $("li.widget h2").index(this);
                var num = $(this).attr('id');
                var cookieName = 'panel_2' + num;
                $(this).next("ul").slideDown(500);
                $(this).addClass("active");
                $(this).removeClass('inactive');
                $('div', this).removeClass('t-active');
                $('div', this).addClass('t');
                $.cookie(cookieName, null, { path: '/', expires: 10 });

          },
          function () {
                var num = $(this).attr('id');
                //var num = $("li.widget h2").index(this);
                var cookieName = 'panel_2' + num;
                var cookieValue = 'closed' + num;
                $(this).next("ul").slideUp(500);
                $(this).removeClass('active');
                $('div', this).removeClass('t');
                $('div', this).addClass('t-active');
                $.cookie(cookieName, cookieValue, { path: '/', expires: 10 });
          }
        );
}
