/**
 * 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: true,
			logAlerts: false // alerts for clients that don't have the firebug console (otherwise silent)
		}
	};
	
	
	/**
	 * Debug/logging
	 * Uses Firebug (window.console) when present
	 */
	$.log = function(message){
		if (!$.ppprd.debug.enabled) {
			return false;
		}
		if (window.console && window.console.debug) {
			console.debug(message);
		}
		else {
			if ($.ppprd.debug.logAlerts) {
				alert(message);
			}
			else {
				return;
			}
		}
	};
	
	
	/**
	 * 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);


	/**
	 * PopupWindow object
	 * spawns/handles ye classic popup windows
	 *
	 * @author AK
	 *
	 * requires: jQuery 1.2.x, 1.3.x
	 */
	window.PopupWindow = function(){
		this.width = 500;
		this.height = 500;
		this.$container = $(window);
		this.offsetLeft = 0;
		this.offsetTop = 0;
		this.menubar = true;
		this.location = false;
		this.resizable = true;
		this.scrollbars = true;
		this.status = true;
		this.name = 'popupWindow';
		this.url = '';
	};
	window.PopupWindow.prototype.prepare = function(){
		var oAnchor = this.anchor;
		if (typeof oAnchor.data('popWinObject') == 'object') {
			var oPopWin = oAnchor.data('popWinObject');
			if (!oPopWin.closed) {
				oPopWin.close();
			}
		}
		var sHref = oAnchor.attr('href');
		if (sHref.indexOf('?') > -1) {
			sHref += '&';
		} else {
			sHref += '?';
		}
		sHref += 'popup=true';
		this.url = sHref;
	};
	window.PopupWindow.prototype.spawn = function(){
		this.prepare();
		var x = this.$container.width() / 2 - (this.width / 2);
		var y = this.$container.height() / 2 - (this.height / 2);
		
		if (x < 0) { x = 0; }
		if (y < 0) { y = 0; }
		
		if (typeof window.screenX !== 'undefined') {
			x += window.screenX;
			y += window.screenY;
		}
		else if (typeof window.screenLeft !== 'undefined') {
			x += window.screenLeft;
			y += window.screenTop / 2;
		}
		
		this.offsetLeft = x;
		this.offsetTop = y;
		
		var oPopWin = window.open(this.url, this.name, 'width=' + this.width + ',height=' + this.height + ',left=' + this.offsetLeft + ',top=' + this.offsetTop);
		
		if (typeof oPopWin != 'undefined') {
			this.anchor.data('popWinObject', oPopWin);
			return true;
		}
		return false;
	};
	
	
	/**
	 *** 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 */
		}
		
		
		/**
		 * Anchors
		 *
		 */
		/** 
		 * 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();
				}
			}
		});
		
		
		/**
		 * Drop-down menu (Superfish)
		 */
		if ($.fn.superfish) {
			$("#menu").superfish({
				speed: 0
			});
		}
		
		// auto-submit genre/maand select
		(function(){
			if ($.fn.autoSelect) {
				var $form = $('#genreForm');
				var $enabled = $form.find('select').autoSelect();
				if ($enabled.length) {
					$form.addClass('autoSelect');
				}
				$form = $('#maandForm');
				$enabled = $form.find('select').autoSelect();
				if ($enabled.length) {
					$form.addClass('autoSelect');
				}
				$form = $('#alternatieveData');
				$enabled = $form.find('select').autoSelect();
				if ($enabled.length) {
					$form.addClass('autoSelect');
				}
			}
		})();
		
		
	});
	
	
})(jQuery);
};