$.fn.ticketshow = function(){
	
	var me = this;
	var delay = 5000;
	var speed = 300;
	var prev;
	var current = 4;
	var next;
	var numSlides = $("img", me).size();
	var rotation;
	
	
	// rotation
	function rotate()
	{
		//console.log("Rotation... Current: "+current+" || Next: "+next);
		
		$('.ticketshow-btn', me).animate({left: '60'});
		$('.ticketshow-btn:eq('+current+')', me).animate({left: '40'});
	
		$("a.slide", me).hide();
		$("a.slide:eq("+next+")").fadeIn(speed);
		
		$('#ticketshow-title').text($('a.slide img:eq('+next+')', me).attr('alt'));
		
		current = (current + 1) % numSlides;
		setup();
	}
	function setup()
	{
		prev = current - 1;
		if(prev < 0) prev = numSlides - 1;
		
		next = (current + 1) % numSlides;
	}
	
	
	$('a', me).addClass('slide');
	
	// append essential elements
	$(me).append('<a href="#" class="ticketshow-btn"><img src="http://www.ticketpredator.com/images/ticketshow/slide_two.jpg" alt="2" style="top: 40px" /></a>');
	$(me).append('<a href="#" class="ticketshow-btn"><img src="http://www.ticketpredator.com/images/ticketshow/slide_three.jpg" alt="3" style="top: 80px" /></a>');
	$(me).append('<a href="#" class="ticketshow-btn"><img src="http://www.ticketpredator.com/images/ticketshow/slide_four.jpg" alt="4" style="top: 120px" /></a>');
	$(me).append('<a href="#" class="ticketshow-btn"><img src="http://www.ticketpredator.com/images/ticketshow/slide_five.jpg" alt="5" style="top: 160px" /></a>');
	$(me).append('<a href="#" class="ticketshow-btn"><img src="http://www.ticketpredator.com/images/ticketshow/slide_one.jpg" alt="1" /></a>');
	
	$(me).append('<div id="ticketshow-bg"></div>');
	$(me).append('<div id="ticketshow-title">Test</div>');
	
	
	$(".ticketshow-btn").each(function(i){
		$(this).click(function(){
			var cur = (i + 1) % numSlides;
		
			clearInterval(rotation);
			rotation = null;
			
			$("a.slide", me).hide();
			$("a.slide:eq("+cur+")").fadeIn(speed);
			
			$('.ticketshow-btn', me).animate({left: '60'});
			$('.ticketshow-btn:eq('+i+')', me).animate({left: '40'});
			
			$('#ticketshow-title').text($('a.slide img:eq('+cur+')', me).attr('alt'));
			
			current = cur;
			setup();
			return false;
		});
	});
	
	// hover
	$(me).hover(function(){}, function(){
		if(rotation == null) rotation = setInterval(rotate, delay);
	});
	
	
	setup();
	rotate();
	rotation = setInterval(rotate, delay);
};

$(document).ready(function(){ $('#ticketshow').ticketshow(); });