
function paginate(url) {
    $("#body_content").slSpinner();
    $.post(url ,
    function(data){
        var node = jQuery(data);
        $("#body_content").slSpinner('destroy');
        $("#body_content").replaceWith(node);
        onBodyContentReload();
   }, "html");
}

function refreshSelectionWidget(){

    var containerId = $("#float_box").parent("div").get(0).id;

    //-- refresh order cd button
    Redarea.Core.Widget.Loader.queue('album', 'cdprint', '{"container":"cd_order_widget"}', 'cd_print_widget', highlightCdPrintWidget);

    Redarea.Core.Widget.Loader.queue('album', 'selection', '{"container":"'+ containerId +'"}', containerId, enableSelectionLinks);
}

function refreshBreadcrumbWidget(albumId){

    var containerId = $(".breadcrumb").attr('id');

    Redarea.Core.Widget.Loader.queue('core', 'breadcrumb', '{"container":"'+ containerId +'", "album":"' +albumId + '"}', containerId);
}

function refreshAccountWidget(){

    var containerId = $("#login_toolbar").attr('id');

    Redarea.Core.Widget.Loader.queue('core', 'login', '{"container":"'+ containerId +'"}', containerId);
}

function highlightCdPrintWidget(){
    //-- highlight order cd button
    $("#cd_print_widget").effect("highlight", {}, 2000);
}

function enableSelectionLinks() {
    //-- per verificare se il float_box e visibile o no
    $(window).trigger("scroll");

    if ($("#box_selection").children().length != 0) {
        $(".deleted_selected").removeClass('disabled');
        $(".move_selected").removeClass('disabled');
        $("#show_selection_slide").removeClass('disabled');
    }
    else {
        $(".deleted_selected").addClass('disabled');
        $(".move_selected").addClass('disabled');
        $("#show_selection_slide").addClass('disabled');
    }
}


$(document).ready(function() {


    //-- widget login
    $('#btn_login_form').click(function() {
        $('#btn_login_form').parents("form").submit();
        return false;
    });

    //-- auth deny login
    $('#btn_login_form_box').click(function() {
        $('#btn_login_form_box').parents("form").submit();
        return false;
    });

    $("#login_error").overlay({
        mask: {
            color: '#000',
            opacity: 0.5
        }
    });

    $(".btn_forgot_pwd").overlay({
        mask: {
            color: '#000',
            loadSpeed: 200,
            opacity: 0.5
        },
        closeOnClick: false,
        onClose:function(){
            $("#auth_forgot_msg").html("");
            $("#forgot_pwd_form").show();
        }
        //load: true
    });

    $("#forgot_pwd_form").validate({
        submitHandler: function(form) {
            sendPwd();
        },
        rules: {
            email:"required"
        },
        messages: {
            email:$('#email').attr("error")
        }
    });

    $("#mail_operatore").overlay({
        mask: {
            color: '#000',
            opacity: 0.5
        }
    });

    setInterval(function(){
       $.get('/keep-alive');
    }, 840000); //14 min


    $(".popup_options").live('click',function () {

            $(this).overlay({
                mask: {
                    color: '#000',
                    loadSpeed: 200,
                    opacity: 0.5
                },

                closeOnClick: false,
                top: 'center',
                left: 'center',

                onLoad: function() {
                    var url = this.getTrigger().attr("href");

                    //-- viene chiuso alla fine del document ready del popup
                    $("#popup_chage_article").slSpinner();

                    $.post(url ,
                          function(data){
                              $('#popup_chage_article').html(data);
                          }, "html");
                },
                onClose: function() {
                    $('#popup_chage_article').html("");
                    //--LUX: commemtato perche gestito solo se premuto salva
                    //reloadMainPage()
                },
                load: true
            });
            return false;

    });

    if( GetCookie("PROVENIENZA") == null && document.referrer!=""  ){

        var today = new Date();
        var expire = new Date();
        expire.setTime(today.getTime() + 3600000*24*60);

        SetCookie('PROVENIENZA', document.referrer, expire, '/', '', '');
    }

});



function login(form, action){

    $("#login_form").addClass("logging");
    //-- auth deny
    $("#btn_login_form_box").addClass("disabled");

    $.post(
        action, //form.attr("action"),
        $(form).serialize(),
        function(data){

            $("#login_form").removeClass("logging");
            //-- auth deny
            $("#btn_login_form_box").removeClass("disabled");

            if(data["code"]==1){

                document.location.href = data["url"];

            }else if(data["code"]==2 || data["code"]==-9 || data["code"]==3){

                document.location.href = data["url"];

            }else{
                $("#btn_login_form").children("span").text( $("#btn_login_form").attr("rel") )
                //-- auth deny
                $("#btn_login_form_box").children("span").text( $("#btn_login_form").attr("rel") )
                //-- display error
                $("#login_error_message").html( data["message"] );
                $("#login_error").data("overlay").load();
            }

       }, "json");
    
    return false;
}

 function sendPwd(){

    $.post(
        $('#forgot_pwd_form').attr("action"),
        {
            'email': $('#email').attr("value")
        },
        function(data){

            $("#auth_forgot_msg").html( data["message"] );
            if( data["code"] == 1 ){
                $("#forgot_pwd_form").hide();
            }

        }, "json");

    return false;

}

//-- COOKIES
function SetCookie (name,value,expires,path,domain,secure) {
  document.cookie = name + "=" + encodeURI(value) +
    ((expires) ? "; expires=" + expires.toGMTString() : "") +
    ((path) ? "; path=" + path : "") +
    ((domain) ? "; domain=" + domain : "") +
    ((secure) ? "; secure" : "");
}

function GetCookie(name) {
    var dc = document.cookie;
    var prefix = name + "=";
    var begin = dc.indexOf("; " + prefix);

    if (begin == -1) {
        begin = dc.indexOf(prefix);
        if (begin != 0) return null;
    } else {
        begin += 2;
    }
    var end = document.cookie.indexOf(";", begin);
    if (end == -1) {
        end = dc.length;
    }
    return decodeURI(dc.substring(begin + prefix.length, end));
}
