$(document).ready(function() {
	RecID = $('#RegistrationRecurringID').val();
	RegistrationID = $('#RegistrationID').val();
			
	// Setup hours and minutes validation
	var fromHours 	= $("#fromHours");
	var fromMinutes = $("#fromMinutes");
	var toHours		= $("#toHours");
	var toMinutes 	= $("#toMinutes");
	
	// Validate times in dialog		
	$("#fromHours, #fromMinutes, #toHours, #toMinutes").change(function() {
		if ($("#fromDate").val() == $("#toDate").val()) {
			if (fromHours.val() > toHours.val()) {
				toHours.val(fromHours.val());
				if (fromMinutes.val() > toMinutes.val()) {
					toMinutes.val(fromMinutes.val());
				}
			}
			if (fromHours.val() == toHours.val() && fromMinutes.val() > toMinutes.val()) {
				toMinutes.val(fromMinutes.val());
			}
				}
	});
				
	// Setup the dialog box for the calendar
	var eventEdit = $("#inputFormPopup");
	eventEdit.dialog({
		width		: 500,
		height		: 210,
		autoOpen 	: false,
		modal		: true,
		position	: 'center'
	});
			
	// What to do on click
	$('#calendar td').live('dblclick', function() {
		var day = $($(this)).find('.day').html();
		// Pre populate the form
		form.mode = 'insert';		// Set the form mode to insert!
		form.populate({day:day});
				
		form.getUiCalendar();		// Init the jquery ui calendar
				
		$("#repeatMe").show();
				
		eventEdit.dialog('option', 'buttons',
		{
			"Avbryt": function() {
				$(this).dialog('close');
			},
			"Ok" : function() {
				var eventRecord = $("#eventForm").serializeArray();	// Get the data from the form
				eventObj.add(eventRecord); // Add it to the event array
				eventObj.populateCurrentCalendar(); // Populate the new data for the calendar!
				form.addEventsToForm(); // Add the event array to form		
				$(this).dialog('close'); // Close the dialog
			}				
		});
		
		// run this at the end
		eventEdit.dialog('open');
	});
			
	// Remove datetime
	$('#calendar td .instance_time').live('click', function() {
		var day 		= $($(this)).parent().parent().find('.day').html();
		var fromTime 	= $(this).find('.fromTime').html();
		var toTime 		= $(this).find('.toTime').html();
		var eventData	= eventObj.getEvent(day, fromTime, toTime);
		eventData[3] 	= 'false';
				
		// Pre populate the form
		form.mode = 'update';		// Set the form mode to update!
		form.populate(eventData);
				
		form.getUiCalendar();		// Init the jquery ui calendar
				
		$("#repeatMe").show();
				
		eventEdit.dialog('option', 'buttons',
		{
			"Slett" : function() {
				var index = $("#index").val();						// Get the index from the form
				eventObj.remove(index);								// Add it to the event array
				eventObj.populateCurrentCalendar();					// Populate the new data for the calendar!
				form.addEventsToForm();								// Add the event array to form
				$(this).dialog('close');
			},
			"Avbryt": function() {
				$(this).dialog('close');
			},
			"Ok" : function() {
				var eventRecord = $("#eventForm").serializeArray();	// Get the data from the form
				eventObj.add(eventRecord); // Add it to the event array
				eventObj.populateCurrentCalendar(); // Populate the new data for the calendar!
				form.addEventsToForm(); // Add the event array to form
				$(this).dialog('close'); // Close the dialog
			}				
		});
				
		// run this at the end
		eventEdit.dialog('open');
	});
			
	// For chaning the active datetime
	$('#calendar td .activity_time').live('click', function() {
		var day 		= $($(this)).parent().parent().find('.day').html();
		var fromTime 	= $(this).find('.fromTime').html();
		var toTime 		= $(this).find('.toTime').html();
		var eventData	= eventObj.getEvent(day, fromTime, toTime);
		eventData[3] 	= 'false';
				
		// Pre populate the form
		form.mode = 'update';
		form.populate(eventData);
		form.getUiCalendar();
				
		$("#repeatMe").hide();	// We hide the repeat section when editing the current registration date
				
		// We set a diffrent button option on this!
		eventEdit.dialog('option', 'buttons',
		{
			"Avbryt": function() {
				$(this).dialog('close');
			},
			"Ok" : function() {
				var eventRecord = $("#eventForm").serializeArray();	// Get the data from the form
				eventObj.currentEventEdit(eventRecord); 				// Add it to the event array
				eventObj.populateCurrentCalendar(); 					// Populate the new data for the calendar!
				$(this).dialog('close'); 							// Close the dialog
			}				
		});	
		eventEdit.dialog('open');
	});
			
	// Change month or year
	$('.calendarChange').live('change', function() {
		var month = $('#monthChange').val();
		var year  = $('#yearChange').val();
		$('#calendarPlaceholder').load('/intranett1/index.php?t=registration.calendar&month='+month+'&year='+year+'&RegistrationRecurringID='+RecID+'&RegistrationID='+RegistrationID, function() {
			eventObj.populateCurrentCalendar(); // Re populate the calendar
		});
	});
});
