BonSlideshow = function(id, params) {
    this.isPlaying = false;
    this.id = id;
    this.elements = jQuery("#" + id + " .ad");
    this.interval = params.interval ? params.interval : 5000; /* (ms) */
    this.currentElementIndex = -1;
    this.timeoutId = null;

    this.init();
    this.play(true);
}

BonSlideshow.prototype.init = function() {
    this.elements.each(function() {
        jQuery(this).hide();
    })

    var bonSlideshow = this;

    jQuery("#" + this.id + " .previous").each(function() {
        jQuery(this).click(function() {

            clearTimeout(this.timeoutId);
            this.timeoutId = setTimeout("BonSlideshow.get('" + this.id + "').play();",
                                        this.interval);
                                        
            bonSlideshow.goToPrevious();
            return false;
        });
    })

    jQuery("#" + this.id + " .next").each(function() {
        jQuery(this).click(function() {

            clearTimeout(this.timeoutId);
            this.timeoutId = setTimeout("BonSlideshow.get('" + this.id + "').play();",
                                        this.interval);

            bonSlideshow.goToNext();
            return false;
        });
    })

    jQuery("#" + this.id + " .item").each(function() {
        jQuery(this).click(function() {

            clearTimeout(this.timeoutId);
            this.timeoutId = setTimeout("BonSlideshow.get('" + this.id + "').play();",
                                        this.interval);

            bonSlideshow.goTo(jQuery(this).text() - 1);
            return false;
        });
    })

    BonSlideshow.add(this);
    
    this.currentElementIndex = 0;
    this.show(0);
    
    this.isPlaying = true;
}

if (typeof BonSlideshow.instances == 'undefined') {
    BonSlideshow.instances = [];
}

BonSlideshow.get = function(id) {
    var obj = null;

    for (var i = 0; i < BonSlideshow.instances.length; i++) {
        if (BonSlideshow.instances[i].id == id) {
            obj = BonSlideshow.instances[i];
        }
    }

    if (obj == null) {
        obj = new BonSlideshow(id);
    }

    return obj;
}

BonSlideshow.add = function(obj) {
    BonSlideshow.instances.push(obj);
}

BonSlideshow.prototype.getElement = function(number) {
    if (typeof number == 'undefined') {
        number = this.currentElementIndex;
    }

    var element = jQuery(this.elements[number]);
    
    return element;
}

BonSlideshow.prototype.goTo = function(index) {
    this.hide(this.currentElementIndex);
    this.show(index);

    this.currentElementIndex = index;
}

BonSlideshow.prototype.goToNext = function() {
    this.hide(this.currentElementIndex);

    var nextElementIndex = this.currentElementIndex + 1;
    
    if (nextElementIndex > this.elements.length - 1) {
        nextElementIndex = 0;
    }

    this.currentElementIndex = nextElementIndex;

    this.show(this.currentElementIndex);
}

BonSlideshow.prototype.goToPrevious = function() {
    this.hide(this.currentElementIndex);

    var previousElementIndex = this.currentElementIndex - 1;

    if (previousElementIndex < 0) {
        previousElementIndex= this.elements.length - 1;
    }

    this.currentElementIndex = previousElementIndex;

    this.show(this.currentElementIndex);
}

BonSlideshow.prototype.play = function(firstTime) {
    if (this.isPlaying) {
        if (typeof firstTime == 'undefined' && !firstTime) {
            this.goToNext();
        }
        this.timeoutId = setTimeout("var temp = BonSlideshow.get('" + this.id + "'); temp.play();", this.interval);
    }
}

BonSlideshow.prototype.pause = function() {
    this.isPlaying = false;
    clearTimeout(this.timeoutId);
}

BonSlideshow.prototype.show = function(elementIndex) {
    var element = jQuery(this.elements[elementIndex]);
    if (element.is("img")) {
        element.attr('src', element.attr('alt'));

        /* Preloading */
        var nextElement = jQuery(this.elements[elementIndex + 1 > this.elements.length - 1
                                            ? 0 : elementIndex + 1]);

        if (nextElement.attr('src') != nextElement.attr('alt')) {
            nextElement.attr('src', nextElement.attr('alt'));
        }
        /* End Preloading */
    }
    else {
        element.show();
    }

    jQuery("#" + this.id + " .paging .item").each(function() {
        jQuery(this).removeClass('active');
        if (jQuery(this).text() - 1 == elementIndex) {
            jQuery(this).addClass('active');
        }
    });

    jQuery(element).show();
}

BonSlideshow.prototype.hide = function(elementIndex) {
    jQuery(this.elements[elementIndex]).hide();
}