function Sticky(xml) {
	this.mode = null;
	this.lastUpdate = new Date();
	this.lastUpdate.setTime(0);
	this.stickyId = getNodeValueByTagName(xml, "sticky_id");
	this.blinkTimer = null;

	this.stickyElement = document.createElement("div");
	this.stickyElement.className = "Sticky";
	this.stickyElement.innerHTML = getNodeValueByTagName(xml, "template");

	this.bodyElement = getElementsByClassName(this.stickyElement, "Body")[0];
	this.editElement = getElementsByClassName(this.stickyElement, "Edit")[0];
	this.editBodyElement = getElementsByClassName(this.editElement, "EditBody")[0];
	this.editColorElements = getElementsByClassName(this.editElement, "EditColor");
	this.statusElement = getElementsByClassName(this.stickyElement, "Status")[0];

	initializeEventListenerForMSIE(this.stickyElement);
	this.stickyElement.addEventListener("click", clickSticky, false);
	this.stickyElement.addEventListener("mouseover", mouseOverSticky, false);
	this.stickyElement.addEventListener("mouseout", mouseOutSticky, false);
	getElementsByClassName(this.stickyElement, "TitleBar")[0].addEventListener("mousedown", mouseDownTitleBar, false);
	getElementsByClassName(this.stickyElement, "Close")[0].addEventListener("click", clickClose, false);
	this.bodyElement.addEventListener("dblclick", dblClickBody, false);
	for (var i = 0; i < this.editColorElements.length; i++) this.editColorElements[i].addEventListener("click", clickEditColor, false);
	getElementsByClassName(this.stickyElement, "EditCancel")[0].addEventListener("click", clickEditCancel, false);

	this.update(xml);

	var Sticky = this;

	function clickSticky() {
		var httpRequest = createHttpRequest();
		Sticky.blink(false);
		setOpacity(Sticky.stickyElement, 1);
		httpRequest.onreadystatechange = function() {
			if (httpRequest.readyState == 4) Sticky.stickyElement.style.zIndex = getNodeValueByTagName(httpRequest.responseXML, "zindex");
		}
		httpRequest.open("POST", window.location.pathname, true);
		httpRequest.setRequestHeader("content-type", "application/x-www-form-urlencoded;charset=UTF-8");
		httpRequest.send("target=sticky&action=set_max_zindex&sticky_id=" + Sticky.stickyId);
	}

	function clickClose() {
		if (!confirm("閉じてもよろしいですか？")) return false;
		var httpRequest = createHttpRequest();
		httpRequest.open("POST", window.location.pathname, true);
		httpRequest.setRequestHeader("content-type", "application/x-www-form-urlencoded;charset=UTF-8");
		httpRequest.send("target=sticky&action=delete&sticky_id=" + Sticky.stickyId);
		Sticky.stickyElement.parentNode.removeChild(Sticky.stickyElement);
	}

	function mouseOverSticky() {
		setOpacity(Sticky.stickyElement, 1);
	}

	function mouseOutSticky() {
		setOpacity(Sticky.stickyElement, 0.7);
	}

	function mouseDownTitleBar(e) {
		Sticky.mouseOffsetLeft = getMouseLeft(e) - parseInt(Sticky.stickyElement.style.left);
		Sticky.mouseOffsetTop = getMouseTop(e) - parseInt(Sticky.stickyElement.style.top);
		startDragSticky(Sticky);
	}

	function dblClickBody() {
		Sticky.mode = "edit";
		Sticky.bodyElement.style.display = "none";
		Sticky.editElement.style.display = "block";
		startEditSticky();
	}

	function clickEditColor(e) {
		for (var i = 0; i < Sticky.editColorElements.length; i++) {
			if (Sticky.editColorElements[i].checked) Sticky.stickyElement.firstChild.className = Sticky.editColorElements[i].value;
		}
	}

	function clickEditCancel(e) {
		Sticky.mode = null;
		Sticky.editElement.style.display = "none";
		Sticky.bodyElement.style.display = "block";
		Sticky.stickyElement.firstChild.className = Sticky.color;
		for (var i = 0; i < Sticky.editColorElements.length; i++) {
			if (Sticky.editColorElements[i].value == Sticky.color) {
				Sticky.editColorElements[i].defaultChecked = true;
				Sticky.editColorElements[i].setAttribute("checked", true);
			}
		}
	}
}

Sticky.prototype.open = function(parentElement) {
	if (this.mode == "edit") {
		this.bodyElement.style.display = "none";
		this.editElement.style.display = "block";
	}
	parentElement.appendChild(this.stickyElement);
}

Sticky.prototype.close = function() {
	this.stickyElement.parentNode.removeChild(this.stickyElement);
}

Sticky.prototype.update = function(xml) {
	if (this.mode != null) return false;

	this.xml = xml;
	this.email = getNodeValueByTagName(xml, "email");
	this.color = getNodeValueByTagName(xml, "color");
	this.lastUpdate.setTime(getNodeValueByTagName(xml, "last_update"));

	this.stickyElement.firstChild.className = this.color;
	this.stickyElement.style.zIndex = getNodeValueByTagName(xml, "zindex");
	this.stickyElement.style.top = getNodeValueByTagName(xml, "top") + "px";
	this.stickyElement.style.left = getNodeValueByTagName(xml, "left") + "px";
	this.editBodyElement.style.width = getNodeValueByTagName(xml, "width") + "px";
	this.editBodyElement.style.height = getNodeValueByTagName(xml, "height") + "px";
	this.bodyElement.style.width = getNodeValueByTagName(xml, "width") + "px";
	this.bodyElement.style.height = getNodeValueByTagName(xml, "height") + "px";
	this.statusElement.innerHTML = getNodeValueByTagName(xml, "name") + " @ " + (this.lastUpdate.getMonth() + 1) + "/" + this.lastUpdate.getDate() + " " + this.lastUpdate.getHours() + ":" + this.lastUpdate.getMinutes();

	for (var i = 0; i < this.editColorElements.length; i++) {
		if (this.editColorElements[i].value == this.color) {
			this.editColorElements[i].defaultChecked = true;
			this.editColorElements[i].setAttribute("checked", true);
		}
	}

	this.editElement.style.display = "none";
	this.bodyElement.style.display = "block";
}

Sticky.prototype.move = function(mouseLeft, mouseTop, endFlag) {
	var left = mouseLeft - this.mouseOffsetLeft;
	var top = mouseTop - this.mouseOffsetTop;
	if (top < 55) top = 55;
	this.stickyElement.style.left = left + "px";
	this.stickyElement.style.top = top + "px";
	if (endFlag) {
		var httpRequest = createHttpRequest();
		httpRequest.open("POST", window.location.pathname, true);
		httpRequest.setRequestHeader("content-type", "application/x-www-form-urlencoded;charset=UTF-8");
		httpRequest.send("target=sticky&action=move&sticky_id=" + this.stickyId + "&left=" + left + "&top=" + top);
		this.mode = null;
	}
}

Sticky.prototype.resize = function(mouseLeft, mouseTop, endFlag) {
	var width = parseInt(this.bodyElement.style.width) + mouseLeft - this.mouseOffsetLeft;
	var height = parseInt(this.bodyElement.style.height) + mouseTop - this.mouseOffsetTop;
	this.bodyElement.style.width = width + "px";
	this.bodyElement.style.height = height + "px";
	this.editBodyElement.style.width = width + "px";
	this.editBodyElement.style.height = height + "px";
	this.mouseOffsetLeft = mouseLeft;
	this.mouseOffsetTop = mouseTop;
	if (endFlag) {
		var httpRequest = createHttpRequest();
		httpRequest.open("POST", window.location.pathname, true);
		httpRequest.setRequestHeader("content-type", "application/x-www-form-urlencoded;charset=UTF-8");
		httpRequest.send("target=sticky&action=resize&sticky_id=" + this.stickyId + "&width=" + width + "&height=" + height);
		this.mode = null;
	}
}

Sticky.prototype.blink = function(status) {
	var opacity = 1;
	var Sticky = this;

	if (status) {
		this.blinkTimer = setInterval(function() {
				if (opacity == 1) opacity = 0.7;
				else opacity = 1;
				setOpacity(Sticky.stickyElement, opacity);
			}, 500);
	} else {
		clearInterval(this.blinkTimer);
	}
	return this.blinkTimer;
}


function TextSticky(xml) {
	this.base = Sticky;
	this.base(xml);

	getElementsByClassName(this.stickyElement, "Resize")[0].addEventListener("mousedown", mouseDownResize, false);

	var TextSticky = this;

	function mouseDownResize(e) {
		TextSticky.mouseOffsetLeft = getMouseLeft(e);
		TextSticky.mouseOffsetTop = getMouseTop(e);
		startResizeSticky(TextSticky);
	}
}
inherit(TextSticky, Sticky);

TextSticky.prototype.update = function(xml) {
	Sticky.prototype.update.apply(this, arguments);
	this.bodyElement.innerHTML = getNodeValueByTagName(xml, "html");
	setNodeValueByTagName(this.editBodyElement, xml, "text");
}


function ImageSticky(xml) {
	this.base = Sticky;
	this.base(xml);
}
inherit(ImageSticky, Sticky);

ImageSticky.prototype.update = function(xml) {
	Sticky.prototype.update.apply(this, arguments);
	this.bodyElement.src = "/image/sticky/" + getNodeValueByTagName(xml, "imagefile");
	this.bodyElement.setAttribute("alt", getNodeValueByTagName(xml, "alt"));
	setNodeValueByTagName(this.editBodyElement, xml, "alt");
}

function PageClipSticky(xml) {
	this.base = Sticky;
	this.base(xml);
alert("PageClipSticky");
	this.pageElement = getElementsByClassName(this.bodyElement, "Page")[0];

	getElementsByClassName(this.stickyElement, "Resize")[0].addEventListener("mousedown", mouseDownResize, false);
	this.bodyElement.addEventListener("mousedown", mouseDownScroll, false);

	var PageClipSticky = this;

	function mouseDownResize(e) {
		PageClipSticky.mouseOffsetLeft = getMouseLeft(e);
		PageClipSticky.mouseOffsetTop = getMouseTop(e);
		startResizeSticky(PageClipSticky);
	}

	function mouseDownScroll(e) {
		PageClipSticky.mouseOffsetLeft = getMouseLeft(e);
		PageClipSticky.mouseOffsetTop = getMouseTop(e);
		alert(getMouseLeft(e));
	}
}
inherit(PageClipSticky, Sticky);

PageClipSticky.prototype.update = function(xml) {
alert("PageClipSticky.prototype.update");
	Sticky.prototype.update.apply(this, arguments);
	this.bodyElement.style.cursor = "move";
	getElementsByClassName(this.bodyElement, "Page")[0].style.overflow = "hidden";
	getElementsByClassName(this.bodyElement, "Page")[0].src = getNodeValueByTagName(xml, "url");
	setNodeValueByTagName(this.editBodyElement, xml, "url");
}