/*
	Update 10-03-2008: Stefan van den Dungen Gronovius
		Complete written in Jquery
*/
		$(document).ready(function(){
			$('.picture-viewer').each(function(){
				initPictureComponent($(this));
			});
		});
		
		function initPictureComponent ($oPictureComp){
			$oPictureComp.find('.picture-navigation ul a').each(function(i){
				// Maak eerste afbeelding de hoofdabeelding
				if( i == 0 ){
					$(this).addClass("active");
					imgSrc = $(this).attr('href').replace('/large','/thumbnail');
					imgAlt = $(this).find('img').attr('alt');
				}
				// Handle the clickfunctie!
				$(this).click(function(){
					$oPictureComp.find('.picture-navigation ul a').removeClass('active');
					setActivePic($(this))
					return false;
				});
			});
			
			$oPictureComp.append('<img class="pic-big" src="/lib/css/han/images/enlarge-pic.gif"/>');
			$('.pic-big').click(function(){
				showLargePic($(this));
			});	
			
			if ( $oPictureComp.find('.picture-navigation ul a').length == 1 ){
				$oPictureComp.find('.picture-navigation').remove();
				return true;
			}
			bodyClass =  $('body').attr('class')
			//$oPictureComp.find('.picture-navigation').prepend('<img class="prev" src="/lib/css/han/images/'+bodyClass+'/picture-nav-prev.gif"/>')
			$oPictureComp.find('.picture-navigation .prev').click(function(){
				rollPic($(this),'prev');
			});
			//$oPictureComp.find('.picture-navigation').append('<img class="next" src="/lib/css/han/images/'+bodyClass+'/picture-nav-next.gif"/>')
			$oPictureComp.find('.picture-navigation .next').click(function(){
				rollPic($(this),'next');
			});

		}

		function rollPic($oPic,way){
			$oNav = $oPic.parent('.picture-navigation');
			iPicCount = $oNav.find('ul li a').length;
			$oNav.find('ul li a').each(function(i){
				if( $(this).hasClass("active") ){
					iActiveID = i+1;
					if( (i != 0 && way == 'prev') || (i != iPicCount-1 && way == 'next') ){
						$(this).removeClass("active");
					}	
					$oId = $(this).parent('li');
				}
			});

			if( way == 'next' ){
				setActivePic($oId.next('li').find('a'));
				return false;
			}
			if( way == 'prev' ){
				setActivePic($oId.prev('li').find('a'));
				return true;
			}
			setActivePic($oId);			
		}
		
		function setActivePic($ob){
			imgAlt = $ob.find('img').attr('alt');
			imgDescription = $ob.attr('title');
			imgSrc = $ob.attr('href');
			
			if( imgSrc != null ){
				imgSrc = imgSrc.replace('/large','/thumbnail');
			}
			$ob.addClass("active");
			$oPV = $ob.parents('.picture-viewer');
			$oPV.find('.main-picture img').attr('src',imgSrc);
			$oPV.find('.main-picture img').attr('alt',imgAlt);
			$oPV.find('.main-picture img').attr('title',imgDescription);
			$oPV.find('.beschrijving').html(imgDescription);
		}

		function showLargePic($ob){
			$oMainPic = $ob.parents('.picture-viewer').find('.main-picture img');
			newSource = $oMainPic.attr('src').replace('/thumbnail','/large');
			window.open(newSource);
		}
		
		
		

// initLinks is a script to open a new window for links in a accessible WCAG 2.0 way
// script found at http://www.w3.org/TR/2006/WD-WCAG20-TECHS-20060427/Overview.html#SCR24
function initLinks( selector , img , lang ){
	img = typeof(img) != 'undefined' ? img : false;
	lang = $('html').attr('lang');
	if( lang == 'en' ){
		message = 'open in a new window';
	} else {
		message = 'opent in een nieuw venster';
	}
	
	
	$( selector ).each( function(){
		if( img ){ 
			$(this).html( $(this).html() + ' <img src="http://redactie.han.nl/lib/images/icons/link_external.gif" alt="'+message+'" title="'+message+'" />' );
		} else {
			if( $(this).attr('title') == '' ){
				$(this).attr('title', message );
			} else {
				$(this).attr('title', $(this).attr('title')+' ('+message+')');
			}
		}
		/*  DGNS: This is THE way for WCAG and XHTML documents. HTML5 supports the target attribute, so why don't we use it anymore? 
			This code is replaced by the code beneath the onclick handlers. If validation fails, use this code instead
		$(this).click( function(){
			return launchWindow( $(this),'click');
		});
		$(this).keypress( function(event){
			return launchWindow( $(this) ,event);
		});
		*/
		$(this).attr('target','_blank');
	})
}



function launchWindow( objAnchor , objEvent ){
	var iKeyCode, bSuccess=false;
	if (objEvent && objEvent.type == 'keypress') {
		if (objEvent.keyCode)
			iKeyCode = objEvent.keyCode;
		else if (objEvent.which)
			iKeyCode = objEvent.which;
		if (iKeyCode != 13 && iKeyCode != 32)
			return true;
	}
	bSuccess = window.open( objAnchor.attr('href') );
	if (!bSuccess){
		return true
	}
	return false;	
}

/** Sitestat routines **/
function sitestat_t1(ns_l){ns_l+="&ns__t="+(new Date()).getTime();ns_pixelUrl=ns_l;
	if(document.images){ns_1=new Image();ns_1.src=ns_l;}else
	document.write("<img src="+ns_l+" width='1' height='1'>");}

function sitestat_t2(ns_l){ns_l+="&ns__t="+(new Date()).getTime();ns_pixelUrl=ns_l;
	ns_0=top.document.referrer;
	ns_0=(ns_0.lastIndexOf("/")==ns_0.length-1)?ns_0.substring(ns_0.lastIndexOf("/"),0):ns_0;
	if(ns_0.length>0)ns_l+="&ns_referrer="+escape(ns_0);
	if(document.images){ns_1=new Image();ns_1.src=ns_l;}else
	document.write("<img src="+ns_l+" width='1' height='1'>");}

function HANMediaplayer(){
	$('.medialist li:first').addClass('active');
	$('.medialist').addClass('interactive');
	$('.medialist li a').removeAttr('href');
	$('.medialist li').click( function(){
		$('.medialist li').removeClass('active');
		$(this).addClass('active');
		sDesc = $(this).attr('data-description');
		sVideo = $(this).attr('data-video');
		$('#youtubeplayer').html('');
		$('#youtubeplayer').append('<div class="video_container"><iframe width="'+$(this).attr('data-vwidth')+'" height="'+$(this).attr('data-vheight')+'" src="'+sVideo+'" frameborder="0" allowfullscreen=""><comment/></iframe></div>');
		$('#youtubeplayer').append('<div class="description">'+$(this).find('.description').html()+'</div>');
	});
}	
	

// Here goes al onload scripts
$().ready( function (){
	initLinks('a.external', true , 'en');
	initLinks('.sharethis a:not(.print)', false, 'en');

	// Search
	var searchmsg = $('#searchlabel').html();
	$('#searchquery').val(searchmsg).click(function(){
		if( $(this).val() == searchmsg ){
			$(this).val('');
		}	
	}).blur( function(){
		if( $(this).val() == '' ){
			$(this).val(searchmsg);
		}			
	});
	


	
	
});

