var Eye = new Object();
var posX = 0;
var posY = 0;
Eye.Control={
	init:function()
	{
		this.eye = document.all ? document.all["eye"] : document.getElementById("eye")
		this.eyeW = 70;
		this.eyeH = 50;
		this.newW = 70;
		this.newH = 50;
		this.zoom = 5;
		this.smallW = 345;
		this.smallH = 491
		this.smallUrl = 'images/small.jpg';
		this.dir = 'temp';
		this.mapid = 1;
		this.setMap(2);
		this.addEvent(this.eyeW,this.eyeH);
	},
	addEvent:function(w,h)
	{
		var self = this;
		Drag.init(this.eye, null, 0, 250 - w, 0, 200 - h);
		document.getElementById("eyebg").onmouseup=function()
		{
			self.mapDrag((event.clientX-16 - self.newW/2 + posX),(event.clientY-110 - self.newH/2 + posY));
		}
		this.eye.onDrag = function(x,y)
		{
			posX = posX + x + w - 220;
			posY = posY + y + h - 170;
			posX = posX<0?0:posX;
			posY = posY<0?0:posY;
			posX = posX>(self.smallW - 250)?(self.smallW - 250):posX;
			posY = posY>(self.smallH - 200)?(self.smallH - 200):posY;
			document.getElementById("eyebg").style.backgroundPosition = -posX + 'px ' + -posY + 'px';
		}
		this.eye.onDragEnd = function(x, y)
		{
			window.frames['mapFrame'].MapInfo.Control.refreshMap(x+posX, y+posY);
		};
		this.eye.onDrag(0,0);
	},
	mapDrag:function(x,y)
	{
		x = x < 0?0:x;
		y = y<0?0:y;
		x = x>(this.smallW-this.newW)?(this.smallW-this.newW):x;
		y = y>(this.smallH-this.newH)?(this.smallH-this.newH):y;
		this.eye.onDrag(x-posX,y-posY);
		var mx = x - posX;
		var my = y - posY;
		this.eye.style.left = mx;
		this.eye.style.top = my;
		window.frames['mapFrame'].MapInfo.Control.refreshMap(x, y);
	},
	mapZoom:function(z)
	{
		var eW = parseInt(this.eyeW * 5 / z);
		var eH = parseInt(this.eyeH * 5 / z);
		this.eye.style.width = eW + 'px';
		this.eye.style.height = eH + 'px';
		this.addEvent(eW,eH);
		var newX = window.frames['mapFrame'].MapInfo.Control.oldX + this.newW / 2;
		var newY = window.frames['mapFrame'].MapInfo.Control.oldY + this.newH / 2;
		newX = parseInt(newX - eW / 2);
		newY = parseInt(newY - eH / 2);
		this.newW = eW;
		this.newH = eH;
		this.mapDrag(newX,newY);
	},
	goMap:function(x,y)
	{
		x = x / 10;
		y = y / 10;
		x = parseInt(x - this.newW / 2);
		y = parseInt(y - this.newH / 2);
		this.mapDrag(x,y);
	},
	setMap:function(n)
	{
		if(n==1)
		{
			this.mapid = 1;
			$('eyebg').style.background='url(images/small2.jpg)';
			this.dir = '200/zqt';
			this.smallW = 613;
			this.smallH = 427;
			$('zqt').className='a1';
			$('cqt').className='a2';
		}
		else
		{
			this.mapid = 2;
			$('eyebg').style.background='url(images/small.jpg)';
			this.dir = '200/city';
			this.smallW = 345;
			this.smallH = 491;
			$('zqt').className='a2';
			$('cqt').className='a1';
		}
		posX = 0;
		posY = 0;
		this.addEvent(this.newW,this.newH);
		this.mapDrag(0,0);
	}
}

var NDEBUG = true;
var Debug = {
	init: function()
	{
		if (NDEBUG == true)
		{
			return;
		}
		var divDebug = document.createElement('div');
		this.divDebug = divDebug;
		with (divDebug.style)
		{
			top = '400px';
			left = '10px';
			width = '300px';
			height = '200px';
			overflow = 'scroll';
			verticalAlign = 'top';
			position = 'absolute';
			backgroundColor = '#fff';
			padding = '5px';
			lineHeight = '110%';
		}
		document.getElementsByTagName('body')[0].appendChild(divDebug);
		this.auto = setInterval(setAuto, 200);
		function setAuto()
		{
			divDebug.style.top = document.documentElement.scrollTop + 400;
		}
		Debug.log('<b>Debug</b>');
		divDebug.ondblclick = function()
		{
			NDEBUG = true;
			Element.remove(divDebug);
		}
		this.height = 0;
	},
	log: function(str)
	{
		if (NDEBUG == true)
		{
			return;
		}
		var now = new Date();
		this.divDebug.innerHTML += '[' + now.getHours() + ':' 
			+ now.getMinutes() + ':'
			+ now.getSeconds() + '] ' + str + '<br>';
		this.divDebug.scrollTop = 10000000;
	}
}
Eye.Search={
	init:function()
	{
		var self=this;
		$('s_btn').onclick=search;
		function search()
		{
			self.goSearch();
		}
	},
	dataElem: [
		'id',
		'proname',
		'xpos',
		'ypos',
		'state'
	],
	goSearch:function()
	{
		var key=$('s_keys').value;
		var self=this;
		var sReplyUrl = 'action.aspx' + window.frames['mapFrame'].noCacheUrl() + '&action=getposlist&keys=' + key + '&mapid=' + Eye.Control.mapid;
		new Ajax.Request(
				sReplyUrl,
				{
					method: 		'get',
					asynchronous:	true,
					onSuccess: 	onSucc,
					onFailure:		onError
				}
		);
		function getNodeValue(node)
		{
			var data = new Array();
			for(var j=0;j<self.dataElem.length;j++)
			{
					var child = node.getElementsByTagName(self.dataElem[j])[0].firstChild.nodeValue;
					data[self.dataElem[j]]=child;
			}
			return data;
		}
		function onSucc(response)
		{
			var xmlDoc = response.responseXML;
			var projects = xmlDoc.getElementsByTagName("project");
			var str = '<ul>';
			for(var i=0;i<projects.length;i++)
			{
				var project = projects[i];
				var data = getNodeValue(project);
				str +='<li>กค<a href="javascript:Eye.Control.goMap('+data.xpos+','+data.ypos+')">' + data.proname + '</a></li>';
			}
			str += '</ul>';
			$('result').innerHTML=str;
		}
		function onError()
		{
			alert('err');
		}
	}
}
//document.onselectstart=function(){return false};
//document.oncontextmenu=function(){return false};
window.onload=function()
{
	Debug.init();
	Eye.Control.init();
	Eye.Search.init();
}