/**
 * @class: JVNews
 * @description: Display contents in tabs 
 * @version: 1.0 
 */
var JVNews = new Class({

	options: {
		width: '100%',
		newsHandler: 'jvnews-pagination',
		newsContainer: 'jvnews-container',		
		currentNewsHandlerClass: 'jvnews-pagination-selected',		
		currentNewsContentClass: 'jvnews-pagination-selected',		
		currentIndex: 0,	
		loaderURL: 'modules/mod_jvnews/tmpl/Default/images/ajax-loader.gif',
		autoRun: 0,	
		ajax: 1,
		ajaxMethod: 'get',
		direction: 1,
		forceWaiting: 0,
		fx: 'fade',
		transition: Fx.Transitions.Expo.easeInOut,				
		duration: 1000,		
		timer: null,
		newsTimer: 5000,
		container: null,
		handlers: null,
		contents: null		
	},
	
	initialize: function(selector, options){	   
		this.setOptions(options);
		this.initNews(selector);
	},
	
	initNews: function(selector){
		var that = this;
		var selector = $(selector);
		if(!selector) return;
		selector.setStyles({
			'width': that.options.width
		});	
		
		if(!selector.getElement('.' + that.options.newsHandler)){
			return;
		}
		that.options.handlers = selector.getElement('.' + that.options.newsHandler).getElements('a');
		that.options.handlers = that.options.handlers.filter(function(el){
			return !el.hasClass('start') && !el.hasClass('next') && !el.hasClass('prev') && !el.hasClass('end')
		});
			
		if(!that.options.handlers.length) return;
		that.options.currentIndex = Math.min(that.options.currentIndex, that.options.handlers.length - 1);
		that.options.container = selector.getElement('.' + that.options.newsContainer);
		that.fx = new Fx.Styles(that.options.container, {
			duration: that.options.duration,
			transition: that.options.transition,
			wait: false
		});
		
		that.options.contents = that.options.container.getChildren();						
		if(that.options.fx == 'slideWidth'){
			that.options.contents.each(function(content, index){
				content.setStyle('position', 'absolute');
				content.setStyle('left', -1 * that.options.contents[0].getSize().size.x);			
			});		
		}
		else if(that.options.fx == 'slideHeight'){
			that.options.contents.each(function(content, index){
				content.setStyle('position', 'absolute');
				content.setStyle('top', -1 * that.options.contents[0].getSize().size.y);			
			});		
		}				
		else if(that.options.fx == 'fade'){
			that.options.contents.each(function(content, index){
				content.setStyles({
					'position': 'absolute',
					'top': 0,
					'opacity': 0
				});			
			});	
		}
		else{
			that.options.fx = 'showHide';
			that.options.contents.each(function(content, index){
				content.setStyles({
					'position': 'absolute',
					'top': 0,
					'display': 'none'
				});			
			});	
		}		
		that.options.handlers.each(function(handler, index){
			if(handler.hasClass(that.options.currentNewsHandlerClass)){
				that.options.currentIndex = index;
			}
			handler.removeEvents('click').addEvent('click', function(e){
				if(e) new Event(e).stop();
				if(!that.options.forceWaiting){					
					if(that.options.ajax == 1 && !this.ajaxcalled){
						if(this.req) this.req.cancel();
						this.req = new Ajax(this.href, {
							method: that.options.ajaxMethod,
							data: {
								'jvnews-ajax': 1
							},													
							onComplete: function(response){
								that.options.contents[index].setHTML(response);
								var imgTags = that.options.contents[index].getElements('img');
								if(imgTags.length){
									var imgSrcs = imgTags.map(function(imgTag){
										return imgTag.src;
									});
									new Asset.images(imgSrcs, {
										onComplete: function(){
											handler.ajaxcalled = true;
											handler.fireEvent('click');
										}
									});
								}
								else{
									handler.ajaxcalled = true;
									handler.fireEvent('click');
								}
							}
						}).request();
						return;
					}
					if(that.options.autoRun == 1){
						$clear(that.options.timer);						
						that.options.timer = setInterval(function(){
							nextLink.fireEvent('click');
						}, that.options.newsTimer);
					}                    
					that[that.options.fx](index);
					new JVTips($$('.JVTip'), { maxTitleChars: 50, fixed: false});              					
				}
			});
		});
		var startLink = selector.getElement('.' + that.options.newsHandler).getElement('a.start');
		if(startLink){
			startLink.removeEvent('click').addEvent('click', function(e){
				if(e) new Event(e).stop();
				that.options.handlers[0].fireEvent('click');		
			});
		}
		var prevLink = selector.getElement('.' + that.options.newsHandler).getElement('a.prev');
		if(prevLink){
			prevLink.removeEvent('click').addEvent('click', function(e){
				if(e) new Event(e).stop();
				that.options.handlers[that.options.currentIndex > 0 ? that.options.currentIndex - 1 : 0].fireEvent('click');			
			});
		}
		var nextLink = selector.getElement('.' + that.options.newsHandler).getElement('a.next');
		if(nextLink){
			nextLink.removeEvent('click').addEvent('click', function(e){
				if(e) new Event(e).stop();
				that.options.handlers[that.options.currentIndex < that.options.handlers.length - 1 ? that.options.currentIndex + 1 : 0].fireEvent('click');		
			});
		}		
		var lastLink = selector.getElement('.' + that.options.newsHandler).getElement('a.end');
		if(lastLink){
			lastLink.removeEvent('click').addEvent('click', function(e){
				if(e) new Event(e).stop();
				that.options.handlers[that.options.handlers.length - 1].fireEvent('click');		
			});
		}
		
		that.options.handlers[that.options.currentIndex].fireEvent('click');		
		if(that.options.autoRun == 1){
			that.options.timer = setInterval(function(){
				nextLink.fireEvent('click');
			}, that.options.newsTimer);
		}			
	},
	
	slideWidth: function(index){
		var that = this;		
		var currentContent = that.options.contents[that.options.currentIndex];		
		var currentHandler = that.options.handlers[that.options.currentIndex];
		if($defined(index)){
			if(that.options.currentIndex != index){
				if(that.options.currentIndex > index){
					that.options.direction = 0;				
				}
				else{
					that.options.direction = 1;
				}
				that.options.currentIndex = index;
			}
		}
		else{
			that.findTab();
		}
		var newContent = that.options.contents[that.options.currentIndex];
		if(that.options.direction == 0){
			var currentX = that.options.container.getSize().size.x;		

		}
		else{
			var currentX = (-1 * that.options.container.getSize().size.x);			
		}
		if(!currentContent.h){
			currentContent.setStyle('width', that.options.container.getParent().getSize().size.x);
			currentContent.h = currentContent.getSize().size.y;
		}
		if(!newContent.h){
			newContent.setStyle('width', that.options.container.getParent().getSize().size.x);
			newContent.h = newContent.getSize().size.y;
		}
		var newHandler = that.options.handlers[that.options.currentIndex];
		newHandler.addClass(that.options.currentNewsHandlerClass);
		var contentShow = new Fx.Styles(newContent, {
			duration: that.options.duration,
			transition: that.options.transition,
			wait: false,
			onStart: function(){
				that.options.forceWaiting = true;
			},
			onComplete: function(){
				that.options.forceWaiting = false;
			}
		});
		contentShow.start({
			'left': [0],
			'opacity': [0, 1]
		});
		that.fx.start({
			'height': [currentContent.h, newContent.h]
		});
		newContent.addClass(that.options.currentNewsContentClass);		
		if(currentContent != newContent){		
			var contentHide = new Fx.Styles(currentContent, {
				duration: that.options.duration,
				transition: that.options.transition,
				wait: false
			});
			currentHandler.removeClass(that.options.currentNewsHandlerClass);			
			contentHide.start({
				'left': [currentX],
				'opacity': [1, 0]
			});
			currentContent.removeClass(that.options.currentNewsContentClass);
		}
	},
	
	slideHeight: function(index){
		var that = this;		
		var currentContent = that.options.contents[that.options.currentIndex];		
		var currentHandler = that.options.handlers[that.options.currentIndex];
		if($defined(index)){
			if(that.options.currentIndex != index){
				if(that.options.currentIndex > index){
					that.options.direction = 0;				
				}
				else{
					that.options.direction = 1;
				}
				that.options.currentIndex = index;
			}
		}
		else{
			that.findTab();
		}		
		var newContent = that.options.contents[that.options.currentIndex];
		if(that.options.direction == 0){
			var currentY = that.options.container.getSize().size.y;			


		}
		else{
			var currentY = (-1 * that.options.container.getSize().size.y);			
		}
		if(!currentContent.h){
			currentContent.h = currentContent.getCoordinates().height;
		}
		if(!newContent.h){
			newContent.h = newContent.getCoordinates().height;


		}
		var newHandler = that.options.handlers[that.options.currentIndex];
		newHandler.addClass(that.options.currentNewsHandlerClass);
		var contentShow = new Fx.Styles(newContent, {
			duration: that.options.duration,
			transition: that.options.transition,
			wait: false,
			onStart: function(){
				that.options.forceWaiting = true;
			},
			onComplete: function(){
				that.options.forceWaiting = false;
			}
		});		
		contentShow.start({
			'top': [0],
			'opacity': [0, 1]
		});
		that.fx.start({
			'height': [currentContent.h, newContent.h]
		});
		newContent.addClass(that.options.currentNewsContentClass);		
		if(currentContent != newContent){		
			var contentHide = new Fx.Styles(currentContent, {
				duration: that.options.duration,
				transition: that.options.transition,
				wait: false
			});
			currentHandler.removeClass(that.options.currentNewsHandlerClass);			
			contentHide.start({
				'top': [0, -currentY],
				'opacity': [1, 0]
			});
			currentContent.removeClass(that.options.currentNewsContentClass);
		}
	},	
	
	fade: function(index){
		var that = this;		
		var currentContent = that.options.contents[that.options.currentIndex];		
		var currentHandler = that.options.handlers[that.options.currentIndex];
		if($defined(index)){
			if(that.options.currentIndex != index){				
				that.options.currentIndex = index;
			}
		}
		else{
			that.findTab();
		}
		var newContent = that.options.contents[that.options.currentIndex];		
		var newHandler = that.options.handlers[that.options.currentIndex];
		newHandler.addClass(that.options.currentNewsHandlerClass);
		var contentShow = new Fx.Styles(newContent, {
			duration: that.options.duration,
			transition: that.options.transition,
			wait: false,
			onStart: function(){
				that.options.forceWaiting = true;
			},
			onComplete: function(){
				that.options.forceWaiting = false;
			}
		});		
		contentShow.start({
			'opacity': [0, 1]
		});
		that.fx.start({
			'height': [currentContent.getCoordinates().height, newContent.getCoordinates().height]
		});
		newContent.addClass(that.options.currentNewsContentClass);		
		if(currentContent != newContent){		
			var contentHide = new Fx.Styles(currentContent, {
				duration: that.options.duration,
				transition: that.options.transition,
				wait: false
			});
			currentHandler.removeClass(that.options.currentNewsHandlerClass);			
			contentHide.start({
				'opacity': [1, 0]
			});
			currentContent.removeClass(that.options.currentNewsContentClass);
		}
	},
	
	showHide: function(index){
		var that = this;		
		var currentContent = that.options.contents[that.options.currentIndex];		
		var currentHandler = that.options.handlers[that.options.currentIndex];
		if($defined(index)){
			if(that.options.currentIndex != index){				
				that.options.currentIndex = index;
			}
		}
		else{
			that.findTab();
		}
		var newContent = that.options.contents[that.options.currentIndex];		
		var newHandler = that.options.handlers[that.options.currentIndex];
		newHandler.addClass(that.options.currentNewsHandlerClass);
		newContent.setStyle('display', '');
		that.fx.start({
			'height': [currentContent.getCoordinates().height, newContent.getCoordinates().height]
		});
		newContent.addClass(that.options.currentNewsContentClass);				
		if(currentContent != newContent){	
			currentHandler.removeClass(that.options.currentNewsHandlerClass);			
			currentContent.setStyle('display', 'none');
			currentContent.removeClass(that.options.currentNewsContentClass);
		}
	},
	
	findTab: function(){
		var that = this; 
		var len = that.options.contents.length;
		if(that.options.direction == 1){
			if(that.options.currentIndex < len - 1){
				that.options.currentIndex++;
			}
			else{
				that.options.currentIndex = 0;
			}
		}
		else{
			if(that.options.currentIndex > 0){
				that.options.currentIndex--;
			}
			else{
				that.options.currentIndex = len - 1;
			}
		}
		
	}
});
JVNews.implement(new Events, new Options);

