/****************************************
(c) 2011 Martijn Hardenberg, updated and simplefied by Tomas Klinkenberg
Bestand: functies.js
****************************************/

function extractID(object) {
	var str = $(object).attr("id");
	return str.substring(str.length - 1, str.length);
}

var $$ = $.fn;
$$.extend({
	//slideshow class
	Slideshow : {
		//constructor
		Ready : function(slides) {
			this.amountofslides = slides;
			for(var i=2;i<this.amountofslides+1;i++){
				$('#home_uitgblok-'+i).hide();
			}
			this.Queue = 0;
			this.Counter = 2;
			this.Interrupted = false;

			clearTimeout(this.tijd);
			this.tijd = setTimeout('$$.Slideshow.Transition();', 5000);
		},
		//resume the slideshow
		Resume : function() {
			this.Interrupted = false;
			clearTimeout(this.tijd);
			this.tijd = setTimeout('$$.Slideshow.Transition();', 2000);
		},
		//interrupt the slideshow
		Interrupt : function() {
			this.Interrupted = true;
		},
		//change the frame of the slideshow
		ChangeFrame : function(counter) {
			$$.Slideshow.Interrupt();
			this.CounterChanged = true;
			this.Counter = counter;
			$$.Slideshow.Transition();
			this.CounterChanged = false;
		},
		//make the transition beween slides
		//slides fast if it is done by user, slow by JS
		Transition : function() {
			//checks if a transition is allowed
			if (this.Interrupted && !this.CounterChanged) {
				return;
			}
			
			//clears the image
			for(var i=1;i<this.amountofslides+1;i++){
				if(this.Counter != i) {
					if(this.CounterChanged)
						$('#home_uitgblok-' + i).fadeOut(10);
					else
						$('#home_uitgblok-' + i).fadeOut(1000);
				}
				$('#home_uitg-' + i).removeClass('active');
			}
			
			//shows the image
			if(this.CounterChanged)
				$('#home_uitgblok-' + $$.Slideshow.Counter).fadeIn(10);
			else
				$('#home_uitgblok-' + $$.Slideshow.Counter).fadeIn(1000);
				
			$('#home_uitg-' + $$.Slideshow.Counter).addClass('active');

			//updates the counter
			$$.Slideshow.Counter++;

			if ($$.Slideshow.Counter > this.amountofslides)
				$$.Slideshow.Counter = 1;
			
			//sets the time for the next transition
			clearTimeout(this.tijd);
			this.tijd = setTimeout('$$.Slideshow.Transition();', 5000);
		}
	}
});

$(document).ready(
	function() {
		//start slideshow
		$$.Slideshow.Ready(3);
	}
);

$("#home_uitgelicht li").mouseover(function() {
	//extracts the ID from this var
	var nmb = extractID(this);
	
	//changes the frame
	$$.Slideshow.ChangeFrame(nmb);
});

$("#home_uitgelicht li").mouseout(function() {
	//resumes slideshow
	$$.Slideshow.Resume();
});
