$(document).ready(function()
{
	/// TinyMCE

	(function()
	{
		var max = 40;

		$('.poemDefault textarea').tinymce(
		{
			// Location of TinyMCE script
			script_url : '/javascript/tinymce/jscripts/tiny_mce/tiny_mce.js',

			// General options
			theme : "advanced",
			language : "nl",

			// Theme options
			theme_advanced_buttons1 : "bold,italic,underline,|,undo,redo",
			theme_advanced_buttons2 : "",
			theme_advanced_buttons3 : "",
			theme_advanced_toolbar_location : "top",
			theme_advanced_toolbar_align : "center",
			theme_advanced_statusbar_location : "bottom",
			theme_advanced_path : false,
			theme_advanced_resizing : true,
			theme_advanced_resize_horizontal : false,

			valid_elements : "b/strong,u,i/em,br",
			convert_newlines_to_brs : true,
			force_br_newlines : true,
			force_p_newlines : false,
			forced_root_block : '',
			remove_linebreaks : false,
			setup : function(ed)
			{
				ed.onKeyDown.add(function(ed, e)
				{
					var lines = $(ed.getBody()).html().match(/<br>/gi);
					var error = $('.poemDefault .error');

					if (e.keyCode == 13 && lines && lines.length >= max - 1)
					{
						e.preventDefault();

						error.text("Maximaal aantal regels is bereikt");
					}
					else
					{
						error.text('');
					}
				});
			}
		});
	})();

	/// Zoom

	(function()
	{
		var fs, fs_def = $('body').css('font-size');
		var item, items = $('.pageZoom ul li a');

		items.each(function()
		{
			item = $(this);
			fs = item.attr('rel');

			item.css({ 'font-size': fs });
			fs == fs_def && item.addClass('active');

			item.click(function()
			{
				items.removeClass('active');
				$(this).addClass('active');

				$('.content .center, .content .center input, .content .center textarea, .content .center select').css({ 'font-size': $(this).attr('rel') });

				return false;
			});
		});
	})();

	/// Menu

	(function()
	{
		$('ul#menuMainRoot').jdMenu(
		{
			showDelay: 100,
			hideDelay: 0,
			disableLinks: false
		});
	})();

	/// Poems

	(function()
	{
		var delta;
		var timeout = 10;
		var slide = 2;
		var offset = 22;

		$(".poemIntro ul").cycle(
		{
			timeout: 1000*timeout,
			after: function(current, next)
			{
				$(current).css({ top: '0px' });

				next = $(next);

				delta = next.height() - next.parent().height() - offset;
				delta > 0 && next.animate({ top: -delta + 'px' }, 1000*slide, 'linear');
			}
		});
	})();

	/// Accordion

	(function()
	{
		$('.accordion a .action .button').click(function()
		{
			$(this).parent().parent().attr('rel') && (document.location.href = $(this).parent().parent().attr('rel') + $(this).attr('class').split(' ').pop());

			return false;
		});

		$('.accordion').accordion(
		{
			collapsible: true,
			active: false,
			header: 'h3',
			autoHeight: false
		});
	})();

	// Share buttons

	(function()
	{
		$('.share').share(
		{
			services: ['facebook', 'twitter']
		});
	})();

	// Automatic captions

	(function()
	{
		$('.pageDefault .text > img').jcaption(
		{
			captionElement: 'div',
			autoWidth: false
		});
	})();

	// Countdown

	(function() {
		$('#countdown').countdown(
		{
			until: new Date(2011, 10, 2, 0, 0, 0),
			labels: ['Jaren', 'Maanden', 'Weken', 'Dagen', 'Uren', 'Minuten', 'Seconden'],
			labels1: ['Jaar', 'Maand', 'Week', 'Dag', 'Uur', 'Minuut', 'Seconde'],
			compactLabels: ['j', 'm', 'w', 'd'],
			format: 'dHMS'
		});
	})();
});
