function cufon()
{
	Cufon.replace('li.navigation a', {hover: true});
	Cufon.replace('h1');
	Cufon.replace('h2');
	Cufon.replace('h2.detail');
	Cufon.replace('h6');
	Cufon.replace('div.fiche-detail-titel');
	Cufon.replace('div.paragraph a', {hover : true});
	Cufon.replace('a.tickets');
	Cufon.replace('span.tickets');
	Cufon.replace('li.sidebar', {hover : true});
	Cufon.replace('.subtitle');

	Cufon.replace(".cufon");
	Cufon.replace('div#top span', {hover: true});
	Cufon.replace('div#top ul li', {hover: true});
	Cufon.replace('span.date-large');
	Cufon.replace("span.footer-text");
	
	Cufon.replace("#advancedSearchLink");

}

var currentIndex = 0;
var numOfImages = 0;
var holderX = 0;
function moveThumbs(direction) {
	var currentImage = $('div.thumbsholder').children()[currentIndex].id;
	currentImage = $("#" + currentImage);
	var imgX = currentImage.position().left;
	var xTo = -imgX;
	$('div.thumbsholder').animate({'left': xTo + "px"}, 500);
}

function checkButtons() 
{
	if (currentIndex < 1) {
		$('#thumbswidget a.prev').hide();
	} else {
		$('#thumbswidget a.prev').show();
	}
	if (currentIndex == numOfImages -1) {
		$('#thumbswidget a.next').hide();
	} else {
		$('#thumbswidget a.next').show();
	}
}

function fixColumns() 
{
	$("#center").show();
	if (controller == "index") {
		if ($("#binnenkort").attr("id")) {
			var h1 = $("#home-calendar").height();
			var h2 = $("#binnenkort").height();
			
			h = h1;
			
			if (h1 < h2) {
				h = h2;				
			}
		} else if ($("#blikvanger").attr("id")) {
			h = $("#blikvanger").offset().top - $("#home-middle").offset().top;
	
		} 
		
		$("#home-middle").height(h+30);
	}
	
	$("#content-fix").height(0);
	$("#nav-fix").height(0);
	$("#nav-spacer").height(0);
	
	var h = $("#footer").offset().top+$("#footer").outerHeight(true)-$("#wrapper").offset().top;
	$("#left, #right").height(h);
	
	h = h-$("#footer").outerHeight(true)-$("#footer-spacer").outerHeight(true)-$("#footer-top").outerHeight(true);
	$("#nav-spacer").height(h);
	
	var l = $("#nav").outerHeight(true);
	var fix = $("#nav-spacer").outerHeight(true);
	var r = $("#content").outerHeight(true) + $("#top").outerHeight(true);
	
	var lFix = fix-l;
	var rFix = fix-r;
	
	if (lFix < 0) {
		lFix = 0;		
	}
	
	if (rFix < 0) {
		rFix = 0;		
	}
	
	$("#nav-fix").height(lFix);

	$("#content-fix").height(rFix);

	if (timing) {
		setTimeout("fixColumns();", timingVar);
	}

	if (background) {
		$("body").css("background-image", "url('" + background + "')");
	}
}

$(window).load(function(){
	fixColumns();
});


var form = "";

function getAantal()
{
	var aantal = 0;
	
	$("input.aantal").each(function(){
		if ($(this).attr("checked")) {
			aantal++;
		} 
	});
	
	$("select.aantal").each(function(){
		var val = parseFloat($(this).val());;
		
		if (val) {
			aantal++ ;
		}
	});
	
	return aantal;
}

function fixPrices()
{
	var aantal = getAantal();
	var aantalTickets = 0;
	var total = 0;
	
	$("input.aantal").each(function(){
		if ($(this).attr("checked")) {
			aantalTickets += parseFloat(aantalAbo);
		} 
	});
	
	$("select.aantal").each(function(){
		var val = parseFloat($(this).val());;
		
		if (val) {
			aantalTickets += val;
		}
	});
	
	$("input.aantal").each(function(){
		var price = 0;
		var id = this.id.split("aantal-").join("");
		
		if ($(this).attr("checked")) {
			if (aantal < 6) {
				price = $("#basis-" + id).html();
			} else if (aantal < 10) {
				price = $("#abo6-" + id).html();
			} else {
				price = $("#abo10-" + id).html();	
			}
			
		} 
		
		price = parseFloat(price);
		total += price*aantalAbo;
		$("#total-" + id).html(price*aantalAbo);
		
	});
	
	$("select.aantal").each(function(){
		var price = 0;
		var id = 0;
		
		//if (parseInt($(this).val()) > 0) {
			if (this.id.indexOf("aantal-") != -1) {
				id = this.id.split("aantal-").join("");
				
				if (aantal < 6) {
					price = $("#basis-" + id).html();
				} else if (aantal < 10) {
					price = $("#abo6-" + id).html();
				} else {
					price = $("#abo10-" + id).html();	
				}
				
				price = parseInt($(this).val()) * parseInt(price);
				
				if (parseInt($(this).val()) == 0) {
					price = 0;
				}
				
				$("#total-" + id).html(price);
			} else if (this.id.indexOf("aantal2-") != -1) {
				id = this.id.split("aantal2-").join("");
				price = parseInt($(this).val()) * parseInt($("#special-" + id).html());
				
				if (parseInt($(this).val()) == 0) {
					price = 0;
				}
				$("#total2-" + id).html(price);
			}
			total += price;
		//}

		
		
	});
	
	$("#abo-total-number-events").html(aantal);
	$("#abo-total-number-tickets").html(aantalTickets);
	$("#abo-total").html(total);
	
	fixColumns();
}
$(document).ready( function() {
	//clear firefox cache for iframe source
	$("iframe").each(function(){
		$(this).attr('src', $(this).attr('src'));
	});
	$("a.toggle-embed").click(function(evt){
		evt.preventDefault();
		$(this).parent().siblings('div.embed').toggle();
		fixColumns();
	});	
	
	$("input.aantal").click(function(){
		fixPrices();
	});
	fixPrices();
	$("#submit_form").click(function(){
		form = $(this).attr("rel");
		return validate_form(form);	
	});
	

	$('div#zetel').hover(
			function(){
				$("img#plan").show();
			}, function() {
				$("img#plan").hide();
			}
	);
	
	$("select.aantal").change(function(){
		var val = parseInt($(this).val());
		
		var id = $(this).attr("id").split("-");
		var number = id[1];
		id = id[0];
		
		var val2 = 0;
		
		var second = null;
		
		if (id == "aantal") {
			second = $("#aantal2-" + number);
			val2 = parseInt(second.val());			
		} else {
			second = $("#aantal-" + number);
			val2 = parseInt(second.val());			
		}
		
		if ((val + val2) > aantalAbo) {
			second.val(aantalAbo-val);
		} else if (val < aantalAbo) {
			second.val(aantalAbo-val);
		}
		
		fixPrices();
	});
	$("span.location").click(function(){
		var temp = $(this).attr("rel");	
		
		if (this.id == "abo-events") {
			var data = "";
			var ids = new Array();
			var prices = new Array();
			var types = new Array();
			
			var aantal = parseInt($("#abo-total-number-events").html());
			
			var type = "";
		
			
			$("input.aantal").each(function(){
				if ($(this).attr("checked")) {
					var id = this.id.split("aantal-").join("");
					
					ids.push(id);
					
					var price = $("#total-" + id).html();
					
					prices.push(price);
					
					if (aantal<6) {
						type = "basis";				
					} else if (aantal < 10) {
						type = "abo6";
					} else {
						type = "abo10";
					}
					
					types.push(type);
				} 
			});
			
			$("select.aantal").each(function(){
				if (parseInt($(this).val()) > 0) {
					var id = 0;
					if (this.id.indexOf("aantal-") != -1) {
						id = this.id.split("aantal-").join("");
						var price = $("#total-" + id).html();
						if (aantal<6) {
							type = "basis";				
						} else if (aantal < 10) {
							type = "abo6";
						} else {
							type = "abo10";
						}
					} else if (this.id.indexOf("aantal2-") != -1) {
						id = this.id.split("aantal2-").join("");
						var price = $("#total2-" + id).html();
						type = $("#label-" + id).html();
						type = type.split("&nbsp;").join(" ");
					}
					
					ids.push(id);					
					prices.push(price);
					types.push(type);
				} 
			});
				
			
			
			data += "ids=" + ids.join(";");
			data += "&prices=" + prices.join(";");
			data += "&types=" + types.join(";");
			
			var validate = true;
			
			if (getAantal() < 6) {
				validate = false;
			}
			if (validate) {
				$.ajax( {
					type : "POST",
					url : "/ajax/save-abo-events/",
					data : data,
					success : function(msg) {
						window.location = temp;			
					}
				});
			} else {
				alert("je moet minstens 6 voorstellingen selecteren!");
			}
			
		} else {
			window.location = temp;			
		}
		
		if (validate) {
			$(this).hide();
		}
		fixColumns();
	})
	
	$("img.step").click(function(){
		var url = $(this).attr("rel");
		
		if (url) {
			window.location = url;
		}
	});
	
	$('#form_contact, #form_newsletter, , #form_grimburger').submit( function() {
		form = this.id;
		return validate_form(form);
	});
	
	$('#form_abo_aantal').change(function(){
		var aantal = $("#form_abo_aantal").val();

		$("div#persons").html("bezig met laden");
		fixColumns();
		
		$.ajax( {
			type : "POST",
			url : "/ajax/get-abo-persons/",
			data : "aantal=" + aantal,
			success : function(msg) {
				$("div#persons").html(msg);
				fixColumns();
				for (var i = 1; i <= aantal; i++) {
					$("input#form_abo_geboortedatum" + i + "").mask("99-99-9999");
				}
				
			}
		});
		
	
		
		
	});
	
	$("a#advancedSearchLink").click(function(event){
		event.preventDefault();
		
		if (tweening) {
			timing = true;
			$("div#advancedSearch").toggle("slow", function() {timing = false;});
		} else {
			$("div#advancedSearch").toggle();
		}
		
		fixColumns();
	});
		
	if ($('div.thumbsholder').attr('class')) {
		holderX = $('div.thumbsholder').offset().left;
		numOfImages = $('div.thumbsholder').children('img.gallerythumb').length;
		checkButtons();
	}
	$('#thumbswidget a.prev').click(function(event) {
		event.preventDefault();
		currentIndex = currentIndex - 1;
		moveThumbs();
		checkButtons();
	});
	
	$('#thumbswidget a.next').click(function(event) {
		event.preventDefault();
		currentIndex = currentIndex + 1;
		moveThumbs();
		checkButtons();
	});
	
	$('div.tickets').hover(
			function(){
				if ($(this).hasClass('click')) {
					$(this).addClass('hover');
					cufon();
				}
			}, function() {
				if ($(this).hasClass('click')) {
					$(this).removeClass('hover');
					cufon();
				}
			}
	);
	$('table#relations tr:last').addClass('last');
	$('table#relations-others tr:last').addClass('last');
	$('a.jsbutton').click(function(event){
		event.preventDefault();
	});
	

	$("img.gallerythumb").click(function(){
		curImage = $(this).attr('src').split("thumb_").join("");
		curAlt = $(this).attr('alt');
		$('#mainimage').fadeOut(500, replaceImage);
	});
	
	$('#mainimage').hover(
			function(){
				var altText = $(this).attr('alt');
				if (altText != "") {
					showTooltip(altText);
				}
			},
			function(){
				hideTooltip();
			}
		);
	cufon();
	$("#print").click(function(event){
		event.preventDefault();
		print();
	});
	
	$("#cart").click(function(event){
		event.preventDefault();
		openWin();
	});
	
	$("#videoHolder, #imageHolder").hover(
		function(){
			showTooltip($(this).attr("rel"));
		},function(){
			hideTooltip();
		}
	);
	
	$("div#top-right").click(function(){
		window.location = website;
	});
	
	$("div#all-events, div#more-news, div#more-pers").click(function(){
		window.location = $(this).attr("rel");
	});
	
	$("div#news ul li, div#pers ul li").click(function(){
		window.location = this.id;
	});
	
	$("tr.row, div#news ul li, div#pers ul li, li.sidebar").hover(
		function(){
			$(this).addClass("hover");
		},
		function(){
			$(this).removeClass("hover");
		}
	);
	
	$("div.search table tr, div#binnenkort table tr, div#detail-relations table tr, #news ul li").hover(
			function(){
				showTooltipThumb(this);
			}, 
			function(){
				hideTooltipThumb();
			}
	);
	$("div#binnenkort table tr, div.row").click(
		function(){
			window.location = this.id;
		}
	);
	
	$("div#blikvanger table tr").click(
		function(){
			window.location = this.id;
		}
	);
	
	$("div.tickets").click(function(){
		/*
		if ($(this).hasClass('click')) {
			window.open($(this).attr("rel"));	
		}
		*/
	});
	
	$("div.fiches table tr, div#detail-relations table tr").click(
		function(){
			var location = this.id;
			
			if (location.indexOf("http://") == -1) {
				window.location = location;
			} else {
				window.open(location);				
			}
		}
	);
});
