//////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// GoTo functions

var busy = true;

$(document).ready(function(){

	slide_to('spot');

});
						   
						   
function slide_to(id) {
	
	inner_width = $(window).width();
	inner_height = $(window).height();
	
	var xy = $("#"+id);
	var offset = xy.offset();

	$('html,body').animate({
		scrollTop: (offset.top-inner_height/2)+50,
		scrollLeft: (offset.left-inner_width/2)+40
	}, 1000, function () { 
		update_map();
		$('#smoke').fadeOut(2000);
		busy = false;
	});
	
	
	
}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Map images

haveArray = new Array();

function update_map() {
	var gridSize = 600;
	
	inner_width = $(window).width();
	inner_height = $(window).height();

	offset_width = $(window).scrollLeft();
	offset_height = $(window).scrollTop();

	colNum = Math.floor(offset_width/gridSize);
	rowNum = Math.floor(offset_height/gridSize);

	// build needed
	needArray = new Array();
	
	gridX = inner_width/gridSize;
	gridY = inner_height/gridSize;
	
	for(i=1; i<5; i++){
		for(j=1; j<5; j++){
			needArray.push((rowNum+i)+'_'+(colNum+j)); //add NEEDED
		}
	}
	
	for(i=0; i<needArray.length; i++){
		tempArray = needArray[i].split("_");
		if(!$("#map_"+tempArray[0]+"_"+tempArray[1]).length){
			$('#map').append("<img id='map_"+tempArray[0]+"_"+tempArray[1]+"' alt='loading...' src='maps/grid/"+tempArray[0]+"-"+tempArray[1]+".png' class='grid' style='top:"+(tempArray[0]*600-600)+";left:"+(tempArray[1]*600-600)+";' />");
		}
	}
	
	for(i=0; i<haveArray.length; i++){
		existID = false;
		for(j=0; j<needArray.length; j++){
			if(haveArray[i] == needArray[j]){
				existID = true;
			}
		}
		if(existID == false){
			$('#grid_'+haveArray[i]).innerHTML = ".";
		}
	}
	
	haveArray = needArray;

	return true;
	
}

/*
function updateCoords(e) {
	var posx = 0;
	var posy = 0;
	
	if (!e)
	    var e = window.event;
	
	if (e.pageX || e.pageY) {
	    posx = e.pageX;
	    posy = e.pageY;
	} else if (e.clientX || e.clientY) {
	    posx = e.clientX + document.body.scrollLeft;
	    posy = e.clientY + document.body.scrollTop;
	}
	posx2 = Math.round(((posx/90)-180)*1000)/1000
	posy2 = Math.round((((posy/90)-90) * -1)*1000)/1000
	
	document.getElementById('alert_div').innerHTML = 'LAT:'+posx2+" LONG:"+posy2;

}
*/

var dragobject={
	z: 0, x: 0, y: 0, offsetx : null, offsety : null, targetobj : null, dragapproved : 0,
	initialize:function(){
		document.onmousemove=dragobject.moveit
		document.onmousedown=dragobject.dragit
		document.onmouseup=function(){
			this.dragapproved = 0;
			document.body.className = 'hand';
			update_map();
		}
	},
	
	dragit:function(e){
		
		var evtobj=window.event? window.event : e
		
		if(busy == false){
			this.dragapproved=1
		}
		
		//winScroll = getScroll();
		offset_width = $(window).scrollLeft();
		offset_height = $(window).scrollTop();
		
		this.xScroll = offset_width;//winScroll[0]
		this.yScroll = offset_height;//winScroll[1]
		 
		this.x = evtobj.clientX;
		this.y = evtobj.clientY;
	
		if (evtobj.preventDefault)
		evtobj.preventDefault()
	},
	
	moveit:function(e){

		var evtobj=window.event? window.event : e
		//updateCoords(e);
		if (this.dragapproved==1){

			document.body.className = 'grab';
			
			xMove = (parseInt(this.xScroll)-parseInt(evtobj.clientX)+this.x);
			yMove = (parseInt(this.yScroll)-parseInt(evtobj.clientY)+this.y);
		
			// Moves mini-map
			//document.getElementById('mark').style.left = xMove / 100;
			//document.getElementById('mark').style.top = yMove / 100;
			
			window.scrollTo(xMove, yMove);
			
			return false
			
		}
	}
}

dragobject.initialize()
