// JavaScript Document

var utils = {
	getCoords : function(inputObj){
		var obj = new Object();
		obj.y = inputObj.offsetTop;
		obj.x = inputObj.offsetLeft;
	  	while((inputObj = inputObj.offsetParent) != null){
			if(inputObj.tagName!='HTML'){
				obj.y += inputObj.offsetTop;
				obj.x += inputObj.offsetLeft;
			}
		}
		return obj;
	}
};

var staticMap = {
	x:null,y:null,lat:null,long:null,width:400,height:415,nwLat:0,nwLong:0,seLat:0,seLong:0,movable:true,obj:null,title:null,info:null,mX:null,mY:null,wPx:null,hPx:null,oX:null,oY:null,ax:null,ay:null,iObj:null,
	init : function(obj){
		this.obj = $('mapWin');
		this.iObj = $('mapInfo');
		this.title = this.obj.getElementsByTagName('div')[0];

		var ic = utils.getCoords($('mapImgContainer'));
		this.y = ic.y;
		this.x = ic.x;
		this.ay = ic.y + this.height;
		this.ax = ic.x + this.width;		
		this.oX = (this.nwLong - this.seLong) / this.width;
		this.oY = (this.nwLat - this.seLat) / this.height;

		document.onmousemove = this.trackMouse;
	},
	trackMouse : function(e){
		if(!staticMap.movable) return;
		if (document.all){
			e = window.event;
		}

		var temp = { w:0, h:0 };
		if(e.pageX){
			staticMap.mX = e.pageX;
			staticMap.mY = e.pageY;
		}
		else{
			staticMap.mX = e.clientX + document.documentElement.scrollLeft; 
			staticMap.mY = e.clientY  + document.documentElement.scrollTop;
			temp.w = temp.h = 1;
		}

		if(staticMap.mX > staticMap.x && staticMap.mX < staticMap.ax && staticMap.mY > staticMap.y && staticMap.mY < staticMap.ay){
			with(staticMap.obj.style){
				visibility = 'visible';
				cursor = 'pointer';
			}

			staticMap.wPx = staticMap.mX - staticMap.x + temp.w;
			staticMap.hPx = staticMap.mY - staticMap.y + temp.h;
			//$('status').innerHTML = "x:"+staticMap.mX+", y:"+staticMap.mY+" - w:"+staticMap.wPx+", h:"+staticMap.hPx;
			staticMap.lat = staticMap.nwLat - (staticMap.oY * staticMap.hPx);
			staticMap.long = staticMap.nwLong - (staticMap.oX * staticMap.wPx);

			with(staticMap.obj.style){
				left = (staticMap.mX - 50) + 'px';
				top = (staticMap.mY - 42) + 'px';
			}
	
			var comp, oncity;
			var len = cities.length;
			for(var i = 0; i < len; i++)
			{
				var arr = cities[i].split(';');
				var diff = Math.abs(staticMap.lat - arr[1]) + Math.abs(staticMap.long - arr[2]);
				if(!comp || diff < comp)
				{
					comp = diff;
					oncity = arr[0];
				}
			}
			if(oncity) // && !mapprice
			{
				staticMap.title.innerHTML = oncity;
			}
		}
		else{
			staticMap.obj.style.visibility = 'hidden'; 
		}
	},
	search : function(){
		$('map_count').getElementsByTagName('span')[0].innerHTML = "...";
		$('miTitle').innerHTML = this.title.innerHTML;
		this.movable = false;
		this.obj.style.visibility = "hidden";
		var co = utils.getCoords(this.obj);
		with(this.iObj.style){
			top = (parseInt(staticMap.obj.style.top) - 16) + "px";
			left = (parseInt(staticMap.obj.style.left) - 6) + "px";
			display = "block";
		}
		
		var url = "ajax.php?cmd=msearch&"+staticMap.getParams();
		new Ajax.Request(url, {onSuccess:function(res){
			$('map_count').getElementsByTagName('span')[0].innerHTML = res.responseText;
		}});
	},
	closeInfo : function(){
		this.movable = true;
		this.iObj.style.display = "none";
	},
	showResults : function(){
		location.href = "map.html?new&"+staticMap.getParams();
	},
	getParams : function(){
		return "lat="+staticMap.lat+"&long="+staticMap.long+"&type="+$('map_style').value+"&minbed="+$('map_min_beds').value+"&minbath="+$('map_min_baths').value+"&fy="+$('map_min_year_built').value+"&minprice="+$('price').value;
	}
};

function switchCriteria(flag){
	if(flag){
		$("map_criteria").style.display = "block";
		$("less_link").style.display = "block";
		$("more_link").style.display = "none";
	}
	else{
		
		$("map_criteria").style.display = "none";
		$("less_link").style.display = "none";
		$("more_link").style.display = "block";
	}
}
