$j = jQuery.noConflict();
JA_NewsHeadline = Class.create();
JA_NewsHeadline.prototype = {
    initialize: function(options){
        this.options = Object.extend({
            autoroll: 0,
            total: 0,
            delaytime: 10
        }, options || {});
        this.elements = new Array();
        this._current = 0;
    },

    start: function() {
        //Get cache news to array
        if(!$j("#ja-newshlcache") || !$j("#ja-newshlcache > div")) return;
        var count = 0;
        var tempElements = new Array();
        $j.each($j(".ja-newshlcache > div"),function() {
            tempElements[count++] = $j(this);
        });
        this.elements = tempElements;
        this.element = this.elements[this._current].clone();
        $j('#jahl-newsitem').html(this.element.html());
        $j(this).everyTime(this.options.delaytime*1000,this.run);
    },

    run: function() {
        if(!this.options.autoroll || this.options.total<2) return;
        this._next = this._current < this.options.total - 1?this._current+1:0;
        this.swap();
    },

    getNext: function() {
        return (this._current < this.options.total - 1)?this._current+1:0;
    },

    getPrev: function() {
        return this._current > 0 ? this._current - 1 : this.options.total - 1;
    },
    pause: function () {
        this.options.autoroll = 0;
        if($('jahl-switcher')) {
            $('jahl-switcher').src = $('jahl-switcher').src.replace('pause','play');
            $('jahl-switcher').title = 'Play';
        }
    },
    next: function() {
        this.pause();
        this._next = this.getNext();
        this.swap();
    },

    prev: function() {
        this.pause();
        this._next = this.getPrev();
        this.swap();
    },

    toogle: function() {
        this.options.autoroll = this.options.autoroll?0:1;
        if($('jahl-switcher')) {
            $('jahl-switcher').src = this.options.autoroll? $('jahl-switcher').src.replace('play','pause'):$('jahl-switcher').src.replace('pause','play');
            $('jahl-switcher').title = this.options.autoroll?'Pause':'Play';
        }
        this.run();
    },

    swap: function() {
        if(!this.elements.length) return;
        this.animate();
    },

    animate: function() {
        this._current = this._next;
        this.element = this.elements[this._current].clone();
        var tmpElements = this.elements;
        var tmpElement = this.element;
        var tmpCurrent = this._current;
        var tmpOptions = this.options;
        var tmpNext = this.getNext();
        var tmpPrev = this.getPrev();
        $j("#jahl-newsitem > div").fadeOut("slow",function(){
            $j('#jahl-newsitem').html(tmpElement.html());
            if($j('#jahl-prev')) $j('#jahl-prev').attr('title',tmpElements[tmpPrev].find("a,:first").html());
            if($j('#jahl-next')) $j('#jahl-next').attr('title',tmpElements[tmpNext].find("a,:first").html());
            if($j('#jahl-indicator')) $j('#jahl-indicator').html((tmpCurrent+1)+"/"+tmpOptions.total);
        });
    }
};