var postItFocusIndex = -1;
var postItIndex = 1;
var mouseX, mouseY;

isNN=!d.all;
isN4=d.layers;
isHot=false;
var ni = -1;
var previousText = new Array(50); var previousX = new Array(50); var previousY = new Array(50); var offsetX = new Array(50); var offsetY = new Array(50); var nowX = new Array(50); var nowY = new Array(50);
var zOrderIndex = 0;
var ddEnabled;

function ddInit(e) {
	var obj;
	if (e && e.id && e.id.indexOf("postIt") == 0) {
		obj = e;
	} else {
		obj = isIE() ? event.srcElement : e;
	}
	ni = getPostItIndex(obj);
	if (ni == -1) return;
	whichDog = getPostIt(ni);
	offsetX[ni]=isIE() ? event.clientX : mouseX;
	offsetY[ni]=isIE() ? event.clientY : mouseY;
	nowX[ni]=parseInt(whichDog.style.left);
	nowY[ni]=parseInt(whichDog.style.top);
	ddEnabled=true;
}

function dd(e) {
	if (isIE()) {
		mouseX = event.clientX;
		mouseY = event.clientY;
	} else {
		mouseX = e.clientX;
		mouseY = e.clientY;
	}
	if (!ddEnabled) return;
	whichDog = getPostIt(ni);
	x = nowX[ni] + mouseX - offsetX[ni];
	y = nowY[ni] + mouseY - offsetY[ni];
	if (x<0) x=0;
	if (y<0) y=0;
	whichDog.style.left=x;
	whichDog.style.top=y;
	return false;
}

function ddN4(whatDog){
	if (!isN4) return;
	N4=eval(whatDog);
	N4.captureEvents(Event.MOUSEDOWN|Event.MOUSEUP);
	N4.onmousedown=function(e){
		N4.captureEvents(Event.MOUSEMOVE);
		N4x=e.x;
		N4y=e.y;
	}
	N4.onmousemove=function(e){
		if (isHot){
			N4.moveBy(e.x-N4x,e.y-N4y);
			return false;
		}
	}
	N4.onmouseup=function(){
		N4.releaseEvents(Event.MOUSEMOVE);
	}
}

function getWindowHeight() {
	var myWidth = 0, myHeight = 0;
	if( typeof( window.innerWidth ) == 'number' ) {
		myHeight = window.innerHeight;
	} else if( d.documentElement && ( d.documentElement.clientWidth || d.documentElement.clientHeight ) ) {
		myHeight = d.documentElement.clientHeight;
	} else if( d.body && ( d.body.clientWidth || d.body.clientHeight ) ) {
		myHeight = d.body.clientHeight;
	}
	return myHeight;
}

function userAddPostIt() {
	if (checkInvalidSession()) {
		return false;
	}

	x = mouseX - (removeMeasure(e("postItTemplate").style.width)/2);
	y = mouseY - 10;

	ni = postItIndex;
	newPostIt = addPostIt("", "", x, y);
	newPostIt.style.zIndex = zOrderIndex++;
	ddInit(newPostIt);
}

function addPostIt(name, text, x, y) {
	var parentNode = e("postItTemplate").parentNode;
	var newPostIt = e("postItTemplate").cloneNode(true);

	newPostIt.id = "postIt" + postItIndex;
	newPostIt.name = name;
	var textArea = findNode(newPostIt, "noteTextArea");
	textArea.value = decodeURIComponent(text);
	newPostIt.style.left = x;
	newPostIt.style.top = y;
	show(newPostIt);
	parentNode.appendChild(newPostIt);
	noteTextAreaResize(textArea);

	previousText[postItIndex] = textArea.value;
	previousX[postItIndex] = x;
	previousY[postItIndex] = y;

	postItIndex++;
	return newPostIt;
}

function dropPostIt(postIt) {
	if (postIt.offsetTop > getWindowHeight()) {
		window.clearInterval(closePostItInterval);
		hide(postIt);
		if (logged) {
			if (postIt.name != "") {	// Post it's "name" must be initialized thus saved in DB to delete it
				vars = {
					name:postIt.name
				}
				ajax.postForPlainText("/user/deletePostIt.do", vars, empty);
			}
		}
	} else {
		gravity+=1;
		postIt.style.top = postIt.offsetTop + gravity;
	}
}

function placePostItOnHomepage(o) {
	var i = getPostItIndex(o);
	var postIt = getPostIt(i);
	vars = {
		name:postIt.name
	}
	ajax.postForPlainText("/user/savePostItOnHomepage.do", vars, placePostItOnHomepageCB);
}

function placePostItOnHomepageCB() {
	alert('This post it has been place on the homepage!');
}

function closePostIt(obj) {
	gravity = 1;
	index = getPostItIndex(obj);
	postIt = getPostIt(index);
	closePostItInterval = window.setInterval('dropPostIt(postIt)', 1)
}

function mouseOverNote(obj) {
	index = getPostItIndex(obj);
	isHot=true;
	if (isN4)
		ddN4(postIt);
	if (ni==-1)
		ni=index;
}

function placeOnTop(obj) {
	index = getPostItIndex(obj);
	if (!ddEnabled) {
		getPostIt(index).style.zIndex=zOrderIndex++;
	}
}

function getPostIt(index) {
	return e("postIt" + index);
}

function savePostIt(index, onUnload) {
	if (logged) {
		if (index == -1) return;
		var postIt = getPostIt(index);
		var textArea = findNode(postIt, "noteTextArea");
		if (previousText[index] != textArea.value || previousX[index] != postIt.offsetLeft || previousY[index] != postIt.offsetTop) {
			bodyVars = {
				name:postIt.name,
				x:postIt.offsetLeft,
				y:postIt.offsetTop,
				noteTextArea:encodeURIComponent(textArea.value)
			}
			ajax.postVars("/user/savePostIt.do", bodyVars, null, saveCB, true, postIt.id);
			previousText[index] = textArea.value;
			previousX[index] = postIt.offsetLeft;
			previousY[index] = postIt.offsetTop;
		}
	}
}

function saveCB(str, h, context) {
	var noteId;
	if (str) {
		noteId = str.getElementsByTagName("noteId")[0];
	}
	if (noteId) {
		noteId = noteId.childNodes[0].nodeValue;
		e(context).name = noteId;
	}
}

function getPostItIndex(obj) {
	while (true) {
		if (obj.id) {
			if (obj.id.indexOf("postIt") == 0) {
				return obj.id.substring("postIt".length, obj.id.length);
			}
		}
		obj = obj.parentNode;
		if (!obj) return -1;
	}
	return -1;
}

function noteTextAreaResize(obj) {
	height = obj.scrollHeight;
	if (height < 50) height = 50;
	obj.parentNode.scrollTop = 0;
	obj.parentNode.style.height = height + 20;
	if (!isIE()) {
		obj.rows = height / 16;
	}
}

function handleMouseUp(e) {
	if (ddEnabled) {
		savePostIt(ni, false);
		var textArea = findNode(getPostIt(ni), "noteTextArea");
		textArea.focus();
		ni=-1;
		ddEnabled=false;
	}
}

function windowUnload(evt) {
	/*
	if (!isIE()) {
		window.scrollbars.visible=true;
	}
	*/
	if (postItFocusIndex != -1) {
		savePostIt(postItFocusIndex, true);
	}
}

function search(s) {
	s = s.toLowerCase();
	var highlightClassName = "highLight";
	textAreas = d.getElementsByTagName("textarea");
	for (a=0; a<textAreas.length; a++) {
		if (textAreas[a].id == "noteTextArea") {
			var postItIndex = getPostItIndex(textAreas[a]);
			var postIt = getPostIt(postItIndex);
			if (textAreas[a].value.toLowerCase().indexOf(s) != -1 && s!="") {
				placeOnTop(postIt);
				if (postIt.childNodes[bIndex].className.indexOf(highlightClassName) == -1) {
					postIt.childNodes[bIndex].className+=" " + highlightClassName;
				}
			} else {
				postIt.childNodes[bIndex].className = postIt.childNodes[bIndex].className.replace(highlightClassName, "");
			}
		}
	}
}

d.onmousemove=dd;
d.onmouseup=handleMouseUp;
window.onunload = windowUnload;

/*
if (!isIE()) {
	window.scrollbars.visible=false;
}
*/