var ie  = (document.all && document.styleSheets);
var ns6 = (document.getElementById && !document.all);
var ns4 = document.layers;

var dragapproved = false;
var NS4drag      = false;
var z, x, y;
var noteBox;

function move(e) {

	if(dragapproved) {

		z.style.right = (ns6 ? temp1 - e.clientX + x : temp1 - event.clientX + x);
		z.style.top   = (ns6 ? temp2 + e.clientY - y : temp2 + event.clientY - y);
		return false;
	}
}

function dragNS4(dragBox) {

	if(!ns4 || !dragBox) {

		return;
	}

	dragBox.captureEvents(Event.MOUSEDOWN | Event.MOUSEUP);
	dragBox.onmousedown = function(e) {

		dragBox.captureEvents(Event.MOUSEMOVE);
		N4x = e.x;
		N4y = e.y;
	}

	dragBox.onmousemove = function(e) {

		if(NS4drag) {

		      dragBox.moveBy(e.x - N4x, e.y - N4y);
		      return false;
		}
	}

	dragBox.onmouseup = function() {

		dragBox.releaseEvents(Event.MOUSEMOVE);
		setCoordsCookie(dragBox, false);
	}
}

function drags(e) {

	if(!ie && !ns6) {

		return;
	}

	var firedobj   = (ns6 ? e.target : event.srcElement);
	var topelement = (ns6 ? "HTML"   : "BODY"          );

	while (firedobj.tagName != topelement && firedobj.className != "note_box") {

		firedobj = (ns6 ? firedobj.parentNode : firedobj.parentElement);
	}

	if(firedobj.className == "note_box") {

		dragapproved         = true;
		z                    = firedobj;
		temp1                = parseInt(z.style.right + 0);
		temp2                = parseInt(z.style.top   + 0);
		x                    = (ns6 ? e.clientX : event.clientX);
		y                    = (ns6 ? e.clientY : event.clientY);
		document.onmousemove = move;

		return false;
	}
}

function release(e) {

	if(dragapproved) {

		dragapproved = false;
		setCoordsCookie(z, false);
	}
}

function setCoordsCookie(obj, hide) {

	var coords = getCoords(obj);
	var cookie = coords[0] + ":" + coords[1] + ":" + (hide ? "1" : "0");

	setCookie("noteCoords", cookie, cookieExpiry(), "/");
}

function cookieExpiry() {

	var expire = new Date();
	expire.setTime(expire.getTime() + 7*24*60*60*1000);
	return expire;
}

function getCoordsCookie() {

	var cookie = getCookie("noteCoords");
	if(cookie != null && cookie.indexOf(":") != -1) {

		var splitCookie = cookie.split(":");
		if(splitCookie[0] && splitCookie[1] && splitCookie[2]) {

			return splitCookie;
		}
	}

	return false;
}

function closeBox() {

	hideNote(noteBox);

	var notes_off = confirm("Do you want to permanently turn off notes?");
	if(notes_off == true) {

		setCoordsCookie(noteBox, 1);
	}
}

function getCoords(obj) {

	var coords = new Array(2);

	if(ie || ns6) {

		coords[0] = obj.style.right;
		coords[1] = obj.style.top;
	}
	else {

		coords[0] = obj.left;
		coords[1] = obj.top;
	}

	return coords;
}

function drawNoteBox(noteText) {

	if(!ns4 && !ie && !ns6) {

		return;
	}

	document.write("<div id=\"notes\" class=\"note_box\">\n");

	// Checks whether stylesheets are on in Netscape 4
	if(ns4 && !document.layers.notes) {

		document.write("</div>\n");
		ns4 = false;
		return;
	}

	document.write("<ilayer width=\"100%\" onSelectStart=\"return false\">\n");
	document.write("<layer width=\"100%\" onMouseover=\"NS4drag=true; if(ns4) dragNS4(notes);\" onMouseout=\"NS4drag=false;\">\n");
	document.write("<center>\n");
	document.write("<table width=\"100%\" cellpadding=\"0\" cellspacing=\"0\" border=\"0\"><tr><td>\n");
	document.write("<img src=\"/images/pixel.gif\" width=\"15\" height=\"15\">\n");
	document.write("</td><td>\n");
	document.write("<center><big>Note</big></center>\n");
	document.write("</td><td align=\"right\" valign=\"top\">\n");
	document.write("<a href=\"javascript: closeBox();\"><img src=\"/images/page_notes_off.gif\" width=\"15\" height=\"15\" border=\"0\" alt=\"Close\"></a>\n");
	document.write("</td></tr></table>\n");
	document.write("</center>\n");

	document.write("<p>\n");
	document.write(noteText);
	document.write("</p>\n");
	document.write("</layer>\n");
	document.write("</ilayer>\n");
	document.write("</div>\n");
}

function loadNotes() {

	if(!ns4 && !ie && !ns6) {

		return;
	}

	var note_box = (ns6 ? document.getElementById("notes") : (ie ? document.all["notes"] : document.layers["notes"]));
	noteBox = note_box;

	if(ns4) {

		if(coords = getCoordsCookie()) {

			note_box.top  = coords[1];
			note_box.left = coords[0];

			if(coords[2] == 1) {

				return;
			}
		}
		else {

			note_box.top  = 185;
			note_box.left = window.innerWidth - 16 - note_box.clip.width - 10;
		}
	}
	else {

		if(coords = getCoordsCookie()) {

			note_box.style.top   = coords[1];
			note_box.style.right = coords[0];

			if(coords[2] == 1) {

				return;
			}
		}
		else {

			note_box.style.top   = 185;
			note_box.style.right = 10;
		}
	}

	showNote(noteBox);
}

function setCookie(name, value, expires, path, domain, secure) {

	var curCookie = name + "=" + escape(value)
		+ ((expires) ? "; expires=" + expires.toGMTString() : "")
		+ ((path)    ? "; path="    + path                  : "")
		+ ((domain)  ? "; domain="  + domain                : "")
		+ ((secure)  ? "; secure"                           : "");

	document.cookie = curCookie;
}

function getCookie(name) {

	var dc     = document.cookie;
	var prefix = name + "=";
	var begin  = dc.indexOf("; " + prefix);

	if (begin == -1) {

		begin = dc.indexOf(prefix);

		if (begin != 0) {

			return null;
		}
	} 
	else {

		begin += 2;
	}

	var end = document.cookie.indexOf(";", begin);

	if (end == -1) {

		end = dc.length;
	}

	return unescape(dc.substring(begin + prefix.length, end));
}

function deleteCookie(name, path, domain) {

	if (getCookie(name)) {

		document.cookie = name + "="
			+ ((path)   ? "; path="   + path   : "")
    			+ ((domain) ? "; domain=" + domain : "")
			+ "; expires=Thu, 01-Jan-70 00:00:01 GMT";
	}
}

function hideNote(obj) {

	if(ie || ns6) {

		obj.style.visibility = "hidden";
	}
	else if(ns4) {

		obj.visibility = "hide";
	}
}

function showNote(obj) {

	if(ie || ns6) {

		obj.style.visibility = "visible";
	}
	else if(ns4) {

		obj.visibility = "show";
	}
}

function resetNotes() {

	deleteCookie("noteCoords", "/");
	loadNotes();
	showNote(noteBox);
}

function notesLink() {

	if(!ns4 && !ie && !ns6) {

		return;
	}

	document.write("<center><a href=\"javascript: resetNotes();\"><font color=\"#000000\">Reset Pop-Up Note</font></a></center>");
}

document.onmousedown = drags;
document.onmouseup   = release;

