jQuery.fx.interval = 5;

var Home = {

	slider: null,
	progression: null,
	nbSlides: null,
	timer: null,	
	slideWidth: 920,
	pauseTime: 10000,
	slideAnimSpeed: 'slow',
	

	init: function(){
		Home.slider = $('.slider div.slides');
		Home.nbSlides = Home.slider.children('div.slide').size();
		Home.slider.data("currentSlide", 1).data("nbSlides", Home.nbSlides);
		
		Home.step_by_step = $('.step-by-step');
		Home.progress_bar = $('.progress-bar-active');
		Home.progress_text = $('.progress-text');
		
		Home.generatePagination();
		Home.timer = window.setTimeout(Home.nextSlide, Home.pauseTime);
	},
	
	
	
	/*
	 *	Create the progress bar pagination
	 */
	generatePagination: function(){
		Home.slider.children('div.slide').each(function(index) {
			var content = '<li>';
			content += '<span class="text">' + (index + 1) + '</span>';
			content += '<span class="active"><span class="top"></span><span class="bottom"></span></span>';
			content += '<span class="old"><span class="top"></span><span class="middle"><span class="pxTopLeft"></span><span class="pxTopRight"></span></span><span class="bottom"></span></span>';
			content += '<span class="unactive"><span class="top"></span><span class="middle"></span><span class="bottom"></span></span>';
			content += '</li>';
			$(content).appendTo(Home.progress_text.children('ul'));
		});
		Home.progress_text.find('ul li:first-child').addClass('active');
		Home.step_by_step.width(Home.progress_text.outerWidth());
		Home.progress_bar.width(Math.round((parseInt(Home.progress_text.find('ul li:first-child').css('marginLeft')) + (parseInt(Home.progress_text.find('ul li:first-child').outerWidth()) / 2))) + "px");
		
		// Home.progress_text.find('li').click(function(e){Home.goToSlide(e);});
	},	
	
	
	
	/*
	 *	Update the progress bar pagination
	 */
	updateProgression: function(toStep){
		Home.progress_text.find('li.active').removeClass('active').addClass('old');
		if(toStep ==  1){
			var new_width = Math.round((parseInt(Home.progress_text.find('ul li:first-child').css('marginLeft')) + (parseInt(Home.progress_text.find('ul li:first-child').outerWidth()) / 2))) + "px";
			Home.progress_text.find('li:not(:first-child)').removeClass('old').addClass('unactive');
			Home.progress_bar.animate({width: new_width, easing: 'linear'}, {duration:Home.slideAnimSpeed, complete: function(){
				Home.progress_text.find('li:first-child').addClass('active');
			}});
		}
		else{
			var new_active_element = Home.progress_text.find('ul li:eq('+ (toStep - 1) +')');
			var position = new_active_element.position();
			var new_width = Math.round(parseInt(position.left) + (parseInt(new_active_element.outerWidth()) / 2) + parseInt(new_active_element.css('marginLeft'))) + "px";
			Home.progress_bar.animate({width: new_width,  easing: 'linear'}, {duration: Home.slideAnimSpeed, complete: function(){
				new_active_element.addClass('active');
				if(new_active_element.get(0) === Home.progress_text.find('ul li:last-child').get(0)){
					Home.progress_bar.animate({width: '100%',  easing: 'linear'}, {duration: 400});
				}
			}});
		}
	},
	
	
	
	/*
	 *	Slide to the next item
	 */
	nextSlide: function(){
		window.clearTimeout(Home.timer);
		// If the current slide is the last slide		
		if(Home.slider.data("currentSlide") == Home.slider.data("nbSlides")){
			Home.updateProgression(1);
			Home.slider.animate({'marginLeft': "0px", easing: 'linear'}, {queue:false,  duration: Home.slideAnimSpeed,
				complete: function(){
					Home.slider.data("currentSlide", 1);
					Home.timer = window.setTimeout(Home.nextSlide, Home.pauseTime);
				}}
			);
		}
		// If the current slide is not the last slide
		else{
			margin = "-=" + Home.slideWidth + "px";
			Home.updateProgression(Home.slider.data("currentSlide") + 1);
			Home.slider.animate({'marginLeft': margin, easing: 'linear'}, {queue:false, duration: Home.slideAnimSpeed,
				complete:function(){
					Home.slider.data("currentSlide", Home.slider.data("currentSlide") + 1);
					Home.timer = window.setTimeout(Home.nextSlide, Home.pauseTime);
				}}
			);
		}
	},
	
	
	
	/*
	 *	Slide to a specific item	
	 */
	goToSlide: function(e){
		window.clearTimeout(Home.timer);
		itemPosition = parseInt($(e.target).html());
		margin = 0 - ((itemPosition - 1) * Home.slideWidth) + "px";
		if(parseInt(Home.slider.data("currentSlide")) != itemPosition){
			Home.updateProgression(itemPosition);
			Home.slider.animate({'marginLeft': margin}, {queue: false, duration: Home.slideAnimSpeed, 
				complete:function(){
					Home.slider.data("currentSlide", itemPosition);
				}}
			);
		}
	}
	
};

$(document).ready(Home.init);
