if(typeof String.prototype.trim !== 'function') {
	String.prototype.trim = function() {
		return this.replace(/^\s+|\s+$/g, ''); 
	}
}


$(document).ready(function(){
	// Google Translate:
	if(typeof( tn_ ) == 'undefined'){
		initLightbox();
		initTitles();
		initMenu();
		initSponsors();
		initLayout();
		/*$('#loader').html('<script type="text/javascript" src="/js/proreader.js"></script>');*/
	} else {
		initLightbox();
		initMenu();
		initSponsors();
		initLayout();
	}
});

function initTitles(){
	$('div#body h2').each(function(){
		replaceTitle($(this), 16);
	});
	$('div#body h3').each(function(){
		replaceTitle($(this), 14);
	});
}

function replaceTitle(elem, size){
	var id = clean(elem.html())+'_w'+elem.width()+'_h'+elem.height()+'_s'+size;
	var filename = id+'.png';
	var string = elem.html().trim();
	elem.replaceWith(
		$('<img>').attr({
			src: '/images/titles/'+filename,
			alt: string,
			title: string,
			id: id
		})
		.addClass('attr_'+size+'_'+elem.width()+'_'+elem.height())
		.width(elem.width())
		.height(elem.height())
		.error(function(){
			var tmp = $(this).attr('class').split('_');
			var size = tmp[1];
			var width = tmp[2];
			var height = tmp[3];
			var parameters = {
				string: $(this).attr('title'),
				width: width,
				height: height,
				size: size
			};
			var gen_url = '/generator/titles?string='+$(this).attr('title')+'&width='+width+'&height='+height+'&size='+size;
			$.ajax({
				url: gen_url,
				data: parameters,
				success: function(data) {
					eval(data);
				}
			});
		})
	);
}

function initMenu(){
	// Handling topmenu
	if ($.browser.msie) {
		// Handling topmenu alternative
		// Can't get nested opacity-animations to work in ie :(
		/*
		$('#nav ul.top').hide();
		$('#nav').hover(
			function () {
				$(this).find('ul.top').show();
			},
			function () {
				$(this).find('ul.top').hide();
			}
		);
		*/
		// Never mind all that, IE is a steaming pile of crap
		// revert to standard css menu
		return;
	} else {
		$('#header').removeClass('classic'); // disable standard-css hovermenu
		$('#nav ul.top').animate({opacity: 0}, 0).hide();
		$('#nav').hover(
			function () {
				$(this).find('ul.top')
					.stop()
					.clearQueue()
					.show()
					.animate({opacity: 0.95}, 250)
					.css('zIndex', 88);
			},
			function () {
				$(this).find('ul.top')
					.stop()
					.clearQueue()
					.animate({opacity: 0}, 500, function() {
						$(this).hide();
					})
					.css('zIndex', 87);
			}
		);
	}
	
	// Handling submenu
	$('#nav ul.top ul.sub').animate({opacity: 0}, 0).hide();
	$('#nav ul.top li').hover(
		function () {
			$(this).css({backgroundColor: '#d9f4ff'});
			$(this).find('ul.sub')
				.stop()
				.clearQueue()
				.show()
				.animate({opacity: 0.95}, 250)
				.css('zIndex', 90);
		},
		function () {
			$(this).css({backgroundColor: ''});
			$(this).find('ul.sub')
				.clearQueue()
				.animate({opacity: 0}, 500, function() {
					$(this).hide();
				})
				.css('zIndex', 89);
		}
	);
	
	// Handling subsubmenu
	$('#nav ul.top ul.sub ul.subsub').animate({opacity: 0}, 0).hide();
	$('#nav ul.top li ul.sub li').hover(
		function () {
			$(this).css({backgroundColor: '#d9f4ff'});
			$(this).find('ul.subsub')
				.stop()
				.clearQueue()
				.show()
				.animate({opacity: 0.95}, 250)
				.css('zIndex', 90);
		},
		function () {
			$(this).css({backgroundColor: ''});
			$(this).find('ul.subsub')
				.clearQueue()
				.animate({opacity: 0}, 500, function() {
					$(this).hide();
				})
				.css('zIndex', 89);
		}
	);
	
	
	// Handling subsubsubmenu
	$('#nav ul.top ul.sub ul.subsub ul.subsubsub').animate({opacity: 0}, 0).hide();
	$('#nav ul.top li ul.sub li ul.subsub li').hover(
		function () {
			$(this).css({backgroundColor: '#d9f4ff'});
			$(this).find('ul.subsubsub')
				.stop()
				.clearQueue()
				.show()
				.animate({opacity: 0.95}, 250)
				.css('zIndex', 90);
		},
		function () {
			$(this).css({backgroundColor: ''});
			$(this).find('ul.subsubsub')
				.clearQueue()
				.animate({opacity: 0}, 500, function() {
					$(this).hide();
				})
				.css('zIndex', 89);
		}
	);
}

var sponsors = new Array();
function initSponsors(){
	var i = 0;
	$('#sponsors a').each(function(){
		$(this).hide().animate({opacity: 0}, 0).show();
		$(this).css({ backgroundImage: 'url( /public/sponsors/'+$(this).attr('rel')+')' });
		$(this).find('span').hide();
		sponsors[i] = $(this);
		i++;
	});
	numSlides = i;
	sponsorSlide(0);
}

function sponsorSlide(num){
	sponsors[num].show().animate({
		opacity: 1
	}, 1000 ).delay(2000).animate({
		opacity: 0
	}, 1000, function() {
		$(this).hide();
		if(sponsors.length == num+1){
			sponsorSlide(0);
		} else {
			sponsorSlide(num+1);
		}
	});
}

var prevElem;
function initLayout(){
	// Nieuws:
	$('.nieuws_item').each(function(){
		if($(this).hasClass('even')){
			prevElem = $(this);
		} else {
			if(prevElem.height() > $(this).height()){
				$(this).height(prevElem.height());
			} else {
				prevElem.height($(this).height());
			}
		}
	});
}

function initLightbox(){
	$('div.content img').each(function(){
		var source = $(this).attr('src');
		var width = $(this).width();
		var height = $(this).height();
		
		$(this).replaceWith(
			$('<a>')
			.attr({ href:source, rel: 'lightbox' })
			.append(
				$('<img>')
				.attr({ src:source })
				.width(width)
				.height(height)
			)
		);
	});
	$('a[rel=lightbox]').lightBox();
}

function charsize(){
	if($('body').hasClass('normal')){
		$('body').removeClass('normal');
		$('body').addClass('big');
		$.ajax({
			url: 'charsize/big'
		});
	} else {
		$('body').removeClass('big');
		$('body').addClass('normal');
		$.ajax({
			url: 'charsize/normal'
		});
	}
}

function clean(input){
	result = input.toLowerCase();
	result = result.replace(/\W/g, ' ');
	result = result.replace(/\ +/g, '-');
	result = result.replace(/\-$/g, '');
	result = result.replace(/^\-/g, '');
	return result;
}
