var hotel_array = new Array();
var markers = new Array();
var old_array = new Array();
var start = 0;
var offset = Math.pow(2,(5 - map.getZoom()));

function marker_click(i) {
	markers[i].openInfoWindowHtml(hotel_array[i][4]);
	flag = 0;
}

function place_hotels(lat, lng, img, info, index, id) {
	var point = new GLatLng(lat, lng);
	var hotel_icon = new GIcon(icon);
	hotel_icon.image = img;
	var marker = new GMarker(point, hotel_icon);
	GEvent.addListener(marker, "click", function() {
		marker.openInfoWindowHtml(info);
		flag = 0;
	});
	GEvent.addListener(marker, "mouseup", function(){
		start = 0;
		update(id, marker.getPoint());
	});
	GEvent.addListener(marker, "mousedown", function(){
		start = 1;
		move(marker);
	});
	markers[index] = marker;
	map.addOverlay(marker, id);
}

function move(marker, id){
	GEvent.addListener(map, "mousemove", function(point){
		if (start == 1 && logged == 1){
			marker.setPoint(new GLatLng(point.y-offset,point.x)); 
		}
	});
}
/*function find_closest(num, array) {
	var lat;
	var lng;
	var tmp_arr;
	var dist_array = new Array();
	var hotels = new Array();
	var final = new Array();
	var a;
	var b;
	var dist;
	var flag = 0;
	var icons = new Array();
	for(i = 0; i < array.length; i++) {
		lat = array[i][0];
		lng = array[i][1];
		tmp_arr = array[i][2];
		a = Math.abs(center_point.lat() - (lat));
		b = Math.abs(center_point.lng() - (lng));
		dist = Math.sqrt(a*a + b*b);
		if(dist_array.length == num) {
			if(dist < dist_array[num-1]) {
				dist_array.pop();
				dist_array.push(dist);
			}
		}
		else
			dist_array.push(dist);
		hotels[dist] = tmp_arr;
		dist_array.sort(sortNumber);
	}
	for(i = 0; i < dist_array.length; i++) {
		final.push(hotels[dist_array[i]]);
	}
	return final;
}*/

function sendData(i){
	setQueryString(i);
	var url="hotels.php";
	httpRequest("POST",url,true, 1);
}

function update(id, point){
	setQuery(id, point);
	var url="marker.php";
	httpRequest("POST",url,true, 2);
}

function setQueryString(i){
	if(i == 0)
		queryString = "center_lat="+encodeURIComponent(center_point.lat())+"&center_lng="+encodeURIComponent(center_point.lng())+"&sw_lat="+encodeURIComponent(bounds.getSouthWest().lat())+"&sw_lng="+encodeURIComponent(bounds.getSouthWest().lng())+"&ne_lat="+encodeURIComponent(bounds.getNorthEast().lat())+"&ne_lng="+encodeURIComponent(bounds.getNorthEast().lng());
	else
		queryString = "center_lat="+encodeURIComponent(center_point.lat())+"&center_lng="+encodeURIComponent(center_point.lng())+"&sw_lat="+encodeURIComponent(bounds.getSouthWest().lat())+"&sw_lng="+encodeURIComponent(bounds.getSouthWest().lng())+"&ne_lat="+encodeURIComponent(bounds.getNorthEast().lat())+"&ne_lng="+encodeURIComponent(bounds.getNorthEast().lng())+"&rank="+encodeURIComponent(i);
}

function setQuery(id, point) {
	queryString = "id="+encodeURIComponent(id)+"&latitude="+encodeURIComponent(point.lat())+"&longitude="+encodeURIComponent(point.lng());
}
/* Wrapper function for constructing a request object.
 	Parameters:
	reqType: The HTTP request type, such as GET or POST.
	url: The URL of the server program.
	asynch: Whether to send the request asynchronously or not. */
  
function httpRequest(reqType,url,asynch,i){
	//Mozilla-based browsers
	if(window.XMLHttpRequest){
		request = new XMLHttpRequest(  );
	} 
	else if (window.ActiveXObject){
		request=new ActiveXObject("Msxml2.XMLHTTP");
		if (! request){
			request=new ActiveXObject("Microsoft.XMLHTTP");
		}
	}
	//the request could still be null if neither ActiveXObject
	//initialization succeeded
	if(request){
		initReq(reqType,url,asynch, i);
	} 
	else {
		alert("Your browser does not permit the use of all of this application's features!");
	}
}

/* Initialize a request object that is already constructed.
 Parameters:
 reqType: The HTTP request type, such as GET or POST.
 url: The URL of the server program.
 isAsynch: Whether to send the request asynchronously or not. */
function initReq(reqType,url,isAsynch, i){
	/* Specify the function that will handle the HTTP response */
	if(i == 1)
		request.onreadystatechange=handleJson;
	else if(i == 2)
		request.onreadystatechange=handleResponse;
	request.open(reqType,url,isAsynch);
	/* Set the Content-Type header for a POST request */
	request.setRequestHeader("Content-Type","application/x-www-form-urlencoded; charset=UTF-8");
	request.send(queryString);
}

//event handler for XMLHttpRequest
function handleResponse(  ){
	if(request.readyState == 4){
		if(request.status != 200){
			alert("A problem occurred with communicating between the XMLHttpRequest object and the server program.");
		}
	}//end outer if
}

function create_nav() {
	var nav_bar = "<table>";
	for(i = 0; i < hotel_array.length; i++) {
		var image = new Image();
		image.src = ""+hotel_array[i][2];
		place_hotels(hotel_array[i][0], hotel_array[i][1], hotel_array[i][3], hotel_array[i][4], i, hotel_array[i][6]);
		nav_bar += hotel_array[i][5];
	}
	nav_bar += "</table>";
	document.getElementById('nav_bar').innerHTML = nav_bar;
}

function handleJson(  ){
	if(request.readyState == 4){
		if(request.status == 200){
			var resp =  request.responseText;
			hotel_array = eval(resp);
			create_nav();
			old_array = hotel_array;
		} 
		else {
			alert("A problem occurred with communicating between the XMLHttpRequest object and the server program."+request.status);
		}
	}//end outer if
}

/*function in_bounds(point_lat, point_lng, b1_lat, b1_lng, b2_lat, b2_lng) {
	if(Math.max(point_lat,b1_lat) == point_lat && Math.min(point_lat,b2_lat) == point_lat && point_lng >= b1_lng && point_lng <= b2_lng)
		return true;
	else
		return false;
}

function leave_inbound() {
	var tmp_array = new Array();
	var another_array = new Array();
	var offset = 0;
	var flag = 1;
	if(old_array.length != 0) {
		var b1_lat = hotel_array[hotel_array.length-1][0];
		var b1_lng = hotel_array[hotel_array.length-1][1];
		var b2_lat = hotel_array[hotel_array.length-1][2];
		var b2_lng = hotel_array[hotel_array.length-1][3];
		for(i = 0; i < old_array.length-1; i++) {
			if(in_bounds(old_array[i][0], old_array[i][1], b1_lat, b1_lng, b2_lat, b2_lng)) {
				tmp_array[i] = old_array[i];
			}
		}		
		for(i = 0; i < tmp_array.length; i++) {
			for(j = 0; j < hotel_array.length - 1; j++) {
				if(tmp_array[i][2] == hotel_array[j][2])
					hotel_array[j] = "";
			}
		}
			
				another_array[i-offset] = tmp_array[i-offset];
				offset++;;
			}
			else {
				for(j = 0; j < tmp_array.length; j++) {
					if(tmp_array[j] == hotel_array[i-offset]) {
						flag = 0;
						break;
					}
				}
				if(flag) {
					another_array[i-offset] = hotel_array[i-offset];
				}
			}
		}
		alert(another_array);
		//return(another_array);
	}
	/*else
		return(hotel_array);
}*/
