function onMove(e){
	var mEvt = new MouseEvent(e);
	var box = {
		xMin : 0,
		xMax : util.getPageXScroll()+util.getInnerWidth()-util.getElementWidth("photo_popup"),
		yMin : 0,
		yMax : util.getPageYScroll()+util.getInnerHeight()-util.getElementHeight("photo_popup")
	}
	//Debug.log("xMax="+document.documentElement.clientWidth+" "+util.getPageXScroll()+" "+util.getInnerWidth()+" "+util.getElementWidth("photo_popup"));
	//Debug.log("mX="+mEvt.x+" sX="+startPoint.x+" oX="+offsetPoint.x+" xMax="+box.xMax+" result="+util.getNumberBtw(startPoint.x+(mEvt.x-(startPoint.x+offsetPoint.x)), box.xMin, box.xMax));
	//Debug.log("mY="+mEvt.y+" sY="+startPoint.y+" oY="+offsetPoint.y+" yMax="+box.yMax+" result="+util.getNumberBtw(mEvt.y-offsetPoint.y, box.yMin, box.yMax));
	//Debug.log("mY="+mEvt.y+" sY="+startPoint.y+" oY="+offsetPoint.y+" yMax="+box.yMax+" result="+util.getNumberBtw(startPoint.y+(mEvt.y-(startPoint.x+offsetPoint.y)), box.yMin, box.yMax));
	//util.setStyles("photo_popup", [["left", util.getNumberBtw(startPoint.x+(mEvt.x-(startPoint.x+offsetPoint.x)), box.xMin, box.xMax)], ["top", util.getNumberBtw(startPoint.y+(mEvt.y-(startPoint.x+offsetPoint.y)), box.yMin, box.yMax)]]);
	util.setStyles("photo_popup", [["left", util.getNumberBtw(mEvt.x-offsetPoint.x, box.xMin, box.xMax)], ["top", util.getNumberBtw(mEvt.y-offsetPoint.y, box.yMin, box.yMax)]]);
}
function stopDrag(e){
	Events.removeEvent("document", "onmousemove", mmID);
	Events.removeEvent("document", "onmousemove", muID);
	mmID = muID = null;
}

function setWishcard(){
	var wishcard = util.element("photo_popup");
	if(wishcard){
	var title_bar = util.getElementsByClassName("wishcard", "title", "div")[0];
		var close_btn = util.getElementsByClassName("wishcard", "close", "span")[0];
		var disable_cont = util.getElementsByClassName("wishcard", "disableInput", "div")[0];
		var disable_input = disable_cont.getElementsByTagName("input")[0];
		var wishcard_btn = util.element("wishcard_btn");
		
		title_bar.onmousedown = function(e){
			e = window.event || e;
			var mEvt = new MouseEvent(e);
			//var offsetX = util.getPageXScroll()+(e.clientX || e.layerX || 0)+util.element("photo_popup").offsetLeft;
			//var offsetY = util.getPageYScroll()+(e.clientY || e.layerY || 0)+util.element("photo_popup").offsetTop;
			//var offsetX = util.getElementX("photo_popup"); //util.getPageXScroll()+(e.clientX || e.layerX || 0)+util.element("photo_popup").offsetLeft;
			//var offsetY = util.getElementY("photo_popup"); //util.getPageYScroll()+(e.clientY || e.layerY || 0)+util.element("photo_popup").offsetTop;
			var startX = util.element("photo_popup").offsetLeft;
			var startY = util.element("photo_popup").offsetTop;
			var offsetX = mEvt.x-startX;
			var offsetY = mEvt.y-startY;
			//Debug.log("startPouint "+startX+" "+startY);
			//Debug.log("offsetPoint "+(mEvt.x-startX)+" "+(mEvt.y-startY));
			startPoint = new Point(startX, startY);
			offsetPoint = new Point(offsetX, offsetY);
			if(mmID == null){
				mmID = Events.addEvent("document", "onmousemove", Delegate.create(this, onMove));
				muID = Events.addEvent("document", "onmouseup", Delegate.create(this, stopDrag));
			}
		}
		
		close_btn.onclick = function(){
			hideWishcard()
			showWishcardButton();
		}
		
		disable_input.onchange = function(){
			if(this.checked){
				util.setCookie("wishcard", "1", 60);
			} else {
				util.deleteCookie("wishcard");
			}
		}
		
		wishcard_btn.onclick = function(){
			util.deleteCookie("wishcard");
			hideWishcardButton();
			showWishcard();
		}
		
		// show wishcard
		if(util.getCookie("wishcard") != "1"){
			showWishcard();
		} else {
			showWishcardButton();
		}
	}
}
function showWishcard(){
	util.setStyle("photo_popup", "display", "block");
	if(firstShow) moveWishcardToCenter();
}
function hideWishcard(){
	util.setStyle("photo_popup", "display", "none");
}
function showWishcardButton(){
	util.setStyle("wishcard_btn", "display", "block");
}
function hideWishcardButton(){
	util.setStyle("wishcard_btn", "display", "none");
}
function moveWishcardToCenter(){
	util.setStyles("photo_popup", [["left", Math.floor((util.getPageXScroll()+util.getInnerWidth()-util.getElementWidth("photo_popup"))/2)], ["top", Math.floor((util.getPageYScroll()+util.getInnerHeight()-util.getElementHeight("photo_popup"))/2)]]);
}

var offsetPoint, startPoint, mmID, muID;
var firstShow = true;


Events.addEvent("window", "onload", setWishcard);
