$(document).ready(function(){
	$('.topmenu a').each(function(){
		if($(this).attr('href') == document.location) $(this).addClass('active');
	});
	if($("#navigation").length){loadNavigation();}
	if($("#sitemap").length){loadSitemap();}
	if($("#rssdata").length){
		$.ajax({type: "GET", url: "rss.xml", dataType: "xml", success: function(r){loadRSS(r);}});
	}
	if($("#rssdata_ticker").length){
		$.ajax({type: "GET", url: "http://www.melanchthon.nl/nieuws/rss.xml", dataType: "xml", success: function(r){loadRSS_ticker(r);}});
	}
	loadSifr();
	if($("#cycle").length){loadCycle();}
	if($("#map").length){loadTooltip();}
	resizeImage();
	if($("#educationDiagram").length){showEducationInfo2();}
});

function resizeImage(){
	var img; var w; var h; var c;
	if($('.content1 .text img')){
		$('.content1 .text img').each(function(){
			w = $(this).width();
			h = $(this).height();
			c = h / w;
			if(w > 380){
				$(this).width(380);
				$(this).height(380 * c);
			}
		});
	}
	if($('.content2 img')){
		$('.content2 img').each(function(){
			w = $(this).width();
			h = $(this).height();
			c = h / w;
			if(w > 140){
				$(this).width(140);
				$(this).height(140 * c);
			}
			$(this).wrap('<a href="'+$(this).attr('src')+'" rel="gallery" class="thickbox"></a>');
		});
	}
}

function loadNavigation(){
	$('#navigation .head .active').next('ul').show();
	$('#navigation .head .active').parent('.head').addClass('open');
	$('#navigation .head').children('a').click(function(){
		if($(this).parent('.head').attr('class') != 'head open'){
			$('#navigation .open ul').slideUp();
			$('#navigation .open').removeClass('open');
		}
		$(this).next('ul').slideToggle();
		$(this).parent('.head').toggleClass('open');
	});
}

function loadSitemap(){
	$('#sitemap .super').children('a').click(function(){
		if($(this).parent('.super').attr('class') != 'super open'){
			$('#sitemap .open ul').slideUp();
			$('#sitemap .open').removeClass('open');
		}
		$(this).next('ul').slideToggle();
		$(this).parent('.super').toggleClass('open');
	});
	
	$('#sitemap .head').children('a').click(function(){
		if($(this).parent('.head').attr('class') != 'head open'){
			$('#sitemap ul .open ul').slideUp();
			$('#sitemap ul .open').removeClass('open');
		}
		$(this).next('ul').slideToggle();
		$(this).parent('.head').toggleClass('open');
	});
}

/*---sifr---*/
function loadSifr() {
	fontLoc = "http://www.melanchthon.nl/flash/scalasansbold.swf";
	if(typeof sIFR == "function"){
		sIFR.replaceElement(named({sSelector:"h1", sFlashSrc:fontLoc, sColor:"#063771", sLinkColor:null, sBgColor:null, sHoverColor:null, sWmode:"transparent", sCase:"upper", sFlashVars:"textalign=left&offsetTop=0"}));
		sIFR.replaceElement(named({sSelector:"h2", sFlashSrc:fontLoc, sColor:"#063771", sLinkColor:null, sBgColor:null, sHoverColor:null, sWmode:"transparent", sCase:"upper", sFlashVars:"textalign=left&offsetTop=0"}));
		sIFR.replaceElement(named({sSelector:"h4", sFlashSrc:fontLoc, sColor:"#063771", sLinkColor:null, sBgColor:null, sHoverColor:null, sWmode:"transparent", sCase:"upper", sFlashVars:"textalign=center&offsetTop=0"}));
	}
}

/*---cycle---*/
function loadCycle() {
	$('#cycle').cycle({timeout: 5000, speed: 1500});
}

/*---tooltip---*/
function loadTooltip() {
	$('#map a').tooltip({track: true, showURL: false});
}

/*---ticker---*/
var headline_count;
var headline_interval;
var old_headline = 0;
var current_headline = 0;
function activateTicker() {
	headline_count = $("#rssticker li").size();
	$("#rssticker li:eq("+current_headline+")").css('top',0);
	headline_interval = setInterval(headline_rotate,5000); //time in milliseconds
	$('#rssticker').hover(function() {
		clearInterval(headline_interval);
	}, function() {
		headline_interval = setInterval(headline_rotate,5000); //time in milliseconds
		headline_rotate();
	});
}

function headline_rotate() {
	current_headline = (old_headline + 1) % headline_count; 
	$("#rssticker li:eq(" + old_headline + ")").animate({top:-33},"slow", function() {
		$(this).css('top','40px');
	});
	$("#rssticker li:eq(" + current_headline + ")").show().animate({top:0},"slow");  
	old_headline = current_headline;
}

/*---rss---*/
function loadRSS(xml) {
	var strRSS = '<ul id="rss">';
	$(xml).find('item:lt(10)').each(function(){
		var txt_title = $(this).find('title').text();
		var txt_link = $(this).find('link').text();
		
		strRSS += '<li><a';
		if(document.location.href == txt_link) strRSS += ' class="active"';
		strRSS += ' href="' + txt_link + '"><span class="title">';
		if(txt_title.length <= 45) strRSS += txt_title;
		else strRSS += txt_title.substring(0,42) + '&#133;';
		strRSS += '</span></a></li>';
	});
	strRSS += '</ul>';
	$("#rssdata").append(strRSS);
}

function loadRSS_ticker(xml) {
	var strRSS = '<ul id="rssticker">';
	$(xml).find('item:lt(5)').each(function(){
		var txt_title = $(this).find('title').text();
		var txt_link = $(this).find('link').text();
		
		strRSS += '<li><h4><a href="' + txt_link + '">';
		if(txt_title.length <= 43) strRSS += txt_title;
		else strRSS += txt_title.substring(0,40) + '&#133;';
		strRSS += '</a></h4></li>';
	});
	strRSS += '</ul>';
	$("#rssdata_ticker").append(strRSS);
	activateTicker();
	loadSifr();
}

/*---educationinfo---*/
function showEducationInfo(td,infoName) {
	$('td').removeClass('active');
	$(td).addClass('active');
	$('.educationInfo').hide();
	$('#'+infoName).show();
}

function showEducationInfo2(){
	$('#educationDiagram a').click(function(){
		var infoName = $(this).attr('rel');
		$('#educationDiagram a').removeClass('active');
		$(this).addClass('active');
		$('.educationInfo').hide();
		$('#'+infoName).show();
	});
}