$(document).ready(function()
{
  $('.expandingHeader').toggle(
		  function(){
			  $(this).next('.transportHead').slideUp(600);
			  $(this).next('.transportHead').next('.foldedshadowBox04').show();
			  $(this).find('.toggleExpand').attr('src','/static/events/basis/img/expand_plus.gif');
		  },
		  function(){
			  $(this).next('.transportHead').slideDown(600);
			  $(this).next('.transportHead').next('.foldedshadowBox04').hide();
			  $(this).find('.toggleExpand').attr('src','/static/events/basis/img/expand_minus.gif');
		  }
  );
  
  try{
		$(function()
		{
			var dpObj = {clickInput:true};
			var $this = this;
			
			// TO DO: juiste url hier plaatsen in plaats van deze statische:
			var eventsCalendarUrl = '/toon/evkalender.html';
			// TO DO: juiste eventGroup invullen:
			var eventGroup = $("#eventgroupid").val();

			$('div.dateElements input').datePicker(dpObj);
			$('.dp-applied-from')
				.bind('dpClosed', function(e, selectedDate, $td)
				{
					var d = selectedDate[0];
					if (d) {
						d = new Date(d);
						$('.dp-applied-to').dpSetStartDate(d.addDays(1).asString());
					}
				})
				.bind('dateSelected', function(e, selectedDate)
				{
					var d = new Date(selectedDate);					
					var evdatefrom = preZeroDate(d.getDate())+'-'+preZeroDate(d.getMonth()+1)+'-'+d.getFullYear();
					var evdateto = '';
					var query = 'eventgroup='+eventGroup+'&evdatefrom='+evdatefrom;
					evdateto = $('.dp-applied-to').val();
					query += '&evdateto='+evdateto;
					
					$("#ajaxloadindicator").removeClass('hide');
					
					updateEventsCalendar(eventsCalendarUrl, query, pagination);
				});
				
			$('.dp-applied-to')
				.bind('dpClosed', function(e, selectedDate)
				{
					var d = selectedDate[0];
					if (d) {
						d = new Date(d);
						$('.dp-applied-from').dpSetEndDate(d.addDays(-1).asString());
					}
				})
				.bind('dateSelected', function(e, selectedDate)
				{
					var d = new Date(selectedDate);					
					var evdateto = preZeroDate(d.getDate())+'-'+preZeroDate(d.getMonth()+1)+'-'+d.getFullYear();
					var evdatefrom;
					var query = 'eventgroup='+eventGroup;
					evdatefrom = $('.dp-applied-from').val();
					query += '&evdatefrom='+evdatefrom;
					query += '&evdateto='+evdateto;
					
					$("#ajaxloadindicator").removeClass('hide');
					
					updateEventsCalendar(eventsCalendarUrl, query, pagination);
				});
		});
	  
	} catch (err) { }
  
	/*fotogalerij, mail en kalender*/
	var window_targets =
	{
		fotogalerij:
			{ width: 650, height: 480, attributes: "location=no,status=yes,menubar=no,scrollbars=no,resizable=no" },
		mail:
			{ width: 500, height: 480, attributes: "location=no,status=yes,menubar=no,scrollbars=no,resizable=no" },
		calendar:
			{ width: 235, height: 212, attributes: "location=no,status=no,menubar=no,scrollbars=no,resizable=no" }
	}
	
	function Pagination(obj)
	{
		this.table = $("#"+obj.target);
		this.table.hide();
		this.tbodies = this.table.children('tbody');
		this.numTbodies = this.tbodies.length;
		var firstBody = this.table.children('tbody:first');
		this.rowsInBody = firstBody.children('tr').length;
		this.currentPage = 0;
		
		this.navigation = new Array();
		this.topNav = obj.topNav;
		this.bottomNav = obj.bottomNav;
		
			
		if(this.numTbodies > 1){
			this.addNavigation();
			this.format(obj.type);
			this.update();
			this.table.show();
			for(var i = 0; i < this.numTbodies; i++){
				if(i > 0){
					$('tbody', this.table).eq(i).hide();
				} else {
					$('tbody', this.table).eq(i).show();
				}
			}
		} else {
			this.table.show();
			this.hideNavigation();
		}
	};
	Pagination.prototype.addNavigation = function()
	{
		if(this.topNav != null)
		{
			var idtop = this.table.attr('id')+'_nav_top';
			this.table.append('<thead><tr><td colspan="10" class="'+this.bottomNav+'" id="'+idbottom+'"></td></tr></thead>');
			this.navigation.push($("#"+idtop));
		} 
		if(this.bottomNav != null)
		{
			var idbottom = this.table.attr('id')+'_nav_bottom';
			this.table.append('<tfoot><tr><td colspan="10" class="'+this.bottomNav+'" id="'+idbottom+'"></td></tr></tfoot>');
			this.navigation.push($("#"+idbottom));
		}
	};
	Pagination.prototype.hideNavigation = function()
	{
		for(var i=0;i<this.navigation.length; i++){
			this.navigation[i].hide();
		}
	};
	//Pagination.pageLength = 10;
	Pagination.prototype.format = function(type)
	{
		var $this = this;
		switch(type){
			case 'prevnext':
			var html = '<a href="javascript:void(0);" class="prev">Vorige '+this.rowsInBody+'</a><a href="javascript:void(0);" class="next">Volgende '+this.rowsInBody+'</a>';
			for(var i=0;i<this.navigation.length;i++){
				this.navigation[i].html(html);
				this.navigation[i].bind("click", function(e){
					var newpage;
					if(e.target.nodeName == 'A'){
						if(e.target.className == 'prev'){
							newpage = $this.currentPage - 1;
						} else {
							newpage = $this.currentPage + 1;
						}
						$this.showSegment(newpage);
					}
				});
			}
			break;
			
			case 'numbers':
				var pagenumbers = '';
				for(var i = 0; i < this.numTbodies; i++){
					pagenumbers += '<a href="javascript:void(0);" id="page'+i+'">'+(i+1)+'</a>';
				}
				for(var i=0;i<this.navigation.length;i++){
					this.navigation[i].html(pagenumbers);
					this.navigation[i].bind("click", function(e){
						if(e.target.nodeName == 'A'){
							var id = e.target.id.substr(4);
							$this.showSegment(id);
						}
					});
				}			
				
			break;
			
			case 'both':
				var nav = '<a href="javascript:void(0);" class="prev"><</a>';
				for(var i = 0; i < this.numTbodies; i++){
					nav += '<a href="javascript:void(0);" id="page'+i+'">'+(i+1)+'</a>';
				}
				nav += '<a href="javascript:void(0);" class="next">></a>';
				for(var i=0;i<this.navigation.length;i++){
					this.navigation[i].html(nav);
					
					this.navigation[i].bind("click", function(e){
						var newpage;
						if(e.target.nodeName == 'A'){
							if(e.target.id.indexOf('page') > -1){
								newpage = e.target.id.substr(4);
								$this.showSegment(newpage);
							} else if(e.target.className == 'prev'){
								newpage = $this.currentPage - 1;
							} else {
								newpage = $this.currentPage + 1;
							}
							$this.showSegment(newpage);
						}
					});
				}
			break;
		}
	};
	Pagination.prototype.showSegment = function(n)
	{
		for(var i=0;i<this.tbodies.length;i++){
			if(i==n){
				$("tbody", this.table).eq(i).show();
			} else {
				$("tbody", this.table).eq(i).hide();
			}
		}
		this.currentPage = n;
		this.update();
	};
	Pagination.prototype.update = function()
	{
		this.tbodies = this.table.children('tbody');
		this.numTbodies = this.tbodies.length;
		for(var i=0;i<this.navigation.length;i++)
		{
			if(this.numTbodies > 1)
			{
				this.navigation[i].show();
				if(this.currentPage == 0)
				{
					$(".prev", this.navigation[i]).hide();
				}
				else 
				{
					$(".prev", this.navigation[i]).show();
				}
				if(this.currentPage == this.numTbodies - 1)
				{
					$(".next", this.navigation[i]).hide();
				}
				else 
				{
					$(".next", this.navigation[i]).show();
				}
				$("a[id^='page']", this.navigation[i]).removeAttr('class');
				$("#page"+this.currentPage, this.navigation[i]).addClass('current');
			}
			else
			{
				this.navigation[i].hide();
			}
		}
	};
	
	function updateEventsCalendar(url, query, pagination)
	{
		var tableHead = $("#events").children('thead');
		$.ajax({
			url:url,
			dataType:'html',
			data:query,
			success:function(html)
			{
				var newBody = $("tbody", html);
				$("#ajaxloadindicator").addClass('hide');
				
				$("#events").children('tbody').remove();
				$("#events").append(newBody);
				for(var i = 0; i < $("#events tbody").length; i++){
					if(i > 0){
						$("#events tbody").eq(i).hide();
					} else {
						$("#events tbody").eq(i).show();
					}
				}
				pagination.update();
			},
			error:function(a, b, c)
			{
				alert('%o %o %o', a, b, c);
			}
		});
	/*	var tableHead = $("#events").children('thead');
		
		$("#events").children('tbody').remove();
		$("#events").load(url + '?' + query + " #events tbody");
		for(var i = 0; i < $("#events tbody").length; i++){
			if(i > 0){
				$("#events tbody").eq(i).hide();
			} else {
				$("#events tbody").eq(i).show();
			}
		}*/
	}
	
	function preZeroDate(d)
	{
		if(Number(d) < 10)
		{
			return "0"+d;
		}
		return d;
	}
	
	var pagination = new Pagination({ target:'events', topNav:null, bottomNav:'paging bottom', type:'prevnext' });
});