﻿// Basket
var basket = {
    url: '/web/basket.aspx',
    urlprint: { 'it-IT': '/servizi/basket.html', 'en-US': '/services/basket.html' },
    divbasket: '#basket ',
    divlist: '.list ',
    contentids: [],
    onstage: false,
    close: function () {
        $(this.divbasket).remove();
        this.onstage = false;
    },
    reload: function () {
        if (this.onstage) {
            for (var j = this.contentids.length - 1; j >= 0; j--) { $(this.divbasket + ' #basketcnt' + this.contentids[j]).remove(); }
            this.contentids = [];
            this.load();
        }
    },
    dump: function () {
        for (var j = this.contentids.length - 1; j >= 0; j--) {
            this.remove(this.contentids[j]);
        }
    },
    add: function (contentid) {
        var sessionid = $.cookie("s") || '';
        $.getJSON(this.url + "?action=a&output=JSON&contentid=" + contentid + "&sessionid=" + sessionid + "&r=" + Math.random(),
			function (data) {
			    flash.show('notice', lang[idlang].basketpagina + ' "' + data.Table[0].t + '" ' + lang[idlang].basketaggiunta);
			    $.cookie("s", data.Table[0].s);
			    basket.contentids.push(data.Table[0].id);
			});
    },
    remove: function (contentid) {
        var sessionid = $.cookie("s") || '';
        $.getJSON(this.url + "?action=d&output=JSON&contentid=" + contentid + "&sessionid=" + sessionid + "&r=" + Math.random(),
			function (data) {
			    $(this.divbasket + '#basketcnt' + data.Table[0].id).remove();
			    basket.reload();
			});
    },
    print: function (contentid) {
        wndprn = window.open(this.urlprint[idlang] + "?id=" + contentid, "print" + contentid, "");
        wndprn.focus();
    },
    printAll: function () {
        wndprn = window.open(this.urlprint[idlang] + "?id=" + this.contentids.join(','), "printAll");
        wndprn.focus();
    },
    list: function (output, url) {
        if (!this.onstage) {
            this.urlprint[idlang] = url;
            this.load();
        }
    },
    load: function () {
        var sessionid = $.cookie("s") || '';
        $.getJSON(this.url + "?action=a&output=JSON&contentid=&sessionid=" + sessionid + "&r=" + Math.random(),
			function (data) {
			    basket.show();
			    var str = [];
			    for (var i = 0; i < data.Table.length; i++) {
			        if (data.Table[i].id) {
			            basket.contentids.push(data.Table[i].id);
			            str.push('<li id="basketcnt' + data.Table[i].id + '"><a href="' + data.Table[i].u + '" target="_blank">' + data.Table[i].t + '</a> <span class="right"><a href="#" onclick="basket.print(' + data.Table[0].id + ');return false;">' + lang[idlang].stampa + '</a>|<a href="#" onclick="basket.remove(' + data.Table[i].id + ');return false;">' + lang[idlang].rimuovi + '</a></span></li>');
			        }
			    };
			    if (str.length > 0) {
			        $(basket.divbasket + ' ' + basket.divlist).html(str.join('\n'));
			        $(basket.divbasket + ' .full').show();
			        $(basket.divbasket + ' .empty').hide();
			        $(basket.divbasket).show();
			    }
			    else {
			        $(basket.divbasket + ' .full').hide();
			        $(basket.divbasket + ' .empty').show();
			        $(basket.divbasket).show();
			    }

			});
    },
    show: function () {
        // box basket.
        if (!this.onstage) {
            var htmlbox = '<div style="display:none;" id="basket">' +
        '<div class="left-column-top">&#160;</div>' +
        '<div class="text">' +
            '<a title="' + lang[idlang].chiudi + '" href="#" onclick="basket.close();" class="right close">' + lang[idlang].chiudi + '</a>' +
            '<h2>' + lang[idlang].cartellastampa + '</h2>' +
            '<div class="full"><p><b>' + lang[idlang].selezionaoperazione + '</b></p>' +
            '<ul class="tools">' +
             '<li><a href="#" onclick="basket.printAll();return false;">' + lang[idlang].stampacartella + '</a></li>' +
                '<li><a href="#" onclick="basket.reload();return false;">' + lang[idlang].ricaricacartella + '</a></li>' +
                '<li><a href="#" onclick="basket.dump();return false;">' + lang[idlang].svuotacartella + '</a></li>' +
            '</ul>' +
            '<p><b>' + lang[idlang].introcartella + '</b></p>' +
            '<ul class="list"></ul></div><div class="empty"><p>' + lang[idlang].cartellavuota + '</p></div>' +
            '<div class="clear"></div>' +
        '</div>' +
        '<div class="left-column-bottom">&nbsp;</div>' +
        '</div>';
            $('body').append(htmlbox);
            this.onstage = true;
        }
    }
};

