$(document).ready(function(){

	var pauseState = 0;
	var pauselink = 'pause';
	var nxtlink = 'nxt';
	var bcklink = 'bck';
	var hs_current = 0;
	var hs_last = 0;
	var hs_timeout = 6000;
	var fwdbck = 'fwd';
	
   	$("#nxt").hide();
   	$("#bck").hide();
   	$("#pause").hide();
	
	
	// Make Array
	var slides = $("#slideshow2 img").get();
	
	// Make Imgs Hide
	for (var i = 0; i < slides.length; i++) {
		jQuery(slides[i]).css('display', 'none');	
	}
	
	// Assign zIndex
	jQuery.each(slides, function(i){
		jQuery(slides[i]).css('zIndex', slides.length - i).css('position', 'absolute').css('top', '0').css('left', '0');
	});
   	
	// Show first
	jQuery(slides[hs_last]).css('display', 'block').css('zIndex', '0');
	//jQuery(slides[hs_current]).css('zIndex', '1').fadeIn('slow');


	var change = function () {
		if ( pauseState == 0 ) {
			for (var i = 0; i < slides.length; i++) {
				jQuery(slides[i]).css('display', 'none');
			}
			
			
			if ( ( hs_current + 1 ) < slides.length ) {
				if (fwdbck == 'bck') {
					hs_current = hs_current - 1;
					hs_last = hs_current + 1;
				} else {
					hs_current = hs_current + 1;
					hs_last = hs_current - 1;
				}
			}
			else {
				hs_current = 0;
				hs_last = slides.length - 1;
			}
			
			
			jQuery(slides[hs_last]).css('display', 'block').css('zIndex', '0');
			jQuery(slides[hs_current]).css('zIndex', '1').fadeIn('slow');
			
			timer = setTimeout(change, hs_timeout);
		}
	}
	
	// PAUSE
	var pause = function() {
		if ( pauseState == 0 ) {
			pauseState = 1;
			clearTimeout(timer);
		}
		else {
			pauseState = 0;
			fwdbck = 'fwd';
			clearTimeout(timer);
			change();
		}
		//
	}
	
	//NEXT
	var nxt = function() {
		fwdbck = 'fwd';
		pauseState = 0;
		change();
		pauseState = 1;
		clearTimeout(timer);
	}
	
	// BACK
	var bck = function() {
		if (hs_current > 0) {
			fwdbck = 'bck';
			pauseState = 0;
			change();
			pauseState = 1;
		}
		clearTimeout(timer);
	}
	
// PSUEDO-HOVER
  $('#slideshow2_holder').hover(function() {
	pauseState = 1;
   	$("#nxt").fadeIn();
	$("#bck").fadeIn();	
	$("#pause").fadeIn();	
  }, function() {
	setTimeout(function(){
		pauseState = 0;
		clearTimeout(timer);
		$("#nxt").fadeOut();
		$("#bck").fadeOut();
		$("#pause").fadeOut();	
			change();

	}, 3000); return false;

  });
	
	// Start It Up
	timer = setTimeout(change, hs_timeout);
	
	
	// Listen for...
	if ( pauselink != null ) {
		jQuery('#' + pauselink).click(pause);
	}
	if ( nxtlink != null ) {
		jQuery('#' + nxtlink).click(nxt);
	}
	if ( bcklink != null ) {
		jQuery('#' + bcklink).click(bck);
	}
	

});