/**
 * Failsafe console.log/debug +  mapping to $.log
 * 
 */
if (!window.console) {
	window.console = { log: function(){}, debug: function(){} };
	$.log = function(){};
} else {
	if (window.console.debug) {
		if  (window.console.firebug) {
			$.log = console.debug;
		} else {
			$.log = function(obj) { console.debug(obj) };
		}
	} else if (window.console.log) {
		$.log = console.log;
	} else {
		$.log = function(){};
	}
}

/**
 * p(eppered) object
 * 
 */
var p = {};

p.gallery = {

$items: null,
$activeItem: null,
$navAnchors: null,
activeIndex: 0,
ready: false,

init: function(listId) {
	
	var _this = this;
	
	var $list = $('#' + listId);
	if ($list.length && $list.children().length > 1) {
		var $items = $list.children(),
			$nav = $('<ul class="nav" />');
			
		_this.$items = $items;
		
		$list.wrap('<div class="gallery" />');
		$list.parent().attr('id', $list.attr('id')).end().removeAttr('id').addClass('images');
		
		var maxHeight = 0;
		
		$items.each(function(i){
			var $this = $(this);
			var $a = $('<a href="#">' + (i+1) + '</a>').click(function(e) {
				e.preventDefault();
				var $this = $(this),
					index = $this.text()-1;
				
				_this.switchSlide(index);
				
			});
			$('<li/>').append($a).appendTo($nav);
			//$.log($this.outerHeight())
			
			var thisHeight = $this.outerHeight();
			if (thisHeight > maxHeight) {
				maxHeight = thisHeight;
			}
		});
		
		$list.css({display: 'block', position: 'relative', minHeight: maxHeight});
		$items.css({position: 'absolute', top: 0, left: 0, width: '100%' });
		
		$nav.insertBefore($list);
		_this.$navAnchors = $nav.find('a');
		_this.$navAnchors.eq(0).parent().addClass('active');
		
		$firstItem = $items.hide().eq(0).show();
		
		_this.$activeItem = $firstItem;
		_this.ready = true;
	
	}		
},

/**
 * @param {int} slideIndex 0-based
 */
switchSlide: function(slideIndex) {
	var _this = this, $newActiveItem, $currentNavItem;
	
	if (!_this.ready || _this.activeIndex == slideIndex) {
		return false;
	}
	else {
		_this.ready = false;
		$newActiveItem = this.$items.eq(slideIndex).fadeIn(700);
		_this.$navAnchors.parent().removeClass('active');
		$currentNavItem = _this.$navAnchors.eq(slideIndex).parent().addClass('active').end();
		_this.$navAnchors.not($currentNavItem).removeAttr('href').addClass('inactive wait').removeClass('enabled');

		this.$activeItem.fadeOut(700, function() {
			_this.$activeItem = $newActiveItem;
			_this.activeIndex = slideIndex;
			_this.$navAnchors.attr('href', '#').removeClass('inactive wait').addClass('enabled');
			_this.ready = true;
		});
	}	
}

}; /*/gallery */

/**
 * audiofragementen
 */
p.audioSnippets = {

init: function() {
	if (typeof swfobject != 'undefined'  && swfobject.hasFlashPlayerVersion('7')) {
		$('#audioFragmenten .mediafragment').click(function() {
			$(this).siblings().removeClass('active');
			$('#audioplayer').remove();
			var sUri = $(this).find('a').attr('href');
			var oUri = parseUri(sUri);
			if (oUri.domain != '')
				sFile = oUri.protocol + '://' + oUri.domain + oUri.path;
			else
				sFile = oUri.path;
			
			$('#audioFragmenten').append('<div id="audioplayer"></div>');

			var so = swfobject.createSWF({
				data:"/flash/audioplayer.swf?playMe=true&myFile=" + sFile,
				width: '125', height: '10'}, { bgcolor: '#ffffff' }, 'audioplayer'); 
			if (so) {
				$(this).addClass('active');
				return false;
			}
		})
		.hover(
			function(){	$(this).addClass('hover') },
			function(){	$(this).removeClass('hover') }
		);
	}
}	
		
}; /*/audioSnippets */



/**
 * Writings
 */
p.writings = {
	init: function() {
		var $section = $('#writings div.section'),
			label = ''
		;		
		
		$section.each(function() {
			var $section = $(this),
				minHeight
			;
			if ($section.hasClass('open')) {
				label = 'verbergen';
			}
			else {
				label = 'tonen';
				$section.children('.content').hide();
			}
			
			$('<a class="toggle" href="#">'+label+'</a>')
				.click(function(e){
					
					var $this = $(this),
						$section = $this.parent(),
						$content = $section.children('.content'),
						section_id = $section.attr('id')
					;
					
					//$section.siblings().children('.content').slideUp(function(){						
					//	$(this).parent().removeClass('open').addClass('closed');
					//});
					
					$content.slideToggle(function(){
						if ($content.is(':hidden')) {
							$section.removeClass('open').addClass('closed');
							$this.text('tonen');
						}
						else {
							$section.removeClass('closed').addClass('open');
							$this.text('verbergen');
						}
					});	
					
					e.preventDefault();
				})
				.appendTo($section)
				
			;
		});
	}
};




/**
 * DOMready scripts
 */
$(function(){
	
	// external links
	$("a[rel='external']").click(function(){window.open(this.href); return false});
	
	// dropmenu
	var oSnelmenu = new DickerFisch('#snelmenu',{Effect:0});
	
	// hide/show telefonisch reserveren uitlegjes
	$(".telResToggleButton").click(function(){
		voorstellingId = $(this).attr("id").substr(18);
		$("#telResToggleItem"+voorstellingId).toggle();
		return false;
	});
	
	sDefaultTxt = 'zoek...';
	$("#formZoeken-zoek").focus(function(){
		if (this.value == sDefaultTxt)
			this.value = '';
	});
	$("#formZoeken-zoek").blur(function(){
		if (this.value == '')
			this.value = sDefaultTxt;
	});

});


function addLoadEvent(func)
{
	var oldonload = window.onload;
	if(typeof window.onload != 'function') {
		window.onload = func;
	} else {
		window.onload = function() {
			oldonload();
			func();
		}
	}
}

/* parseUri JS v0.1, by Steven Levithan (http://badassery.blogspot.com)
*/
function parseUri(sourceUri){
    var uriPartNames = ["source","protocol","authority","domain","port","path","directoryPath","fileName","query","anchor"];
    var uriParts = new RegExp("^(?:([^:/?#.]+):)?(?://)?(([^:/?#]*)(?::(\\d*))?)?((/(?:[^?#](?![^?#/]*\\.[^?#/.]+(?:[\\?#]|$)))*/?)?([^?#/]*))?(?:\\?([^#]*))?(?:#(.*))?").exec(sourceUri);
    var uri = {};
    
    for(var i = 0; i < 10; i++){
        uri[uriPartNames[i]] = (uriParts[i] ? uriParts[i] : "");
    }

    if(uri.directoryPath.length > 0){
        uri.directoryPath = uri.directoryPath.replace(/\/?$/, "/");
    }
    
    return uri;
}

