/**
 * 
 */
 
jQuery.fn.extend({
	// Based off of the plugin by Clint Helfers, with permission.
	// http://blindsignals.com/index.php/2009/07/jquery-delay/
	delay: function( time, type ) {
		time = jQuery.fx ? jQuery.fx.speeds[time] || time : time;
		type = type || "fx";

		return this.queue( type, function() {
			var elem = this;
			setTimeout(function() {
				jQuery.dequeue( elem, type );
			}, time );
		});
	}
});

var lastPage = 0;
var active = 0;

function startTeaser()
{
	var n = 0;
	var url = a[0];
//	$('#teaser_link_' + n).css('background-image','url(' + knob_highlight + ')');
	$('#teaser_link_' + n).addClass('teaser_highlight');
	lastPage = n;
	$.get(url, function(data) {
		$('#teaser_content').fadeOut(0);	
		$('#teaser_content').html(data);
		$('#teaser_content').fadeIn(1000, function(){
			
			if (++n == a.length)
				n = 0;
				/*
			$('#teaser').delay(9000).queue(function(){
				requestPage(n, true);
			});*/
			active = setInterval(function () {requestPage(n);}, 9000);
		});
		//$('#teaser_content').dequeue();
	});
}



function requestPage(n, isQueue) {
	var url = a[n];
	clearInterval(active);
	var reqN = n;

	$('#teaser_content').stop(true, true);
	$.get(url, function(data) {
		$('#teaser_content').fadeOut(1000, function() {
			$(this).html(data);	
			$('#teaser_link_' + lastPage).removeClass();
			$('#teaser_link_' + lastPage).addClass('teaser');			
			$('#teaser_link_' + reqN).addClass('teaser_highlight');
			lastPage = reqN;
			$(this).fadeIn(1000, function(){
			});
		});
	});
	if (++n == a.length)
		n = 0;
	active = setInterval(function () {requestPage(n);}, 9000);
}


