$(document).ready(function() {
	
	// setup the slideshow
	$('.hp_poster_slide').hide();
	$('.hp_poster_slide:first').show();
	
	var killtimer = false;
	
	$.timer(5000,function(timer){
		if(killtimer){
			timer.reset(5000);
			killtimer = false;
		} else {
			advance_slideshow();
		}
	});

	
	$('.poster_dot').click(function(){
		var clickdot = this;
		if(!$(this).hasClass('poster_dot_on'))
		{
			$('.poster_dot').removeClass('poster_dot_on').addClass('poster_dot_off');
			$(this).removeClass('poster_dot_off').addClass('poster_dot_on');

			var fadeout_image = $('.hp_poster_slide:visible');
			var fadein_image = '#' + $(this).attr('rel');
			$('#hp_poster_frame').append(fadeout_image);

			$(fadein_image).fadeIn(500,function(){
				fadeout_image.fadeOut(500);

			});
			killtimer = true;
		}
	});

});


function advance_slideshow()
{
	var fadeout_image = $('.hp_poster_slide:visible');
	var fadein_dot  = $('.poster_dot_on').removeClass('poster_dot_on').addClass('poster_dot_off').next();
	if(fadein_dot.length == 0)
	{
		fadein_dot = $('.poster_dot:first');
	}
	fadein_dot.removeClass('poster_dot_off').addClass('poster_dot_on');
	var fadein_image = '#' + fadein_dot.attr('rel');
	
	$('#hp_poster_frame').append(fadeout_image);
	
	$(fadein_image).fadeIn(500,function(){
		fadeout_image.fadeOut(500);
		
	});
}



/*
 *
 *	jQuery Timer plugin v0.1
 *		Matt Schmidt [http://www.mattptr.net]
 *
 *	Licensed under the BSD License:
 *		http://mattptr.net/license/license.txt
 *
 */
 jQuery.timer = function (interval, callback)
 {
	var interval = interval || 100;

	if (!callback)
		return false;
	
	_timer = function (interval, callback) {
		this.stop = function () {
			clearInterval(self.id);
		};
		
		this.internalCallback = function () {
			callback(self);
		};
		
		this.reset = function (val) {
			if (self.id)
				clearInterval(self.id);
			
			var val = val || 100;
			this.id = setInterval(this.internalCallback, val);
		};
		
		this.interval = interval;
		this.id = setInterval(this.internalCallback, this.interval);
		
		var self = this;
	};
	
	return new _timer(interval, callback);
 };
