$(document).ready(function() {
	core();
});

/*
 * Place your custom code inside this function
 */
function custom()
{
	$("#jsddm li:last").addClass('last');
	
	$("#rotator-prev").click(function() {	
		clearTimeout(sliderTimer);
			
		prev = $(this).parents('#rotator').find('.stripTransmitter').find('.current').parents('li').prev('li');
		if(prev.html() != null)
		{
			prev.find('a').click();
		}	
		else
		{
			$(this).parents('#rotator').find('.stripTransmitter').find('li:last').find('a').click();
		}
	});
	
	$("#rotator-next").click(function() {	
		clearTimeout(sliderTimer);
							
		next = $(this).parents('#rotator').find('.stripTransmitter').find('.current').parents('li').next('li');
		if(next.html() != null)
		{
			next.find('a').click();
		}	
		else
		{
			$(this).parents('#rotator').find('.stripTransmitter').find('li:first').find('a').click();
		}
	});
}

function hanleRandomDivs()
{
	$(".random").each(function()
	{
		var nthChild = Math.floor(Math.random() * $(this).children().size() + 1);

		$(this).find(":nth-child(" + nthChild + ")").show();
	});
}

function handleVideos()
{
	$(".videoItem").each(function(i)
	{
		var vars = { 'file': $(this).find('img').attr('rel'), 'stretching': 'exactfit'};
		var params = { menu: 'false', allowfullscreen: 'true' };
		var attributes = { id: $(this).find('img').attr('id'), name: $(this).find('img').attr('id') };

		swfobject.embedSWF('http://www.websitebeheermodule.nl/Lib/WYSIWYG/swf/player.swf', $(this).find('img').attr('id'), $(this).find('img').width(), $(this).find('img').height(), "9.0.0", "", vars, params, attributes);
	});
}

function setWrapperHeights()
{
	$(".wrapper").each(function()
	{
		var maxHeight = 0;
		$(this).find('.workspaceItem').each(function()
		{
			var top = parseInt($(this).css('top').replace('px',''));
			var height = $(this).height();

			if(top+height > maxHeight)
			{
				maxHeight = top+height;
			}
		});

		$(this).parent().height(maxHeight);
		$(this).show();
	});
}

function handleImageHrefs()
{
	$('.imageItem').find('img').each(function(i) {
		if($(this).attr('href'))
		{
			$(this).wrap('<a href="' + $(this).attr('href') + '" />')
		}

	});

}

function handleDatePickers()
{
	$(".datepicker").each(function() {
		$(this).datepicker({
			dateFormat: $(this).attr('rel')
		});
	});
}

function handleManditory()
{

	$(".mandatory:first").each(function() {
		$(this).parents('form').submit(function () {

			var pass = true;

			$(".mandatory").each(function() {
				if($(this).val() == '')
				{
					$(this).addClass('mandatory');
					$(this).focus();

					if($(this).attr('rel') != '')
					{
						alert($(this).attr('rel'));
					}
					else
					{
						alert('Field ' + $(this).attr('name') + ' is empty');
					}

					pass = false;
				}
			});

			return pass;
		});
	});


}

function core()
{
	custom();

	handleImageHrefs();

	// setWrapperHeights();

	handleDatePickers();

	handleManditory();

	hanleRandomDivs();

	handleVideos();
}


