﻿/*
	마우스 재스추어 v0.9
	
	제작자 : 최영규
	제작일 : 2007년 1월 14일
	연락처 : http://hooriza.com/
	         hooriza.nospam at gmail.com
*/

var Gesture = {
	
	responses : {
			
		"DR" : function() {
			if (navigator.userAgent.indexOf("MSIE") > -1){
				window.open("about:blank", "_self").close();
			} else {
				alert("Internet Explorer 에서만 창닫기 기능을 사용하실 수 있습니다 ");
			}
		},

		"L" : function() {
			history.back();
		},
		
		"R" : function() {
			history.forward();
		},

		"LD" : function() {
			window.showAll();
		},
		
		"RD" : function() {
			window.hideAll();
		}
		
	},

	angles : null,
	befpos : null,
	tinymove : 5,
	noctxmenu : false,
	msgbox : null,
	msgbox_timer : null,
	
	box : null,
	
	showMessage : function(msg) {

		return;
		
		if (!Gesture.msgbox) {

			Gesture.msgbox = document.createElement("div");
			
			with (Gesture.msgbox.style) {
				position = "absolute";
				border = "1px solid #000";
				backgroundColor = "#fff";
				fontSize = "12px";
				fontFamily = "맑은 고딕, Gulim, Dotum";
				padding = "15px";
				// visibility = "hidden";
			}
			
			document.body.appendChild(Gesture.msgbox);
		}
		
		var org_style = Gesture.msgbox.style.cssText;
		with (Gesture.msgbox.style) {
			left = "0";
			top = "0";
			width = "100%";
			height = "100%";
			border = "0";
			padding = "0";
		}
		
		var scr_size = [ Gesture.msgbox.offsetWidth, Gesture.msgbox.clientHeight ];
		// Gesture.msgbox.style.cssText = org_style;
		
		alert(scr_size);
		
		var text = "";
		var val;
		
		for (var key in Gesture.msgbox) {
			
			try {
				val = parseInt(Gesture.msgbox[key]);
	
				if (!isNaN(val)) {
					text += key + ":" + val + "\n";
				}
			} catch(e) {}
		}
		
		alert(text);
		
		/*
		
		Gesture.msgbox.style.visibility = "hidden";
		Gesture.msgbox.style.display = "block";
		
		var body = document.documentElement || document.body;
		var scroll = [ body.scrollLeft, body.scrollTop ];

		Gesture.msgbox.innerHTML = msg;
		
		var org_height = Gesture.msgbox.clientHeight;

		with (Gesture.msgbox.style) {
			height = "100%";
			width = "100%";
		}
		Gesture.msgbox.style.visibility = "visible";
		var scr = [ Gesture.msgbox.offsetWidth, Gesture.msgbox.offsetHeight ];
		alert(scr);
		
		var screen_height = Gesture.msgbox.offsetHeight;
		Gesture.msgbox.style.height = "auto";		

		Gesture.msgbox.style.left =
			parseInt((document.body.clientWidth - Gesture.msgbox.offsetWidth) / 2) + scroll[0] + "px";
			
			alert(screen_height);

		Gesture.msgbox.style.top =
			parseInt((screen_height - org_height) / 2) + scroll[1] + "px";
		
		Gesture.msgbox.style.visibility = "visible";
		
		Gesture.msgbox_timer = window.setTimeout(Gesture.hideMessage, 1000);
		*/
	},
	
	hideMessage : function() {
		
		Gesture.msgbox.style.visibility = "hidden";
		Gesture.msgbox.style.display = "none";		
	},
	
	markPosition : function (x, y) {
	
		if (!Gesture.box) {
			Gesture.box = document.createElement("div");
			document.body.appendChild(Gesture.box);
		}
		
		var body = document.documentElement || document.body;
		var scroll = [ body.scrollLeft, body.scrollTop ];

		var mark = document.createElement("div");
		with (mark.style) {
			position = "absolute";
			width = "3px";
			height = "3px";
			left = (x - 2 + scroll[0]) + "px";
			top = (y - 2 + scroll[1]) + "px";
			fontSize = "9px";
			overflow = "hidden";
			backgroundColor = "#777";
		}
		
		Gesture.box.appendChild(mark);
	},
	
	clearMarkPosition : function () {
		
		if (Gesture.box) {
			document.body.removeChild(Gesture.box);
			Gesture.box = null;
		}
	},
	
	recordTracking : function (x, y, text) {

		if (typeof Gesture.markPosition == "function") {
			Gesture.markPosition(x, y);
		}
				
		if (Gesture.angles) {
			
			if (Gesture.befpos) { // 이전에 움직인 기록이 있으면

				var incr = [ x - Gesture.befpos[0], y - Gesture.befpos[1] ]; // 상하좌우로 얼마나 움직였나
				var size = Math.sqrt(incr[0] * incr[0] + incr[1] * incr[1]); // 움직인 양은 얼마나 되나
				
				if (Gesture.tinymove > 0 && size < 10) { // 너무 조금 이동했으면 5번까지는 SKIP
					Gesture.tinymove--;
					return;
				}
				
				var angle = Gesture.getAngle(incr[0], incr[1]);

				if (!Gesture.isMinorAngle(angle)) { // 각도를 계산해서 저장
					Gesture.angles.push(angle);
				}
			}
			
			Gesture.befpos = [ x, y ];
		}
	
		Gesture.tinymove = 5;
	},
	
	mousemove : function (e) {
		e = e || window.event;
		
		if (Gesture.angles) {
			Gesture.recordTracking(e.clientX, e.clientY);
		}
	},
	
	mousedown : function (e) {
		e = e || window.event;
		
		if (e.button == 2) {
			Gesture.angles = [];
			Gesture.befpos = null;
			
			Gesture.recordTracking(e.clientX, e.clientY);
		}
		
		Gesture.noctxmenu = false;
	},
	
	mouseup : function (e) {
		e = e || window.event;
		
		if (e.button == 2 && Gesture.angles)	{
			Gesture.recordTracking(e.clientX, e.clientY);
			
			if (typeof Gesture.clearMarkPosition == "function") {
				Gesture.clearMarkPosition();
			}
			
			Gesture.noctxmenu = Gesture.angles.length;
			
			var gesture = Gesture.analyzeDirection();
			Gesture.angles = null;

			Gesture.doAction(gesture);
		}
	},
	
	contextmenu : function(e) {
		
		if (Gesture.noctxmenu) {
			
			if (e.preventDefault) {
				e.preventDefault();
				e.stopPropagation();
			} else {
				e.returnValue = false;
				e.cancelBubble = true;
			}						

			Gesture.noctxmenu = false;
		}
		
	},
	
	analyzeDirection : function () {
	
		var angle, bangle = null;
		var direction = "";
		
		for (var i = 0; !isNaN(angle = Gesture.angles[i]); i++) {
			if (angle != bangle) direction += Gesture.getDirection(angle);
			bangle = angle;
		}
		
		return direction;
	},
	
	getAngle : function (x, y) {

		var angle;
					
		angle = Math.atan(y / x) / Math.PI * 180;
		angle = Math.abs(angle);
		
		if (x >= 0 && y <= 0) { // 1사분면
			angle = 90 - angle;
		} else if (x >= 0 && y > 0) { // 2사분면
			angle = angle + 90;
		} else if (x < 0 && y > 0) { // 3사분면
			angle = 90 - angle + 180;
		} else if (x < 0 && y <= 0) { // 4사분면
			angle = angle + 270;
		}
		
		return (parseInt((angle + 45 / 2) / 45) * 45) % 360;
	},
	
	isMinorAngle : function (angle) {
		return (angle + 45) % 90 ? false : true;
	},
	
	getDirection : function (angle) {
		var direction = [ "U", "R", "D", "L" ];
		return direction[angle / 90];
	},
	
	doAction : function (gesture) {
		if (Gesture.responses[gesture]) {
			Gesture.responses[gesture]();
		}
			
		Gesture.showMessage("TEST");
	}

};

if (typeof document.attachEvent == "undefined") {
	document.attachEvent = function(event, callback) {
		return document.addEventListener(event.substr(2), callback, false);	
	}
}

document.attachEvent("onmousemove", Gesture.mousemove);
document.attachEvent("onmousedown", Gesture.mousedown);
document.attachEvent("onmouseup", Gesture.mouseup);
document.attachEvent("oncontextmenu", Gesture.contextmenu);
