
function AutoArrange(folderID)
{
	var c = prompt("How many columns would you like to arrange to?", "3")
	if (c == null)
		return
	width = $(window).width();
	Goto('notebook.asp?action=autoarrange&id='+folderID+'&cols='+c+'&width='+width);
}

function PersistNote(id, l, t, w, h, z)
{
	folderid = $('#folderid').val()
	url = 'worker_notes.asp?action=persistpos&id='+id+'&folderid='+folderid+'&l='+l+'&t='+t+'&w='+w+'&h='+h+'&z='+z
	//alert(url);
	$.get(url);
}

function MoveToFolder(noteID, folderid)
{
	url = 'worker_notes.asp?action=movetofolder&id='+noteID+'&folderid='+folderid
	//alert(url);

	$.get(url);
	$('#note_'+noteID).hide('fast');
}

function ConfirmDeleteNote()
{
	return confirmSubmit('Are you sure you want to delete this note?');
}

function DeleteNote(what, id)
{
	if (!ConfirmDeleteNote())
		return;
	url = 'worker_notes.asp?action=deletenote&id='+id
	$('#note_'+id).hide('fast');
	//alert(url)
	$.get(url);
}

function DuplicateNote(id)
{
	Goto('notebook.asp?action=duplicatenote&id='+id+'&scrollpos='+GetScrollY());
}

function ColOnMouse(who, isiton)
{
	if (lastclick == who)
		return;
	if (isiton)
		who.style.border='1px solid #444499'
	else
		who.style.border='1px solid #EEEEEE'
}
var lastclick;
function ColOnClick(who)
{
	who.style.border='1px solid #FF0000'
	if (lastclick != null)
		lastclick.style.border='1px solid #EEEEEE'
	//who.style.backgroundColor="#FFFFCC"
	lastclick = who;
	$('#col').val(who.style.backgroundColor);
}

function EditNote(id)
{
	url = 'worker_notes.asp?action=starteditnote&id='+id+'&scrollpos='+GetScrollY()
	//alert(url)
	$.getScript(url);
}

function GetSelectedNotes()
{
	ret = ""
	$(".grouped").each(function(i) {
		ret += $(this).attr('id').split("_")[1] + ",";
	});
	return ret
}

function DuplicateFormat(id)
{
	s = GetSelectedNotes()
	if (s.length < 2) {
		alert('No notes selected. Select notes using shift-click.');
		return;
	}
	Goto('notebook.asp?action=duplicateformat&from='+id+'&to='+s);
}


function DeleteSelectedNotes()
{
	s = GetSelectedNotes()
	if (s.length < 2) {
		alert('No notes selected. Select notes using shift-click.');
		return;
	}
	n = s.split(",").length-1
	if (confirmSubmit('Are you sure you want to delete the '+n+' selected notes?'))
		Goto('notebook.asp?action=deletemultiplenotes&ids='+s);
}

function NoteMenu(what, id)
{
	var t = document.getElementById('g_tt');
	if (t == null) return;
	h = '<table class="colbdr or" cellspacing=0 width=210>';
	h += '<tr><td OnClick="DuplicateNote('+id+');" OnMouseover="hL2(this);" OnMouseOut="dL2(this);" class=sc><span class="smalltext">Duplicate Note</span></td></tr>'
	h += '<tr><td OnClick="DuplicateFormat('+id+');" OnMouseover="hL2(this);" OnMouseOut="dL2(this);" class=sc><span class="smalltext">Format selected notes like this one</span></td></tr>'
	h += '<tr><td OnClick="Goto(\'notebook.asp?action=editnote&id='+id+'\');" OnMouseover="hL2(this);" OnMouseOut="dL2(this);" class=sc><span class="smalltext">Edit old-style</span></td></tr>'
	h += '<tr><td OnClick="Goto(\'notebook.asp?action=lookupnote&id='+id+'\');" OnMouseover="hL2(this);" OnMouseOut="dL2(this);" class=sc><span class="smalltext">Lookup note</span></td></tr>'
	h += '<tr><td style="height:5px;" class=hdr_sel></td></tr>'
	h += '<tr><td OnClick="DeleteNote(0, '+id+');" OnMouseover="hL2(this);" OnMouseOut="dL2(this);" class=sc><span class="smalltext">Delete Note</span></td></tr>'
	h += '</table>';
	t.innerHTML = h
	SetTooltipPosition(what);
	t.style.display = "block";
	t.style.width = "220px";
	$(t).css('z-index', GetHighestZIndex()+10);

}

function FolderMenu(what, id, userLevel)
{
	var t = document.getElementById('g_tt');
	if (t == null) return;
	h = '<table class="colbdr or" cellspacing=0 width=160>';
	h += '<tr><td OnClick="SelectAllNotes(true);" OnMouseover="hL2(this);" OnMouseOut="dL2(this);" class=sc><span class="smalltext">Select All Notes</span></td></tr>'
	h += '<tr><td OnClick="SelectAllNotes(false);" OnMouseover="hL2(this);" OnMouseOut="dL2(this);" class=sc><span class="smalltext">Unselect All Notes</span></td></tr>'
	h += '<tr><td style="height:5px;" class=hdr_sel></td></tr>'
	h += '<tr><td OnClick="DeleteFolder('+id+');" OnMouseover="hL2(this);" OnMouseOut="dL2(this);" class=sc><span class="smalltext">Delete Folder</span></td></tr>'
	h += '<tr><td OnClick="DeleteSelectedNotes();" OnMouseover="hL2(this);" OnMouseOut="dL2(this);" class=sc><span class="smalltext">Delete Selected Notes</span></td></tr>'
	if (userLevel >= 10) {
		h += '<tr><td style="height:5px;" class=hdr_sel></td></tr>'
		h += '<tr><td OnClick="ConvertFolderToTemplate('+id+');" OnMouseover="hL2(this);" OnMouseOut="dL2(this);" class=sc><span class="smalltext">Convert to Template</span></td></tr>'
	}
	h += '</table>';
	t.innerHTML = h
	SetTooltipPosition(what);
	t.style.display = "block";
	t.style.width = "220px";
	$(t).css('z-index', GetHighestZIndex()+10);

}

function ConvertFolderToTemplate(id)
{
	if (confirmSubmit('Are you sure you want to make this folder into a template accessible to everyone?'))
		Goto('notebook.asp?action=duplicatefolder&newowner=1&id='+id);
}

function SelectAllNotes(isit)
{
	if (isit)
		$(".noteDiv").addClass('grouped')
	else
		$(".noteDiv").removeClass('grouped')
}

function DeleteFolder(id)
{
	if (confirmSubmit('Are you sure you want to delete this folder?'))
		Goto('notebook.asp?action=deletefolder&id='+id);
}

function GetHighestZIndex()
{
	var zIdx = Math.max.apply(null,$.map($('.noteDiv'), function(e,n){
		   if($(e).css('position')=='absolute')
						   return parseInt($(e).css('z-index'))||1 ;
		   }));
	return zIdx;
}

function confirmDelNote()
{
	return confirmSubmit('Are you sure you want to delete this note?');
}

function MoveToolbar() {
    var v = document.getElementById('notebooktoolbar');
    if (v == null) return;
    var py = 0;
    if (document.documentElement && document.documentElement.scrollTop) {
		py = document.documentElement.scrollTop;
    } else if (document.body) {
		py = document.body.scrollTop;
    }

    py -= 100;
    if (py < theTop-84)
    	py = theTop;
    else
    	py += theTop;
    if (py == old && v != null) {
		v.style.top = py + 'px';
		v.style.left = '132px';
    }
    old = py;
    h = $(window).height();
    v.style.height = h + 'px'
    setTimeout('MoveToolbar()', 50);
}

$(document).ready(function() {
	var divs = $(".innerNoteDiv");
	for (i = 0; i < divs.length; i++) {
		var v = divs[i];
		h = $(v.parentNode).height();
		$(v).height(h-22);
	}

});

function Bleh() {
	opt = $.cookie("dialogoptions");
	if (opt != null) {
		opt = opt.split(',')
		alert(parseInt(opt[0]))
		alert(parseInt(opt[1]))
		$("#dialog").width(parseInt(opt[0]))
		$("#dialog").height(parseInt(opt[1]))
	}
	setTimeout(function(){ $("#dialog").dialog('open') }, 100);
}

$(function() {
     $(".noteDiv").click(function(event) {
	  if (event.ctrlKey || event.shiftKey) {
	       $(this).toggleClass('grouped');
	  }
     });
	var h = 340;
	var w = 550;
	opt = $.cookie("dialogoptions");
	if (opt != null) {
		h = parseInt(opt.split(",")[1]);
		w = parseInt(opt.split(",")[0]);
	}
	$("#dialog").dialog({
		autoOpen: false,
		height:h,
		width:w,
		modal: false,
		resizable: true,
		minWidth: 550,
		minHeight: 340,
		buttons: {
			'Submit': function() {
				title = $("#title").val()
				note = $("#note").val()
				id = $("#id").val()
				$('#editnote').submit()
				$(this).dialog('close');
			},
			Cancel: function() {
				$(this).dialog('close');
			}
		},
		open: function(event, ui) {

			$("#title").val($(this).dialog('option', 'settitle'));
			$("#note").val($(this).dialog('option', 'setnote'));
			$("#theid").val($(this).dialog('option', 'setid'));
			$("#col").val($(this).dialog('option', 'setcol'));
			$("#scrollpos").val($(this).dialog('option', 'setscrollpos'));
			$("#note").height($("#dialog").height()-$("#title").height()-90);
			$("#note").width($("#dialog").width());

		},
		resize: function(event, ui) {
			$("#note").height($("#dialog").height()-$("#title").height()-90);
			$("#note").width($("#dialog").width());
		},
		resizeStop: function(event, ui) {
			//alert($.cookie("dialogoptions"))
			opt = [$("#dialog").width(), $("#dialog").height()]
			$.cookie("dialogoptions", opt, { expires: 365 });
		},
		dragStop: function(event, ui) {

		}
	});

	$(".noteDiv").resizable({
		minHeight: 80,
		minWidth: 120,
		opacity: 0.75,
		zIndex: 2700,
		grid: [20, 20],

		start: function(event, ui) {
			var v = $(this).find(".innerNoteDiv");
			v.hide();
			zIdx = GetHighestZIndex() + 1;
			$(this).css('z-index', zIdx);
			},

		stop: function(event, ui) {
			id = $(this).attr('id');
			w = $(this).width();
			h = $(this).height();
			zIdx = GetHighestZIndex() + 1;
			PersistNote(id.substring(5, id.length), -1, -1, w, h, zIdx);
			$(this).css('z-index', zIdx);
			var v = $(this).find(".innerNoteDiv");
			v.height(h-22);
			v.show()
			}
		});

	$(".noteDiv").draggable({
		//containment: '#containment-wrapper',
		snap: true,
		scroll:true,
		cursor: 'crosshair',
		handle: '.hdr_sel',
		opacity: 0.75,
		zIndex: 2700,
		grid: [10, 10],
		start: function(event, ui) {
		  posTopArray = [];
		  posLeftArray = [];
		  if ($(this).hasClass("grouped")) {
		       $(".grouped").each(function(i) {
			    thiscsstop = $(this).css('top');
			    if (thiscsstop == 'auto') thiscsstop = 0; // For IE

			    thiscssleft = $(this).css('left');
			    if (thiscssleft == 'auto') thiscssleft = 0; // For IE

			    posTopArray[i] = parseInt(thiscsstop);
			    posLeftArray[i] = parseInt(thiscssleft);
		       });
		  }

		  begintop = $(this).offset().top;
		  beginleft = $(this).offset().left;
		  $(".folder").each(function(i) {
			src = $(this).attr('src');
			if (src == "images/folder_32_closed.png") {
				$(this).attr('src', "images/folder_32.png");
			}
		  });
		},
		drag: function(event, ui) {
		  var topdiff = $(this).offset().top - begintop;
		  var leftdiff = $(this).offset().left - beginleft;

		  if ($(this).hasClass("grouped")) {
		       $(".grouped").each(function(i) {
			    $(this).css('top', posTopArray[i] + topdiff);
			    $(this).css('left', posLeftArray[i] + leftdiff);
		       });
		  }
		},
		stop: function(event, ui) {
			  $(".folder").each(function(i) {
				src = $(this).attr('src');
				if (src == "images/folder_32.png") {
					$(this).attr('src', "images/folder_32_closed.png");
				}
			  });
			var topdiff = $(this).offset().top - begintop;
			var leftdiff = $(this).offset().left - beginleft;
			id = $(this).attr('id');
			l = ui.offset.left;
			t = ui.offset.top;
			if (l < 230) {
				l = 230
				$(this).css({left: l})
			}
			if (t < 120) {
				t = 120
				$(this).css({top: t})
			}

			if ($(this).hasClass("grouped")) {
				$(".grouped").each(function(i) {
					thisID = $(this).attr('id');
					thisID = thisID.substring(5, thisID.length)
					t = posTopArray[i] + topdiff;
					l = posLeftArray[i] + leftdiff;
					zIdx = GetHighestZIndex() + 1;
					PersistNote(thisID, l, t, -1, -1, zIdx);
					$(this).css('z-index', zIdx);
				});
			} else {
				zIdx = GetHighestZIndex() + 1;

				PersistNote(id.substring(5, id.length), l, t, -1, -1, zIdx);
				$(this).css('z-index', zIdx);
			}
	
			}
		});

	$(".folderDiv").droppable({
		tolerance: 'pointer',
		greedy: true,
		drop: function(event, ui) {
			id = $(this).attr('id');
			folderID = id.substring(4, id.length);

			if ($(ui.draggable).hasClass("grouped")) {
				$(".grouped").each(function(i) {
					id = $(this).attr('id');
					noteID = id.substring(5, id.length);
					MoveToFolder(noteID, folderID);
				});
			} else {
				id = $(ui.draggable).attr('id');
				noteID = id.substring(5, id.length);
				MoveToFolder(noteID, folderID);
			}

		},
		over: function(event, ui) {
			var v = $(this).find('img').attr({src: 'images/folder_32_edit.png'});
		},
		out: function(event, ui) {
			var v = $(this).find('img').attr({src: 'images/folder_32.png'});
		}
	});
});


