JA_NewsHeadline = new Class ({
	initialize: function(options){
		this.options = Object.extend({
			autoroll: 0,
			total: 0,
			delaytime: 10,
            
            // Cantidad de menús que se encuentran en el lado lateral derecho
            newsmenu: 2,
            
            // Id u objetos con los que funciona el JA_News
            jnext: "jahl-next",
            jprev: "jahl-prev",
            jnewsmenu: "ja-newsmenu",
            jnewsitem: "jahl-newsitem",
            jswitcher: "jahl-switcher",
            jindicator: "jahl-indicator",
            jnewstitle: "ja-newstitle",
            jnewshlcache: "ja-newshlcache"            
		}, options || {});
		this.elements = [];
		this._current = 0;
        
        // Variable auxiliar
        this._i = 0;
        
        // Array de jnewsmenu o menús del lado lateral
        this._jnewsmenu = [];
        
        
	},

	start: function() {
        // Capturamos los menús del lado lateral o jnewsmenu
        for (this._i=0; this._i<this.options.newsmenu; this._i++)
        {
            this._jnewsmenu[this._i] = new Fx.Slide(this.options.jnewsmenu + (this._i + 1));
            //alert(this.options.jnewsmenu + (this._i + 1));
            // Ocultamos los menús
            this._jnewsmenu[this._i].hide();
        }
    
		//Get cache news to array
		if(!$(this.options.jnewshlcache) || !$(this.options.jnewshlcache).getChildren()) return;
		$(this.options.jnewshlcache).getChildren().each(function (el){
			el._title = el.getElement('.' + this.options.jnewstitle)?el.getElement('.' + this.options.jnewstitle).getProperty('title'):'';
			el.setStyle('opacity', 0);
			this.elements.push(el);
		},this);
		this.element = this.elements[this._current].clone();
		this.element.inject($(this.options.jnewsitem));
		this.element.setStyle('opacity', 1);
		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.timer = setTimeout(this.swap.bind(this), this.options.delaytime*1000);	
	},
	
	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;
	},
    
    /* Valida si el anuncio se encuentra y retorna el número del anuncio */
    getOther: function(num) {
        return num < this.options.total ? num : 0;
    },

	next: function() {
		this._next = this.getNext();
		this.swap();
	},
	
	prev: function() {
		this._next = this.getPrev();
		this.swap();
	},
    
    /* Permite capturar un aviso cualquiera*/
    other: function(num) {
        if (this._current == num)
            return;
            
        this._next = this.getOther(num);
        this.ocultar();
        
        if ((this._next > 3) && (this._next < 10))
            this._jnewsmenu[0].show();
        else if ((this._next > 9) && (this._next < 16))
            this._jnewsmenu[1].show();

        this.swap();
    },
    
    /* Oculta los jnewsmenu */
    ocultar: function()
    {
        for (this._i=0; this._i<this.options.newsmenu; this._i++)
            this._jnewsmenu[this._i].hide();
    },
	
	toogle: function() {
		clearTimeout(this.timer);
		this.options.autoroll = this.options.autoroll?0:1;
		Cookie.set('JAHL-AUTOROLL',this.options.autoroll);
		if($(this.options.jswitcher)) {
			$(this.options.jswitcher).src = this.options.autoroll? $(this.options.jswitcher).src.replace('play','pause'):$(this.options.jswitcher).src.replace('pause','play');
			$(this.options.jswitcher).title = this.options.autoroll?'Pause':'Play';
		}
		this.run();
	},

	swap: function() {
		if(!this.elements.length) return;
		clearTimeout(this.timer);
		this.anim1();
	},

	anim1: function() {
		new Fx.Style(this.element, 'opacity', {onComplete:this.anim2.bind(this)}).start(1, 0);
	},

	anim2: function() {
		this._current = this._next;
		this.element = this.element.replaceWith(this.elements[this._current].clone());
		new Fx.Style(this.element, 'opacity', {onComplete:this.run.bind(this)}).start(0, 1);
		if($(this.options.jprev)) $(this.options.jprev).setProperty('title', this.elements[this.getPrev()]._title);
		if($(this.options.jnext)) $(this.options.jnext).setProperty('title', this.elements[this.getNext()]._title);
		if($(this.options.jindicator)) $(this.options.jindicator).innerHTML = (this._current+1)+"/"+this.options.total;
	}
});
