// usage: log('inside coolFunc',this,arguments);
// http://paulirish.com/2009/log-a-lightweight-wrapper-for-consolelog/
window.log = function(){
  log.history = log.history || [];   // store logs to an array for reference
  log.history.push(arguments);
  if(this.console){
    console.log( Array.prototype.slice.call(arguments) );
  }
};


$(document).ready(function() {
	/**
	* TEASER
	**/
	startTeaser();
	
	// MEGAKNALLERS
	initSlide();
	
	// Slide buttons
	$('#button_box_links > img').bind('click', function(event) {
		slideBackwardClick();
	});
	$('#button_box_rechts > img').bind('click', function(event) {
		slideForwardClick();
	});
	$('#scroll_box').bind('mouseenter ', function(event) {
		stopAutoSlide(0);
	});
	$('#scroll_box').bind('mouseleave ', function(event) {
		startAutoSlide();
	});
});

 
var slide_pos = 2;
var slide_items = 0;
var slide_pages = 0;
var slide_perpage = 1;
var direction = 'forward';
var interVal = null;
var timeOut = null;
 
function initSlide() {
	slide_items = $('#scroll_box > .item').length;
	slide_pages = Math.ceil(slide_items / slide_perpage);
	updateSlide();
	startAutoSlide();
}
 
function updateSlide() {
	if(slide_pos == slide_pages) {
		$('#button_box_rechts > img').hide();
	} else {
		$('#button_box_rechts > img').show();
	}
	if(slide_pos == 1) {
		$('#button_box_links > img').hide();
	} else {
		$('#button_box_links > img').show();
	}
}

function slideForwardClick() {
	//stopAutoSlide();
	slideForward();
} 
function slideForward() {
	
	if(slide_pos < slide_pages && slide_pos < slide_pages) {
		$('#scroll_box').animate({"left": "-=374px"}, "slow");
		slide_pos++;
	}
	updateSlide();
	
}
function slideBackwardClick() {
	//stopAutoSlide();
	slideBackward();
} 
function slideBackward() {
	if(slide_pos > 1) {
		$('#scroll_box').animate({"left": "+=374px"}, "slow");
		slide_pos--;
	}
	updateSlide();
}


function startAutoSlide() {
	log('(re)starting autoslide');
	interVal = setInterval("doAutoSlide()",8000);
}

function stopAutoSlide(restart) {
	log('stopAutoSlide');
	clearInterval(interVal);
	clearTimeout(timeOut);
	if(restart == 1) {
		timeOut = setTimeout("startAutoSlide()",5000);
	}
	
}

function doAutoSlide() {
	log("doAutoSlide");
	if(direction == 'forward' && slide_pos < slide_pages) {
		log("slideForward");
		slideForward();
	} else if(direction == 'backward' && slide_pos > 1) {
		log("slideBackward");
		slideBackward();
	}
	
	/**
	* Nieuwe direction bepalen
	**/
	if(slide_pos == slide_pages) {
		// Einde, terug
		direction = 'backward';
	} else if(slide_pos == 1) {
		// Begin, naar voren
		direction = 'forward';
	}
}















/**
* TEASER (homepage)
**/
var TeaserTimeout = null;
var TeaserIndex = null;
var TeaserMouseOn = false;
var TeaserLastIndex = 1;
var TeaserInterval = null;
var TeaserDirection = 'forward';

function startTeaser() {
	//console.log('starting teaser');
	$('#slides_voorpagina_tabs > div').bind('mouseenter', function(event) {
		if(TeaserTimeout == null)
			clearTimeout(TeaserTimeout);
		
		index = $(this).attr('id').substring(3);
		TeaserIndex = index;
		TeaserTimeout = setTimeout("gotoTeaserItem()",100); // om te voorkomen dat elke tab meteen infade.
	});
	
	/**
	* Mouse events opvangen zodat we niet "autoplayen" tijdens mouseover
	**/
	$('#slides_voorpagina').bind('mouseenter', function(event) {
		TeaserMouseOn = true;
	});
	$('#slides_voorpagina').bind('mouseleave', function(event) {
		TeaserMouseOn = false;
	});
	
	TeaserInterval = setInterval("doAutoplay()",5000);			
	
}

function doAutoplay() {
	if(TeaserMouseOn == false) {
		if(TeaserDirection == 'forward') {
			TeaserIndex = parseInt(TeaserLastIndex)+1;
		} else {
			TeaserIndex = parseInt(TeaserLastIndex)-1;
		}
		gotoTeaserItem();
	}
}

function gotoTeaserItem() {
	if(TeaserIndex == null)
		return false;

	slide = TeaserIndex;
	$('#slides_voorpagina_slides > div').each(function(index, value) {
		if($(this).attr('id') != 'slide'+slide) {
			if($(this).css('display') != "none") {
				$(this).fadeOut("slow");
			}
			$('#tab'+(index+1)).removeClass('active');
		} else {
			$('#slides_voorpagina_slides').stop();
		 $(this).fadeIn("slow");
			$('#tab'+(index+1)).addClass('active');
		}
	});
	
	/**
	* Richting updaten
	**/
	if(TeaserIndex == $('#slides_voorpagina_slides > div').length) {
		TeaserDirection = 'backward';
	} else if(TeaserIndex == 1) {
		TeaserDirection = 'forward';
	}
	
	TeaserLastIndex = TeaserIndex; // voor autoplay
	TeaserIndex = null;
	TeaserTimeout = null;
}
