/**
 * Class PromoSlider
 * 
 * @include "jquery/jquery-min-1.5.js"
 */

(function(){ 

	/**
	 * Constructor
	 */	
	PromoSlider = function() {
		this.scope = this;
		
		// Containers
		this.layout = $(window);
		this.navigationContainer = $(".main-navigation");
		this.navigationItemContainer = this.navigationContainer.find("LI");
		this.sliderContainer = $(".promo-slider");
		this.sliderItemContainer = this.sliderContainer.find("LI");
		this.sliderItemMainContainer = $(".logo>A");
		this.sliderNavigationContainer = $(".promo-navigation");
		this.sliderNavigationItemContainer = this.sliderNavigationContainer.find("LI");
		
		//Other
		
		this.sliderItemIndex = 0;
		this.hashArray = ['main', 'site', 'seo', 'context-ads', 'media-ads', 'support'];
		this._layoutWidth = 0;
	};	
	
	PromoSlider.prototype = {
	
		init:function() {
			this.buildSliderItems();
			this.layout.resize($.proxy(this.scope, "buildSliderItems"));
			this.sliderNavigationItemContainer.click($.proxy(this.scope, "currentIndexCounter"));
			this.navigationItemContainer.click($.proxy(this.scope, "gotoFromNavigation"));
			this.sliderItemMainContainer.click($.proxy(this.scope, "gotoFromNavigation"));
			//this.layout.hashchange($.proxy(this.scope, "updateSliderItemsPosition"))			
		},	
		
		updateSliderItemsPosition:function(e) {
			var _flag = true;
			var _hash_array = location.hash.split('/');
			var _index = this.getCurrentItemIndex();

			for(var i in this.hashArray) {
				if(this.hashArray[i] == _hash_array[1]) {
					_index = i;
				}
			}		
			
			if(_index == -1 && e['originalEvent']['type']!=undefined) {
				this.setCurrentItemIndex(0);
				this.gotoToItem(this.getCurrentItemIndex(), false);
			} else {
				this.setCurrentItemIndex(_index);
				this.gotoToItem(this.getCurrentItemIndex(), true);
			}
			
			if(e!=undefined) {
				if(e.originalEvent.type=='hashchange') {
					_flag = false;
				}
			}			
			
			this.gotoToItem(this.getCurrentItemIndex(), _flag);
		},
		
		buildSliderItems:function() {
			this._layoutWidth = this.layout.width();
			this.sliderContainer.css('width', (this.sliderItemContainer.length*this._layoutWidth)+'px');
			this.sliderItemContainer.width(this._layoutWidth);
			this.updateSliderItemsPosition();
			return false;
		},

		gotoToItem:function(index, animate) {
			var _x = -(index*this._layoutWidth)+'px';
			
			if(!animate) {
				this.sliderContainer.animate({left:_x}, 1000, 'easeInQuint', $.proxy(this.scope, "changeHash"));
			} else {
				this.sliderContainer.css('left', _x);
				this.changeHash();
			}
			
			this.sliderItemContainer.removeClass('current');
			this.sliderContainer.find('LI:eq('+this.getCurrentItemIndex()+')').addClass('current');		

			this.navigationItemContainer.removeClass("active");
			this.navigationContainer.find('LI:eq('+this.getCurrentItemIndex()+')').addClass('active');
		},
		
		changeHash:function() {
			location.hash = "/"+this.hashArray[this.getCurrentItemIndex()]+"/";
			return false;
		},
		
		gotoFromNavigation:function(e) {
			if($(e.currentTarget).parent().hasClass('logo')) {
				this.setCurrentItemIndex(0);
				this.gotoToItem(this.getCurrentItemIndex(), false);			
			} else {
				this.setCurrentItemIndex($(e.currentTarget).index());
				this.gotoToItem(this.getCurrentItemIndex(), false);			
			}
			e.preventDefault();	
		},		
		
		currentIndexCounter:function(e) {
			e.preventDefault();
			
			var _counter = this.getCurrentItemIndex()

			if($(e.currentTarget).hasClass('next-arr')) { 
				_counter+=1; 
			}

			if($(e.currentTarget).hasClass('prev-arr')) { 
				_counter-=1; 
			} 

			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 Number(this.sliderItemIndex);
		}
	}
})();

$(function(){ 
	var promoSlider = new PromoSlider();
	promoSlider.init();
	
	$('.link.scroll').click(function (event)
	{
		event.preventDefault();
		
		$.scrollTo($('#scrollTo'), 'fast', {axis: 'y', offset: {top: -15}});
	});
})
