$(document).ready(function(){ overview.init();});

var isVideoOverview = false;
// Functions used in the overview block
var overview = {
	possibleIds : new Array('ul.img-overview-nav', 'ul.overview-nav'),
	listId : '',
	currentBlock : null,
		
	init: function(){
		
		// Check if we're dealing with an img or text overview, by checking which of the possible id's is present
		for ( var i=0; i<this.possibleIds.length; i++){
			if (  $(this.possibleIds[i]).length == 1  ){
				this.listId = this.possibleIds[i];
				break;
			}
		}
		// if it's an image block, we have to calc the exact width of the list.
		if ( this.listId.indexOf('img') > -1){
			var nrOfLis = $(this.listId).children('li').length;
			var width = $(this.listId).children('li:first').width();
			$(this.listId).css('width', (width * nrOfLis + nrOfLis  ) );
		}
		if(!isVideoOverview){
		
			// initialize menu for text or image overview (have different class names, because different look&feel 
			$( this.listId + ' li a').bind('click', overview.openBlock);
			$('.sub-block').hide();
			// if deeplinked, open appropriate sub-block
			var pos = document.location.href.lastIndexOf('#');
			if( pos > -1 ){ 
				this.openBlock( document.location.href.slice(pos+1) );	
			}else{ 
				// Show the first subblock
				this.openBlock( $(this.listId + ' li:first a').attr('href').slice(1) );
			}
			
		}else if(isVideoOverview){
			$( this.listId + ' li a').bind('click', overview.playVideo);
			overview.playVideo();
		}
	},
	openBlock : function(blockId){
		var divId = this.href ? this.href.slice( this.href.lastIndexOf('#') + 1 ) : blockId ;
		
		if (overview.currentBlock){ overview.currentBlock.hide(); }
		overview.currentBlock = $('#' + divId ).fadeIn();
		overview.updateNav();
		return false;
	},
	playVideo: function(){
		var blockId = this.href ? this.href.slice( this.href.lastIndexOf('#') + 1 ).replace('block-', '') : null;
		
		if (blockId){
			var flv = videoSources['vid' + blockId];
		}else{
			// not triggerd by link, so load first video
			for ( var i in videoSources){
				var flv = videoSources[i];
				break
			}
		}
		if( flv != ''){
			try{
				thisMovie('videoPlayer').playVid(flv, 'Dirk-Jan');
			}catch(e){}
		}		
		return false;
	},
	updateNav : function(){
		var divId = this.currentBlock.attr('id');
		$( this.listId + ' li a').each( function(){
			var a = $(this); 
			if (  a.attr('href').indexOf(divId) > 0 )
				a.addClass('current');
			else{
				a.removeClass('current');
			}
		});
	}
}

function thisMovie(movieName) {
	if (document.embeds && document.embeds[movieName]){
		return document.embeds[movieName]; 
	} else if (window.document[movieName]) {
		return window.document[movieName];
	} else {
		return document.getElementById(movieName);
	}
}