var promotedScrolls = new Array();

var PromotedProducts = function(div, id) {
    var that = this;
    this.div = div;
    this.selector = jQuery(this.div);
    this.maxPlaceholderCapacity = (this.selector.width() < 600) ? 4 : 5;
    this.scrollCounter = 0;
    this.scrolling = false;
    this.id = id;
    this.animation = null;
    this.horizontal = true;

    this.init = function() {
        if (that.maxPlaceholderCapacity < that.selector.find('.promoted_product').size()) {
            that.animation = setInterval("promotedScrolls["+that.id+"].step(-1)", 5000);
            that.horizontal = (this.selector.width() > this.selector.height());

            // allow hints in doc structure
            if (this.selector.closest('.promoted_vertical').size() > 0) {
                that.horizontal = false;
            }
            if (this.selector.closest('.promoted_horizontal').size() > 0) {
                that.horizontal = true;
            }

            this.selector.find('.promoted_forward').click(function () {
                that.step(-1);
            });
            this.selector.find('.promoted_backward').click(function () {
                that.step(1);
            });
            this.selector.mouseover(function () {
                clearInterval(that.animation);
            });
            this.selector.mouseout(function () {
                that.animation = setInterval("promotedScrolls["+that.id+"].step(-1)", 5000);
            });
        }
        else {
            this.selector.find('.promoted_arrows').css('opacity','0.5');
        }
    };

    this.step = function(dir) { // dir = 1 -> forward, dir = -1 -> backward
        if (dir > 0) {
            that.scrollCounter = Math.max(1, that.scrollCounter+1);
        }
        else {
            that.scrollCounter = Math.min(-1, that.scrollCounter-1);
        }
        if (!that.scrolling) {
            that.scroll();
        }
    };

    this.afterAnim = function() {
        that.scrolling = false;

        if (that.scrollCounter != 0) {
            that.scrollCounter = (Math.abs(that.scrollCounter)/that.scrollCounter)*(Math.abs(that.scrollCounter)-1);
        }
        if (that.direction == -1) {
            that.selector.find('.promoted_product:first').remove();
            that.direction = 0;
        }
        if (that.scrollCounter != 0) {
            that.scroll();
        }

     };

    this.scroll = function() {

        that.scrolling = true;

        if (that.scrollCounter > 0) {
            var product = that.selector.find('.promoted_product:last');
            that.selector.find('.promoted_products').prepend('<div class="promoted_product">'+product.html()+'</div>');
            var first = that.selector.find('.promoted_product:first');
            if (that.horizontal) {
                var animateBy = first.width();
                first.css('margin-left', '-'+animateBy+'px');
                first.animate({'marginLeft': '0px'}, 1000, 'easeInOutSine', that.afterAnim);
            }
            else {
                var animateBy = first.height();
                first.css('margin-top', '-'+animateBy+'px');
                first.animate({'marginTop': '0px'}, 1000, 'easeInOutSine', that.afterAnim);
            }
            that.selector.find('.promoted_product:last').remove();
            that.direction = 1;
        }
        else {
            var first = that.selector.find('.promoted_product:first');
            if (that.horizontal) {
                var animateBy = first.width() + parseInt(first.css('margin-right'));
                first.animate({'marginLeft': '-'+animateBy+'px'}, 1000, 'easeInOutSine', that.afterAnim);
            }
            else {
                var animateBy = first.height() + parseInt(first.css('margin-bottom'));
                first.animate({'marginTop': '-'+animateBy+'px'}, 1000, 'easeInOutSine', that.afterAnim);
            }
            var product = that.selector.find('.promoted_product:first');
            that.selector.find('.promoted_products').append('<div class="promoted_product">'+product.html()+'</div>');
            if (undefined != window.fancyBoxOpts) {
                that.selector.find('.promoted_product:last a[rel=gallery]').fancybox(fancyBoxOpts);
            }
            that.direction = -1;
        }
    };

    this.init();
};

jQuery(document).ready(function () {
    jQuery('.control_promoted').each(function() {
        var rand = Math.round(Math.random()*65535);
        promotedScrolls[rand] = new PromotedProducts(this, rand);
    });
});


