$(document).ready(function (){
	var screen_height = document.body.clientHeight;
	var newMICounter = 0;
});

function addlistType() {
	var val = $('#listtype').val();
    
	if(val == 'new'){
		$('#listtype_div').append('<div style="display:inline;margin-left:10px;"><input type="text" name="listtype_new"></div>');
	}
    
}

$(document).ready(function() {
	$("#sortable").sortable();
});

function newAgendaItem(title, id, start, end, allDay) {
	showLoading();
	
	var insertid;
	$.ajax({
		url: "modules/Agenda/ajax/newAgendaItem.php",
		data:"title="+title+"&id="+id+"&start="+start+"&end="+end+"&allDay="+allDay,
		async: false,
		success: function(data) {
			hideLoading();
			insertid = data;
		}
	});
	return insertid;
}

function editItem(id,calEvent, sectionid, lang) {
	showLoading();
	showEditArea();
	
	if(isNaN(calEvent)==false){
		var itemid = calEvent;
	}else{
		var itemid = calEvent.id;
	}

	if(id > 0){
		$("#cms_edittitle").html(LANGITEM["edit_element"]);
	}else{
		$("#cms_edittitle").html(LANGITEM["new_element"]);
	}
	
	$.ajax({
		url: "modules/Agenda/ajax/editItem.php?agendaid=" + id+"&id="+itemid+"&sectionid="+sectionid+"&lang="+lang,
		success: function(data) {
			$("#cms_edit").html(data);
			hideLoading();
			loadSection(sectionid, lang);
		}
	});
}



function saveAgendaItem(form, sectionid, lang) {	
	if (!checkFormValues(form)) {
		showMessage(LANGITEM["msg1"], "info");
		return false;
	}else{ 
		showLoading();
		postVars = makePostString(form);
		postVars = postVars.substring(0,postVars.length-1);
		
		$.ajax({
			type: "POST",
			url: "modules/Agenda/ajax/saveItem.php",
			data: postVars,
			success: function(data) {
				//alert(data);
				if (data == 1) {
					showMessage(LANGITEM["msg3"], "check");
					var ok = true
				} else {
					showMessage(LANGITEM["msg4"], "error");
					handleError(data);
					var ok = false;
				}
				hideEditArea(false);
				hideLoading();
							
				loadSection(sectionid, lang);
			}
			
		});
	
		return false;
	}
}

function viewEventDetails(eventid,calEvent){
	var screen_height = document.body.clientHeight;
	var window_height = window.screen.height;

	if(eventid==null){
		eventid = calEvent.id;
	}
	
	$.ajax({
		url: 'modules/Agenda/ajax/eventDetails.php',
		data: 'eventid='+eventid,
		success:function(data){
			$("#dialog").html(data);
			
			$("#dialog").dialog({
				show: "drop",
				hide: "drop",
				minWidth:990,
				minHeight: screen_height,
				modal:true,
				sync: false
				//,position:["right", "top"]
			});
			
			var dialog_height = $('#dialog').height();
			$(".ui-widget-overlay").css('height', window_height+'px');
			if(dialog_height > screen_height){
				$(".ui-widget-overlay").css('height', dialog_height+'px');
			}

			$(".ui-dialog-titlebar").hide();

		}
	});
}

function viewPastEventDetails(eventid,calEvent){
	var screen_height = document.body.clientHeight;
	var window_height = window.screen.height;
	
	if(eventid==null){
		eventid = calEvent.id;
	}
	
	$.ajax({
		url: 'modules/Agenda/ajax/pastEventDetails.php',
		data: 'eventid='+eventid,
		success:function(data){
			$("#dialog").html(data);
			$("#dialog").dialog({
				show: "drop",
				hide: "drop",
				minWidth: 680,
				height: screen_height,
				modal:true
			,position:["right", "top"]
			});
			
//			$("#dialog").css({
//				"position":"fixed", 
//				"background":"#fff", 
//				"top":"0", 
//				"right":"0"
//			});

			$(".ui-dialog-titlebar").hide();
			
			var dialog_height = $('#dialog').height();
			$(".ui-widget-overlay").css('height', window_height+'px');
			if(dialog_height > screen_height){
				$(".ui-widget-overlay").css('height', dialog_height+'px');
			}


		}
	});
}

function eventRegister(eventid){
	
	$.ajax({
		url: 'modules/Agenda/ajax/registerFrm.php',
		data:'eventid='+eventid,
		success: function(data){
			$("#register").html(data);
			$("#register").dialog({
				//title: 'Event aanmelden',
				show: "drop",
				hide: "drop",
				width:990,
				height: screen_height,
				modal:true
				//,position:["right", "top"]
			//				buttons:{
			//					Aanmelden: function(){
			//						var name = $('#name').val();
			//						var email = $('#email').val();
			//						
			//						var check_form = checkFormValues(document.forms["eventRegistration"]);
			//						if(check_form==true){
			//							$("#register").dialog("close");
			//							saveEventRegistration(eventid, name, email);
			//						}
			//					}
			//				}
			});
			
//			$("#register").css({
//				"position":"fixed", 
//				"background":"#fff", 
//				"top":"0", 
//				"right":"0"
//			});
			$(".ui-dialog-titlebar").hide();
			
			var dialog_height = $('#dialog').height();
			if(dialog_height > screen_height){
				$(".ui-widget-overlay").css('height', dialog_height+'px');
			}

			
//			$(".ui-dialog").css({
//				"position":"fixed", 
//				"top": "0", 
//				"border":"none"
//			});
		}
		
	});
}

function saveEventRegistration(form){
	if(checkForm(form)==true){
		postVars = makePostString(form);
		postVars = postVars.substring(0,postVars.length-1);
		
		$.ajax({
			url: 'modules/Agenda/ajax/saveEventRegistration.php',
			//data: 'eventid='+eventid+'&name='+name+'&email='+email,
			data: postVars,
			type: 'POST',
			success: function(data){
				if(data =='success'){
					$('#register').html("U zult spoedig een e-mail van ons ontvangen.");
					$("#register").dialog({
						//title: 'Bedank voor uw aanmelden',
						show: "drop",
						hide: "drop",
						width:990,
						height: screen_height,
						modal:true
						//,position:["right", "top"]
					});
				}
//				$("#register").css({
//					"position":"fixed", 
//					"background":"#fff", 
//					"top":"0", 
//					"right":"0"
//				});
				$(".ui-dialog-titlebar").hide();
				
//				$(".ui-dialog").css({
//					"position":"fixed", 
//					"top": "0", 
//					"border":"none"
//				});
			}
		});
		return true;
	}else{
		return false;
	}
}

function deleteEventItem(itemid, sectionid, lang){
	if(itemid >0){
		var r = confirm(LANGITEM["del_confirm"]);
		if (r == true) {
			$.ajax({
				url: 'modules/Agenda/ajax/deleteItem.php',
				type: 'POST',
				data: 'id='+itemid,
				success: function(data){
					hideEditArea(false);
					loadSection(sectionid, lang);
				}
			});
		}
	}
	return false;
}
