$(document).ready(function(){

	initHomeAnimation();			   
	initSearchFocus();
	initCalendar();
	initPodium();
	initArchive();
	
	$("#social_icons img").css({ opacity: 0.5 });
	
	$("#social_icons img").hover(function() {
	  $(this).animate({ opacity: 1 });
	}, function() {
	  $(this).animate({ opacity: 0.5 });
	});

	
	$("img[align='left']").addClass("alignleft");
	$("img[align='right']").addClass("alignright");
	
});

var maxCount = 0;
var itemCount = 0;

function initArchive(){
	if($(".archive").length){
		
		$(".archive:first").css("padding-top", "5px");
		$(".archive:last").css("padding-bottom", "5px");
		
		$(".archive").each(function(){
									
			$(this).find("li").addClass("item");
			$(this).find("li:first").removeClass("item");
									
			$(this).find("li.item").css("display", "none");
			
			$(this).find("li:first").click(function(){
				if($(this).parents(".archive").hasClass("activeArchive")){
					$(this).parents(".archive").find("li.item").slideUp("fast");
					$(this).parents(".archive").removeClass("activeArchive");
				} else {
					$(this).parents(".archive").find("li.item").slideDown("fast");
					$(this).parents(".archive").addClass("activeArchive");
				}
			});
		});
	}
}


function initPodium(){
	if($("#podium").length){
		
		$("#podium .item .text").each(function(){
			var words = "";
			var arr = $(this).text().split(" ");
			for(var i = 0; i < arr.length; i++){
				if(arr[i]){
					words += "<span class=\"word\">" + arr[i] + "</span>";
				}
			}
			$(this).html(words);
			
			var y = 0;
			$(this).find(".word").each(function(){
				if($(this).position().top != y){
					$(this).addClass("newline");
					$(this).before("<div class=\"line\"></div>");
				}					
				y = $(this).position().top;
			});
		});
		
		$("#podium .item").hide();
		$("#podium .item").eq(itemCount).show();
	
		maxCount = $("#podium .item").length;
		window.setInterval("tickerTape()", 5000); 
	}
}

function tickerTape(){
	$("#podium .item").eq(itemCount).fadeOut();
	
	itemCount++;
	if(itemCount >= maxCount){
		itemCount = 0;
	}
	
	$("#podium .item").eq(itemCount).fadeIn();
}

// Animation homepage
function initHomeAnimation(){
	if ($('#animationHome').length) {
		$('#animationHome').flash(
		{ 
			src: '../swf/home.swf',
			width: 934,
			height: 288,
			wmode: 'opaque',
			flashvars: { pathToXml: '../php/AjaxController.php' }
		},
		{ version: 8 }
		);
	}
}

// Hide input field content
function initSearchFocus(){
    if ($('#q').length) {
		$('#q').focus(function() {
			if ($(this).val()=="zoek cursussen..."||$(this).val()=="Vul hier je trefwoord in") $(this).val("");
		});
	}
    if ($('#searchfield').length) {
		$('#searchfield').focus(function() {
			if ($(this).val()=="Zoeken") $(this).val("");
		});
	}
}

// Initialise the calendar
function initCalendar(){
	if($('#calendar').length){
		$('#calendar .next, #calendar .prev').click(function(){
			var getMonth = $(this).attr('rel');
			$.ajax({
				type: "POST",
				dataType: "html",
				url: "/php/AjaxController.php",
				data: "action=rGetCalendar&month="+getMonth,
				success: function(msg){
					if(msg.length){
						$('#calendar').replaceWith(msg);
						initCalendar();
					}
				}
			});
		});
	}
}
