$(document).ready(function()
{
	
	$('div.events_add img#large').click(function()
	{
		$('div#uploadevent').show();
	});
	
	function ajaxFileUpload()
	{
		//starting setting some animation when the ajax starts and completes
		$("#loading")
		.ajaxStart(function(){
			$("#imageForm").hide();
			$(this).show();
		})
		.ajaxComplete(function(){
			$(this).hide();
			$("div#uploadevent").hide();
			$("#imageForm").show();
		});
		
		$.ajaxFileUpload
		(
			{
				url:'../php/ajax_addEventImage.php', 
				secureuri:false,
				fileElementId:'uploadImage_field',
				dataType: 'json',
				success: function (data, status)
				{
					if(data.msg != '') {
						$('img#large').attr('src',path+'upload/temp/events/'+data.msg+'.jpg');
						$('input#addEvent_poster').val(data.msg);
					}
				},
				error: function (data, status, e)
				{
					//alert(e);
				}
			}
		)
		
		return false;
	}
	
	// 
	$("#imageForm").submit(function()
	{
		ajaxFileUpload();
		return false;
	});
	
	$('div.events_add div.submit').click(function()
	{
		$('form#addEventForm').submit();
	});
	
	$('form#addEventForm').validate({
		errorPlacement:	function(error, element) {},
		rules: {
			addEvent_naam: {
				required: true,
				minlength: 2
			},
			addEvent_datum: {
				required: true,
				dateISO: true
			},
			addEvent_locatie: {
				required: true,
				minlength: 2
			},
			addEvent_organisatie: {
				required: true,
				minlength: 2
			},
			addEvent_tickets_adk: {
				required: {
					depends: function(element)
					{
						if($('#addEvent_tickets_vvk').val().length==0 && $('#addEvent_tickets_adk').val().length==0) {
							return true;
						} else {
							return false;
						}
					}	
				}
			},
			addEvent_tickets_vvk: {
				required: {
					depends: function(element)
					{
						if($('#addEvent_tickets_vvk').val().length==0 && $('#addEvent_tickets_adk').val().length==0) {
							return true;
						} else {
							return false;
						}
					}	
				}
			},
			addEvent_vvk_locatie: {
				required: {
					depends: function(element)
					{
						if($('#addEvent_tickets_vvk').val().length==0) {
							return false;
						} else {
							return true;
						}
					}
				}
			},
			addEvent_beschrijving: {
				required: true,
				minlength: 2
			},
			addEvent_poster: {
				required: {
					depends: function(element)
					{
						if($('#addEvent_poster').val().length==0) {
							$('#large').attr('src',path+'images/addposter_error.gif');
						}
						return true;
					}
				}
			}
		},
		messages: {},
		submitHandler: function()
		{
			//alert($('#addEvent_tickets_vvk').val().length);
			var myData = $('form#addEventForm').serialize();
			$.post("../php/ajax_addEvent.php", myData, function(data)
			{
				if(data == 'succes') {
					$("#large_bg").hide();
					$("#large").hide();
					$("#addEventForm").hide();
					$("#dashedAddEvent").hide();
					$("div.events_add div.submit").hide();
					$("#successAddEvent").show();
				}
			});
		}
	});
});
