
function ImageWindow(id) {

	this.minSize = {width:56,height:67};
	this.maxSize = {width:0,height:0};
	/*
	this.windowElem = imgWindow;
	this.windowFrame = imgWindowBg;
	this.windowBar = imgWindowBar;
	this.imgElem = infoImage;
	*/
	this.windowElem = document.getElementById(id);
	this.windowFrame = document.getElementById(id + "Bg");
	this.windowBar = document.getElementById(id + "Bar");
	this.imgElem = document.getElementById(id + "Image");
	this.delta = {x:20, y:20};
	this.interval = 20;
	this.intervalId;

	var self = this;
	// ズーム
	this.imgElem.onclick = function() { self.imageClick(); }
	this.imgSpreaded = false;
	// ドラッグ
	this.windowBar.onmousedown = function() { self.dragStart(); }
	this.mp = {x:0,y:0};
	this.cp = {x:0,y:0};
}

ImageWindow.prototype.show = function(imgSrc) {

	this.imgElem.src = imgSrc;
	this.windowElem.style.visibility = "visible";
}

ImageWindow.prototype.hide = function() {

	this.windowElem.style.visibility = "hidden";
}

ImageWindow.prototype.setSize = function(sz) {

	this.windowElem.style.width = sz.width + "px";
	this.windowElem.style.height = sz.height + "px";
	this.windowFrame.style.width = sz.width + "px";
	this.windowFrame.style.height = sz.height + "px";
	this.windowBar.style.width = sz.width + "px";
	this.imgElem.style.width = (sz.width - 6) + "px";
	this.imgElem.style.height = (sz.height - 17) + "px";
}

ImageWindow.prototype.setLocation = function(loc) {

	this.windowElem.style.top = loc.y + "px";
	this.windowElem.style.left = loc.x + "px";
}

ImageWindow.prototype.imageClick = function() {

	this.setFocus();

	var self = this;
	if (this.imgSpreaded) {
		this.intervalId = setInterval(function() { self.doShrink(); }, this.interval);
	}
	else {
		this.intervalId = setInterval(function() { self.doSpread(); }, this.interval);
	}
}

ImageWindow.prototype.doShrink = function() {

	var isExit = true;
	if (HDOMElement.getWidth(this.windowElem) > this.minSize.width) {
		this.windowElem.style.visibility = "hidden";
		HDOMElement.subWidth(this.windowElem, this.delta.x);
		HDOMElement.subWidth(this.windowFrame, this.delta.x);
		HDOMElement.subWidth(this.windowBar, this.delta.x);
		HDOMElement.subWidth(this.imgElem, this.delta.x);
		this.windowElem.style.visibility = "visible";
		isExit = false;
	}
	if (HDOMElement.getHeight(this.windowElem) > this.minSize.height) {
		this.windowElem.style.visibility = "hidden";
		HDOMElement.subHeight(this.windowElem, this.delta.y);
		HDOMElement.subHeight(this.windowFrame, this.delta.y);
		HDOMElement.subHeight(this.imgElem, this.delta.y);
		this.windowElem.style.visibility = "visible";
		isExit = false;
	}

	if (isExit) {
		clearInterval(this.intervalId);
		this.imgSpreaded = false;
		return;
	}
}

ImageWindow.prototype.doSpread = function(dir) {

	var isExit = true;
	if (HDOMElement.getWidth(this.windowElem) < this.maxSize.width) {
		this.windowElem.style.visibility = "hidden";
		HDOMElement.addWidth(this.windowElem, this.delta.x);
		HDOMElement.addWidth(this.windowFrame, this.delta.x);
		HDOMElement.addWidth(this.windowBar, this.delta.x);
		HDOMElement.addWidth(this.imgElem, this.delta.x);
		this.windowElem.style.visibility = "visible";
		isExit = false;
	}
	if (HDOMElement.getHeight(this.windowElem) < this.maxSize.height) {
		this.windowElem.style.visibility = "hidden";
		HDOMElement.addHeight(this.windowElem, this.delta.y);
		HDOMElement.addHeight(this.windowFrame, this.delta.y);
		HDOMElement.addHeight(this.imgElem, this.delta.y);
		this.windowElem.style.visibility = "visible";
		isExit = false;
	}

	if (isExit) {
		clearInterval(this.intervalId);
		this.imgSpreaded = true;
		return;
	}
}

ImageWindow.prototype.dragStart = function() {

	this.setFocus();

	this.mp.x = parseInt(this.windowElem.style.left, 10);
	this.mp.y = parseInt(this.windowElem.style.top, 10);

	this.cp.x = event.clientX;
	this.cp.y = event.clientY;

	var self = this;
	this.windowBar.onmousemove = function() { self.doDrag(); }
	this.windowBar.onmouseup = function() { self.dragEnd(); }

	this.windowBar.setCapture();
}

ImageWindow.prototype.doDrag = function() {

	var top = this.mp.y - (this.cp.y - event.clientY);
	var left = this.mp.x - (this.cp.x - event.clientX);

/*
	status = " mp:(" + this.mp.x + "," + this.mp.y + ")"
		+ " client:(" + event.clientX + "," + event.clientY + ")"
		+ " (" + left + "," + top + ")";
*/
	//status = "(" + left + "," + top + ")";

	this.windowElem.style.top = top;
	this.windowElem.style.left = left;
}

ImageWindow.prototype.dragEnd = function() {

	this.windowBar.onmousemove = "";
	this.windowBar.onmouseup = "";
	this.windowBar.releaseCapture();
}

ImageWindow.prototype.setFocus = function() {

	for (var i = 0; i < this.windowElem.parentElement.children.length; i++) {
		this.windowElem.parentElement.children[i].style.zIndex = 0;
	}
	this.windowElem.style.zIndex = 1;
}

