var StanleyPark	= {
	UI: {}
};

StanleyPark.UI.General = function() {
	
	var init	= function() {
		StanleyPark.UI.Home.init();
	};


	return {
		init: init
	};

}();



StanleyPark.UI.Home = function() {
	
	var slideshowID = 'slideshow';
	var slideshow;
	var timer;
	
	var images	= ['blue', 'orange', 'green', 'purple'];
	var inactive_image = 'brown';
	var image_path	= '/img/dot-%s.gif';
	
	var speed 	= 4; // seconds
	
	var init	= function() {
		initSlideshow();
	};
	
	var initSlideshow = function() {
		slideshow = $('#'+slideshowID);
		
		if (slideshow.length) {
		
			initNav();
		
			$.get('/_ajax/slideshow/get', function(data){
				slideshow.find('img:first').after(data);
				slideshow.find('img:first').remove();
				startSlideshow();
				createDots();
			});
			
		}
	};
	
	var startSlideshow = function()	{
		timer = setInterval('StanleyPark.UI.Home.showNextImage()', speed*1000);
		$('#stop-slideshow').text('Stop Slideshow');
	};
	
	var stopSlideshow = function() {
		clearInterval(timer);
		timer = false;
		$('#stop-slideshow').text('Start Slideshow');
	};
	
	var showNextImage = function() {
		var img = slideshow.find('img.feature:last');
		img.fadeOut(function(){
			img.prependTo(slideshow).show();
			
			switchDots(img, $('img.feature:last'));
		});
		
	};
	
	var initNav = function() {
		var nav_html = '<div id="slideshow-util"><a id="stop-slideshow" href="#" id="stop-slideshow"></a></div>';
		slideshow.append(nav_html);
		
		$('#stop-slideshow').click(function(e){
			e.preventDefault();
			if (timer) {
				stopSlideshow();
			}else{
				showNextImage();
				startSlideshow();
			}
			
		});
	};
	
	var createDots = function() {
		var imgs = slideshow.find('img.feature');
		var i, l;
		var ul	= $('<ul id="images"></ul>');
		var dotidx = 0;
		for (i=imgs.length-1; i>=0; i--) {
			var id = ($(imgs[i]).attr('id').replace('img', 'dot'));

			ul.append('<li><a href="#" id="link'+id+'"><img src="/img/dot-'+inactive_image+'.gif" alt="" class="'+images[dotidx]+'" id="'+id+'" /></a></li>');
			
			if (dotidx+1 >= images.length) {
				dotidx = 0;
			}else{
				dotidx++;
			}
		}
		
		var firstimg = ul.find('img:first');
		firstimg.attr('src', image_path.replace('%s', firstimg.attr('class')));
		
		ul.find('a').click(function(e){
			e.preventDefault();
			stopSlideshow();
			var previmg = slideshow.find('img.feature:last');
			var img = $('#'+$(this).find('img:first').attr('id').replace('dot', 'img'));
			switchDots(previmg, img);
			var idx = img.prevAll().size();
			var tmp = $('<div></div>');
			slideshow.find('img.feature:gt('+idx+')').each(function(i, o){
				$(o).appendTo(tmp);
			});
			tmp.find('img').prependTo(slideshow);
		});
		
		
		$('#slideshow-util').append(ul);
	};
	
	var switchDots = function(oldimg, newimg) {
		var olddot = $('#'+$(oldimg).attr('id').replace('img', 'dot'));
		var newdot = $('#'+$(newimg).attr('id').replace('img', 'dot'));
		
		newdot.attr('src', image_path.replace('%s', newdot.attr('class')));
		olddot.attr('src', image_path.replace('%s', inactive_image));
	};
	
	return {
		init: init,
		showNextImage: showNextImage
	};
	
	
}();

jQuery(function($) { StanleyPark.UI.General.init(); });
