var slideshow = {
  fire: function(seconds){
	var interval = 0;
	var time = seconds * 1000;
	var slideNum = 0;
	var pause = false;
	var pauseTime = 0;
	var restack = false;
	var container = $('#slideshow');
	var slideshow = $('#slideshow img');
	var numberOfSlides = slideshow.length;
	var bullets = $('<div class="bullets"></div>');
	var caption = $('<div class="caption"></div>');
		
	//If slideshow has more than 1 slide then build show/////////////////////
	if (numberOfSlides > 1 && seconds){
		// Arrange order of slides and add links to bullet navigation
		$(slideshow).each(function(i){
			$(this).css({
				'z-index' : 0
			});
			//$(bullets).append('<a href="#" class="navB' + (i+1) +'">' + (i+1) + '</a>');
		});
		
		// Set default positions of first and second slide
		$(slideshow).eq(0).css({'z-index' : 2});
		$(slideshow).eq(1).css({'z-index' : 1});
		//comment($(slideshow).eq(0).attr("name"));
		
		// Load container with bullet and caption
		/*$(container)
			.append(bullets, caption)	
		createCaption();*/
		
		// Add current class to first bullet
		//currentBullet(slideNum);
		
		// Add functionality to navigation links
		$('#slideshow a')
			.click(function(e){
				e.preventDefault();
				if ($(slideshow).is(":animated")){
					return;
				}else{
					var linkName = $(this).html();
					arrangeNavStack(linkName);
					pauseTime = 0;
					
				    if(!pause){
					  pause = true;
					  startClock();
					  pauseAndPlay(); 
				    }else{
					  return;  
				    } 
				}
			});
		// Start Clock
		setTimeout(function() {startClock();},time)
	}
	
	function createCaption(){
		var position = bullets.position();
		var capW = $(caption).width();
		var capH = $(caption).height();
		var bulletH = $(bullets).height();
		var capP = $('#slideshow p');
		$(caption).css({'left': ( position.left - capW), 'top': ((position.top - capH) + bulletH)})
		$(capP).each(function(i){
			$(caption).append(this);
		});
	};
	
	// Add class to current bullet
	function currentBullet(bulletNum){
		$('.bullets a').removeClass('current');
		$('.bullets a').eq(bulletNum).addClass('current');
	}
	
	//Comment
	function comment(slideName){
		if(slideName){
			var comment = $('#slideshow .' + slideName);
			var capP = $('#slideshow p');
			var visible = $(comment).is(':visible');
			if(!visible){
			  if($.browser.msie){
				$(capP).hide();
			    $(comment).show();	
			  }else{
				$(capP).fadeOut(500);
			    $(comment).fadeIn(1000); 
			  } 
			}	
		}else{
			return;
		}
	};
	
	// Arrange navigation Stack
	function arrangeNavStack(linkName){
		restack = true;
		
		$(slideshow).each(function(i){
			$(this).css({
				'z-index' : 0
			});
		});
		
		var slideName = $(slideshow).eq(linkName - 1).attr('name')
			if((linkName - 1) == 0){
				$(slideshow).eq(0).css({'z-index' : 2});
				$(slideshow).eq(1).css({'z-index' : 1});
			}else if((linkName - 1) == numberOfSlides - 1){
				$(slideshow).eq(linkName - 1).css({'z-index' : 2});
				$(slideshow).eq(0).css({'z-index' : 1});
			}else{
				$(slideshow).eq(linkName - 1).css({'z-index' : 2});
				$(slideshow).eq(linkName).css({'z-index' : 1});
			}
			slideNum = linkName - 1;
			//currentBullet(linkName - 1);
			//comment(slideName);			
	}
		
	// ENGINE
	function engine (currentSlide){	
		$(slideshow).eq(currentSlide).fadeOut(function(){
			if(currentSlide == numberOfSlides - 2){
				++slideNum;
				$(slideshow).eq(currentSlide + 1).css({'z-index': 2});
				$(slideshow).eq(0).css({'z-index': 1});
			}else if(currentSlide == numberOfSlides - 1){
				slideNum = 0;
				$(slideshow).eq(0).css({'z-index': 2});
				$(slideshow).eq(1).css({'z-index': 1});
			}else{
				++slideNum;
				$(slideshow).eq(currentSlide + 1).css({'z-index': 2});
				$(slideshow).eq(currentSlide + 2).css({'z-index': 1});
			}
			$(this).show().css({'z-index' : 0});
		});		
	};
	
	// IGNITION
	function ignite(){
		if(restack){
				restack = false;
				$(slideshow).each(function(i){
					$(this).css({
						'z-index' : 0
					});
				});
				if(slideNum == 0){
					$(slideshow).eq(0).css({'z-index' : 2});
					$(slideshow).eq(1).css({'z-index' : 1});
				}else if(slideNum == numberOfSlides - 1){
					$(slideshow).eq(slideNum).css({'z-index' : 2});
					$(slideshow).eq(0).css({'z-index' : 1});
				}else{
					$(slideshow).eq(slideNum).css({'z-index' : 2});
					$(slideshow).eq(slideNum + 1).css({'z-index' : 1});
				}
			}
			
		if(slideNum == numberOfSlides - 1){
			interval = 0;
			$('.currentTime').replaceWith('<div class="currentTime">' + interval + '</div>');
			
			var slideName = $(slideshow).eq(0).attr('name');
			//currentBullet(0);
			//comment(slideName);
		}else{
			++interval
			$('.currentTime').replaceWith('<div class="currentTime">' + interval + '</div>');
			
			var slideName = $(slideshow).eq(slideNum + 1).attr('name');
			//currentBullet(slideNum + 1);
			//comment(slideName);
		}	
		engine(slideNum);
	};
	
	//PAUSE AND PLAY
	function pauseAndPlay(){
			if(pause && pauseTime <= 3){
				++pauseTime
				setTimeout(function() {pauseAndPlay();},1000)
			}else if(!pause){
				return;
			}else{
				pause = false;
				startClock();	
			}
		}
	
	//CLOCK
	function startClock(){
			if(!pause){
				ignite();
				setTimeout(function() {startClock();},time)
			}else{
				return;
			}		
		};   
  }
}
$(document).ready(function() {
  slideshow.fire(3); //Set the number of seconds between each slide.
});

