var ShoppingLocation = new Array () ;
var ShoppingCenterClick = false;

function Shopping_GetDataList() {

    check_streetview(); // in Streetview.js

	var TL = GM_ConvertPxToDeg(GM_Offset_Left,GM_Offset_Top);
	var BR = GM_ConvertPxToDeg(GM_Offset_Right,GM_Offset_Bottom);
	Shopping_Top = TL.Y ;
	Shopping_Bottom = BR.Y ;
	Shopping_Left = TL.X ;
	Shopping_Right = BR.X ;
	var URL = "/CommonLibrary/AU_Shopping.php?level="+GM_Level+"&t="+encodeURI(Shopping_Top)+"&l="+encodeURI(Shopping_Left)+"&r="+encodeURI(Shopping_Right)+"&b="+encodeURI(Shopping_Bottom)+"&Ref=" + encodeURI(Math.round(Math.random(0,1)*100000000000)) ;
	Shopping_Global_Request("ListData",URL);
}

function Shopping_Global_Request(ID,url) {
	var url_post = url;
	var url_send = null;
	if (url.indexOf('?')>0)
	{
		url_post = url.substring(0,url.indexOf('?'));
		url_send = url.substring(url.indexOf('?')+1);
	} else {
		url_post = url ;
		url_send = null ;
	}

	var http_request = false;

	if (window.XMLHttpRequest) { // Mozilla, Safari,...
		http_request = new XMLHttpRequest();
		if (http_request.overrideMimeType) {
			http_request.overrideMimeType('text/xml');
			// See note below about this line
		}
	} else if (window.ActiveXObject) { // IE
		try {
			http_request = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
				http_request = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) {}
		}
	}

	if (!http_request) {
		alert('Giving up :( Cannot create an XMLHTTP instance');
		return false;
	}
	http_request.onreadystatechange = function() { Shopping_Global_RequestReady(ID,http_request) }
	if (url_send != null)
	{
		http_request.open('POST', url_post, true);
		http_request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		http_request.setRequestHeader("Content-length", url_send.length);
		http_request.setRequestHeader("Connection", "close");
		http_request.send(url_send);
	} else {
		http_request.open('GET', url_post, true);
		http_request.send(url_send);
	}
}


function Shopping_Global_RequestReady(ID,http_request2) {
 try
 {
	if (http_request2.readyState == 4) {
		if (http_request2.status == 200) {
			Shopping_RequestDone(ID,http_request2) ;
		} else {
			//alert('There was a problem with the data request. please refresh the page.');
		}
	}
 }catch(e) {}
}


function Shopping_RequestDone(ID,http_request3) {
	var Data = Trim(http_request3.responseText) ;
	if (Data)
	{
		var Reg = /^DetailData_/ ;
		if (ID == 'ListData') {
		    Shopping_SaveDataList(Data);
		}
	}
}

function Shopping_SaveDataList(Data) {
    var D = Data.split("\n");
    Shopping_Total = D[0];
    ShoppingLocation=new Array();

    var temp = '';
	var addrTemp = '';

    if (Shopping_Total>0)
    {
    	for (i=1;i<D.length;i++)
    	{
    	    var T = D[i].split("\t");
    	    var ID = T[0];
    	    var Lon = T[1];
    	    var Lat = T[2];
    	    var MallName = T[3];
    	    var iconName = T[4];

      	    if (!ShoppingLocation[ID])
      	    {
          		var PX = GM_ConvertDegToPx(Lon, Lat);
          		    ShoppingLocation[ID] = new NewShoppingLocation(ID) ;
          		    ShoppingLocation[ID].X = PX.X ;
          		    ShoppingLocation[ID].Y = PX.Y ;
          		    ShoppingLocation[ID].Long = Lon;
          		    ShoppingLocation[ID].Lat  = Lat;
          		    ShoppingLocation[ID].MallName  = MallName;
          		    ShoppingLocation[ID].iconName  = iconName;
      	    }
    	 }
		ViewShoppingCenterPoints();		 
    }
}

function NewShoppingLocation(ID) {
	this.ID   = ID ;
	this.X    = 0;
	this.Y    = 0;
	this.Long = 0.00;
	this.Lat  = 0.00;
	this.MallName  = "";
	this.iconName  = "";

}


function ViewShoppingCenterPoints()
{
    var WL = document.getElementById('GM_DrawingLayer12');
    WL.innerHTML = "" ;
	
		
    for (var i in ShoppingLocation)
    {		
		var W = ShoppingLocation[i] ;
		var Img = document.createElement('img');
		Img.id     = 'GM_Shopping_' + W.ID ;
		Img.name   = Img.id ;
		Img.src = "images/" + W.iconName;
		Img.LX     = W.X ;
		Img.LY     = W.Y ;
		Img.Long   = W.Long;
		Img.Lat    = W.Lat;
		Img.MyID   = W.ID ;
		Img.style.position = 'absolute' ;
		Img.style.cursor = 'pointer' ;
		Img.alt = (W.MallName)?W.MallName:'Click To View Detail' ;
		Img.title = Img.alt;
		
		Img.style.top = Img.LY - Math.round(parseInt(Img.height)) - GM_YCenter + 'px';
		Img.style.left = Img.LX - Math.round(parseInt(Img.width)/3) - GM_XCenter + 'px';

		Img.onload = function() {
			this.style.top = this.LY - Math.round(parseInt(this.height)) - GM_YCenter + 'px';
			this.style.left = this.LX - Math.round(parseInt(this.width)/3) - GM_XCenter + 'px';
		}

      	Img.onclick = function() {
          if (GM_StreetviewPoint) {
      			Streetview_Close();
      			showFacing('still');
      			if ((!LoadedStreetview))
      			{
/*					StreetviewLocation[this.MyID] = new NewStreetviewLocation(this.MyID);
					StreetviewLocation[this.MyID].X = ShoppingLocation[this.MyID].X ;
					StreetviewLocation[this.MyID].Y = ShoppingLocation[this.MyID].Y ;
					StreetviewLocation[this.MyID].Long = ShoppingLocation[this.MyID].Long;
					StreetviewLocation[this.MyID].Lat  = ShoppingLocation[this.MyID].Lat;					
*/      			    Streetview_MatchPos =0;
					ShoppingCenterClick = true;
      			    shoppingPointOnclick(this.MyID);
      			    setTimeout('Streetview_OpenDialogBox('+this.MyID+')', 800);
      			}
      		} else {
      			PanChangeLevelStreetView(this.MyID);
      		}
    	}
		
		WL.appendChild(Img);
		Img = null ;
	}
}

function shoppingPointOnclick(ID) {
    if (ShoppingLocation[ID])
    {
		var W = ShoppingLocation[ID] ;
		var tmpPixel = GM_ConvertDegToPx(W.Long,W.Lat) ;
		var tmpLonglat = GM_ConvertPxToDeg(tmpPixel.X,parseInt(tmpPixel.Y-200));
		SV_FocusId   = W.ID;
		SV_FocusLong = W.Long;
		SV_FocusLat  = W.Lat;
		GM_RecenterDeg(tmpLonglat.X,tmpLonglat.Y,1);
		SV_updateCenterViewIconByLongLat(W.Long, W.Lat);
		Streetview_MatchLoc = W.ID ;
    } else {
		SV_FocusId = ID;
    }
}



