/** Mini month calendar */
var calendarTimeout = null;
var calendarInit = false;

function show_calendar(sourceElement)
{
	cbox = $('#mini-calendar-box');
	cbox.hide();
	elm = $(sourceElement);
	eOffset = elm.offset();

	if (calendarInit == false) {
		now = new Date();
		month = now.getFullYear();
		if (now.getMonth() < 9)
			month += "0";
		month += (now.getMonth() + 1);
		fill_month(month)

		calendarInit = true;
	}

	cbox.css('position', 'absolute');
	cbox.css('background-color', 'white');
	cbox.css('border', '1px solid #EFEFEF');
	cbox.css('top', (eOffset.top + elm.height()));
	cbox.css('left', (eOffset.left));
	cbox.show();

	cbox.mouseout(function() { calendarTimeout = window.setTimeout('close_calendar()', 1000); });
	cbox.mouseover(function () { window.clearTimeout(calendarTimeout); });

	return false;
}

function close_calendar()
{
	cbox = $('#mini-calendar-box');
	cbox.fadeOut();
}

function fill_month(month)
{
	months = ['Januari', 'Februari', 'Maart', 'April', 'Mei', 'Juni', 'Juli',
		'Augustus', 'September', 'Oktober', 'November', 'December'];

	m = month.match(/([0-9]{4})([0-9]{2})/);
	month = m[2] - 1;
	year = parseInt(m[1]);

	cbox = $('#mini-calendar-box');

	// Set header and navigation-links
	$('.month-name', cbox.get(0)).html(months[month] + " " + year);

	t_month = month;
	t_year = year;
	if (t_month < 1) {
		t_month = "12";
		t_year--;
	}
	else if (t_month < 10) {
		t_month = "0" + t_month;
	}
	t_input_back = '' + t_year + '' + t_month;
	$('.nav-back a', cbox.get(0)).get(0).onclick = function () { fill_month(t_input_back); };

	t_month = month + 2;
	t_year = year;
	if (t_month > 12) {
		t_month = "01";
		t_year++;
	}
	else if (t_month < 10) {
		t_month = "0" + t_month;
	}
	t_input_fwd = '' + t_year + '' + t_month;
	$('.nav-forward a', cbox.get(0)).get(0).onclick = function () { fill_month(t_input_fwd); };

	// Fill table and make links
	max_days = days_in_month(year, month);
	if (month == 0) {
		t_year = year - 1;
		t_month = 11;
	}
	else {
		t_year = year;
		t_month = month - 1;
	}

	max_days_prev = days_in_month(t_year, t_month);
	first_day = new Date();
	first_day.setFullYear(year, month, 1);
	offset_days = first_day.getDay();

	cur_day = (offset_days - 2) * -1;
	now = new Date();
	today = now.getDate();
	now_mon = now.getMonth();

	for (i = 0; i < 6; i++) {
		for (j = 0; j < 7; j++) {
			if (cur_day < 1) {
				t_month = month - 1;
				t_day = max_days_prev + cur_day;
				t_class = "other_month";
			}
			else if (cur_day > max_days) {
				t_month = month + 1;
				t_day = cur_day - max_days;
				t_class = "other_month";
			}
			else {
				t_month = month;
				t_day = cur_day;
				t_class = "";
			}

			// Check if there is an event.
			t_html = "";
			fmtDate = get_formatted_date(t_day, t_month, t_year);
			print_link = false;
			if (in_array(event_dates, fmtDate)) {
				print_link = true;
				t_html += "<a href=\"scripts/events.php?date="+fmtDate+"\">";
			}

			t_html += t_day

			if (print_link) {
				t_html += "</a>";
			}

			cell = $('.r'+ i +' .c' + j);
			cell.html(t_html)
			cell.removeClass('today');
			cell.removeClass('other_month');
			cell.addClass(t_class);
			if (cur_day == today && t_month == now_mon) {
				cell.addClass('today');
			}

			cur_day++;
		}
	}
}

function in_array(haystack, needle) {
	for (k = 0; k < haystack.length; k++) {
		if (haystack[k] == needle) {
			return true
		}
	}
	return false;
}

function days_in_month(year, month) {
	return 32 - new Date(year, month, 32).getDate();
}

function get_formatted_date(t_day, t_month, t_year)
{
	m = t_month + 1;
	if (m < 10) {
		m = "0" + m;
	}
	d = t_day;
	if (d < 10) {
		d = "0" + d;
	}
	return "" + t_year + "" + m + "" + d;
}

/** Site-interaction functions */

function change_genre(element)
{
	$(element).parents('form').get(0).submit();
}

function change_month(element)
{
	$(element).parents('form').get(0).submit();
}

function show_image(src)
{
	// Set source
	$('.picture-container img').attr('src', src +',324');

	// Disable youtube
	$('.youtube-container').addClass('hidden');
	
	// Enable picture
	$('.picture-container').removeClass('hidden');
}

function show_youtube()
{
	// Disable picture
	$('.picture-container').addClass('hidden');

	// Enable youtube
	$('.youtube-container').removeClass('hidden');
}

/** Help-functions */

function help_open(helpid)
{
	el = document.getElementById(helpid);
	el.style.display = "block";
}

function help_close(helpid)
{
	el = document.getElementById(helpid);
	el.style.display = "none";
}

function help_move(event,helpid)
{
	var scrollTop = 0;
	if (document.body.scrollTop > 0) {
		scrollTop = document.body.scrollTop;
	}
	else {
		scrollTop = document.documentElement.scrollTop;
	}

	if (event.pageX) {
		// !msie
		xPos = event.pageX;
		yPos = event.pageY;
	}
	else {
		// msie
		xPos = window.event.clientX;
		yPos = window.event.clientY;


		xPos += document.body.scrollLeft;
		yPos += scrollTop;
	}

	el = document.getElementById(helpid);

	var right = xPos + 20 + el.offsetWidth;
	var bottom = yPos + 20 + el.offsetHeight;
	var up = false;
	var left = false;

	if (typeof(window.innerWidth) == "number") {
		if (right > window.innerWidth && window.innerWidth > el.offsetWidth) {
			left = true;
		}
		
		if (bottom > (window.innerHeight + scrollTop) && window.innerHeight > el.offsetHeight) {
			up = true;
		}
	}
	else {
		if (right > document.documentElement.clientWidth && document.documentElement.clientWidth > el.offsetWidth) {
			left = true;
		}
		if (bottom > (document.documentElement.clientHeight + scrollTop) && document.documentElement.clientHeight > el.offsetHeight) {
			up = true;
		}
	}

	if (left) {
		el.style.left = (xPos - 20 - el.offsetWidth) + "px";
	}
	else {
		el.style.left = xPos + 20 + "px";
	}
	if (up) {
		if (typeof(window.innerHeight) == "number") {
			el.style.top = ((window.innerHeight - 10 - el.offsetHeight) + scrollTop) + "px";
		}
		else {
			el.style.top = ((document.documentElement.clientHeight - 10 - el.offsetHeight) + scrollTop) + "px";
		}
	}
	else {
		el.style.top  = yPos + 10 + "px";
	}
}

/**
 * Validate an emailaddress
 *
 * uses http://en.wikipedia.org/wiki/E-mail_address as reference
 */
function validate_email(email)
{
	var parts = email.split(/@/);

	if (parts.length != 2) {
		// not composed of local part and domain part
		return false;
	}

	var local_part = parts[0];
	var domain_part = parts[1];


	// check the local part

	var reg = /^([A-Za-z0-9!#\$%\*\/\?\|\^\{\}`~&'+\-=_.]+)$/;

	if (reg.test(local_part)) {
		if (local_part.indexOf("..") >= 0) {
			// double dot in local part
			return false;
		}
		if (local_part.indexOf(".") == 0) {
			// dot at start of local part
			return false;
		}
		if (local_part.lastIndexOf(".") == local_part.length-1) {
			// dot at end of local part
			return false;
		}
	}
	else {
		reg = /^\"([^\"]+)\"$/;

		if (!reg.test(local_part)) {
			// failed escaped local part
			return false;
		}
	}


	// check the domain part

	reg = /^[[]([0-9:.]+)[]]$/;
	
	if ((domain_part == "") || (domain_part == "[]")) {
		// invalid domain part
		return false;
	}
	else if (reg.test(domain_part)) {
		// check the ip address

		// (still missing)
	}
	else {
		// check the labels

		var labels = domain_part.split(/[.]/);
		reg = /^([A-Za-z0-9-]+)$/;

		var i;
		for(i=0; i < labels.length; i++) {
			var label = labels[i];

			if (label == "") {
				// empty label
				return false;
			}
			if (!reg.test(label)) {
				// invalid characters
				return false;
			}
			if (label.indexOf("-") == 0) {
				// hyphen at start of label
				return false;
			}
			if (label.lastIndexOf("-") == label.length-1) {
				// hyphen at end of label
				return false;
			}
		}
	}

	return true;
}

/* --- month calendar (page) --- */

function calender_setup_events()
{
	$("table.calendar tbody tr td div").hover(calendar_day_hover_on, calendar_day_hover_off)
}

function calendar_day_hover_on()
{
	offset = $(this).offset();
	
	$(this).css("top", offset.top + "px");
	$(this).css("left", offset.left + "px");
	$(this).css("position", "absolute");
	$(this).css("z-index", "9");
	
	$(this).addClass("hover");
	
	
	total_height = get_ul_height($(this).find("ul").get(0));
	
	if (total_height >= (89 - 28)) {
		$(this).find("ul").css("position", "relative");
	}
	else {
		$(this).find("ul").css("position", "absolute");
		$(this).find("ul").css("bottom", "0");
	}
	
	calender_expand_day_lists(this);
	
	// ie6 min-height fix:
	if ($(this).height() < 89) {
		$(this).css("height", "89px");
	} 
}

function calendar_day_hover_off()
{
	calender_collapse_day_lists(this);
	
	$(this).css("top", "0");
	$(this).css("left", "0");
	$(this).css("position", "relative");
	$(this).css("z-index", "1");
	
	$(this).removeClass("hover");
	
	$(this).find("ul").css("position", "absolute");
	$(this).find("ul").css("bottom", "0");
}

function calender_collapse_all_day_lists()
{
	$("table.calendar tbody tr td div").each(function (i) {
		calender_collapse_day_lists(this);
	});
}

function calender_collapse_day_lists(daycontainer)
{
	$(daycontainer).find("ul").each(function (i) {
		var total_height = 0;
		
		$(this).find("li").each(function (j) {
			total_height += ($(this).height() + 4);
			
			if (total_height >= (89 - 28)) {
				$(this).hide();
			}
		});
	});
}

function get_ul_height(ulelement)
{
	var total_height = 0;
	
	$(ulelement).find("li").each(function (j) {
		total_height += ($(this).height() + 4);
	});
	
	return total_height
}

function calender_expand_day_lists(daycontainer)
{
	$(daycontainer).find("ul li").each(function (i) {
		$(this).show();
	});
}

/* --- header --- */

function initNewsletter()
{
	var so = new SWFObject('img/newsletter.swf', 'newsletter-form', '197', '175', '8', '#FFFFFF');
	so.write('newsletter');	
}

/* --- Header resizing --- */

// JavaScript Document
var sizeInterval;
var dest_n;
var w;
var easing = 0.2;
var element = document.getElementById('flashHeader');
var curr_height;
var sizeInterval;

function resize(expand,watt){
	clearInterval(sizeInterval);
	if(expand){
		if(watt){
			try {
				makeCall(239);
			} catch (e) {}
		}
		$('#flashHeader').animate({
				height: '239px'
			}, 500, 'swing');
	}
	else{
		$('#flashHeader').animate({
				height: '117px'
			}, 500, 'swing', function () {
				try {
					makeCall(117)
				} catch (e) {}
			}
		);
	}
}

function set_flasheader_height(newHeight) {
	$('#flashHeader').animate({
			height: newHeight +'px'
		}, 500, 'swing');
}

function initHeader(){
	var so = new SWFObject('data/headers/header.swf', 'header', '100%', '100%', '8', '#F7F139');
	//var so = new SWFObject('swf/header.swf', 'header', '100%', '100%', '8', '#F7F139');
	so.addParam('scale','noscale');
	so.addParam('align','top');
	so.addParam('allowScriptAccess','sameDomain');
	so.write('flashHeader');
	
	$('#flashHeader').css('overflow', 'hidden');
}

function thisMovie(movieName) {
	var isIE = navigator.appName.indexOf("Microsoft") != -1;
	return (isIE) ? window[movieName] : document[movieName];
}

// call function inside Flash
function makeCall(str) {
	var movie = thisMovie("header");

	movie.resize(str);
}

/* --- slide news and program list  a.item-info --- */

var mouseover_ticketlink = false;
var hoverintent_fired_over = false;

$(document).ready(function(){
	$(".list.program ul li").hoverIntent (
		function () {	
			if (!mouseover_ticketlink) {
				$(this).find(".extra-details").show();
				var height = $(this).find("span.program-info").height();	
				if (height < 142 ) {
					height = 142;
				}
				$(this).find("a.item-info").animate({ 
						height: height+"px"
					}, 600 , function(){});	
			}
			else {
				hoverintent_fired_over = this;
			}
		},
		function () {
			$(this).find("a.item-info").animate({ 
				height: "97px"
			}, 200 );	
			$(this).find(".extra-details").hide();
		}
	);

	$(".list.program ul li a.ticketlink").hover (
		function () {
			mouseover_ticketlink = true;
		},
		function () {
			mouseover_ticketlink = false;

			if (hoverintent_fired_over) {
				el = hoverintent_fired_over;
				hoverintent_fired_over = false;

				$(el).find(".extra-details").show();
				var height = $(el).find("span.program-info").height();	
				if (height < 142 ) {
					height = 142;
				}
				$(el).find("a.item-info").animate({ 
						height: height+"px"
					}, 600 , function(){});	
			}
		}
	);

	$(".list.news .extra-details").each(function(){
			var length = $(this).html().length;
			var text =  $(this).html();
			if ((length > 75) && (text != "")) {
				var newtext = text.substr(0, 75);
				var moretext = text.substr(75,1000);
				$(this).empty().append(newtext + "<span class=\"intro-temp\">...</span><span class=\"intro-extra\">" + moretext + "</span>");
			
			}
			$(this).find(".extra-details").hide();
	});

	$(".list.news ul li a.item-info").hoverIntent (
		function () {
			$(this).find(".intro-temp").css("display","none");
			$(this).find(".intro-extra").show();
			var height = $(this).find("span.news-info").height();
			if (height < 142) {
				height = 142;
			}
			$(this).animate({ 
					height: height+"px"
				}, 600 , function(){});	
		},
		function () {
			$(this).animate({ 
				height: "97px"
			}, 200 );	
			$(this).find(".intro-temp").css("display","inline");
			$(this).find(".intro-extra").hide();
		}
	);

	$("#newsletter-dropdown a").click(function() { 
		$("div#newsletter").show();
		$("#newsletter-dropdown").hide();
		return false;
	});

	calender_collapse_all_day_lists();
	calender_setup_events();
});
