﻿(function(){ 

	/**
	 * Constructor
	 */	
	Slider = function() {
		this.scope = this;
		
		// Containers
		this.sliderContainer = null;
		this.sliderItemContainer = null;
		this.sliderNavigationContainer = null;
		this.sliderNavigationItemContainer = null;
		this.sliderNavigationControl = null;
		
		this.containerItemClass = '';
	
		//Other
		
		this.sliderItemIndex = 0;
		this.prevSliderItemIndex = 0;
		this.sliderTimeout = null;
		this.sliderTimeoutInterval = 10000;
		this.isSlideShows = false;
		this.controlMode = 'pause';

	};	
	
	Slider.prototype = {
	
		init:function(container, navigation, _containerItemClass, isSlideShow) {

			if(_containerItemClass!=undefined) {
				this.containerItemClass = _containerItemClass;
			} 
			this.isSlideShows = isSlideShow;
			this.sliderContainer = $(container);
			this.sliderItemContainer = this.sliderContainer.find("LI"+_containerItemClass)
			this.sliderNavigationContainer = $(navigation);
			this.sliderNavigationItemContainer = this.sliderNavigationContainer.find("LI").not('.control');
			this.sliderNavigationControl = this.sliderNavigationContainer.find("LI.control");
			
			this.sliderNavigationItemContainer.click($.proxy(this.scope, "currentIndexCounter"));
			this.sliderNavigationControl.click($.proxy(this.scope, "switchMode"));
			
			this.gotoToItem(this.sliderItemIndex);	

		},
		
		switchMode:function(e) {
			var currentItem = $(e.currentTarget);
			if(currentItem.hasClass('pause')) {
				this.controlMode = 'play';
				this.isSlideShows = false;
				currentItem.removeClass('pause').addClass('play');
				clearInterval(this.sliderTimeout);
			} else {
				this.controlMode = 'pause'
				this.isSlideShows = true;
				currentItem.removeClass('play').addClass('pause');
				
				var _counter = this.getCurrentItemIndex()+1;

				if(_counter>this.sliderItemContainer.length-1) { 
					_counter = 0;
				}
				
				if(_counter<0) { 
					_counter = this.sliderItemContainer.length-1; 
				}

				this.setCurrentItemIndex(_counter);
				this.gotoToItem(this.getCurrentItemIndex());								
			}
			return false;
		},
		
		gotoToItem:function(index, animate) {
			this.sliderItemContainer.fadeOut(500);
			this.sliderContainer.find('LI'+this.containerItemClass+':eq('+index+')').fadeIn(500);
			this.sliderNavigationItemContainer.removeClass('active')
			this.sliderNavigationContainer.find('LI:eq('+index+')').addClass('active');
			if(this.isSlideShows){
				clearInterval(this.sliderTimeout);
				this.sliderTimeout = setInterval($.proxy(this.scope, "slideshow"), this.sliderTimeoutInterval);
			}				
		},
		
		slideshow:function() {
			var index = this.getCurrentItemIndex();
				index+=1;
				if(index>=this.sliderItemContainer.length) { index = 0; }	
				this.setCurrentItemIndex(index);
				this.gotoToItem(index);
		},
	
		currentIndexCounter:function(e) {

			e.preventDefault();
			
			if(this.isSlideShows){
				clearInterval(this.sliderTimeout);
			}				
			
			var _counter = $(e.currentTarget).index();

			if(_counter>this.sliderItemContainer.length-1) { 
				_counter = 0;
			}
			
			if(_counter<0) { 
				_counter = this.sliderItemContainer.length-1; 
			}

			this.setCurrentItemIndex(_counter);
			this.gotoToItem(this.getCurrentItemIndex());
			
		},
		
		setCurrentItemIndex:function(index) {
			this.sliderItemIndex = index;
		},
		
		getCurrentItemIndex:function() {
			return this.sliderItemIndex;
		}		
		
	}
})();

$(function(){ 
	var _promoSlider = new Slider();
	_promoSlider.init('.promo-photo', '.promo-navi', '', true);
})
