// JScript File
var mapWidth = 800;
var mapHeight = 500;
var search;

// This function performs a GET Web request.
function getItems()
{
    // Instantiate a WebRequest.
    var wRequest = new Sys.Net.WebRequest();

    var zoomLevel = map.GetZoomLevel();
    var tags = queryString("tags");
    var creator = queryString("creator");
    if(typeof(search) == "undefined" || search == "" || search == null) 
	search = queryString("search");	
    
    var rectangle = map.GetMapView();
    var topLeft = rectangle.TopLeftLatLong;
    
    // Bottom Right is inaccurate so we need ot calculate it.
    // To do this, convert top left to pixels, then add 800 and 500 then convert back
    //var bottomRight = rectangle.BottomRightLatLong;
    
    //RJP -Shouldnt hard code size of map as it should be style based. Cant do the folllowing coz the map div isnt sized yet as the code is poorly written and loaded
    //mapWidth = document.getElementById("myMap").pixelWidth;
    //mapHeight = document.getElementById("myMap").pixelHeight;
    
    var topLeftPixel = map.LatLongToPixel(map.GetCenter());
    var bottomRightXPixel = mapWidth;//topLeftPixel.x + mapHeight;
    var bottomRightYPixel = mapHeight;//topLeftPixel.y + mapWidth;
    //var bottomRight = map.PixelToLatLong(bottomRightXPixel, bottomRightYPixel);    
    // V5
    var bottomRight = map.PixelToLatLong(new VEPixel(bottomRightXPixel, bottomRightYPixel));    

    
    var url = "";
    
    url = url + "json/items.aspx?max=40"
    
        if(queryString("filterbylocation") != "f")
        {
	        url = url + "&topLeftLat=" + topLeft.Latitude + "&topLeftLong=" + topLeft.Longitude;
	        url = url + "&bottomRightLat=" + bottomRight.Latitude + "&bottomRightLong=" + bottomRight.Longitude;
	    }
    if(tags != "false")
        url = url + "&tags=" + tags;
    url = url + "&zoom=" + zoomLevel;
    if(creator != "false")
        url = url + "&creator=" + creator;
    if(search != "false")
        url = url + "&search=" + search;

    //window.location = url;
    // Set the request URL.      
    wRequest.set_url(url);

    // Set the request verb.
    wRequest.set_httpVerb("GET");
           
    // Set the request callback function.
    wRequest.add_completed(OnWebRequestCompleted);
 
    // Execute the request.
    wRequest.invoke();    
}

function getItem(itemid)
{
    // Instantiate a WebRequest.
    var wRequest = new Sys.Net.WebRequest();

    var url = "json/items.aspx?itemid=" + itemid;

    //window.location = url;
    // Set the request URL.      
    wRequest.set_url(url);

    // Set the request verb.
    wRequest.set_httpVerb("GET");
           
    // Set the request callback function.
    wRequest.add_completed(OnWebRequestCompleted);
 
    // Execute the request.
    wRequest.invoke();    
}

function PageQuery(q) {
	if(q.length > 1) this.q = q.substring(1, q.length);
	else this.q = null;
	this.keyValuePairs = new Array();
	if(q) {
		for(var i=0; i < this.q.split("&").length; i++) {
			this.keyValuePairs[i] = this.q.split("&")[i];
		}
	}
	this.getKeyValuePairs = function() { return this.keyValuePairs; }
	this.getValue = function(s) {
		for(var j=0; j < this.keyValuePairs.length; j++) {
			if(this.keyValuePairs[j].split("=")[0] == s)
				return this.keyValuePairs[j].split("=")[1];
		}
		return false;
	}
	this.getParameters = function() {
		var a = new Array(this.getLength());
		for(var j=0; j < this.keyValuePairs.length; j++) {
			a[j] = this.keyValuePairs[j].split("=")[0];
		}
		return a;
	}
	this.getLength = function() { return this.keyValuePairs.length; }	
}
function queryString(key){
var page = new PageQuery(window.location.search); 
return unescape(page.getValue(key)); 
}

// This callback function processes the request return values. It is called asynchronously by the current executor.
function OnWebRequestCompleted(executor, eventArgs) 
{    
    if(executor.get_responseAvailable()) 
    {
            mapitems = eval('(' + executor.get_responseData() +')');
            ItemsReceived(mapitems);
    }
}
if (typeof(Sys) !== "undefined") Sys.Application.notifyScriptLoaded();

