var animatingsidebar = false;
var overlay = '<div id="jquery-overlay"></div><div id="jquery-lightbox"><div id="lightbox-container-image-box"><div id="lightbox-container-image"><div id="videoplayer"></div></div></div><div id="lightbox-container-image-data-box"><div id="lightbox-container-image-data"><div id="lightbox-image-details"><a href="javascript: closeflowplayer()" id="lightbox-secNav-btnClose"><img src="images/lightbox-btn-close.gif"></a></div></div></div></div>';

Cufon.set('fontFamily','TemperaRoseA').replace('#menu',{hover: true});
Cufon.set('fontFamily','TemperaRoseC').replace('#submenu',{hover: true});
//Cufon.set('fontFamily','TemperaRoseC').replace('#search');
Cufon.set('fontFamily','TemperaRoseC').replace('#sidebar h4');
Cufon.set('fontFamily','TemperaRoseA').replace('h1');
Cufon.set('fontFamily','TemperaRoseA').replace('h2');
Cufon.set('fontFamily','TemperaRoseC').replace('h3');
Cufon.set('fontFamily','TemperaRoseC').replace('#footer');
Cufon.set('fontFamily','TemperaRoseC').replace('#list_course strong');


$(window).load(function() {
	fixlayout();
});

$(document).ready(function() {
       $('textarea[maxlength]').keyup(function(){   
           var limit = parseInt($(this).attr('maxlength'));  
           var text = $(this).val();  
           var chars = text.length;  
          if(chars > limit){  
              var new_text = text.substr(0, limit);  
              $(this).val(new_text);  
           }  
       });  						   
						   
	fixlayout();
	
	$("h4:first-child").addClass("first");
	//$("*:last-child").addClass("last");
		
	//
	//	Disable Background Selection
	//
	$(function(){
		$.extend($.fn.disableTextSelect = function() {
			return this.each(function(){
				if($.browser.mozilla){//Firefox
					$(this).css('MozUserSelect','none');
				}else if($.browser.msie){//IE
					$(this).bind('selectstart',function(){return false;});
				}else{//Opera, etc.
					$(this).mousedown(function(){return false;});
				}
			});
		});
		$("#background").disableTextSelect();
	});
	
	//
	//	Search field
	//
	$("#search .category").click(function(){
		if ($("#search .category").hasClass("active")) {
			$("#search .category ul").animate({height: 15});
			$("#search .category").removeClass("active");
		} else {
			$("#search .category ul").animate({height: 140});
			$("#search .category").addClass("active");
		}
	});

	$("#search .category ul li").click(function(){
		$("#search_category").val($(this).attr("rel"));
		$("#search .category ul li:first").remove();
		$(this).clone().prependTo($("#search .category ul"));
	});
	
	var scval = $("#search_category").val()
	if (scval) {
		$("#search .category ul li:first").remove();
		$("#search .category ul li[rel='"+scval+"']").clone().prependTo($("#search .category ul"));
	}

	//
	//	Fancy image animations
	//
	var times = $(".fancy_image").size();
	if (times > 9) {
		$(".fancy_image .title").css({marginTop: "140px",opacity: 1});
		$(".fancy_image .subtitle").css({marginTop: "170px",opacity: 1});
	} else {
		$(document).everyTime(250, function(i) {
			var obj = $(".fancy_image:eq("+(i-1)+")");
			$(obj).find(".title").animate({marginTop: "160px",opacity: 0.5},200).animate({marginTop: "130px",opacity: 0.7},200).animate({marginTop: "140px",opacity: 1},300);
			
			$(document).oneTime(100,function(){
				$(obj).find(".subtitle").animate({marginTop: "190px",opacity: 0.5},200).animate({marginTop: "160px",opacity: 0.7},200).animate({marginTop: "170px",opacity: 1},300);
			});
		}, times);
	}
	
	
	//
	//	Date Slider
	//
	$(".datenav .next a").live('click',function(){ if (!animatingsidebar) { slide_datelist('left',$(this)); } return false; });
	$(".datenav .prev a").live('click',function(){ if (!animatingsidebar) { slide_datelist('right',$(this)); } return false; });
	
	//
	//	Lightbox & Videobox
	//
	$("a.lightbox").lightBox();
	
	$("a.videobox").click(function(){
		var href = $(this).attr("href");
		initflowplayer(href);
		return false;
	});
	
	//
	//	Related expander
	//
	
	$(".related .expand").click(function(){
		$(".related .expand").parent().find("ul").slideUp();
		$(this).parent().find("ul").slideDown();
		return false;
	});
	
	//
	//	Fancy imagelist
	//
	
	if ($("#fancy_imagelist").size()) {
		$("#columncenter ul li").mouseover(function(){
			var index = $("#columncenter ul li").index($(this));
			var offset = $(this).offset();
			var toppos = $("#columncenter").offset();
			var paddingtop = ((offset.top - toppos.top - 70) < 0) ? 0 : (offset.top - toppos.top - 70);
			//$("#fancy_imagelist").stop(true).animate({paddingTop: paddingtop});
			$("#fancy_imagelist ul").stop(true).animate({scrollTop: (index*200)});
		});
	}
	
	//
	//	Cursuslijst
	//
	
	if ($("#list_course #columncenter ul").size()) {
		format_courselist();
		
		$("#list_course .related a").click(function(){
			switch_courselist($(this));
			return false;
		});
		
		var page = 1;
		$("#list_course .more a").click(function(){
			var showfrom = (page * 20);
			var elemheight = $("#list_course #columncenter ul li:first").height();
			if (!$("#list_course #columncenter ul li:visible:gt("+showfrom+")").size()) {
				$("#list_course .more a").html('meer cursussen');
				showfrom = 0;
				page = 0;
			}
			var top = (page * 20 * elemheight);
			$("#list_course #columncenter #list").stop().animate({scrollTop: top},600);
					
			
			if (!$("#list_course #columncenter ul li:visible:gt("+(showfrom + 20)+")").size()) {
				$("#list_course .more a").html('naar boven');
			}
			
			format_courselist();
			page++;
			return false;
		});
	}
	
	//
	//	Cursusdetail
	//
	
	$("#courseprice a").click(function(){
		link = $(this);
		$("#coursepricedata").slideToggle('fast',function(){
			$('html, body').animate({scrollTop: (link.offset().top)}, 1000);	
			fixlayout();
		});
		return false;
	});
	
	$("#coursepricedata").find("input, select").blur(function(){
		calc_course_price();
	});
	
	$("#coursepricedata button").click(function(){
		calc_course_price();
	});
	
	function calc_course_price() {
		var student = 'nietwaar';
		if($("#coursepricedata input[name='student']").attr('checked')) {
			student = 'waar';
		}
		if ($("#coursepricedata input[name='birth_day']").val() && $("#coursepricedata select[name='birth_month']").val() && $("#coursepricedata input[name='birth_year']").val() && $("#coursepricedata input[name='postcode']").val()) {
			var data = {birth_day: $("#coursepricedata input[name='birth_day']").val(),
						birth_month: $("#coursepricedata select[name='birth_month']").val(),
						birth_year: $("#coursepricedata input[name='birth_year']").val(),
						postcode: $("#coursepricedata input[name='postcode']").val(),
						student: student,
						type: $("#coursepricedata select[name='type']").val(),
						imasid: $("#coursepricedata input[name='imasid']").val()};
			$("#courseprice span a").load('modules/cursusprijs2.inc.php',data);
		}
	}
	
	if ($("#coursepricedata input[name='auto']").val() == 'true') {
		$("#coursepricedata button").click();
	}
	

	//
	//	Google Maps
	//
	if ($("#googlemaps").size()) { go_go_google_maps(); }
	
	function go_go_google_maps() {
		var latlng = new google.maps.LatLng(50.85245000250733,5.694866180419922);
		var myOptions = {
			zoom: 13,
			center: latlng,
			mapTypeId: google.maps.MapTypeId.ROADMAP
    	};
		var map = new google.maps.Map(document.getElementById("googlemaps"), myOptions);
		
		var marker1 = new google.maps.Marker({
			position: new google.maps.LatLng(50.851204,5.699029),
			map: map,
			title:"Kumulus Muziekschool"
		});
		
		var marker2 = new google.maps.Marker({
			position: new google.maps.LatLng(50.851014,5.681348),
			map: map,
			title:"Kumulus Beeldende kunsten / Theaterschool / Schrijversschool / Kunstverkennen"
		});
		
		var marker3 = new google.maps.Marker({
			position: new google.maps.LatLng(50.866022,5.835360),
			map: map,
			title:"Kumulus Heuvelland"
		});
		
		var marker4 = new google.maps.Marker({
			position: new google.maps.LatLng(50.849713,5.684931),
			map: map,
			title:"Impulse, ballet & dans [studio 1 & 2]"
		});
		
		var route1 = 'http://maps.google.nl/maps?q=Kumulus Muziekschool&hl=nl&vps=2&jsv=184a&sll=50.851204,5.699029&ie=UTF8&view=map&f=d&daddr=Sint Maartenspoort 2, 6221 BA, Maastricht';
		var route2 = 'http://maps.google.nl/maps?q=Kumulus Beeldende kunsten&hl=nl&vps=2&jsv=184a&sll=50.851014,5.681348&ie=UTF8&view=map&f=d&daddr=Herbenusstraat 89, 6211 RB Maastricht';
		var route3 = 'http://maps.google.nl/maps?q=Kumulus Heuvelland&hl=nl&vps=2&jsv=184a&sll=50.866022,5.835360&ie=UTF8&view=map&f=d&daddr=Steenstraat 2, 6301 ED Valkenburg a/d Geul';
		var route4 = 'http://maps.google.nl/maps?q=Impulse, ballet & dans [studio 1 & 2]&hl=nl&vps=2&jsv=184a&sll=50.849713,5.684931&ie=UTF8&view=map&f=d&daddr=Brusselsestraat 10D, 6211 PE Maastricht';
		
		var infowindow1 = new google.maps.InfoWindow({ content: '<div style="width:250px; height: 100px;"><strong>Kumulus Muziekschool</strong><br>Sint Maartenspoort 2<br>6221 BA Maastricht<br>Telefoon (+31 - 043) 350 56 69<br><a href="'+route1+'" target="_blank">Routebeschrijving</a></div>' });
		var infowindow2 = new google.maps.InfoWindow({ content: '<div style="width:250px; height: 100px;"><strong>Kumulus Beeldende kunsten</strong><br>Herbenusstraat 89<br>6211 RB Maastricht<br>Telefoon (+31 - 043) 350 56 56<br><a href="'+route2+'" target="_blank">Routebeschrijving</a></div>' });
		var infowindow3 = new google.maps.InfoWindow({ content: '<div style="width:250px; height: 100px;"><strong>Kumulus Heuvelland</strong><br>Steenstraat 2<br>6301 ED Valkenburg a/d Geul<br>Telefoon (+31 - 043) 601 31 40<br><a href="'+route3+'" target="_blank">Routebeschrijving</a></div>' });
		var infowindow4 = new google.maps.InfoWindow({ content: '<div style="width:250px; height: 100px;"><strong>Impulse, ballet & dans [studio 1 & 2]</strong><br>Brusselsestraat 10D<br>6211 PE Maastricht<br>Telefoon (+31 - 043) 321 35 59<br><a href="'+route4+'" target="_blank">Routebeschrijving</a></div>' });

		google.maps.event.addListener(marker1, 'click', function() { infowindow1.open(map, marker1); infowindow2.close(); infowindow3.close(); infowindow4.close(); });
		google.maps.event.addListener(marker2, 'click', function() { infowindow2.open(map, marker2); infowindow1.close(); infowindow3.close(); infowindow4.close(); });
		google.maps.event.addListener(marker3, 'click', function() { infowindow3.open(map, marker3); infowindow1.close(); infowindow2.close(); infowindow4.close(); });
		google.maps.event.addListener(marker4, 'click', function() { infowindow4.open(map, marker4); infowindow1.close(); infowindow2.close(); infowindow3.close(); });
	}
	
	//
	//	PNG Fix
	//
	$(document).pngFix();
	
});

// Datumlijst aan rechterkant van website animeren
function slide_datelist(dir,obj) {
	var numin = $("#sidebar .datelist li").size();
	var url = $(obj).attr("href");
	animatingsidebar = true;
	
	switch(dir) {
		case "left":	var posout1 = "-20px";		var posout2 = "300px";		var neutral = "-300px";		break;
		case "right":	var posout1 = "20px";		var posout2 = "-300px";		var neutral = "300px";		break;
	}

	$("#cache").load(url +" #sidebar","",function(){
		$(document).everyTime(100, function(i) {
			$(".datelist li:eq("+(i-1)+")").stop(true,true).animate({marginLeft: posout1},200).animate({marginLeft: posout2},300,'',function(){
				if (i == numin) {
					$("#cache .datelist li").css({marginLeft: neutral});
					$("#sidebar .datelist").html($("#cache .datelist").html());
					$("#sidebar .datenav").html($("#cache .datenav").html());
					
					var numout = $("#sidebar .datelist li").size();
					$(document).everyTime(100, function(n) {
						$(".datelist li:eq("+(n-1)+")").stop(true,true).animate({marginLeft: "0px"},200);
						if (n == numout) { animatingsidebar = false; $("#cache").html("."); }
					});
				}
			});
		},numin);
	});
}

// Videoplayer openen in popup
// daarna videoplayer starten
function initflowplayer(href) {
	$("body").append(overlay);
	$("#jquery-overlay").fadeIn();
	$("#jquery-lightbox").css({width: "600px", height: "500px", top: "100px", left: "50%", marginLeft: "-250px"});
	$("#lightbox-container-image-box").css({width: "620px", height: "520px"});
	$("#lightbox-secNav-btnClose").css({float: "none", textAlign: "center"});
	
	flowplayer("videoplayer","flash/flowplayer-3.1.3.swf", {
		clip: { autoPlay: true, autoBuffering: true },
		playlist: [href],
		controls: null
	});
	return false;
}

// Videoplayer sluiten
function closeflowplayer() {
	$f().stop();
	$("#jquery-overlay").remove();
	$("#jquery-lightbox").remove();
}

// Content/Sidebar hoogte herstellen
// hoogte object is bepalend
function fixlayout(){
	var height = 700;
	var hc = $("#content").css({height: 'auto'}).height();
	var hs = $("#sidebar").css({height: 'auto'}).height();
	
	height = hc > height ? hc : height;
	height = hs > height ? hs : height;
	
	$("#content").height(height);
	$("#sidebar").height(height);
}


// Cursuslijst formatteren
function format_courselist() {
	var num = $("#list_course #columncenter ul li:visible").size();
	if (num > 19) 	{ $("#list_course #columncenter .more").show(); }
	else 			{ $("#list_course #columncenter .more").hide(); }
	
	if (num == 0) 	{ $("#list_course #columncenter .none").show(); }
	else			{ $("#list_course #columncenter .none").hide(); }
}

function switch_courselist(object) {
	var cl = "."+$(object).attr("class");
	var rel = $(object).closest("li").attr("rel");
	
	var current = $("#list_course #columnleft .related li[rel='"+rel+"'] "+cl+"").closest("li").hasClass("active");

	$("#list_course #columnleft .related li[rel='"+rel+"']").removeClass("active");
	if (!current) { $("#list_course #columnleft .related li[rel='"+rel+"'] "+cl+"").closest("li").addClass("active"); }
	
	$("#list_course #columncenter ul li").show();
	
	var age = $("#list_course #columnleft .related li[rel='age'].active");
	if ($(age).size()) {
		var cls = $(age).find("a").attr("class");
		$("#list_course #columncenter ul li").not("."+cls+"").hide();
	}

	var day = $("#list_course #columnleft .related li[rel='day'].active");
	if ($(day).size()) {
		var cls = $(day).find("a").attr("class");
		$("#list_course #columncenter ul li").not("."+cls+"").hide();
	}

	var cat = $("#list_course #columnleft .related li[rel='cat'].active");
	if ($(cat).size()) {
		var cls = $(cat).find("a").attr("class");
		$("#list_course #columncenter ul li").not("."+cls+"").hide();
	}

	format_courselist();
	$("#list_course #columncenter #list").scrollTop(0);
}

function goto(url) {
	document.location.href = url;
}
