var iSlide	= 0;
var zIndex	= 0;
var iFont	= 13;

Shadowbox.init({
	language: 'nl',
	players:  ['img', 'html', 'swf', 'flv'],
	autoplayMovies: false,
	continuous: true,
	counterLimit: 0,
	enableKeys:false
});

window.onload = function()	// buiten jquery om
{
	if ( $("[id^='shadowbox;popup']").length > 0 )
	{
		var sdArray	= stringToArray( $("[id^='shadowbox;popup']").attr('id'), ';');
		var sdOpen	= {player: 'html',content: $("[id^='shadowbox;popup']").html() };
		
		if ( sdArray['height'] )
			sdOpen.height	= sdArray['height'];
		if ( sdArray['width'] )
			sdOpen.width	= sdArray['width'];
		if ( sdArray['title'] )
			sdOpen.title	= sdArray['title'];
		
		Shadowbox.open(sdOpen); 
	}
};

function fontZoom(arg)
{
	if ( arg == 0 )
		iFont = 13;
	else
		iFont	= iFont + parseInt(arg);
	
	$('#footer').css('font-size', (iFont-3) + 'px');
	$('.centertext a.workshopaanmelden,#searchbar').css('font-size', (iFont-1) + 'px');
	$('body,#text strong,#text b,#text h4,.centertext a.workshopaanmelden,'
		+ '#container div.nieuws li,#container div.meerwetenover li,.meerwetenover li a').css('font-size', iFont + 'px');
	$('#home #content p.first,#text h3,dl.archief dt').css('font-size', (iFont+1) + 'px');
	$('#text small.datum,#text h2').css('font-size', (iFont+4) + 'px');
	
	document.cookie = "fontzoom="+iFont+"; path=/"
}
function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}


$(document).ready(function() 
{
	var cookieFont	= readCookie('fontzoom');
	if ( cookieFont != null )
		fontZoom( parseInt(readCookie('fontzoom')) - iFont );

	textAccordion();
	init();	
});

function homeSlide()
{
	iPrevSlide	= iSlide;
	iSlide--;
	if ( iSlide < 0 )
		iSlide = $('#slider li').length - 1;

	tempIndex	= ++zIndex;
	$('#slider li:eq(' + iPrevSlide + ')').css('z-index', ++zIndex);
	$('#slider li:eq(' + iSlide + ')').css('z-index', tempIndex);
	$('#slider li:eq(' + iPrevSlide + ')').animate({opacity: 0}, 750, null, function () 
	{		
		$('#slider li:eq(' + iSlide + ')').css('z-index', ++zIndex);
	}).animate( { opacity:1}, 1 );	

	setTimeout('homeSlide()', 5000);
}

function textAccordion()
{
	if ( $('.accordion').length > 0 && $('.right').length > 0 )
	{
		var sNew	= '';
		var sHtml 	= $('.accordion .right').html();
		var aH2		= sHtml.split( new RegExp( "<h2>", "i" ) );
		sNew		+= aH2[0];
		
		for ( i = 1; i < aH2.length; i++ )
		{
			var tH2Split = aH2[i].split( new RegExp( "</h2>", "i" ) );
			
			var sSpanString	= '';
			var aSpan		= tH2Split[1].split( new RegExp( "<blockquote>", "i" ) );
			sSpanString		+= aSpan[0];
			
			for ( j = 1; j < aSpan.length; j++ )
			{
				var tSpanSplit = aSpan[j].split( new RegExp( "</blockquote>", "i" ), 2 );
				sSpanString += '<div class="toggle">' 
					+ tSpanSplit[0].replace(/<em>/i, '<a href="javascript:void();" id="accbq' + i + '' + j + '">').replace(/<\/em>/i, '</a>') 
					+ '</div><div class="accbq" id="div-accbq' + i + '' + j + '">' + tSpanSplit[1] + '</div>';
			}
			
			sNew	+= '<h2><a href="javascript:void();" id="acc' + i + '">' + tH2Split[0] 
				+ '</a></h2><div class="acc" id="div-acc' + i + '">' + sSpanString + '</div>';
		}
		
		$('.accordion .right').html( sNew );
		$('.accordion .acc, .accordion .accbq').css('display', 'none');
		$('.accordion h2 a').each( function(i)
		{
			$(this).click(function()
			{
				$('.accordion .acc').slideUp();

				if ( $('.accordion div#div-' + $(this).attr('id') ).css('display') == 'block' )
					$('.accordion div#div-' + $(this).attr('id') ).slideUp();
				else
					$('.accordion div#div-' + $(this).attr('id') ).slideDown();
			});
		});
		$('.accordion .toggle a').each( function(i)
		{
			$(this).click(function()
			{
				$('.accordion .accbq').slideUp();

				if ( $('.accordion div#div-' + $(this).attr('id') ).css('display') == 'block' )
					$('.accordion div#div-' + $(this).attr('id') ).slideUp();
				else
					$('.accordion div#div-' + $(this).attr('id') ).slideDown();
			});
		});
	}
}

function createEmailBySplit(splitString)
{
	var eParts 		= base64_decode(splitString.split(' ')[1]).split('|');
	return eParts[0] + '@' + eParts[1] + '.' + eParts[2];
}

function hideWorkshopElements()
{
		var sVis	= 'visible';
		if ( $('input#nieuwsbrief').attr('checked') == true && $('input#workshop').attr('checked') == false )
			sVis	= 'hidden';
		$('dt.workshop,dd.workshop').css('visibility', sVis);
}

function init()
{
	/*if ( parseInt( document.body.clientWidth )> 900 )
		$('body,html').css('overflow-x', 'hidden');*/

	$('#breadcrumbs li a').before('<span>&raquo;</span> ');
	$('input[type=text]').addClass('text');
	$('input[type=submit]').addClass('button');
	$('#footer li:first').addClass('first');
	
	/* emailadressen beschermen */
	$('span.eprotecttext').each( function(i)
	{
		$(this).text( createEmailBySplit( $(this).attr('class') ) );
	});	
	$('span.eprotectlink').each( function(i)
	{
		var mailtoLink	= '<a href="mailto:' + createEmailBySplit( $(this).attr('class') ) + '">' + $(this).text() + '</a>';
		$(this).html(mailtoLink);
		$(this).removeAttr('title');
	});
	
	$('a[rel=external]').each( function(i)
	{
		$(this).attr('target', '_blank');
		$(this).addClass('external');
	});
	
	if ( $('#tweets').length > 0 )
	{
		var aVars = new Array();
		aVars = {"aantal":3,"users":''};
		aVars = replaceArrayVars(aVars, $('.tweet').attr('title') );
		$('.tweet').attr('title', '');

		$('.tweet').tweet({
			join_text: 'auto',
			username: aVars['users'].split('|'),
			avatar_size: 48,
			count: parseInt(aVars['aantal']),
			auto_join_text_default: 'I said,', 
			auto_join_text_ed: 'I',
			auto_join_text_ing: 'I was',
			auto_join_text_reply: 'I replied',
			auto_join_text_url: 'I was checking out',
			loading_text: 'loading tweets...'
		});
	}
	
	if ( $('.carousel').length > 0 )
	{
		var aVars = new Array();
		aVars = {"aantal":2,"seconden":5};
		aVars = replaceArrayVars(aVars, $('li#archiefitem img').attr('class') );
		var iAantal	= parseInt( aVars['aantal'] ) > 0 ? parseInt( aVars['aantal'] ): 2;
		var iSec	= parseInt( aVars['seconden'] ) > 1 ? parseInt( aVars['seconden'] ) * 1000: 5000;
		
		if ( $('#mycarousel li').length > iAantal )
		{
			$('.carousel').after('						<div class="nav">'
				+ '							<a href="#" class="prev" title="Vorige"><span>vorige</span></a>'
				+ '							<a href="#" class="next" title="Volgende"><span>volgende</span></a>'
				+ '						</div>');
			
			$('.carousel').jCarouselLite({
				vertical: true,
				hoverPause:true,
				visible: iAantal,
				scroll: iAantal,
				btnNext: ".next",
				btnPrev: ".prev",
				auto: iSec,
				speed:400
			});
		}
	}
	
	iSlide	= zIndex = $('#slider li').length-1;
	setTimeout('homeSlide()', 5000);
	
	if ( $("[class^='gmaps:']").length > 0 && $('#map').length > 0 ) {
		initializeGoogleMaps();
	}
	$('#groep1 li, #groep2 li').each( function() {
		$(this).find('span:first').css('cursor', 'pointer');
		$(this).click( function() {
			$('#description').html('');
		});
	});
	$('#groep2 li').each( function() {
		$(this).click( function() {
			$('#description').html('<br /><p>'+$(this).find('p:first').html()+'</p>');
		});
	});
	
	$('#formlarge input,#formlarge textarea,#searchbar input[type=text]').each( function() {
		if ( $(this).attr('title') != '' )
		{
			var sTitle	= $(this).attr('title') + '...';
			if ( $(this).val() == '' )
				$(this).val( sTitle );

			$(this).focus(function () {
				if ( $(this).val() == sTitle )
					$(this).val('');
			});
			$(this).blur(function () {
				//alert( $(this).val() );
				if ( $(this).val() == '' )
					$(this).val(sTitle);
			});
		}
	});

	$('#formlarge > form').submit(function() {
		$('#formlarge input,#formlarge textarea').each( function() {
			if ( $(this).attr('title') != '' && $(this).val() == $(this).attr('title') + '...' )
				$(this).val('');
		});
	});
	
	/* img checkboxes */
	$('input[type=checkbox].useimg').each( function() {
		$(this).after( '<a class="fakecheck' + ( (this.checked) ? ' fakechecked': '' ) 
			+ '" href="#' + this.id + '" id="fake' + this.id + '"><span>' + this.id + '</span></a>' );
		$(this).css('display','none');
	});
	$('.fakecheck').click(function(){
		($(this).hasClass('fakechecked')) ? $(this).removeClass('fakechecked') : $(this).addClass('fakechecked');
		$(this.hash).trigger('click');
		hideWorkshopElements();
		
		return false;
	});
	hideWorkshopElements();
	
	$('#fontzoom a').each( function() {
		$(this).attr('title', $(this).text() );
	});
};