var requesting = false;

var JANEWSFP_TELINE4 = new Class({
	initialize: function(options) {
		
		this.options = $extend({
			moduleid: 0,
			duration: 400,
			page: 0
		}, options || {});
		
		var moduleid = this.options.moduleid;
		this.panelwrap = $('ja-zinfp-links-'+moduleid);	
		
		this.jaTooltips = new Tips(this.panelwrap.getElements('.jahasTip'), { maxTitleChars: 50, fixed: false, className: 'tool-tip janews-tool'});
		
		this.panelwrap.setStyle('height', this.panelwrap.getElement('ul.active').offsetHeight);
		
		this.panels = $$('#ja-zinfp-links-'+moduleid + ' ul.box');				
		this.cur_activePanel = this.panelwrap.getElement('ul.active');
		
		this.next_bt = $('ja-zinfp-'+moduleid).getElement('.ja-zinfp-links-actions a.next');
		this.prev_bt = $('ja-zinfp-'+moduleid).getElement('.ja-zinfp-links-actions a.prev');
		this.next_img	= $('ja-zinfp-'+moduleid).getElement('.ja-zinfp-links-actions span.next');
					
		if(this.next_bt!=null){
			this.next_bt.addEvent('click', function () {	    	
		    	this.getLinks();
		    }.bind(this));
		    
		    this.prev_bt.addEvent('click', function () {
		    	this.previous();
		    }.bind(this));
		}
	},
	
    getLinks: function () {
		var moduleid = this.options.moduleid;
		this.cur_activePanel = this.panelwrap.getElement('ul.active');
		
		if(this.cur_activePanel.getNext()!=null){
			this.cur_activePanel.removeClass('active');
			this.activePanel = this.cur_activePanel.getNext();
			this.activePanel.addClass('active');
			this.next();
		}else{		
			var pages = this.panelwrap.getChildren().length;
			if(this.options.page<pages) return;
			
			this.next_bt.setStyle('display', 'none');
			this.next_img.setStyle('display', 'inline');				
			
			// if click button submit.
			if(requesting) return;	    	
	    	requesting = true;
	    	
	    	var url = location.href;
	    	if(url.indexOf('#')>-1){
	    		url = url.substr(0, url.indexOf('#'));
	    	}
	    	if(url.indexOf('?')>-1) url += '&';
	    	else url += '?';
	    	url += 'janews_fp_ajax=1&rand=' + (Math.random() * Math.random());
	    	url += '&count=' + this.panelwrap.getChildren().length;
	    	url += '&moduleid=' + moduleid;  
	    	
	        new Ajax(url, {
	            method: 'get',
	            onComplete: function (data) {
	        		this.next_bt.setStyle('display', 'inline');
	        		this.next_img.setStyle('display', 'none');
	    		
	        		requesting = false;
	        		if(data.trim()!=''){
		        		this._currentH = this.panelwrap.getElement('ul.active').ofssetTop;
		        		this.cur_activePanel = this.panelwrap.getElement('ul.active');
		        		this.panelwrap.getElement('ul.active').removeClass('active');
		        		
		        		var ul = new Element('ul', {'class': 'active box'});
		        		ul.innerHTML = data;
		        		ul.inject(this.panelwrap)
		               	this.activePanel = this.panelwrap.getElement('ul.active');
		               	/*tooltip*/		        		
		               	this.panels = $$('#ja-zinfp-links-'+moduleid + ' ul.box');		
		               	this.anim = new animNewsMoveVir(this);
		               	this.next();
		               	
		               	ul.getElements('.jahasTip').each(this.jaTooltips.build, this.jaTooltips);
		        	}
	
	            }.bind(this),
	            
	            onFailure: function () {
	            	this.next_bt.setStyle('display', 'inline');
	        		this.next_img.setStyle('display', 'none');
	        		
	            	requesting = false;
	                alert('fail request');
	            }
	        }).request();
		}
    },
    
    next: function(){    	
    	this.anim.move (this.cur_activePanel, this.activePanel, false);  
    	
    	if (!this.mainfx) this.mainfx = new Fx.Style(this.panelwrap, 'height',{duration:this.options.duration});
		this.mainfx.stop();
		this.mainfx.start(this.activePanel.offsetHeight);

    },
    
    previous: function(){
    	this.cur_activePanel = this.panelwrap.getElement('ul.active');    	
    	var activePanel = this.cur_activePanel.getPrevious();
    	if(activePanel!=null){
    		this.activePanel = activePanel;
    		this.cur_activePanel.removeClass('active');
    		this.activePanel.addClass('active');
    		this.anim.move (this.cur_activePanel, this.activePanel, false);
    		if (!this.mainfx) this.mainfx = new Fx.Style(this.panelwrap, 'height',{duration:this.options.duration});
    		this.mainfx.stop();
    		this.mainfx.start(this.activePanel.offsetHeight);
    	}    	
    }
	
});

var animNewsMoveVir = new Class ({
	initialize: function(tabwrap) {
		this.options = tabwrap.options || {};
		this.tabwrap = tabwrap;
		this.changeEffect = new Fx.Elements(this.tabwrap.panels, {duration: this.options.duration});
	
	    var top = 0;
	    this.tabwrap.panels.each (function (panel) {
	      panel.setStyle('top', top);     
	      top += Math.max(panel.offsetHeight,  panel.getParent().offsetHeight);
	    });
	    this.tabwrap.panels.setStyle('left', 0);
	},
	move: function (curTab, newTab, skipAnim) {
		if(skipAnim==false)
		{
      //reposition newTab
      
			this.changeEffect.stop();
			var obj = {}; 
			var offset = newTab.offsetTop.toInt();
			i=0;
			this.tabwrap.panels.each(function(panel) {
				obj[i++] = {'top':[panel.offsetTop.toInt(), panel.offsetTop.toInt() - offset]};			
			});
			this.changeEffect.start(obj);
		}
	}
});

