/**
 * 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
 */

var P = {};


	/**
	 *** Various page onDOMReady scripts follow ***
	 */
	$(function(){
		
		// share
		p.share = {
			init: function() {
				var popWin = null;
				$('.com-icons .facebook-icon a,  .com-icons .twitter-icon a').click(function(e) {
					var $this = $(this);
				   
					popWin = new PopupWindow();
					popWin.anchor = $this;
					popWin.width = 550;
					popWin.height = 436;
					if (popWin.spawn()) {
						 e.preventDefault();
					}	  
					
				});
			}
		};
		
		/**
		* 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();
				}
			}
		});
		
	});


var p = {};

p.share = {
	init: function(elementId) {
		var popWin = null;
		$('#'+elementId).find('.share .twitter a, .share .hyves a').click(function(e) {
			var $this = $(this);
			
			popWin = new PopupWindow();
			popWin.anchor = $this;
			popWin.width = 550;
			popWin.height = 436;
			if (popWin.spawn()) {
				e.preventDefault();
			}			

		});
	}
};

p.products = {

init: function() {
	
	// bekijk alles toggle
	var $prodBox = $("#producten .prodBox"),
		$prodBoxSections = $prodBox.children(),
		prodBoxHeight = $prodBox.height(),
		$toggleProductsBtn = $('#toggleProducts'),
		$hoverTriggers = $("#producten li h3"),
		cOffset = $prodBox.offset(),
		$hoverBoxContentWrap = $('<div class="wrapper"></div>');
		$hoverBox = $('<div class="hoverbox"></div>').append($hoverBoxContentWrap).appendTo($prodBox),
		hoverBoxWidth = $hoverBox.width(),
		applyIEfix = false
	;
	
	$prodBox.height(150);
		
	// IE7 fix. Needed because $prodBox content will overflow otherwise
	if ($.browser.msie && parseInt($.browser.version) < 8) {
		applyIEfix = true;
		$prodBoxSections.height(150);
	}
	
	$toggleProductsBtn.toggle(
		function() {
			//$prodBox.children().css('height', 'auto');
			$prodBox.animate({
				height: prodBoxHeight
			}, 500, function() {
				$toggleProductsBtn.text('Inklappen');
			});
			
			if (applyIEfix) {
				$prodBoxSections.animate({height: prodBoxHeight}, 500);
			}
		},
		function() {
			if (applyIEfix) {
				$prodBoxSections.animate({height: 150}, 500);
			}
			
			$prodBox.animate({
			   height: 150
		   }, 500, function() {
			   $toggleProductsBtn.text('Bekijk alles');
		   });
	});
	
	// product hover
	$hoverTriggers.each(function() {		
		var $this = $(this),
			offset = $this.offset();
		
		if ($this.parents('.productItem').nextAll('.productItem').length >= 2) {
			$this.data('hoverBox-left', offset.left + $this.width() + 40 - cOffset.left);
		} else {
			$this.data({
				'hoverBox-left': offset.left - hoverBoxWidth + 10 - cOffset.left,
				'hoverBox-class': 'hoverboxLeft'
			});			
		}
	});
	
	// show div hoverbox
	$hoverTriggers.hoverIntent(function(){
			var	$this = $(this),
				offset = $this.offset(),
				$desc = $this.next();
			
			if ($desc.length) {
				$hoverBoxContentWrap.html($this.next().html());
				
				$hoverBox.css({
					top: offset.top - $hoverBox.height()/2 + $this.height()/2 - cOffset.top,
					left: $this.data('hoverBox-left')
				});
				
				if (typeof $this.data('hoverBox-class') == 'undefined') {
					$hoverBox.removeClass('hoverboxLeft');
				}
				else {
					$hoverBox.addClass($this.data('hoverBox-class'));
				}
				
				if ($.browser.msie) {
					$hoverBox.show();
				} else {
					$hoverBox.fadeIn();
				}
			}
		},
		function(){
			if ($.browser.msie) {
				$hoverBox.hide();
			} else {
				$hoverBox.fadeOut("fast",function() {});
			}
		}
	);
	
}

}; /* p.products */


;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 (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>');
	});
};


/**
 * 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 = 'no';
	this.location = 'yes';
	this.resizable = 'yes';
	this.scrollbars = 'yes';
	this.status = 'yes';
	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 + ',menubar=' + this.menubar + ',location=' + this.location + ',resizable=' + this.resizable + ',scrollbars=' + this.scrollbars  + ',status=' + this.status);
	
	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);
	
	// Image Replace
	$('#bodyHome .submenu ul.menuItems li a')
		.wrapInner('<span class="txtwrap"/>')
		.ImageReplace()
		.append('<span class="hover"/>')
		.parent()
			.addClass('ir');
	;
	
	
	/**
	 * external (rel="external") and
	 */
	$("a[rel='external']").click(function(){
		window.open(this.href);
		return false
	});
	
	/**
	 * iebanner.js
	 * Renders & handles close button for ie6-be-gone banner
	 * 
	 * prereq: none
	 * 
	 * @author AK
	 */
	
	var oBanner = document.getElementById('ie6banner');
	if (oBanner !== null) {
		var oA = document.createElement('a');
		oA.innerHTML = '<img src="/images/icons/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);
};
