/*
Script: 	
	JVSlider - The JV Slider Module allows to display the images with effects.

License:
	Proprietary - JoomlaVi Club members only

Copyright:
	Copyright (C) JoomlaVi. All rights reserved.
*/

var JVSlider = new Class({

	options:{
		arrows: 1,
		autoHideArrows: 0,
		keyboard: 1,
		autoPlay: 1,
		width: false,
		height: false,
		itemsperclick: 1,
		duration: 750,		
		transition: Fx.Transitions.Sine.easeInOut,
		contentClass: '.jv-slider-content',		
		itemsClass: '.jv-slider-items',		
		arrowsClass: '.jv-slider-arrows'		
	},
	
	initialize: function(selector, options){
		this.setOptions(options);
		this.selector = $(selector);
		if(!this.selector) return;		
		this.setup();		
	},	
	
	setup: function(data){
		var that = this;
		if(!this.selector) return;
		this.contentElement = this.selector.getElement(that.options.contentClass);
		this.itemElement = this.selector.getElement(that.options.itemsClass);		
		var itemsSize = this.selector.getSize();
		this.options.width = this.options.width || itemsSize.size.x;
		this.options.height = this.options.height || itemsSize.size.y;
		this.selector.setStyles({
			'width': that.options.width,
			'height': that.options.height
		});
		this.items = this.itemElement.getChildren();
		this.len = this.items.length;
		if(!this.len) return;
		var itemWidth = 0;		
		this.items.each(function(item, index){
			itemWidth += item.getCoordinates().width + item.getStyle('margin-left').toInt() + item.getStyle('margin-right').toInt();
			if(index == 0){
				that.itemsshow = Math.ceil(that.options.width / itemWidth);
			}
		});
		this.itemElement.setStyle('width', itemWidth);
		if(this.len == this.itemsshow) return;
		this.items[0].addClass('first');
		this.items[this.len - 1].addClass('last');
		this.current = 0;
		this.slider = new Fx.Scroll(this.contentElement, {
			'duration': that.options.duration,
			'transition': that.options.transition,
			'wait': false			
		}).toLeft();
		if(that.options.arrows == 1){
			this.arrows = this.selector.getElement(that.options.arrowsClass);	
			this.initArrows();
		}
		if(that.options.keyboard == 1){
			this.initKeyboard();
		}
	},
	initArrows: function(){
		var that = this;
		if(this.options.autoHideArrows == 1){
			this.arrows.fx = new Fx.Styles(this.arrows, {wait: false}).set({opacity:0});
			this.selector.addEvents({
				'mouseenter': function(){
					this.arrows.fx.stop().start({opacity: 1});
				}.bind(this),
				'mouseleave': function(){
					this.arrows.fx.stop().start({opacity: 0});
				}.bind(this)			
			});
		}
		else{
			this.arrows.fx = new Fx.Styles(this.arrows, {wait: false}).set({opacity:1});
		}
		
		['prev', 'next'].each(function(func, index){
			if(that.arrows.getElement('.' + func)){
				that.arrows.getElement('.' + func).addEvent('click', function(){
					that[func].call(that);
				});
			}
		});		
	},
	initKeyboard: function(){
		var that = this;
		var keyupEvt = function(e){
			e = new Event(e);			
			switch(e.key){
				case 'left': 
					that.prev();
					break;
				case 'right': 
					that.next();
					break;				
			}
		}.bind(this);		
		document.addEvent('keyup', keyupEvt);				
	},
	prev: function(){			
		if(this.current - this.options.itemsperclick >= 0){
			this.current -= this.options.itemsperclick;
		}
		else{
			this.current = 0;
		}
		this.slider.toElement(this.items[this.current]);
	},
	next: function(){
		if(this.current + this.options.itemsperclick < this.len){
			this.current += this.options.itemsperclick;
		}
		else{
			this.current = this.len - 1;
		}
		this.current = Math.min(this.current, this.len - this.itemsshow);
		this.slider.toElement(this.items[this.current]);
	}
});
JVSlider.implement(new Options);
JVSlider.implement(new Events);
