/*globals $ */
var reeltoys;
if (!reeltoys) {
    reeltoys = {};
}

reeltoys.Index = function () {
    this.previewList = null;
    this.prIdx = -1;
};

reeltoys.Index.prototype.init = function () {
    //Imposta i dati del prodotto
    if (this.previewList.length > 0) {
        this.changeProduct();
    }
};

reeltoys.Index.prototype.changeProduct = function () {
    var tmfunc,
        self = this;

    if (this.prIdx >= (this.previewList.length - 1)) {
        this.prIdx = 0;
    } else {
        this.prIdx = this.prIdx + 1;
    }
    this.fillProduct();
    tmfunc = function() {self.changeProduct();};
    setTimeout(tmfunc, 5000);
};

reeltoys.Index.prototype.fillProduct = function () {
    var sel,
        self = this;

    sel = "#bh_cod, #bh_tit, #bh_img, #bh_dsc";
    $(sel).fadeOut("slow", function () {
        var pr;

        pr = self.previewList[self.prIdx];
        //Codice, nome, categoria
	    $("#bh_cod").html("Codice " + pr.cod);
        $("#bh_tit").html(pr.name);
        $("#bh_img").css("background-image", "url(" + pr.img_url + ")");
        $("#bh_dsc").html(pr.descr);
        $("#bh_container").unbind("click")
            .bind("click", { prod: pr }, function (event) {
                location.href = event.data.prod.link_url;
            });
        $(sel).fadeIn("slow");
    });
};
