var Promobox = {
    t : null,
    datafeed : '/internal-apis/site/promobox/spinner.xml',
    data : [],
    lastitem : 0,
    item_focus : 0,
    load : function (spinnerContainerId, datafeed) {
        Promobox.datafeed = datafeed;
        if (Promobox.buildPlayer(spinnerContainerId)) {
            $.get(Promobox.datafeed, function (xmlDoc) {
                // var xmlDoc = req.responseXML.documentElement;
                var totcnt = xmlDoc.getElementsByTagName("product").length;
                var pspages_html = "";
                for (var x = 0; x < totcnt; x = x+1) {
                    var product = xmlDoc.getElementsByTagName("product")[x];
                    Promobox.data[x] = {};
                    Promobox.data[x].objecturl = product.getElementsByTagName("objecturl")[0].firstChild.nodeValue;
                    Promobox.data[x].splashscreen = product.getElementsByTagName("splashscreen")[0].firstChild.nodeValue;
                    Promobox.data[x].previewpic = product.getElementsByTagName("previewpic")[0].firstChild.nodeValue;
                    Promobox.data[x].caption = product.getElementsByTagName("caption")[0].firstChild.nodeValue;
                    Promobox.data[x].infolink = product.getElementsByTagName("infolink")[0].firstChild.nodeValue;
                    Promobox.data[x].buylink = product.getElementsByTagName("buylink")[0].firstChild.nodeValue;
                    Promobox.data[x].price = product.getElementsByTagName("price")[0].firstChild.nodeValue;
                    Promobox.data[x].prev_price = product.getElementsByTagName("prev_price")[0].firstChild.nodeValue;
                    Promobox.data[x].splash_type = product.getElementsByTagName("splash_type")[0].firstChild.nodeValue;
                    Promobox.data[x].splash_text = product.getElementsByTagName("splash_text")[0].firstChild.nodeValue;
                    Promobox.lastitem = x;
                    
                    pspages_html += '<a href="javascript: Promobox.loadItem('+x+')" class="pspage'+x+'" title="'+Promobox.data[x].caption+'"></a>';
                }
                $("#promospinner_pages").html(pspages_html);
                Promobox.loadItem(Promobox.item_focus);
            });
        }
    },
    buildPlayer : function (spinnerContainerId) {
        var el = $(spinnerContainerId);
        if (el) {
            if ($("#promobox").css('display') == 'none') $("#promobox").slideDown();
            var html = "";
            html += '<div id="promospinner_window"><div></div></div>';
            html += '<div id="promospinner_pages"></div>';
            html += '<div id="promospinner_info">';
                html += '<div id="ps_info_left"></div>';
                html += '<div id="ps_info_right"></div>';
            html += '</div>';
            el.html(html);
            return true;
        }
        return false;
    },
    loadItem : function(id) {
        clearTimeout(Promobox.t);
        if (id > Promobox.lastitem) {
            id = 0;
        }
        Promobox.item_focus = id;
        var win = $("#promospinner_window div");
        win.css({'display' : 'none'});
        win.html('<a href="'+Promobox.data[id].infolink+'"><img src="/spinnerdata/'+Promobox.data[id].splashscreen+'" border="0" width="518" height="212"></a>');
        win.fadeIn('slow');
        
        var left = $("#ps_info_left");
        left.html('<h1>'+Promobox.data[id].caption+'</h1>');
        if (Promobox.data[id].price) left.append('<span class="prtag">'+Promobox.data[id].price+'</span>');
        
        var right = $("#ps_info_right");
        right.html('<a href="'+Promobox.data[id].infolink+'" class="btn small">'+r('More info')+'</a>&nbsp;<a href="'+Promobox.data[id].buylink+'" class="btn narrow">'+r('Buy')+'</a>');
        
        
        $("#promospinner_pages a").each(function() {
            $(this).removeClass("active");
        });
        $(".pspage"+id).addClass("active");
        
        var next = Promobox.item_focus+1;
        Promobox.t = setTimeout(function() {
            Promobox.loadItem(next);
        }, 5000);
        
    },
    loadOffersbox : function(query_string) {
        if (typeof query_string == 'undefined') var query_string = '';
        if ($('#offersbox')) {
            var url = '/internal-apis/site/promobox/offersbox' + query_string;
            $.get(url, function(resp) {
                var box = $('#offersbox');
                box.css({'display' : 'none'});
                box.html(resp);
                box.fadeIn('slow');
            });
        }
    }
}
