$(document).bind('ready.popup', function() {

	$('#popup')
	.css({
		display:	'none',
		position:	'absolute',
		zIndex:		40,
		top:		0,
		left:		0,
		width:		'100%',
		height:		$(document).height()
	})
	// Background events
	.bind('loadBG', function() {
		$('<div id="popupBackground"></div>').appendTo(this);
		$('#popupBackground').css({
			position:	'absolute',
			zIndex:		40,
			top:		0,
			height:		0,
			width:		'100%',
			opacity:	0.6,
			background:	'black'
		})
		.animate({
			height: 	$(document).height()
		}, 500, function() {
		//}, 1000, 'easeOutBounce', function() {
			$('#popup')
			.data('bgStatus', 'visible')
			.trigger('show');
		});
	})
	.bind('checkBG', function() {
		$('#popupBackground')
		.css({
			height: 	$(document).height()
		});
	})
	.bind('hideBG', function() {
		$('#popupBackground')
		.animate({
			height:		0
		}, 'normal', 'easeInCubic', function() {
			$(this).remove();
			$('#popup')
			.data('bgStatus', 'hidden')
			.trigger('destroy');
		});
	})
	// Popup screen events
	.bind('loadScreen', function() {
		var imgSrc	  = (IE6) ? 'closebox.gif' : 'closebox.png';
		var popupHTML = '<div id="popupContainer"><div id="popupTitlebar">'+
						'<img src="img/'+imgSrc+'" class="close" alt="sluiten" title="sluiten" />'+
						'</div><div id="popupContent"><div><center>inhoud wordt geladen...</center></div></div><div id="popupFooter"></div></div>';

		$(popupHTML).appendTo('#popup')
		.css({
			display:	'none'
		}).fadeIn(500, function() {
			$('#popup')
			.data('scrStatus', 'visible')
			.trigger('show');
			$.scrollTo('#popupTitlebar', 300);
		});

		$('#popupContainer img.close').click(function() {
			$(this).trigger('hide');
		});
	})
	.bind('hideScreen', function() {
		$('#popupContainer')
		.fadeOut(500, function() {
			$(this).remove();
		});
		$('#popup')
		.data('scrStatus', 'hidden')
		.trigger('scrHidden');
	})
	// Content ecents
	.bind('loadContent', function(event, type, data) {

		switch (type) {
			case 'url':

				$.get(data, function(htmlData) {
					$('#popup')
					.data('cntStatus', 'loaded')
					.data('html', htmlData)
					.trigger('show');
				}, 'html');

			break;


		}
	})
	// Main Control events
	.bind('enable', function(event, type, data) {

		$(this)
		.css('display', 'block')
		.trigger('reset')
		.trigger('loadBG')
		.trigger('loadScreen')
		.trigger('loadContent', [type, data]);

	})
	.bind('destroy', function() {

		if ($(this).data('bgStatus')=='hidden' && $(this).data('scrStatus')=='hidden') {
			$(this)
			.css('display', 'none');
		}
		return;
	})
	.bind('reset', function() {
		$(this)
		.data('bgStatus', 'hidden')
		.data('scrStatus', 'hidden')
		.data('cntStatus', 'notLoaded');
		return;
	})
	// Visibility events
	.bind('show', function(event) {

		if ($(this).data('bgStatus')=='visible' && $(this).data('scrStatus')=='visible' && $(this).data('cntStatus')=='loaded') {
			$('#popupContent > div')
			.html($('#popup').data('html'));
			$(this).trigger('checkBG');

			if (IE6) fixPNG();
		}
		return;
	})
	.bind('hide', function() {

		$(this)
		.trigger('hideBG')
		.trigger('hideScreen');

	});

});

function openVideo(source, type) {
	if (typeof(type) == "undefined") type = 'production';
	$('#popup').trigger('enable', ['url', 'popup/video.php?id='+source+'&type='+type+'&lang='+CURRENT_LANG]);
}
function newComment(pid, ccode) {
	$('#popup').trigger('enable', ['url', 'popup/comments.php?new=1&pid='+pid+'&ccode='+ccode+'&lang='+CURRENT_LANG]);
}
function readComments(pid, ccode) {
	$('#popup').trigger('enable', ['url', 'popup/comments.php?pid='+pid+'&ccode='+ccode+'&lang='+CURRENT_LANG]);
}
function readPublicity(pid, ccode) {
	$('#popup').trigger('enable', ['url', 'popup/publicity.php?pid='+pid+'&ccode='+ccode+'&lang='+CURRENT_LANG]);
}
function bookForm(pid, did, ccode) {
	$('#popup').trigger('enable', ['url', 'popup/book.php?pid='+pid+'&did='+did+'&ccode='+ccode+'&lang='+CURRENT_LANG]);
}
function tipFriend(pid, ccode) {
	$('#popup').trigger('enable', ['url', 'popup/tipfriend.php?pid='+pid+'&ccode='+ccode+'&lang='+CURRENT_LANG]);
}

function registerHuisgenoot() {
	$('#popup').trigger('enable', ['url', 'popup/register.php?type=huisgenoot&ccode=616ed0&lang='+CURRENT_LANG]);
}
function registerHuisvriend() {
	$('#popup').trigger('enable', ['url', 'popup/register.php?type=huisvriend&ccode=f8a51b&lang='+CURRENT_LANG]);
}
function registerOpdehoogte() {
	$('#popup').trigger('enable', ['url', 'popup/register.php?type=opdehoogte&ccode=934c99&lang='+CURRENT_LANG]);
}

function printAgenda() {

	var $agendaDiv	= $('div.binAgenda div.content');
	var agendaPos	= $agendaDiv.offset();

	// Set visibility to false, so we can add content without it being shown
	$agendaDiv.css({
		background:		'url(img/ajax_loader_'+COLOR_NAME+'.gif) no-repeat center center',
		height:			49,
		overflow:		'hidden'
	});

	// Load agenda
	$.get('inc/ajax_agenda.php?lang='+CURRENT_LANG, function(data) {

		// Add content
		$agendaDiv.html(data);
		$agendaDiv.children().css({
			visibility: 	'hidden'
		});

		// Fix position calendar
		var date			= new Date();
		var year			= date.getFullYear();
		var month			= date.getMonth();
		var currDateID		= '#' + year + '_' + (month+1);
		var scrollToIndex	= $('table.agenda th').index($(currDateID));

		$agendaDiv.find('div.agendaPaginate').show(0);
		$agendaDiv.find('div.agendaContainer').serialScroll({
			items:		'th',
			prev:		'.agendaPaginate span.prev',
			next:		'.agendaPaginate span.next',
			duration:	1200,
			force:		true,
			stop:		true,
			lock:		false,
			cycle:		false,
			easing:		'easeOutQuart'
		})
		.scrollTo($(currDateID), 0, {
			axis 	: 'x',
			onAfter : function() {
				$(this).trigger( 'goto', [ scrollToIndex ] );
				//console.log('scrolled to current date');

				// Calculate height of children
				var newHeight	= 0;
				$agendaDiv.children().each(function() {
					newHeight 	+= $(this).outerHeight(true);
				});

				$agendaDiv.animate({
					height:		newHeight
				}, 1000, function() {
					$(this).children().css({
						visibility:	'visible'
					}).end()
					.css({
						background:	 	'',
						height:			'auto'
					});
				});
			}
		});

		// New style, use buble up to determine which button and only 1 bound event
		$agendaDiv.mouseover(function(e) {
			if (e.target.nodeName.toLowerCase()!='a') return false;

			var $target	= $(e.target).parent();

			$('.agendaData td.date').html($target.find('span.date').text());
			$('.agendaData td.title')
				.html('<span class="'+$target.find('span.color').text()+'">'+$target.find('span.name').text()+'</span>');
			$('.agendaData td.start').html($target.find('span.times').html());
			$('.agendaData td.location').html($target.find('span.locations').html());

			return true;
		}).mouseout(function(e) {
			if (e.target.nodeName.toLowerCase()!='a') return false;

			$('.agendaData td.date').html('');
			$('.agendaData td.title').html('');
			$('.agendaData td.start').html('');
			$('.agendaData td.location').html('');

			return true;
		});

		if (IE6) fixPNG();
	});
}

function fixSocialMedia() {
	if ($('#social-media-box').length==1) {
		// Fix menu
		var smHeight	= $('#social-media-box').height();
		var mHeight		= $('#menu').height();
		var topHeight	= $('#menu').find('.top').height();
		//var botHeight	= $('#menu').find('.bottom').height();
		var but1Height	= $('#menu').find('.button').height();
		var but2Height	= $('#menu').find('.button_lang').height();
		var rightHeight	= topHeight + but1Height + but2Height;

		if (mHeight - rightHeight < smHeight) {
			$('#social-media-box').css('margin-top', -(mHeight - rightHeight));
		}
	}
}

function togglePublications() {
	$('div.publicities-hidden').toggle();
}
