var PromoCarousel = {
    slideWidth: 586,
    currentOffset: 0,
    create: function(path, options) {

        var i, id, nav, elem, that=this;
        this.currentSlide = 0;
        this.currentOffset = 0;

        if(dojo.query(path + ' .promos').length === 0) {
            return;
        }

        this.promosContainer = dojo.query(path + ' .promos')[0];
        this.promoElems = dojo.query(path + ' .promo');
        this.prev = dojo.query(path + ' .prev')[0];
        this.next = dojo.query(path + ' .next')[0];

        // Holds promo objects, indexed by id
        this.promos = {};

        // Holds an index to the promo in each slot, indexed by slot
        this.promoLocations = [];

        this.numPromos = this.promoElems.length;
        this.promoButtonElems = dojo.query(path + ' .button');
        this.promoButtons = {};

        if(dojo.isIE) {
            nav = dojo.query('.featured-carousel .navigation ul')[0];
            dojo.style(nav, "display", "none");
        }

        for(i=0; i < this.promoElems.length; i++) {
            id = i;
            this.promos[id] = new pg.Promo({
                elem: this.promoElems[i],
                id: id,
                active: (i === 0)
            });
            this.promoLocations[i] = id;
        }

        for(i=0; i<this.promoButtonElems.length; i++) {
            this.promoButtons[i] = new pg.PromoButton({
                id: i,
                elem: this.promoButtonElems[i]
            });
        }

        dojo.connect(this.next, "onclick", this, "onClickNext");
        dojo.connect(this.prev, "onclick", this, "onClickPrev");
        dojo.subscribe("promoRequested", function(m) {
            that.goToSlide(m.id);
        });
        this.setCurrentSlide(0);
    },
    setCurrentSlide: function(n) {
        this.currentSlide = n;
        dojo.publish("promoChanged", [{id: this.currentSlide}]);
    },
    goToSlide: function(n) {
        var currPos, destPos;

        currPos = this.promoLocations.indexOf(this.currentSlide);
        destPos = this.promoLocations.indexOf(n);

        this.previousSlide = currPos;
        this.setCurrentSlide(n);

        if(currPos > destPos) {
            this.jump("right", currPos - destPos);
        } else if(currPos < destPos) {
            this.jump("left", destPos - currPos );
        }
    },
    getPrevSlide: function() {
        return this.promos[this.previousSlide];
    },
    getCurrSlide: function() {
        return this.promos[this.currentSlide];
    },
    onClickPrev: function() {
        /*
        if(this.promoLocations.indexOf(this.currentSlide) === 0) { 

            this.shift("right", 1);
            this.jump("left", 1, false);
        }
        */

        var slide = this.currentSlide === 0 ?
            this.numPromos - 1: this.currentSlide - 1;

        this.goToSlide(slide);
    },
    onClickNext: function() {
        /*
        if(this.promoLocations.indexOf(this.currentSlide) ===
            (this.promoLocations.length - 1)) {

            this.shift("left", 1);
            this.jump("right", 1, false);
        }
        */
        this.goToSlide((this.currentSlide + 1) % this.numPromos);
    },
    onPromoRequested: function(m) {
        this.goToSlide(m.id);
    },
    animate: function() {
        var that = this;
        dojo.animateProperty({
            node: this.promosContainer,
            properties: {
                left: this.currentOffset,
                onEnd: function() {
                    setTimeout(function() {
                        var prevSlide = that.getPrevSlide();
                        that.getCurrSlide().activate();

                        if(prevSlide) {
                            prevSlide.deactivate();
                        }
                    }, 500);
                }
            }
        }).play();
    },
    shift: function(direction, n) {
        var i, promo;
        n = n || 1;

        for(i=0; i<n; i++) {
            switch(direction) {
                case "left":
                    promo = dojo.query("li", this.promosContainer)[0];
                    dojo.place(promo, this.promosContainer, "last");
                    this.promoLocations.push(this.promoLocations.shift());
                    break;
                case "right":

                    promo =dojo.query("li",
                        this.promosContainer)[this.numPromos - 1];

                    dojo.place(promo, this.promosContainer, "first");
                    this.promoLocations.unshift(this.promoLocations.pop());
                    break;
            }
        }
    },
    jump: function(direction, n, animated) {

        n = n || 0;
        animated = (animated === undefined) ? true : false;
        switch(direction) {
            case "left":
                this.currentOffset = this.currentOffset - n*this.slideWidth;
                break;
            case "right":
                this.currentOffset = this.currentOffset + n*this.slideWidth;
                break;
        }

        if(animated) {
            this.animate();
        } else {
            dojo.style(this.promosContainer, "left", this.currentOffset + "px");
        }
    }
}
pc = PromoCarousel;
dojo.declare("pg.Promo", null, {

    id: null,
    elem: null,
    constructor: function(args) {
        this.elem = args.elem;
        this.id = args.id;
        this.detailsElem = dojo.query('.details', this.elem)[0];

        if(!args.active) {
            this.deactivate();
        }
    },
    activate: function() {
        dojo.fadeIn({
            node: this.detailsElem
        }).play();
    },
    deactivate: function() {
        dojo.style(this.detailsElem, "opacity", 0);
    }
});
dojo.declare("pg.PromoButton", null, {

    id: null,
    elem: null,
    constructor: function(args) {

        dojo.safeMixin(this, args);
        dojo.connect(this.elem, "onclick", this, "onClick");
        dojo.subscribe("promoChanged", this, "onPromoChanged");

    },
    highlight: function() {
        dojo.addClass(this.elem, "selected");
    },
    unhighlight: function() {
        dojo.removeClass(this.elem, "selected");
    },
    onClick: function(e) {
        dojo.publish("promoRequested", [{id: this.id}]);
    },
    onPromoChanged: function(m) {
        if(m.id === this.id) {
            this.highlight();
        } else {
            this.unhighlight();
        }
    }
});

