/**
 * Common functions/objects & page scripts
 * 
 * Also Peppered config (jQuery.ppprd) is defined here 
 * 
 * prereq: jQuery (1.2.x, 1.3.x)
 * load: head
 * 
 * @author AK
 */

;if (typeof jQuery !== 'undefined') {
(function($){

/**
 * various functions and definitions
 *
 */

/**
 * Peppered config
 */
$.ppprd = {
	debug: {
		enabled: false,
		logAlerts: false // alerts for clients that don't have the firebug console (otherwise silent)
	}
};	

/**
 * Debug/logging
 * Uses Firebug (FF), or native console (Safari, IE8)
 * Note: Firebug window.console only exists if the panel is open/active
 */

if ($.ppprd.debug.enabled) {
	if (window.console && window.console.debug) {
		if  (window.console.firebug) {
			$.log = console.debug;
		}
		else { // Safari doesn't like the above function mapping
			$.log = function(obj) { console.debug(obj) };
		}
	}
	else if (window.console && window.console.log) {
		$.log = console.log;
	}
	else if ($.ppprd.debug.logAlerts) {
		$.log = function(obj) { alert(obj) };
	}
	else {			
		$.log = function(){};
	}		
}
else {
	$.log = function(){};
}


/**
 * Image Replace
 * Appends some spans to selection, to aid in IR (css)
 * @author AK|Peppered
 */
$.fn.ImageReplace = function(){
	return this.each(function(){
		$(this).addClass('ir');
		$(this).append('<span class="ir-aid"><span></span></span>');
	});
};


/**
 * autoSelect, jQuery plugin
 * 
 * Auto-submit forms when select options have changed
 * Smoothens out (almost) behavioural differences between browsers.
 * Inpsired on: http://themaninblue.com/writing/perspective/2004/10/19/
 * 
 * usage: $selection.autoSelect({options})
 * 
 * @author AK | Peppered
 * @version 1.0.1
 */
(function(a){a.fn.autoSelect=function(){return this.each(function(){var b=a(this);b.validKeyChange=true;b.initValue=a(this).val();b.change(function(){if(b.validKeyChange&&(this.value!=b.initValue)){b.initValue=this.value;this.form.submit()}});b.keydown(function(c){if((c.keyCode==13||c.keyCode==9)&&this.value!=b.initValue){b.validKeyChange=true;b.change()}else{if(c.keyCode==27||((c.keyCode==13||c.keyCode==9)&&this.value==b.initValue)){this.value=b.initValue}else{b.validKeyChange=false}}})})}})(jQuery);



/**
 * bg img resize function
 * 
 * requires: jQuery (developed & tested with 1.3.2)
 * 
 * globals (window): bgImgWidth, bgImgHeight
 * 
 * @author AK
 * 
 * @todo minification for prod.
 *  
 */	
$.scaleableBgImg = function(options) {		
	options = $.extend({
		bgImgWidth: 0,
		bgImgHeight: 0,
		contentDiv: 'body'
	}, options);
	
	var imgContainer = $('#bg');
	var img = imgContainer.children('img');
	var contentDiv = $('options.contentDiv');
	
	var contentWidth = contentDiv.width();
	var contentHeight = contentDiv.height();
	
	var bgImgWidth = options.bgImgWidth;
	var bgImgHeight = options.bgImgHeight;
	
	var imgXAspect = bgImgWidth/bgImgHeight;
	var imgYAspect = bgImgHeight/bgImgWidth;
	
	var minWidth = bgImgWidth;	/* min.width of that image should have */
		
	function resize() {
		var viewportWidth = $(window).width();
		var viewportHeight = $(window).height();
		
		img.width('100%');
		var newImgWidth  = img.width();
		
		// not shorter than minWidth
		if (newImgWidth < minWidth) {
			newImgWidth = minWidth;
		}
		var newImgHeight = newImgWidth * imgYAspect;
		
		// height not shorter than viewportHeight or contentHeight
		if (newImgHeight < viewportHeight) {
			imgContainer.css('overflowY', 'visible');
			if (newImgHeight < viewportHeight) {
				imgContainer.height(viewportHeight);
				newImgHeight = viewportHeight;
			}
			else {				
				imgContainer.height(contentHeight);
				newImgHeight = contentHeight;
			}
			newImgWidth = newImgHeight * imgXAspect;
		}
	
		// set proper imgContainer height & hide Y-overflow (no unnecessary y-space/v.scrollbar)
		if (newImgHeight > contentHeight && contentHeight > viewportHeight) {
			imgContainer.height(contentHeight);
			imgContainer.css('overflowY', 'hidden');
		}
		else if (contentHeight < viewportHeight) {
			imgContainer.height(viewportHeight);
			imgContainer.css('overflowY', 'hidden');
		}
		else {
			imgContainer.height('100%');
			imgContainer.css('overflowY', 'visible');
		}
	
		
		// set width & hide X-overflow for imgContainer if larger than contentWidth (no unnecessary x-space/h.scrollbar)
		if (newImgWidth > viewportWidth) {
			if (contentWidth > viewportWidth) {
				imgContainer.width(contentWidth);
			}
			else {
				imgContainer.width('100%');
			}
			imgContainer.css('overflowX', 'hidden');
		}
		else {
			imgContainer.width('100%');
			imgContainer.css('overflowX', 'visible');
		}
		
		// set the new width & height
		newImgWidth = Math.round(newImgWidth);
		newImgHeight = Math.round(newImgHeight);
		img.width(newImgWidth);
		img.height(newImgHeight);
		
		// set cookie so inital w&h can be set via css for smooth browsing experience
		document.cookie = 'bgImgWidth='+newImgWidth+'; path=/';
		document.cookie = 'bgImgHeight='+newImgHeight+'; path=/';
	}
	
	$(window).bind('resize', function(){
		resize();	
	});
	
	resize();
};



/**
 ****** Various page onDOMReady scripts follow *****************************
 */

$(function(){

	// body js/jquery is enabled class
	$('body').removeClass('nojs').addClass('jsfx');
	
	// refresh queue (prevent timeout)
	if (typeof Xajax != 'undefined') 
		window.setInterval("xajax_UpdateActivity()", 270000);
	
	/**
	 * cross browser stuff
	 */
	// correct boxmodel input[type="text"] FF<3
	if ($.browser.mozilla && parseFloat($.browser.version) < 1.9) {
		$("body").addClass("FF2");
	}
	// ie6 workarounds for unsupported css
	if ($.browser.msie && parseFloat($.browser.version) < 7) {
		$("body").addClass("IE6");
		$("acronym[title]").addClass('hasTitle'); /* note: ie6 doesn't support abbr element */
	}
	

	/** 
	 * hover images for certain anchor types
	 */
	$('a.more').addClass('more-fx').wrapInner('<span class="hoverHelper" />');
	$('a.next').addClass('next-fx').wrapInner('<span class="hoverHelper" />');
	$('a.prev').addClass('prev-fx').wrapInner('<span class="hoverHelper" />');
	
	/**
	 * external (rel="external") and
	 * popup (rel="popup") handling
	 */
	var oPopWin = null;
	$("a[rel='external']").click(function(){
		window.open(this.href);
		return false
	});
	$("a[rel='popup']").click(function(e){
		if (typeof PopupWindow !== 'undefined') {
			popWin = new PopupWindow();
			popWin.anchor = $(this);
			popWin.width = 500;
			popWin.height = 500;
			if (popWin.spawn()) {
				e.preventDefault();
			}
		}
	});
	
	
	// auto-submit
	(function(){
		if ($.fn.autoSelect) {
			//  genre/maand select
			var $form = $('#genreMaandForm');
			var $enabled = $form.find('select').autoSelect();
			if ($enabled.length) {
				$form.addClass('autoSelect');
			}
			// archief year select (PH)
			$form = $('#archiefForm');
			$enabled = $form.find('select').autoSelect();
			if ($enabled.length) {
				$form.addClass('autoSelect');
			}
			// programma dag select (IK)
			$form = $('body.sc-IK #ik_dagForm');
			$enabled = $form.find('select').autoSelect();
			if ($enabled.length) {
				$form.addClass('autoSelect');
			}
		}
	})();	
	
	
	// showList hover
	$('.show-btn .hover').hover(function(){		
		$(this).children('ul').show();	
	}, function(){
		$(this).children('ul').hide();
	});	
	
	$('.show-btn .hover a').click(function(){
		return false;
	});		
	
});

/**
 * swf object
 */	
swfobject.registerObject("mySlideshow", "9.0.0", "/flash/expressInstall.swf");
	

/**
 * Tabs
 */
$(function(){
	if ($('ul.tabs').length > 0) { 
		// create onload function for all portlets with tabs, will also be called after ajax calls.
		
		function getHref(elm) {
			// gets content of href of elm
			// NB fix for IE7 needed, in the case when content is regenerated by jquery-ajax
			// somehow the hole ulr is prepended in that case, so a substring is needed.
			return $(elm).attr('href').substring($(elm).attr('href').indexOf('#'));
		}
		
		tabbed_portlets_onload = function(){
			
			$('.tabContents').hide(); // hide all tab-contents
			
			$('ul.tabs').each(function () {				
				if ($(this).find('a.active').length == 0) {					
					$(this).find('a:first').addClass('active'); // make the first a in every ul active					
				}				
			});
			
			$('ul.tabs').find('a.active').each(function () {
				$(getHref(this)).show(); // frankW	bud-bugfix			
			});
			
			$('.tabButton').click(function() { // bind click event to link				
				tabId = getHref(this);
				$(this).parents('.block-item:first').find('.tabButton').removeClass('active');	// Remove active class from all links
				$(this).addClass('active'); //Set clicked link class to active
				$(this).parents('.block-item:first').find('.tabContents').hide();				
				$(tabId).show();				
				return false;
			});
			
			// also make stars for reactions tab
			if($('input[type=radio].star').rating) {
				$('input[type=radio].star').rating()
			}
		}
		tabbed_portlets_onload();
		// create porlet specific function, to be called after reload portlet templates via ajax
		actueel_intern_onload = actueel_extern_onload = tabbed_portlets_onload;
		
		//$('#Omschrijving-tab').show();
		
		//hover states on Tabs
		$('.tabButton').hover(
			function() { $(this).addClass('hover'); }, 
			function() { $(this).removeClass('hover'); }
		);
		
			
	}
}); /* /Tabs */

/**
 * bekijk alle speeldata 
 * 
 */	
$(function() {
	$('.JsHidden').hide();			
	
	$('#toggleBtn1').click(function() {
		$(".JsHidden").toggle();
		$(".btnTable").toggleClass('bla');
		if ($(".btnTable").hasClass('bla')){
			$('li#toggleBtn1 a').text('Bekijk eerste speeldata');					
		} else {
			$('li#toggleBtn1 a').text('Bekijk alle speeldata');					
		}
		return false;
	});
	
	//meer informatie tonen
	
	//bekijk alle speeldata 		
	
	$('#toggleBtn2').click(function() {				
		$(".show-omschrijving").toggleClass('hidden');				
		if ($(".show-omschrijving").hasClass('hidden')){
			$('p#toggleBtn2 a').text('meer');					
		} else {
			$('p#toggleBtn2 a').text('minder');					
		}
		return false;
	});
	
	//bekijk alle text artiesten		
	if (parseFloat($('.jsOmchrijvingMaker').css('height')) > 100) {
		$(".meerTextbtn").show();	
		$('.jsOmchrijvingMaker').animate( { height:"100px" }, 1000);		
		$('#toggleBtn3').click(function() {				
			$(".JsHidden").toggle();
			$(".meerTextbtn").toggleClass('toggleHidden');
			if ($(".meerTextbtn").hasClass('toggleHidden')){
				$('li#toggleBtn3 a').text('Minder');					
				$('.jsOmchrijvingMaker').css('height','auto');
			} else {
				$('li#toggleBtn3 a').text('Meer');					
				$('.jsOmchrijvingMaker').animate( { height:"100px" }, 1000); 
			}
			return false;
		});		
	}		
});


/**
 * Beeldenspoor mouseover
 * 
 */	
$(function() {
    $('.overzichtBeeldItem').hover(function() {
        beeldnummer = this.id.substr(5);
        $(this).parents('div:first').find('.infoContainer').html(" - nummer " + beeldnummer);
    }, 
    function() {
        $('.infoContainer').html("&nbsp;");
    });
});


/**
 * iebanner.js
 * Renders & handles close button for ie6-be-gone banner
 * 
 * prereq: none
 * 
 * @author AK
 */
$(function(){
	var oBanner = document.getElementById('ie6banner');
	if (oBanner !== null) {
		var oA = document.createElement('a');
		oA.innerHTML = '<img src="/images/btn_close.gif" width="14" height="14" alt="sluiten" border="0" />';
		oA.setAttribute('href', '#');
		oA.setAttribute('title', 'Verberg deze banner voor deze browser-sessie');
		oA.onclick = function(){
			document.cookie = 'ie6bannerclosed=true; path=/';
			var oBanner = document.getElementById('ie6banner');
			oBanner.parentNode.removeChild(oBanner);
			return false;
		}
		oBanner.appendChild(oA);
	}	
});


})(jQuery);
};