$(document).ready(function()
{
	//initForms();
	initAjax();
	submitVote();
	initAjaxConfirm();
	submitWorkshop();
	checkHeight();
});

function initAjax()
{
	$('a.ajax').unbind('click');
	$('a.ajax').click(function() {
		ajaxCall(this);

		return false; 
	});
}

function initAjaxConfirm()
{
	$('a.ajaxConfirm').click(function() {
		if(confirm(this.title) === true)
		{
			ajaxCall(this);
		}
		return false; 
	});
}

function checkHeight()
{
	if($('#voteForm-container').html() != '')
	{
		if($('#voteForm-container .errors').length > 0)
		{
			$('#votePopup').css('height', '289px');
			$('#votePopup .middle-left').css('height', '249px');
			$('#votePopup .middle-right').css('height', '249px');
			$('#votePopup .voteContent').css('height', '249px');
		}
		
		if($('#voteForm-container .succesMsg').length > 0)
		{
			var height = parseInt($('#voteForm-container').css('height').replace('px', ''));
			
			if(height == 0)
			{
				height = 80;
			}
			
			var popupHeight = (260 + height) + 'px';
			
			var contentHeight = (220 + height) + 'px';
			
			$('#votePopup').css('height', popupHeight);
			$('#votePopup .middle-left').css('height', contentHeight);
			$('#votePopup .middle-right').css('height', contentHeight);
			$('#votePopup .voteContent').css('height', contentHeight);
		}
	}
	if($('#workshopForm-container').length > 0)
	{
		if($('#workshopForm-container .errors').length > 0)
		{
			var height = parseInt($('#workshopForm-container .errors').css('height').replace('px', ''));
		}
		if($('#workshopForm-container .succes').length > 0)
		{	
			var height = parseInt($('#workshopForm-container .succes').css('height').replace('px', ''));
		}
		
		var popupHeight = (340 + height) + 'px';
		
		var contentHeight = (300 + height) + 'px';
		
		$('#workshopPopup').css('height', popupHeight);
		$('#workshopPopup .middle-left').css('height', contentHeight);
		$('#workshopPopup .middle-right').css('height', contentHeight);
		$('#workshopPopup .voteContent').css('height', contentHeight);
	}
}


function initForms()
{
	/*var forms = Array('voteForm');

	for (i = 0; i < forms.length; i++)
	{
		form = $("#" + forms[i]);
		if (null != form.attr("id"))
		{
			form.submit(function() {

				formSubmit(this);
				return false;
			});
		}
	}*/
}

function submitVote()
{
	$('#submitVote').unbind();
	$('#submitVote').click(function()
	{
		//alert('1');
		$('#voteForm-container').html('');
		
		var container = $("#voteForm-container");
		
		if (null != container.attr("id"))
		{
			//alert('2');
			var sendForm = new Object();
			sendForm.type = "POST";
			sendForm.data = $("#voteForm").serialize();
			sendForm.url = $('#voteForm').attr('action');
			sendForm.success = function (response) {
				container.html(response);

				//initForms();
			    initAjax();
			    submitVote();
			    checkHeight();
			};
			//alert('3');
			// container.html('<img src="/images/ajax-loader.gif" alt="loading" />');
			$.ajax(sendForm);
			return false;
		}
		else
		{
			alert('Container for AJAX replacement is missing!');
		}
		return false;
	});
}
function submitWorkshop()
{
	$('#submitWorkshop').unbind();
	$('#submitWorkshop').click(function()
			{
		//alert('1');
		var container = $("#workshopForm-container");
		
		if (null != container.attr("id"))
		{
			//alert('2');
			var sendForm = new Object();
			sendForm.type = "POST";
			sendForm.data = $("#workshopForm").serialize();
			
			sendForm.url = $('#workshopForm').attr('action');
			sendForm.success = function (response) {
				container.html(response);
				
				//initForms();
				initAjax();
				submitWorkshop();
				checkHeight();
			};
			//alert('3');
			// container.html('<img src="/images/ajax-loader.gif" alt="loading" />');
			
			if (confirm("Weet u zeker dat u wilt deelnemen aan de workshop 'Video maken en promoten'?"))
			{
				$.ajax(sendForm);
			}

			return false;
		}
		else
		{
			alert('Container for AJAX replacement is missing!');
		}
		return false;
			});
}
