var aFontSizes	= new Array();
var iCurrentSize= 1;
aFontSizes[0]	= 0.8;
aFontSizes[1]	= 1;
aFontSizes[2]	= 1.2;
aFontSizes[3]	= 1.4;

$(document).ready(function() 
{
	init();
});

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

function setFontSize(pCurrentSize)
{
	iCurrentSize	= pCurrentSize;
	$.cookie('fontSizeIndex', iCurrentSize, { expires: 1, path: '/' });
	$('#content,#home').css('font-size', aFontSizes[iCurrentSize] + 'em');
}

function init()
{
	$('#breadcrumbs li a').before('<span>&raquo;</span> ');
	$('a.skip').focus( function () { $(this).addClass('focus'); });
	$('a.skip').blur( function () { $(this).removeClass('focus'); });

	$('#topbar ul').append('<li><a href="#" class="small">a</a> - <a href="#" class="large">A</a></li>');
	$('#topbar li:last, #footer li:last').css('background', 'none');
	$('#topbar div.container').append('<label for="newwindow" class="newwindow"><input type="checkbox" name="newwindow" id="newwindow" value="1" />externe links in nieuw venster openen</label>');
	$('input#newwindow').change( function () {
		$.cookie('newWindow', ( $(this).attr('checked') == true ? '1': '0'), { expires: 1, path: '/' });
		toggleLinkTarget();
	});
	if ( $.cookie('newWindow') == 1 )
		$('input#newwindow').attr('checked', 'true');
	
	$('a.small,a.large').click( function () 
	{
		if ( $(this).attr('class') == 'small' && typeof aFontSizes[(iCurrentSize-1)] != 'undefined' )
			setFontSize( (iCurrentSize-1) );
		else if ( $(this).attr('class') == 'large' && typeof aFontSizes[(iCurrentSize+1)] != 'undefined' )
			setFontSize( (iCurrentSize+1) );
	});
	if ( typeof aFontSizes[$.cookie('fontSizeIndex')] != 'undefined' )
		setFontSize( parseInt($.cookie('fontSizeIndex') ) );
	
	/* pull quotes */
	$('span.quote').each(function() {
		text = $(this).text().replace( /\((.*)\)/gi, " " );
		var className	= $(this).is('.right') ? 'quote right': 'right';
		if ( $(this).parent().get(0).tagName.toLowerCase() == 'li' )
			$(this).parent().parent().parent().prepend('<blockquote class="' 
				+ className + '"><p>' + text + '</p></blockquote>');
		else
			$(this).parent().prepend('<blockquote class="' 
				+ className + '"><p>' + text + '</p></blockquote>');
	});
	
	/* portfoliocategorieen */
	$('#cats li').click(function(){
		if ( $(this).find('a').attr('href') )
			window.location=$(this).find('a').attr('href');return false;
	});
	$('#cats li').hover( function() {
		if ( $(this).find('a').attr('href') )
			$(this).addClass('hover');
	}, function() {
		$(this).removeClass('hover');
	} );
	/* portfolio items */
	$('#items a').each( function(i)
	{
		$(this).find('img').animate({opacity: '0.7'});
		var tekst	= $(this).attr('title');
		$(this).removeAttr('title');
		$(this).find('img').before('<h3>' + ( tekst.length > 20 ?  tekst.substr(0,18) + '&hellip;': tekst ) + '</h3>');
	});	
	
	$('#items a').hover(function() {
		$(this).find('img').animate({opacity: '1'}, 'fast');
	}, function() {
		$(this).find('img').animate({opacity: '0.7'}, 'fast');
	});
	
	/* afkorten van lange zinnen in rechter kader */
	$('.item .substr a').each( function(i)
	{
		var tekst	= $(this).html();
		$(this).html( ( tekst.length > 30 ?  tekst.substr(0,28) + '&hellip;': tekst ) );
	});	
	
	/* 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');
	});	
	
	/* paragraph link */
	$('div.bericht h2[id], div.bericht h3[id]').each( function (i)
	{
		var idName = $(this).attr('id');
		if ( idName != '' )
			$(this).append(
				' <a class="paragraph" title="Permalink `' + $(this).text() + '`" href="#' + idName + '">#' + idName + '</a>'
				+ ' <a class="paragraph" title="Naar boven" href="#topbar">#top</a>');
	});
	
	/* links en buttons */
	$('a[@href$=pdf]').addClass('pdf');
	$('a[@href$=doc]').addClass('doc');
	$('a[@href$=zip]').addClass('zip');
	$('a[@href$=rar]').addClass('rar');
	$('input[type=submit]').addClass('button');
	$('a[@rel=external]').each( function (i)
	{
		if ( !$(this).find('img').attr('src') )
		{
			var aText 		= $(this).html();
			var lastSpace	= aText.lastIndexOf(' ');
			if ( lastSpace > 0 )
				$(this).html( aText.substring(0,lastSpace) + ' <span class="external">' + aText.substring(1+lastSpace) + '</span>' );
			else
				$(this).html( '<span class="external">' + aText + '</span>' );
		}
	});	
	toggleLinkTarget();
	
	if ( $('#content').length > 0 )
		$('a[rel=lightbox]').lightBox();
};

function toggleLinkTarget()
{	
	if ( $.cookie('newWindow') == '1' )
		$('a[@rel=external]').attr('target', '_blank');
	else
		$('a[@rel=external]').removeAttr('target');
}