// JavaScript Document

var view = false;
var dragging = false;
var texttoedit = "";
var textid = 0;
var resize_total=0;

function process(data,sel_id) {
	$("#calendar").append(data);
	cpos = $("#calendar").offset();
	pos = $("#"+sel_id).offset();
	$(".container").droppable({ accept: ".container" });
	$(".container").draggable( { containment: '#calendar', grid: [101, 16], handle: '.drag', revert: 'valid',
		start: function() {
			dragging = true;
		},
		stop: function() { 
			dragging = false;
			texttoedit = $(this).find("p").text();
			post_location_id = $('input[name="location_id"]').val();
			textid = $(this).attr("id");
			pos = $("#"+textid).position();
			post_height = $("#"+textid).height();
			$.post("calendar_remove.php", { id: textid });
			$.post("calendar_add.php", { event: texttoedit, location_id: post_location_id, height: post_height, top: pos.top, left: pos.left },
				function(data){
					$("#"+textid).remove();
					process(data,sel_id);
				});

		} 
	});
	
	$(".data").resizable({
		maxHeight: 500,
		maxWidth: 100,
		minHeight: 16,
		minWidth: 100,
		grid: 16,
		containment: '#content',
		stop: function(event, ui) {
			texttoedit = $(this).find("p").text();
			post_location_id = $('input[name="location_id"]').val();
			textid = $(this).parent().attr("id");
			pos = $("#"+textid).position();
			
			overlap=false;
			$("#calendar .container").each(function() {
				tpos = $(this).position();
				totp = pos.top+$("#"+textid).height();
				if ($(this).attr("id")!=textid && pos.left==tpos.left && pos.top<tpos.top && totp>tpos.top) {
					//post_height = $("#"+textid).height();
					//alert($(this).attr("id")+" "+totp+" "+tpos.top);
					overlap=true;
				}
			});
			
			if(resize_total<1030 && overlap==false) {
				post_height = $("#"+textid).height();
			}

			$.post("calendar_remove.php", { id: textid });
			$.post("calendar_add.php", { event: texttoedit, location_id: post_location_id, height: post_height, top: pos.top, left: pos.left },
				function(data){
					$("#"+textid).remove();
					process(data,sel_id);
				});
		},
		start: function(event, ui) {
			textid = $(this).parent().attr("id");
			post_height = $("#"+textid).height();
		},
		resize: function(event,ui) {
			pos=$(this).parent().position();
			resize_total = ui.size.height+pos.top;
		}
	});

	$(".data p").click(function() {
		textid = $(this).parent().parent().attr("id");
		if (!dragging) { 
			view=true;
			$('#dialog2').dialog('open');
			texttoedit = $(this).text();
			$("#dialog2").html(texttoedit);
		}
	})

}

$(document).ready(function() {
						   
	var sel_id;
	
	$('.min15').click(function () {
		if(!view) {
			$('input[name="event"]').val("");
			$('#dialog').dialog('open');
		}
		sel_id = this.id;						
	})


	$('#dialog').dialog({
		autoOpen: false,
		width: 400,
		modal: false,
		buttons: {
				'Add Class': function() {
					post_event = $('input[name="event"]').val();
					post_location_id = $('input[name="location_id"]').val();
					post_id = sel_id;
					tp = $("#calendar").offset();
					pos = $("#"+sel_id).offset();
					topp = pos.top-tp.top;
					leftp = pos.left-tp.left;
					$.post("calendar_add.php", { event: post_event, location_id: post_location_id, height: '48', top: topp, left: leftp },
						function(data){
							process(data,sel_id);
						});
					$(this).dialog('close');
				},
				Cancel: function() {
					$(this).dialog('close');
				}
			}
			
	});

	$('#dialog2').dialog({
		autoOpen: false,
		width: 400,
		modal: false,
		buttons: {
				'Close': function() {
					view=false;
					$(this).dialog('close');
				},
				'Edit': function() {
					view=false;
					$(this).dialog('close');
					$('input[name="event2"]').val(texttoedit);
					$('#dialog3').dialog('open');
				},
				'Delete': function() {
					view=false;
					$("#"+textid).remove();
					$(this).dialog('close');
					$.post("calendar_remove.php", { id: textid });
				}
			}
			
	});

	$('#dialog3').dialog({
		autoOpen: false,
		width: 400,
		modal: false,
		buttons: {
				'Save Class': function() {
					pos = $("#"+textid).position();
					post_height = $("#"+textid).height();
					$.post("calendar_remove.php", { id: textid });
					post_event = $('input[name="event2"]').val();
					post_location_id = $('input[name="location_id"]').val();
					post_id = sel_id;
					$.post("calendar_add.php", { event: post_event, location_id: post_location_id, height: post_height, top: pos.top, left: pos.left },
						function(data){
							$("#"+textid).remove();
							process(data,sel_id);
						});
					$(this).dialog('close');
				},
				Cancel: function() {
					$(this).dialog('close');
				}
			}
			
	});
	
	post_location_id = $('input[name="location_id"]').val();
	$.post("calendar_refresh.php", { lid: post_location_id },
	function(data){
		process(data,sel_id);
	});
	

   $(document).keypress(function(e) { 
		if ((e.which && e.which == 13) || (e.keyCode && e.keyCode == 13)) { 
			if ($('#dialog').dialog('isOpen')) {
				$(this).find(":button:contains('Add Class')").click();
			} else if ($('#dialog2').dialog('isOpen')) {
				$(this).find(":button:contains('Edit')").click();
			} else if ($('#dialog3').dialog('isOpen')) {
				$(this).find(":button:contains('Save Class')").click();
			}
			return false; 
		} 
	}); 
	
})