var RockUK	= {
	UI: {}
};

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

	var initMapBoxout = function() {
		
		$('.boxinner img.map').hover(function(){
			$(this).attr('src', '/img/map-home-on.gif');
		}, function(){
			$(this).attr('src', '/img/map-home.gif');
		});
		
	};


	return {
		init: init
	};

}();



RockUK.UI.Home = function() {
	
	var slideshowID = 'slideshow';
	var slideshow;
	var timer;
	
	var speed 	= 3; // 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();
			});
			
		}
	};
	
	var startSlideshow = function()	{
		timer = setInterval('RockUK.UI.Home.showNextImage()', speed*1000);
		$('#stop-slideshow').html('<img src="/img/icon-pause.gif" alt="Pause" />');
	};
	
	var stopSlideshow = function() {
		clearInterval(timer);
		timer = false;
		$('#stop-slideshow').html('<img src="/img/icon-play.gif" alt="Play" />');
	};
	
	var showNextImage = function() {
		var img = slideshow.find('img.feature:last');
		img.fadeOut(function(){
			img.prependTo(slideshow).show();
		});
		
	};
	
	var showPrevImage = function() {
		slideshow.find('img.feature:first').hide().appendTo(slideshow).fadeIn();
	};
	
	var initNav = function() {
		var nav_html = '<a id="slideshow-prev" href="#"><img src="/img/icon-prev.gif" alt="Previous image" /></a> ';
		nav_html += '<a id="stop-slideshow" href="#"></a> ';
		nav_html += '<a id="slideshow-next" href="#"><img src="/img/icon-next.gif" alt="Next image" /></a>';
		$('#slideshow-util').append(nav_html);
		
		$('#stop-slideshow').click(function(e){
			e.preventDefault();
			if (timer) {
				stopSlideshow();
			}else{
				showNextImage();
				startSlideshow();
			}
			
		});
		
		$('#slideshow-next').click(function(e){
			e.preventDefault();
			stopSlideshow();
			showNextImage();
		});

		$('#slideshow-prev').click(function(e){
			e.preventDefault();
			stopSlideshow();
			showPrevImage();
		});
	};
	
	
	return {
		init: init,
		showNextImage: showNextImage
	};
	
	
}();

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

