$(document).ready(function() {
	jQuery('ul.votes').ImageLoader(500, 450, 190);
	jQuery('ul.video').ImageLoader(500, 450, 90);
	jQuery('div.imageswapper').ImageSwapper(500, 450, 190);
	initExternalLinks();
	
	Shadowbox.init({
		handleOversize: "resize",
		continuous: false,
		modal: true,
		displayNav: true,
		displayCounter: true,	
		onFinish: function(){
			if (getUrlParam("/stem-op-de-bruid/")) {
				parent.document.getElementById("sb-nav-previous").style.display='none';
				parent.document.getElementById("sb-nav-next").style.display='none';
				parent.document.getElementById("sb-counter").style.display='none';
				
				jQuery("a#sb-nav-close").click(function(){
						window.location.href = window.location.href;			
				})
			}					
		}
	});
	
});

(function(){
	/*
	 * jQuery image loader, flashy pops them up while loading...
	 * @author Lee Boonstra, <lee.boonstra@efocus.nl>
	 * @since 4 apr 2011
	 * @version 1.0
	 * @copyright eFocus
	 * 
	 * @uses jQuery 1.2.6, <http://www.jquery.com> or higher
	 */
	$.fn.ImageSwapper = function(){	
		return this.each(function(){
						
			var elContainer = jQuery(this);
			var elImage = elContainer.find('img');
			var arrUrls = elContainer.find('span.url');
			var url = "";
			var i = 0;

			if (arrUrls.length > 1) {
				var elPrevButton = '<a href="#" class="prev"><strong>prev</strong></a>';
				var elNextButton = '<a href="#" class="next"><strong>next</strong></a>';
				elContainer.append(elPrevButton);
				elContainer.append(elNextButton);
				$('a.prev').addClass('hidden');
			}
			
			$('a.prev').click(function(){
				if(i!==0){
					$('a.next').removeClass('hidden');
					i = i-1;
				 	url = $(arrUrls[i]).text();
					$(elImage).attr('src', url);
				}
				
				if(i===0){
					$('a.prev').addClass('hidden');
				}
				
				return false;
			});
			
			$('a.next').click(function(){
				if(i!==arrUrls.length-1){
					$('a.prev').removeClass('hidden');
					i = i+1;
					url = $(arrUrls[i]).text();
					$(elImage).attr('src', url);	
				}
				
				if(i===arrUrls.length-1){
					$('a.next').addClass('hidden');
				}
				
				return false;
			});		
		});
	};
})(jQuery);

(function(){
	/*
	 * jQuery ImageSwapper
	 * @author Lee Boonstra, <lee.boonstra@efocus.nl>
	 * @since 20 mei 2011
	 * @version 1.0
	 * @copyright eFocus
	 * 
	 * @uses jQuery 1.2.6, <http://www.jquery.com> or higher
	 */
		$.fn.ImageLoader = function(intFade, intDelay, intImageSize){		
			return this.each(function(){
				var strName = jQuery(this).attr("class");	
				var elContainer = jQuery(this);
				var images = elContainer.find('img');
		
				if(getCookie("imageload_" + strName) == false){
					var i = 0;
					
					//reset the images first
					images.css({
						opacity: 0,
						height: 0		
					});
					
					images.each(function(){
						var image = jQuery(this);
						setTimeout(function(){
							 image.animate(
							 { 
							 	height: intImageSize +"px",
								opacity: 1 
							 }, intFade,
							 function(){
								image.parent().addClass("loaded");
							});					
						}, i)
						i += intDelay
					});
					
					setCookie("imageload_" + strName, true);
				} else {
					images.each(function(){
						var image = jQuery(this);
						image.parent().addClass("loaded");
					});
				}
			});
	}
})(jQuery);



/**
 * Opens external links valid in a new window without the target attribute.
 * 
 * @author Mirjam Verloop, <mirjam.verloop@efocus.nl>
 * @author Lee Boonstra, <lee.boonstra@efocus.nl>
 * @version 1.0, 19 jul, 2010
 * @requires <a href="http://www.efocus.nl/" class="external">eFocus</a>
 * @return void
 */
function initExternalLinks() {
	var arrExternalLinks = $('a.external-link-new-window, a.internal-link-new-window');
	if (arrExternalLinks.length > 0) {
		arrExternalLinks.each(function() {
				$(this).click(function(e) {
				window.open($(this).attr('href'));
				return false;
			});
		});	
	}
}


/**
 * Incase the list item has no previous list item, add class
 * 
 * @author Lee Boonstra, <lee.boonstra@efocus.nl>
 * @version 1.0, 11 jan, 2011
 * @param list - ul item
 * @return void
 */
function setFirstChild(list){
	$(list).children().each(function(){
		if ($(this).prev().length == 0) {
			$(this).addClass('first-child');
		}
	});
}

/**
 * Set Cookie
 * @param string the name of the cookie
 * @param value of the cookie
 * @param int nr of days before expire
 */ 
function setCookie(c_name,value,expiredays){
	var exdate=new Date();
	exdate.setDate(exdate.getDate()+expiredays);
	document.cookie=c_name+ "=" +escape(value)+((expiredays==null) ? "" : "; expires="+exdate.toGMTString())+ "; path=/";
}

/**
 * Get Cookie based on Cookie name
 * @param string the name of the cookie
 */ 
function getCookie(c_name){
	if (document.cookie.length>0){
	  c_start=document.cookie.indexOf(c_name + "=");
	  if (c_start!=-1){ 
	    c_start=c_start + c_name.length+1; 
	    c_end=document.cookie.indexOf(";",c_start);
	    if (c_end==-1) c_end=document.cookie.length;
	    return unescape(document.cookie.substring(c_start,c_end));
	    } 
	  }
	return false;
}

/**
 * Test if url parameter is in url
 * @param the name of the parameter from the URL to retrieve
 * @return boolean
 */
function getUrlParam(name) {
	var url = window.location.href;
	if(url.indexOf(name) !== -1){
		return true;
	} else {
		return false;
	}
}
