var slideshow_handler = {
    container: null,
	nav_items: [],
    total_items: null,
    last_item: null,
    delay: 4000,
	animation: true,
	slides: [],
	to: null,
	is_animating: false,
    
    setup: function()  {
		this.container = $('#home-slideshow');
		if(this.container.length == 0) return;
		$(this.container).addClass('loaded');

		this.slides = $('.slide', this.container);

		//create menu
		var menu_ul = '<ul>';
		this.slides.map(function(i, el){
		  var current_heading = $('h2', el).first();
		  menu_ul += '<li><a href="#"><span>' + $(current_heading).text() + '</span></a></li>';
		});
		menu_ul += '</ul>';
		$(this.container).append(menu_ul);

		this.nav_items = $(this.container).find('li');
		$(this.nav_items).first().addClass('active first-child');
		this.total_items = this.nav_items.length;
		
		this.last_item = 0;
		
		$(this.slides).first().addClass('active-slide');

		//vertical align
		$(this.nav_items).map(function(i, el){
			var current_text = $('span', el).first();
			var text_height = $(current_text).innerHeight();
			if(text_height <  80) {
				var padding_half = Math.round((80- text_height)/2);
				$(current_text).css({'padding-top':padding_half});
			}
			
		})      	

		$('li a', this.container).bind('click', function(event) {
			event.preventDefault();
			
			var parent_li = $(event.currentTarget).parents('li').first();
			if($(parent_li).hasClass('active')) return;
			
			slideshow_handler.animation = false;
			clearTimeout(slideshow_handler.to);
			
			var current_index = $(slideshow_handler.nav_items).index(parent_li);
			
			slideshow_handler.switchPic(current_index);
		});	

		this.to = setTimeout(function(){

			next_index = slideshow_handler.last_item + 1;
			if(next_index == slideshow_handler.total_items) {
				next_index = 0;
			}
			
			slideshow_handler.switchPic(next_index);
		}, slideshow_handler.delay);
		
    },
    
    switchPic: function(next_index) {
		if(slideshow_handler.is_animating) return;
		slideshow_handler.is_animating = true;
		
		$(slideshow_handler.container).removeClass('slideshow-over');

		$(slideshow_handler.nav_items).removeClass('active');
		$(slideshow_handler.nav_items[next_index]).addClass('active');

		var last_pic = $('#home-slideshow .active-slide');
		$(last_pic).stop(true, true).animate({
			queue: false,
			opacity: 0
		  }, 400, function() {
			
		});
		setTimeout(function(){
		  $(last_pic).removeClass('active-slide');
		  slideshow_handler.is_animating = false;
		  $(slideshow_handler.container).addClass('slideshow-over');
		}, 420);
	
		var next_pic = $('#home-slideshow .slide')[next_index];
		$(next_pic).css({'opacity':'0'});
		$(next_pic).addClass('active-slide');
		$(next_pic).stop(true, true).animate({
			queue: false,
			opacity: 1
		  }, 400, function() {
			// Animation complete.
		});
	
		this.last_item = next_index;
		
		if(slideshow_handler.animation == true) {
			slideshow_handler.to = setTimeout(function(){
	
				next_index = slideshow_handler.last_item + 1;
				if(next_index == slideshow_handler.total_items) {
					next_index = 0;
				}
				
				slideshow_handler.switchPic(next_index);
			}, slideshow_handler.delay);
		}
		
    }	
}
