var AjaxServer = "";
var AjaxProcess = 0;

var url = document.location.href;
var regex = eval("/^(http(s)*)(:\\/\\/)(192.168.0.?)/");
var mapServer = (regex.test(url))?"192.168.0.15":"210.50.2.214";
var ajaxServer = (regex.test(url))?"192.168.0.5":"www.street-directory.com.au";
var imageURL = "http://"+ajaxServer+"/accommodation/genmap/";

function genMap (divName)
{
	this.BASE_MAP_URL = "http://"+mapServer+"/map/";
	this.BASE_AJAX_URL = "http://"+ajaxServer+"/accommodation/genmap/ws/";
	AjaxServer = this.BASE_AJAX_URL;
	this.divName = divName ;
	//this.longitude = 144.985 ;
	//this.latitude =  -37.81 ;
	//this.longitude = 145.00157809991;
	//this.latitude  = -37.815988373535816;
	this.longitude = 145.104412 ;
	this.latitude =  -37.850283 ;
	this.MaxLevel = 15;
	this.level = 5 ;
	this.width = 400 ;
	this.height = 400 ;
	this.top = 0 ;
	this.left = 0 ;
	this.panableFlag = true ;
	this.viewType = "normal" ;
	this.ausway = 0; // add by Bui Koh
	this.imageAlt = "";

	this.scale = 0 ;
	this.xCenter = 0 ;
	this.yCenter = 0 ;
	this.xStart = 0 ;
	this.yStart = 0 ;
	this.xCurr = 0 ;
	this.yCurr = 0 ;
	this.xNext = 0 ;
	this.yNext = 0 ;
	this.imgWidth = 200 ;
	this.imgHeight = 200 ;
	this.borderLeft = "" ;
	this.borderTop = "" ;
	this.borderRight = "" ;
	this.borderBottom = "" ;
	this.mouseType = "normal" ;
	this.serverHost = "210.50.2.214" ;
//	this.errorImg = "http://"+ajaxServer+"/streetdirectory/resources/default/images/error.gif" ;	
	this.errorImg = "http://www.street-directory.com.au/images/error.gif" ;	
	this.myCommand = "" ;
	this.xRuler = 0 ;
	this.yRuler = 0 ;
	this.xxRuler = 0 ;
	this.yyRuler = 0 ;
	this.flagRuler = 0 ;
	this.genmapClickAction = 1;
	this.ObjClickTarget = '';
	this.flagAnim = 0;
	this.flagAusway = 0 ;
//	this.browser   = document.getElementById&&!document.all;
    this.browser   = window.stop;
	
	this.MAP_File_Type = 'r';
	this.MAP_Source    = 'psma';
	this.MAP_Country   = 'au';
	
	this.LoadedMap = new Array();
	this.LoadedStreetViewMap = new Array();


	// nilai2 default
	this.longMin = 106.720409792149 ;	
	this.longMax = 161.742073361529 ;
	this.latMin = - 44.7196259244811 ;
	this.latMax = - 8.75833753905317 ;
/*
	this.totWidth = new Array ()
	this.totWidth[1] = 3083 ;
	this.totWidth[2] = 16834 ;
	this.totWidth[3] = 42087 ;
	this.totWidth[4] = 173318 ;
	this.totWidth[5] = 1083239 ;
	this.totWidth[6] = 2166478 ;
	this.totWidth[7] = 4334014 ;

	this.totHeight = new Array ()
	this.totHeight[0] = 0 ;
	this.totHeight[1] = 2016 ;
	this.totHeight[2] = 11004 ;
	this.totHeight[3] = 27508 ;
	this.totHeight[4] = 113500 ;
	this.totHeight[5] = 708000 ;
	this.totHeight[6] = 1415976 ;
	this.totHeight[7] = 2833000 ;
	this.totHeight[8] = 5665286 ;
	this.totHeight[9] = 10274654 ;		
*/	

   this.totWidth = new Array();
   this.totWidth[1] = 525;
   this.totWidth[2] = 1050;
   this.totWidth[3] = 2115;
   this.totWidth[4] = 4230;
   this.totWidth[5] = 8460;
   this.totWidth[6] = 16925;
   this.totWidth[7] = 33850;
   this.totWidth[8] = 67700;
   this.totWidth[9] = 135405;
   this.totWidth[10] = 270810;
   this.totWidth[11] = 541620;
   this.totWidth[12] = 1083239;
   this.totWidth[13] = 2166478;
   this.totWidth[14] = 4334014;
   this.totWidth[15] = 8668028;
   this.totWidth[16] = 15720475;
   this.totWidth[17] = 31440950;
   
   
   this.totHeight = new Array();
   this.totHeight[0] = 0;
   this.totHeight[1] = 343;
   this.totHeight[2] = 686;
   this.totHeight[3] = 1382;
   this.totHeight[4] = 2765;
   this.totHeight[5] = 5529;
   this.totHeight[6] = 11062;
   this.totHeight[7] = 22125;
   this.totHeight[8] = 44250;
   this.totHeight[9] = 88500;
   this.totHeight[10] = 177000;
   this.totHeight[11] = 354000;
   this.totHeight[12] = 708000;
   this.totHeight[13] = 1416000;
   this.totHeight[14] = 2832000;
   this.totHeight[15] = 5664000;
   this.totHeight[16] = 11328000;
   this.totHeight[17] = 22656000;
   //

   /* Nilai dari level 7 dan keatas adalah hasil perhitungan magic (masih perlu diganti kalo tidak pas) */
      		
}

function GM_Point()
{
  this.Long = 0 ;
	this.Lat  = 0 ;
	this.X    = 0 ;
	this.Y    = 0 ;
	this.Dist = 0 ;
}

/*------------------------------------------------- SETTER -------------------------------------------------------*/
genMap.prototype.setBaseMapURL = function(url)
{
	this.BASE_MAP_URL = url;
}

genMap.prototype.setBaseAjaxURL = function(url)
{
	this.BASE_AJAX_URL = url;
	AjaxServer = this.BASE_AJAX_URL;
}

genMap.prototype.setLongLat = function(longitude, latitude)
{
	this.longitude = longitude ;
	this.latitude = latitude ;
}

genMap.prototype.setImageAlt = function(imageAlt)
{
	this.imageAlt = imageAlt ;
}

genMap.prototype.setLevel = function(level)
{
	this.level = level ;
}

genMap.prototype.setDimension = function(width, height)
{
	this.width = width ;
	this.height = height ;
}

genMap.prototype.setPanableFlag = function(panableFlag)
{
	this.panableFlag = panableFlag ;
}

genMap.prototype.setViewType = function(viewType)
{
	this.viewType = viewType ;
}

genMap.prototype.setServerHost = function(serverHost)
{
	this.serverHost = serverHost ;
}

genMap.prototype.setBorderLeft = function(borderLeft)
{
	this.borderLeft = borderLeft ;
}

genMap.prototype.setBorderTop = function(borderTop)
{
	this.borderTop = borderTop ;
}

genMap.prototype.setBorderRight = function(borderRight)
{
	this.borderRight = borderRight ;
}

genMap.prototype.setBorderBottom = function(borderBottom)
{
	this.borderBottom = borderBottom ;
}

genMap.prototype.setMouseType = function(mouseType)
{
  this.mouseType = mouseType ;    
  this.setMouseFunction () ;
}

/*------------------------------------------------- GETTER -------------------------------------------------------*/
genMap.prototype.getLong = function()
{
	return this.longitude ;
}

genMap.prototype.getLat = function()
{
	return this.latitude ;
}

genMap.prototype.getLevel = function()
{
	return this.level ;
}

genMap.prototype.getWidth = function()
{
	return this.width ;
}

genMap.prototype.getHeight = function()
{
	return this.height ;
}

genMap.prototype.getPanableFlag = function()
{
	return this.panableFlag ;
}

genMap.prototype.getViewType = function()
{
	return this.viewType ;
}

genMap.prototype.getServerHost = function()
{
	return this.serverHost ;
}

genMap.prototype.getCurrLong = function(xc,yc)
{
	if(xc)
	{
		var P = this.changeToLongLat(xc , yc) ;
	}
	else
	{
		var P = this.changeToLongLat(this.xCenter , this.yCenter) ;
	}
	return P[0] ;
}

genMap.prototype.getCurrLat = function(xc,yc)
{
	if(xc)
	{
		var P = this.changeToLongLat(xc , yc) ;
	}
	else
	{
		var P = this.changeToLongLat(this.xCenter , this.yCenter) ;
	}
	return P[1] ;
}

genMap.prototype.getMouseType = function()
{
	return this.mouseType ;
}

/*--------------------------------------------------MAP-------------------------------------------------------*/

genMap.prototype.changeToPixel = function (lon, lat)	// function untuk mengubah longlat n kembaliin dalam bentuk pixel
{
	this.scale = this.totWidth[this.level] / (this.longMax - this.longMin);	// mendapatkan skala
	var pixX = Math.round( this.scale * (lon - this.longMin) ) ;	// mendapatkan x dalam pixel
	var pixY = Math.round( this.scale * (this.latMax - lat) ) ;		// mendapatkan y dalam pixel
	return new Array (pixX,pixY) ;	
}

genMap.prototype.initCenter = function ()	// function menentukan xcenter 'n ycenter awal dalam map(pixel)
{
	var P = this.changeToPixel(this.longitude, this.latitude) ;
	this.xCenter = P[0] ;
	this.yCenter = P[1] ;	
	this.xStart = P[0] ;
	this.yStart = P[1] ;
	for (var i in this.LoadedMap)
	{
		this.LoadedMap[i] = 0 ;
	}
	for (var i in this.LoadedStreetViewMap)
	{
    this.LoadedStreetViewMap[i] = 0;
  }
}

genMap.prototype.changeToLongLat = function (pixX, pixY)	// function untuk mengubah pixel n kembaliin dalam bentuk longlat
{
	this.scale = this.totWidth[this.level] / (this.longMax - this.longMin);	// mendapatkan skala
	lon = (pixX/this.scale) + this.longMin ;	// mendapatkan x dalam longitude
	lat = this.latMax - (pixY/this.scale) ;		// mendapatkan y dalam latitude
	return new Array (lon, lat) ;		
}

genMap.prototype.setCanvas = function ()	// set canvas untuk div di depan dan buat div untuk map
{
	var div = document.getElementById(this.divName) ;
		
	var canvasDiv = document.getElementById("canvasDiv_" + this.divName) ;	
	if (!document.getElementById("canvasDiv_" + this.divName))
	{
		var canvasDiv = document.createElement("div") ;
		canvasDiv.id = "canvasDiv_" + this.divName ;
		canvasDiv.style.zIndex = 1 ;

		div.appendChild(canvasDiv) ;
	}	
	canvasDiv.innerHTML = "" ;
	canvasDiv.style.position = 'relative' ;
	canvasDiv.style.overflow = 'hidden' ;
	canvasDiv.style.left = parseInt(this.left) + 'px' ;	
	canvasDiv.style.top = parseInt(this.top) + 'px' ;	
	canvasDiv.style.width = parseInt(this.width) + 'px' ;	
	canvasDiv.style.height = parseInt(this.height) + 'px' ;	
	canvasDiv.style.borderLeft = this.borderLeft ;	   	
	canvasDiv.style.borderTop = this.borderTop ;	   	
	canvasDiv.style.borderRight = this.borderRight ;	   	
	canvasDiv.style.borderBottom = this.borderBottom ;	
	canvasDiv.align = "center" ;

	
	var mapDiv = document.getElementById("mapDiv_" + this.divName) ;
	if (!document.getElementById("mapDiv_" + this.divName))
	{
		var mapDiv = document.createElement("div") ;
		mapDiv.id = "mapDiv_" + this.divName ;		
		canvasDiv.appendChild(mapDiv) ;
	}
	mapDiv.style.left = 0 + 'px' ;
	mapDiv.style.top = 0 + 'px' ;
	mapDiv.obj = this ;	
	mapDiv.style.position = 'absolute' ;
	
	for (var i = 1; i<=10; i++)
	{
		var GM_DrawingLayer = document.createElement('div') ;
		GM_DrawingLayer.id = 'GM_DrawingLayer' + i ;
		GM_DrawingLayer.name = 'GM_DrawingLayer' + i ;
		GM_DrawingLayer.style.position = 'absolute' ;
		GM_DrawingLayer.style.zIndex = 9999990 + i ;
		GM_DrawingLayer.style.visibility = 'visible';
		mapDiv.appendChild(GM_DrawingLayer);
	}
  	
	if (!document.getElementById("mapDivNormal_" + this.divName))
	{
		var mapDivN = document.createElement("div") ;
		mapDivN.id = "mapDivNormal_" + this.divName ;		// create id unik, sesuai nama divcanvas
		mapDiv.appendChild(mapDivN) ;
	}
	var mapDivN = document.getElementById("mapDivNormal_" + this.divName) ;
	mapDivN.innerHTML = "" ;
	mapDivN.style.left = 0 + 'px' ;
	mapDivN.style.top = 0 + 'px' ;
	mapDivN.obj = this ;
	mapDivN.style.visibility = 'hidden' ;
	mapDivN.style.position = 'absolute' ;
	
	if (!document.getElementById("mapDivSatelite_" + this.divName))
	{
		var mapDivS = document.createElement("div") ;
		mapDivS.id = "mapDivSatelite_" + this.divName ;		// create id unik, sesuai nama divcanvas
		mapDiv.appendChild(mapDivS) ;
	}
	var mapDivS = document.getElementById("mapDivSatelite_" + this.divName) ;
	mapDivS.innerHTML = "" ;
	mapDivS.style.left = 0 + 'px' ;
	mapDivS.style.top = 0 + 'px' ;
//	mapDivS.obj = this ;
	mapDivS.obj = null ;
	mapDivS.style.visibility = 'hidden' ;
	mapDivS.style.position = 'absolute' ;
	
	if (!document.getElementById("mapDivHybrid_" + this.divName))
	{
		var mapDivH = document.createElement("div") ;
		mapDivH.id = "mapDivHybrid_" + this.divName ;		// create id unik, sesuai nama divcanvas
		mapDiv.appendChild(mapDivH) ;
	}
	var mapDivH = document.getElementById("mapDivHybrid_" + this.divName) ;
	mapDivH.innerHTML = "" ;
	mapDivH.style.left = 0 + 'px' ;
	mapDivH.style.top = 0 + 'px' ;
//	mapDivH.obj = this ;
	mapDivH.obj = null ;
  mapDivH.style.visibility = 'hidden' ;
	mapDivH.style.position = 'absolute' ;		

	if (!document.getElementById("mapDivAusway_" + this.divName))
	{
		var mapDivA = document.createElement("div") ;
		mapDivA.id = "mapDivAusway_" + this.divName ;		// create id unik, sesuai nama divcanvas
		mapDiv.appendChild(mapDivA) ;
	}
	var mapDivA = document.getElementById("mapDivAusway_" + this.divName) ;
	mapDivA.innerHTML = "" ;
	mapDivA.style.left = 0 + 'px' ;
	mapDivA.style.top = 0 + 'px' ;
//	mapDivA.obj = this ;
	mapDivA.obj = null ;
	mapDivA.style.visibility = 'hidden' ;
	mapDivA.style.position = 'absolute' ;	
	
	this.setMouseFunction () ;	
}

genMap.prototype.setMouseFunction = function ()
{  
  //alert (this.mouseType) ;  
  var mapDiv = document.getElementById("mapDiv_" + this.divName) ;
  
  switch(this.mouseType)
  { //url("images/cursor-hand.gif");
    case "normal"   : //skinableCursor.init(mapDiv,'images/cursor-hand.gif', 'normalCur');  break;// 
//    mapDiv.style.cursor = 'url(images/cursor-hand.gif),url(images/cursor-hand.cur),auto;'; break;
//	mapDiv.style.cursor = (window.stop)?"-moz-grab":"/CommonLibrary/move.cur" ;
	mapDiv.style.cursor = (window.stop)?"-moz-grab":"" ;
    case "ruler"    : mapDiv.style.cursor = 'url(http://www.street-directory.com.au/accommodation/genmap/images/cursor-selection.gif),url(http://www.street-directory.com.au/accommodation/genmap/images/cursor-selection.cur),auto;'; break;
    case "zoom_in"  : mapDiv.style.cursor = 'url(http://www.street-directory.com.au/accommodation/genmap/images/cursor-zoom_in.gif),url(http://www.street-directory.com.au/accommodation/genmap/images/cursor-zoom_in.cur),auto;'; break;
    case "zoom_out" : mapDiv.style.cursor = 'url(http://www.street-directory.com.au/accommodation/genmap/images/cursor-zoom_out.gif),url(http://www.street-directory.com.au/accommodation/genmap/images/cursor-zoom_out.cur),auto;'; break;
    case "distance" : mapDiv.style.cursor = 'url(http://www.street-directory.com.au/accommodation/genmap/images/cursor-ruler.gif),url(http://www.street-directory.com.au/accommodation/genmap/images/cursor-ruler.cur),auto;'; break;
  }
  
  
  
  if (this.mouseType == "normal")
  {    
    if (this.panableFlag == true)
	  {	    
  		mapDiv.onmousedown = this.startDrag ;
  		mapDiv.onmousemove = this.moveDrag ;
  		mapDiv.onmouseup = this.stopDrag ;
  		//mapDiv.onmouseout = this.stopDrag ;	
	  }
  }
  else if (this.mouseType == "zoom_in" || this.mouseType == "zoom_out")
  {    
		mapDiv.onmousedown = this.startRuler ;
		mapDiv.onmousemove = this.moveRuler ;
		mapDiv.onmouseup = this.stopRuler ;	
		if ( this.mouseType == "zoom_in" || this.mouseType == "zoom_out" )
    {
       mapDiv.onclick  = this.zoomProcess ;
    } else {
       mapDiv.onclick = "";
    }		  
  }
  else if (this.mouseType == "none")
  {    
		mapDiv.onmousedown = "" ;
		mapDiv.onmousemove = "" ; 
		mapDiv.onmouseup = "" ; 	  
  }
}

var RulerObj;
var RulerX1,RulerX2,RulerY1,RulerY2 ;
var OS;

function NewOffSet() {
	this.left = '' ;
	this.top = '' ;
}

function ShowOffSet(ID) {
	var left = 0 ;
	var top = 0 ;
	var NewID = document.getElementById(ID);
	left = 0 ;
	top = 0 ;
	while (NewID.offsetParent) {
		top += parseInt(NewID.offsetTop) ;
		left += parseInt(NewID.offsetLeft) ;
		NewID = NewID.offsetParent ;
	}
	var NewObj = new NewOffSet();
	NewObj.left = left ; 
	NewObj.top = top ; 
	return NewObj ;
}

genMap.prototype.zoomProcess = function (mouseEvent)
{
    if (!mouseEvent) mouseEvent = window.event;
    if ((mouseEvent.button == 0) || (mouseEvent.button == 1)){
      this.obj.xRuler = parseInt(mouseEvent.clientX,10);
    	this.obj.yRuler = parseInt(mouseEvent.clientY,10);
    	RulerObj = document.getElementById('mapDiv_' + this.obj.divName);
    	OS = ShowOffSet('canvasDiv_' + this.obj.divName);
    	var aa = getBodyScroll() ;
    	RulerX1 = this.obj.xRuler - parseInt(RulerObj.style.left+0,10) - parseInt(OS.left+0,10) +  aa[0];
    	RulerY1 = this.obj.yRuler - parseInt(RulerObj.style.top+0,10) - parseInt(OS.top+0,10) + aa[1];
    	var posClick  = new GM_Point();
    	posClick.X    = RulerX1 + parseInt(this.obj.xStart);
      posClick.Y    = RulerY1 + parseInt(this.obj.yStart);
      var temp      = this.obj.changeToLongLat(posClick.X, posClick.Y)
      posClick.Long = temp[0];
      posClick.Lat  = temp[1];       
      var LVL = this.obj.level;       
      if (this.obj.getMouseType() == "zoom_in" && this.obj.level < this.obj.MaxLevel) 
      {
        LVL = LVL + 1;
        this.obj.changeLevel(LVL, posClick.Long, posClick.Lat);
      } else if ( this.obj.getMouseType() == "zoom_out" && this.obj.level > 1 )
      {
        LVL = LVL - 1;
        this.obj.changeLevel(LVL, posClick.Long, posClick.Lat);      
      }      
    }
}

genMap.prototype.startRuler = function (mouseEvent)
{  
    if (!mouseEvent) mouseEvent = window.event;
    if ((mouseEvent.button == 0) || (mouseEvent.button == 1)){
    	if (AjaxProcess > 0) return;
    	this.obj.flagRuler = 1 ;
    	this.obj.xRuler = parseInt(mouseEvent.clientX,10);
    	this.obj.yRuler = parseInt(mouseEvent.clientY,10);

    	RulerObj = document.getElementById('mapDiv_' + this.obj.divName);
    	OS = ShowOffSet('canvasDiv_' + this.obj.divName);
    	var aa = getBodyScroll() ;
    	RulerX1 = this.obj.xRuler - parseInt(RulerObj.style.left+0,10) - parseInt(OS.left+0,10) +  aa[0];
    	RulerY1 = this.obj.yRuler - parseInt(RulerObj.style.top+0,10) - parseInt(OS.top+0,10) + aa[1];
    	return false ;  
    }   
}

var TLRuler = 0 ;
var BRRuler = 0 ;
var CCRuler = 0 ;

genMap.prototype.moveRuler = function (mouseEvent)
{
    if (!mouseEvent) mouseEvent = window.event;
    if ((mouseEvent.button == 0) || (mouseEvent.button == 1)){
    	if (AjaxProcess > 0) return;
	    this.obj.xxRuler = parseInt(mouseEvent.clientX,10);
    	this.obj.yyRuler = parseInt(mouseEvent.clientY,10);                              

    	if(this.obj.flagRuler == 1)
    	{ 
      	    LayerDraw.clear();
      	    var aa = getBodyScroll() ;      
      	    RulerX2 = this.obj.xxRuler - parseInt(RulerObj.style.left+0,10) - parseInt(OS.left+0,10) +  aa[0];
      	    RulerY2 = this.obj.yyRuler - parseInt(RulerObj.style.top+0,10) - parseInt(OS.top+0,10) + aa[1];
      /*
      GM_SetLine(LayerDraw, 2, "#CC0066", RulerX1,RulerY1,RulerX2,RulerY1 );  //ke kanan atas
      GM_SetLine(LayerDraw, 2, "#CC0066", RulerX1,RulerY1,RulerX1,RulerY2 );  //ke  bawah kiri
      GM_SetLine(LayerDraw, 2, "#CC0066", RulerX1,RulerY2,RulerX2,RulerY2 ); //ke kanan bawah
      GM_SetLine(LayerDraw, 2, "#CC0066", RulerX2,RulerY1,RulerX2,RulerY2); // ke bawah kanan
      */
                 
      	    if (!document.getElementById("Ruler_Div"))
      	    {
            	var pdiv = document.getElementById("mapDiv_" + this.obj.divName) ;
            	var img = document.createElement("img") ;
            	img.src = imageURL + "images/b.gif" ;
            	img.id = "RulerImg" ;
            	img.style.opacity = 0.7 ;                        
            	var div = document.createElement ("div") ;                    
            	div.id = "Ruler_Div" ;
            	div.style.position = "absolute" ;
            	div.style.zIndex = 9999 ;            
            	div.appendChild(img) ;
            	div.style.filter = "alpha(opacity=70)" ;                           
            	pdiv.appendChild(div) ;            
      	    }
      	    var div = document.getElementById("Ruler_Div") ;
      	    var img = document.getElementById("RulerImg") ;          
      
      	    div.style.border = "dashed 1px" ;
      	    div.style.visibility = 'visible';
      
      	    if (this.obj.yyRuler > this.obj.yRuler)
      	    {
        	var top = RulerY1 ;
        	var h = RulerY2 - RulerY1 ;
      	    }
      	    else
      	    {
        	var top = RulerY2 ;
        	var h = RulerY1 - RulerY2 ;
      	    }
         
      	    if (this.obj.xxRuler > this.obj.xRuler)
      	    {
        	var left = RulerX1 ;
        	var w = RulerX2 - RulerX1 ;
            }
      	    else
      	    {
        	var left = RulerX2 ;
        	var w = RulerX1 - RulerX2 ;
      	    }
      	    img.style.height = h + 'px' ;
      	    img.style.width = w + 'px' ; 
  
      	    div.style.left = left + 'px' ;
      	    div.style.top = top + 'px'  ;      
  
      	    div.style.height = img.style.height ;
      	    div.style.width = img.style.width ;       
	}
    	return false ; 
    }
}

var zoomWidth  = 0;
var zoomHeight = 0;

genMap.prototype.stopRuler = function (e)
{  
  if (AjaxProcess > 0) return;
  this.obj.flagRuler = 0 ;  
  
  if(this.obj.xRuler == this.obj.xxRuler && this.obj.yRuler == this.obj.yyRuler)
  {
    var pdiv = document.getElementById("mapDiv_" + this.obj.divName) ;
    if (document.getElementById("Ruler_Div"))
    {
      var div = document.getElementById("Ruler_Div") ;            
      var img = document.getElementById("RulerImg") ;  
      div.style.left = 0 + 'px' ;
      div.style.top = 0 + 'px'  ;                   
      div.style.border = "0px" ;              
      img.style.height = 0 + 'px' ;
      img.style.width = 0 + 'px' ;                        
    }                  
  }
  else
  {
    if (document.getElementById("Ruler_Div"))
    {
      var div = document.getElementById("Ruler_Div") ;  
  
      var xtes = parseInt(div.style.left) + parseInt(this.obj.xCurr) - parseInt(this.obj.xStart) - parseInt(this.obj.width / 2);
      var ytes = parseInt(div.style.top) + parseInt(this.obj.yCurr) - parseInt(this.obj.yStart) - parseInt(this.obj.height / 2) ;
      //alert ("HEHE : " + parseInt(div.style.left) + " | " + parseInt(this.obj.xCurr)) ;
      var xtes2 = ( parseInt(div.style.left) + parseInt(div.style.width) ) + parseInt(this.obj.xCurr) - parseInt(this.obj.xStart) - parseInt(this.obj.width / 2);
      var ytes2 = ( parseInt(div.style.top) + parseInt(div.style.height) ) + parseInt(this.obj.yCurr) - parseInt(this.obj.yStart) - parseInt(this.obj.height / 2) ;    
    
      var posLeft    = parseInt(div.style.left) + parseInt(this.obj.xStart);
      var posTop     = parseInt(div.style.top) + parseInt(this.obj.yStart);
      var posRight   = parseInt(div.style.left) + parseInt(div.style.width) + parseInt(this.obj.xStart);
      var posBottom  = parseInt(div.style.top) + parseInt(div.style.height) + parseInt(this.obj.yStart);
      var posXCenter = parseInt(div.style.left) + Math.round(parseInt(div.style.width)/2) + parseInt(this.obj.xStart);
      var posYCenter = parseInt(div.style.top) + Math.round(parseInt(div.style.height)/2) + parseInt(this.obj.yStart);
      zoomWidth      = posRight - posLeft;
      zoomHeight     = posBottom - posTop;
      //alert("Left  = " + posLeft + "  |  Top   = " + posTop + "\nRight = " + posRight + "  |  Bottom  = " + posBottom + "\nPosXCenter = " + posXCenter + "  |  PosYCenter = " + posYCenter);         
         
      TLRuler = this.obj.changeToLongLat(parseInt(posLeft), parseInt(posTop)) ;
      BRRuler = this.obj.changeToLongLat(parseInt(posRight), parseInt(posBottom)) ;
      CCRuler = this.obj.changeToLongLat(parseInt(posXCenter), parseInt(posYCenter));
            
      //alert (this.obj.getMouseType()) ;              
      cekMouseType (this.obj) ;                                       
    }  
  }
  return false ;  
}

function cekMouseType (obj)
{  
  //var a = document.getElementById("mapDiv_map")
  //(this.mapObj.getMouseType().indexOf("zoom") >= 0) ? document.getElementById("Ruler_Div").style.visibility = 'hidden' : "";
    //alert (obj.getLevel())  ;
          
    var TLBRCC     = new Array();
    TLBRCC[0]      = new GM_Point();
    TLBRCC[0].Long = TLRuler[0];
    TLBRCC[0].Lat  = TLRuler[1];
    TLBRCC[1]      = new GM_Point();
    TLBRCC[1].Long = BRRuler[0];
    TLBRCC[1].Lat  = BRRuler[1];
    TLBRCC[2]      = new GM_Point();
    TLBRCC[2].Long = CCRuler[0];
    TLBRCC[2].Lat  = CCRuler[1];
    
  if (this.mapObj.getMouseType() == "ruler")
  {    
    for (i = 0 ; i < 10 ; i ++)
    {
      if (document.getElementById("Layer"+i))
      {
        var input = document.getElementById("Layer"+i) ;              
        if(input.code != undefined)
        {          
          if (document.getElementById(input.code))
          {
            var LS = document.getElementById(input.code) ;
            LS.innerHTML = "" ;
          
            if (document.getElementById("Balloon_" + this.mapObj.divName))
            {
              var divLayer = document.getElementById("Balloon_" + this.mapObj.divName) ;
              divLayer.innerHTML = "" ;    
            }    
          }          
        }        
        input.checked = "" ;        
      }      
    }   
    var butLayDiv = document.getElementById("buttonTypeLayersDiv_" + this.mapObj.divName) ;
    if (butLayDiv.show == 0)
    {
      butLayDiv.show = 1 ;
    }    
//    this.mapObj.showLayers() ;   

  }
  else if (this.mapObj.getMouseType() == "zoom_in")
  {    
    var LVL = CheckingLevel_For_ZoomIn(TLBRCC[2].Long, TLBRCC[2].Lat, TLBRCC);
    this.mapObj.changeLevel(LVL, TLBRCC[2].Long, TLBRCC[2].Lat);
    this.mapObj.setMouseType("zoom_in");
  }
  else if (this.mapObj.getMouseType() == "zoom_out")
  { 
    var temp = CheckingLevel_For_ZoomOut(this.mapObj.getCurrLong(), this.mapObj.getCurrLat(), TLBRCC);
    var data = temp.split("|");    
    this.mapObj.changeLevel(parseInt(data[2]), TLBRCC[2].Long, TLBRCC[2].Lat);
    this.mapObj.setMouseType("zoom_out");    
  }
}

function CheckingLevel_For_ZoomIn(X,Y,XYList) 
{
	var LVL  = 1;
	var j    = this.mapObj.MaxLevel  ;
	for (var i = j ; i >= 1 ; i--) {
		var map_scale = (this.mapObj.longMax - this.mapObj.longMin) / this.mapObj.totWidth[i] ;
		var lm_delta_x = (this.mapObj.width/2) * map_scale;
		var lm_delta_y = (this.mapObj.height/2) * map_scale;		
		var lm_long_min = X - lm_delta_x;
		var lm_long_max = X + lm_delta_x;
		var lm_lat_min = Y - lm_delta_y;
		var lm_lat_max = Y + lm_delta_y;
		var status = 0;
		for (var k=0; k<XYList.length; k++)
		{
			if (XYList[k].Long)
			{
				if (!((XYList[k].Long >= lm_long_min && XYList[k].Long <= lm_long_max) && (XYList[k].Lat >= lm_lat_min && XYList[k].Lat <= lm_lat_max))) {
					status = 1;
				}
			}
		}
		if (status == 0) {
			LVL = i;
			return LVL ; 
		}
	}	
	return LVL ;
}

function CheckingLevel_For_ZoomOut(X,Y,XYList) 
{
	var LVL  = 1;
	var MS   = (this.mapObj.longMax - this.mapObj.longMin) / this.mapObj.totWidth[this.mapObj.getLevel()] ;
	//alert(this.mapObj.getLevel());
	var LDX  = (this.mapObj.width/2) * MS;
  var LDY  = (this.mapObj.height/2) * MS;   
	var j    = this.mapObj.MaxLevel ;	
	for (var i = 1 ; i <=j ; i++) {
		var map_scale = (this.mapObj.longMax - this.mapObj.longMin) / this.mapObj.totWidth[i] ;
		var lm_delta_x = (zoomWidth / 2) * map_scale;
		var lm_delta_y = (zoomHeight / 2) * map_scale;
		//alert(lm_delta_x + "  =  " + LDX + "   |   " + lm_delta_y + "  =  " + LDY + "\nLevel = " + i);
		if ( (lm_delta_x <= LDX) && (lm_delta_y <= LDY) )
		{      
		  //alert("Masuk");
		  LVL = i;
      var NewX = X + ((X - XYList[2].Long)*2);
      var NewY = Y + ((Y - XYList[2].Lat)*2);
      return NewX + "|" + NewY + "|" + LVL;
    }
	}
	return X + "|" + Y + "|" + LVL ;
}

function getLongLat(X, Y)
{
  return this.mapObj.changeToLongLat(parseInt(X), parseInt(Y)) ;
}

var AJAX_Ruler ;

function getDataRuler()
{
  var url = this.BASE_AJAX_URL + "dataRuler.php" ;
  url = url + "?top=" + TLRuler[1] ;
  url = url + "&left=" + TLRuler[0] ;
  url = url + "&bottom=" + BRRuler[1] ;
  url = url + "&right=" + BRRuler[0] ;
  url = url + "&level=" + this.mapObj.getLevel() ;
  url = url + "&sid=" +  Math.round(Math.random(0,1)*100000000000) ;              
  AJAX_Ruler = new ajax();
  AJAX_Ruler.Ajax_ID = "GetDataRuler";
  AJAX_Ruler.url = url ;
	AJAX_Ruler.method = "GET" ;
	AJAX_Ruler.timeout = 60 ;
	AJAX_Ruler.callback = "viewDataRuler();" ;    
	AJAX_Ruler.request();                  
}

function viewDataRuler ()
{ 
  dataFull = AJAX_Ruler.resultText();
    
  //alert (dataFull) ;  
}

genMap.prototype.getRowCol = function ()	// function untuk menentukan banyak kolom, baris 'n buffer yg diperlukan untuk perulangan draw
{	  
  this.ausway = this.checkAusway();   
  this.showToolTipAusway() ;  
/*  if(this.flagAusway == 1 && this.ausway == 1 && (this.level == 12 || this.level == 13 || this.level == 14) )  
  { this.setViewType("ausway") ;
  } else {
      this.setViewType("normal") ;
  }
*/
      this.setViewType("normal") ;


//  var mapDiv = document.getElementById("mapDivNormal_" + this.divName) ;
//  mapDiv.innerHTML = "";	  
	var col = Math.ceil (this.xCenter / this.imgWidth) ;	// mendapatkan jumlah banyaknya kolom sesuai besarnya canvas yang ada
	var row = Math.ceil (this.yCenter / this.imgHeight) ;	// mendapatkan jumlah banyaknya baris sesuai besarnya canvas yang ada

    var bufcol = Math.ceil( ( (1/2) * this.width) / this.imgWidth) ;	// mendapatkan banyaknya tempat cadangan (buffer) colom (supaya kalo di pan ada image cadangan tanpa harus load lagi)
    var bufrow = Math.ceil( ( (1/2) * this.height) / this.imgHeight) ;	// mendapatkan banyaknya tempat cadangan (buffer) colom (supaya kalo di pan ada image cadangan tanpa harus load lagi)
  

	// buffer yang dipakai disesuaikan dengan bentuk canvas, tergantung buffer mana yang paling besar (bufcol or bufrow)
	var buffer = 0 ;
	if(bufcol > bufrow)	{
		buffer = bufcol ;
	} else {
		buffer = bufrow ;
	}
	
/*	if (this.browser)	{
		if (LP) window.stop() ;    		
	} else {
		document.execCommand('Stop') ;
	}
*/
//  toText("total : "+this.LoadedMap.length,true);
//	toText(this.LoadedMap);
 for (var i in this.LoadedMap) {
		if (document.getElementById(i))
		{
			var Img = document.getElementById(i) ;			
			if (!Img.complete)
			{
				this.LoadedMap[i] = "" ;				
			}
		}			
	}
 
  var cnt = [];
	// melakukan perulangan draw dari tengah 	
	for(var buf = 0 ; buf <= buffer ; buf++)
	{
		for(var i = col-buf ; i <= col+buf ; i ++)
		{
			for(var j = row-buf ; j <= row+buf ; j ++)
			{
	       var imgId = "imgMapNormal_" + this.divName + "_" + i + "_" + j ;	// menampung sementara image id
          if(cnt[imgId]) continue;
          cnt[imgId]= 1;
//			  if(cnt[i+"_"+j]) continue;
//			  cnt[i+"_"+j] = 1;

				switch (this.viewType)
				{
					case "normal" :
						this.drawNormalMap(i,j) ;						
						break ;
					case "satelite" :								
						this.drawSateliteMap(i,j) ;
						break ;
					case "ausway" :
					  this.drawAuswayMap(i,j);
					  break ;
					case "hybrid" :	
						if (this.level > 4)
						{
							this.drawSateliteMap(i,j) ;
							this.drawHybridMap(i,j) ;
						}
						else
						{
							this.drawSateliteMap(i,j) ;
							this.drawNormalMap(i,j) ;
						}
						break ;
					case "streetview" :
						this.drawNormalMap(i,j) ;
						this.drawStreetviewMap(i,j) ;
						break ;
				}										
			}
		}
	}
	//toText("Length: " + cntlen);
	
}


var flag = false;
genMap.prototype.drawNormalMap = function (i,j)	// function untuk draw peta
{
	var imgId = "imgMapNormal_" + this.divName + "_" + i + "_" + j ;	// menampung sementara image id
	var mapDiv = document.getElementById("mapDivNormal_" + this.divName) ;	
	var MapUrl = this.BASE_MAP_URL + this.MAP_File_Type + "_" + this.MAP_Source + "_" + this.MAP_Country + "/" + this.getEncode(this.level) + "/" + this.getEncode(j) + "/" + this.getEncode(i) + ".png";
	
	//document.getElementById('DEBUG').innerHTML += MapUrl+"<br>";
		
	//var MapUrl = "http://" + this.serverHost + "/data/smallmap/australia/BhJJ/L" + this.getEncode(this.level) + "/R" + this.getEncode(j) + "C" + this.getEncode(i) + ".gif" ;	
	//window.prompt (MapUrl,MapUrl)
	if (this.LoadedMap[imgId] != 1)
  { 
    this.LoadedMap[imgId] = 1;
  	if( !document.getElementById(imgId) )	// cek apakah id tsb sudah pernah di create, kalo udah, dia ga create lagi
  	{
  		var img = document.createElement("img") ;
  		// Old Src => img.src = "http://" + this.serverHost + "/data/smallmap/australia/BhJJ/L" + this.getEncode(this.level) + "/R" + this.getEncode(j) + "C" + this.getEncode(i) + ".gif" ;
      //img.src = "http://192.168.0.15/data/ras_format/level" + this.level + "/R" + this.getEncode(j) + "C" + this.getEncode(i) + ".png" ;
		  img.src = MapUrl;      
    	img.style.position = "absolute" ;
  		img.id = imgId ;
  		img.style.width = this.imgWidth + 'px' ;
  		img.style.height = this.imgHeight + 'px' ;
  		img.style.left = parseInt((i - 1) * this.imgWidth) - this.xStart + 'px' ;
  		img.style.top = parseInt((j - 1) * this.imgHeight) - this.yStart + 'px' ;
  		img.doneLoad = false ;	// var untuk cek apakah uda done blom pas load
  		img.errorSrc = this.errorImg ;	
//  		img.style.cursor = "hand" ;
  		img.onload = function ()
  		{
  			this.doneLoad = true ;
  		}
  		img.onerror = function ()	// nih fungsi buat sendiri, kalo error dia akan ganti src gambar dengan gambar error (warna biru)
  		{
  			this.doneLoad = false ;
  			this.src = this.errorSrc ;
  		}		
    	img.alt = this.imageAlt;
  		mapDiv.appendChild (img) ;		
  	} else{
  		  var img = document.getElementById(imgId) ;
				img.src = MapUrl ;        
    }
  }	else {
    var img = document.getElementById(imgId);
    /*if ( img.src.indexOf(MapUrl) < 0 )
    {
      img.src = MapUrl;
    }*/
  }
	var img = document.getElementById(imgId) ;
	if (this.viewType == "hybrid")
	{
		var GM_HybOpa = (this.level > 4) ? 100 : 40 ;
		if (this.browser)
		{
			//img.style.MozOpacity = GM_HybOpa / 100;
			img.style.opacity = GM_HybOpa / 100 ;
		}
		else 
		{
			img.style.filter = "alpha(opacity=" + GM_HybOpa + ");";					
		}
	} 
	else if (this.viewType == "normal")
	{
		var GM_HybOpa = 100 ;
		if (this.browser)
		{
			//img.style.MozOpacity = GM_HybOpa / 100;
			img.style.opacity = GM_HybOpa / 100 ;
		}
		else 
		{
			img.style.filter = "alpha(opacity=" + GM_HybOpa + ");";					
		}
	}
}

genMap.prototype.drawAuswayMap = function (i,j)	// function untuk draw peta
{
	var imgId = "imgMapAusway_" + this.divName + "_" + i + "_" + j ;	// menampung sementara image id
	var mapDiv = document.getElementById("mapDivAusway_" + this.divName) ;	
	var MapUrl = this.BASE_MAP_URL + this.MAP_File_Type + "_ausway_" + this.MAP_Country + "/" + this.getEncode(this.level) + "/" + this.getEncode(j) + "/" + this.getEncode(i) + ".gif";
	if (this.LoadedMap[imgId] != 1)
  { 
    this.LoadedMap[imgId] = 1;
  	if( !document.getElementById(imgId) )	// cek apakah id tsb sudah pernah di create, kalo udah, dia ga create lagi
  	{
    var img = document.createElement("img") ;
      img.src = MapUrl;      
    	img.style.position = "absolute" ;
  		img.id = imgId ;
  		img.style.width = this.imgWidth + 'px' ;
  		img.style.height = this.imgHeight + 'px' ;
  		img.style.left = parseInt((i - 1) * this.imgWidth) - this.xStart + 'px' ;
  		img.style.top = parseInt((j - 1) * this.imgHeight) - this.yStart + 'px' ;
  		img.doneLoad = false ;	// var untuk cek apakah uda done blom pas load
		img.errorSrc = 	this.BASE_MAP_URL + this.MAP_File_Type + "_" + this.MAP_Source + "_" + this.MAP_Country + "/" + this.getEncode(this.level) + "/" + this.getEncode(j) + "/" + this.getEncode(i) + ".png";
//  		img.style.cursor = "hand" ;
  		
  		img.onload = function ()
  		{
  			this.doneLoad = true ;
  		}
  		img.onerror = function ()	// nih fungsi buat sendiri, kalo error dia akan ganti src gambar dengan gambar error (warna biru)
  		{
  			this.doneLoad = false ;
  			this.src = this.errorSrc ;
  		}		
  		mapDiv.appendChild (img) ;		
  	}
  }	else {
    var img = document.getElementById(imgId);
    
    if ( img.src.indexOf(MapUrl) < 0 )
    {
      img.src = MapUrl;
    }
  }
}


genMap.prototype.drawSateliteMap = function (i,j)	// function untuk draw peta
{
	var imgId = "imgMapSatelite_" + this.divName + "_" + i + "_" + j ;	// menampung sementara image id
	var mapDiv = document.getElementById("mapDivSatelite_" + this.divName) ;	

	if( !document.getElementById(imgId) )	// cek apakah id tsb sudah pernah di create, kalo udah, dia ga create lagi
	{
		var img = document.createElement("img") ;
//		img.src = "http://" + this.serverHost + "/data/smallmap/ausphoto/BhJJ/L" + this.getEncode(this.level) + "/R" + this.getEncode(j) + "C" + this.getEncode(i) + ".gif" ;

    img.src = this.BASE_MAP_URL + this.MAP_File_Type + "_ausway_" + this.MAP_Country + "/" + this.getEncode(this.level) + "/" + this.getEncode(j) + "/" + this.getEncode(i) + ".gif";		
		img.style.position = "absolute" ;
		img.id = imgId ;
		img.style.width = this.imgWidth + 'px' ;
		img.style.height = this.imgHeight + 'px' ;
		img.style.left = parseInt((i - 1) * this.imgWidth) - this.xStart + 'px' ;
		img.style.top = parseInt((j - 1) * this.imgHeight) - this.yStart + 'px' ;
		img.doneLoad = false ;	// var untuk cek apakah uda done blom pas load
//		img.errorSrc = this.errorImg ;	

		img.errorSrc = 	this.BASE_MAP_URL + this.MAP_File_Type + "_" + this.MAP_Source + "_" + this.MAP_Country + "/" + this.getEncode(this.level) + "/" + this.getEncode(j) + "/" + this.getEncode(i) + ".png";

//		img.style.cursor = "hand" ;
		
		img.onload = function ()
		{
			this.doneLoad = true ;
		}
		img.onerror = function ()	// nih fungsi buat sendiri, kalo error dia akan ganti src gambar dengan gambar error (warna biru)
		{
			this.doneLoad = false ;
			this.src = this.errorSrc ;
		}
		mapDiv.appendChild (img) ;
	}
}

genMap.prototype.drawHybridMap = function (i,j)	// function untuk draw peta
{
	var imgId = "imgMapHybrid_" + this.divName + "_" + i + "_" + j ;	// menampung sementara image id
	var mapDiv = document.getElementById("mapDivHybrid_" + this.divName) ;	

	if( !document.getElementById(imgId) )	// cek apakah id tsb sudah pernah di create, kalo udah, dia ga create lagi
	{
		var div = document.createElement("div") ;
		var img = document.createElement("img") ;
		img.src = "http://" + this.serverHost + "/data/smallmap/aus_hybrid/BhJJ/L" + this.getEncode(this.level) + "/R" + this.getEncode(j) + "C" + this.getEncode(i) + ".png" ;
    //img.src = this.BASE_MAP_URL + this.MAP_File_Type + "_" + this.MAP_Source + "_" + this.MAP_Country + "/" + this.getEncode(this.level) + "/" + this.getEncode(j) + "/" + this.getEncode(i) + ".png";		
		img.style.position = "absolute" ;
		img.id = imgId ;
		img.style.width = this.imgWidth + 'px' ;
		img.style.height = this.imgHeight + 'px' ;
		img.style.left = 0 + 'px' ;
		img.style.top = 0 + 'px' ;
		img.doneLoad = false ;	// var untuk cek apakah uda done blom pas load
		img.errorSrc = this.errorImg ;		

		div.id = "imgMapHybridDiv_" + this.divName + "_" + i + "_" + j ;
		div.style.position = "absolute" ;
		div.style.left = parseInt((i - 1) * this.imgWidth) - this.xStart + 'px' ;
		div.style.top = parseInt((j - 1) * this.imgHeight) - this.yStart + 'px' ;
		div.style.width =  this.imgWidth + 'px' ;
		div.style.height = this.imgHeight + 'px' ;
	
		img.onload = function ()
		{
			this.doneLoad = true ;
		}
		img.onerror = function ()	// nih fungsi buat sendiri, kalo error dia akan ganti src gambar dengan gambar error (warna biru)
		{
			this.doneLoad = false ;
			this.src = this.errorSrc ;
		}
		//alert (img.src) ;
		div.appendChild (img) ;
		mapDiv.appendChild (div) ;
	}

	var GM_HybOpa = (this.level > 4) ? 100 : 0 ;
	var img = document.getElementById(imgId) ;
	if (this.browser)
	{
		img.style.opacity = GM_HybOpa / 100 ;
	}
	else 
	{
		transparantPNG(div,img) ;
	}	
}

genMap.prototype.getEncode = function (str)	// function untuk mengembalikan hasil encrypt code level, row, 'n col gambar peta
{
	str = str+"" ;
	//var code = "J-o-h-k-y-Z-2-4-7-9" ;	// string code
	var code = "I-n-T-1-m-4-P-S-7-d" ;  // new string code
	var arrCode = code.split("-") ;

	for(var i = 0 ; i < arrCode.length ; i ++)
	{
		var Regex = new RegExp(i+"","g") ;
		str = str.replace(Regex,arrCode[i]) ;
	}
	return str ;
}

genMap.prototype.getCenter = function ()	// memindahkan posisi div peta yang uda di draw agar sesuai dengan div canvas/klip
{
	var mapDiv = document.getElementById("mapDiv_" + this.divName) ;	

	this.xCurr = parseInt(-this.xCenter,10) + (this.width / 2) + this.xStart ;
	this.yCurr = parseInt(-this.yCenter,10) + (this.height / 2) + this.yStart ;	
	mapDiv.style.left = parseInt(this.xCurr,10) + 'px' ;
	mapDiv.style.top = parseInt(this.yCurr,10) + 'px' ;	
}

function updateLayers()
{
  if(this.mapObj.getLevel() > 10)
  {   
     for(var i=1 ; i<6 ; i++)
     {      
       var input = document.getElementById("Layer" + i);       
       if (SV_Active['Layer'+i]) 
       {
          if ( input ) input.checked = true;
       }
     }
//     checkLayers();      
  }
  
}


function purge(d) {
    var a = d.attributes, i, l, n;
    if (a) {
        l = a.length;
        for (i = 0; i < l; i += 1) {
            n = a[i].name;
            if (typeof d[n] === 'function') {
                d[n] = null;
            }
        }
    }
    a = d.childNodes;
    if (a) {
        l = a.length;
        for (i = 0; i < l; i += 1) {
            purge(d.childNodes[i]);
        }
    }
}

genMap.prototype.changeLevel = function(level, longitude, latitude)
{
  var sd = document.getElementById("SD");
  if (sd) sd.innerHTML += "<br>Long = " + lon + "  |  Lat = " + lat + "  |  Level = " + level;
	
	if(longitude != "" && latitude != "")
	{
		this.longitude = longitude ;
		this.latitude = latitude ;
	}
	this.level = level ;
	purge(document.getElementById(this.divName));
	this.start () ;      
//  this.showLayers();
  //showButtonType(true);
//  setTimeout(updateLayers, 2500);    
  this.showhideButtonAusway() ;  		
}

genMap.prototype.startDrag = function (mouseEvent)	// function yang dipanggil saat klik mouse (onmousedown)
{

  var mapDiv = document.getElementById("mapDiv_" + this.obj.divName) ;
//  mapDiv.style.cursor = (window.stop)?"-moz-grabbing":"/CommonLibrary/panning.cur" ;
 mapDiv.style.cursor = (window.stop)?"-moz-grabbing":"" ;
  
  if (!mouseEvent) mouseEvent = window.event;
  if ((mouseEvent.button == 0) || (mouseEvent.button == 1)){

    if (AjaxProcess > 0) return;
    this.obj.ObjClickTarget  = (mouseEvent.target)?mouseEvent.target:mouseEvent.srcElement;
    if ( this.obj.ObjClickTarget )
    {   
	if ( this.obj.ObjClickTarget.id.indexOf("imgMap") >= 0 )
   	{  
      if (this.obj.genmapClickAction == 1)
      {
    		this.obj.flagMove = 1 ;
    		this.obj.startX = parseInt(mouseEvent.clientX,10);
    		this.obj.startY = parseInt(mouseEvent.clientY,10);    		
      }
    }  	
  }
  return false ;
 }
}

genMap.prototype.moveDrag = function (mouseEvent) // function yang dipanggil saat menahan sambil menggerakkan mouse (onmousemove)
{
    if (!mouseEvent) mouseEvent = window.event;
    if ((mouseEvent.button == 0) || (mouseEvent.button == 1)){

  	if (AjaxProcess > 0) return;
	var mapDiv = document.getElementById("mapDiv_" + this.obj.divName) ;	
	if(this.obj.flagMove == 1 && this.obj.genmapClickAction == 1)
	{
		var moveX = parseInt(mouseEvent.clientX,10);
		var moveY = parseInt(mouseEvent.clientY,10);			
		mapDiv.style.left = this.obj.changePositionX(moveX, this.obj.startX) + 'px' ;
		mapDiv.style.top = this.obj.changePositionY(moveY, this.obj.startY) + 'px' ;

		this.obj.movX = parseInt(this.obj.movX) + (parseInt(moveX,10) - parseInt(this.obj.startX,10)) ;		
		this.obj.movY = parseInt(this.obj.movY) + (parseInt(moveY,10) - parseInt(this.obj.startY,10)) ;
		this.obj.mvX = this.obj.mvX + (parseInt(moveX,10) - parseInt(this.obj.startX,10)) ;
		this.obj.mvY = this.obj.mvY + (parseInt(moveY,10) - parseInt(this.obj.startY,10)) ;		

		this.obj.startX = moveX ;
		this.obj.startY = moveY ;
		
		this.obj.flagMove = 1;
	}
	return false ;
    }
}

/*
    DC on Wednesday, 7th May 2008
    Action : 
    1. Adding function checkLayers to check if user already select any
       layers control and run the function of each selected layer.
    2. Adding function LoadingStart to give message when processing data
       with ajax
    3. Adding function LoadingStop to close message when processing done.
*/

genMap.prototype.isValidToLoadData = function()
{
  if (this.flagMove == 1) {
    window.mapObj = this ;
		window.setTimeout(function () { this.mapObj.isValidToLoadData(); } , 2500) ;    
    return false;
  } else if (this.flagMove == 0) {
    //checkLayers();
    this.flagMove = 2;
    return true;
  }
}

genMap.prototype.isValidToLoadDataAfterAnim = function()
{
  if (this.flagAnim == 1) {
    window.mapObj = this ;
		window.setTimeout(function () { this.mapObj.isValidToLoadDataAfterAnim(); } , 4000) ;    
    return false;
  } else if (this.flagAnim == 0) {
    //checkLayers();
    this.flagAnim = 2;
    return true;
  }
}

function checkLayers()
{
  if (DataLayers) {
    var dataLine = DataLayers.split("\n") ; // DataLayers uda diisi di genmap.map.js
    var LayersQty = parseInt(dataLine[0]) ;	 
   	dataLine.shift() ;  
	  for (var i = 0 ; i < LayersQty ; i ++ )
	  {	  
	    var dataDetail = dataLine[i].split(',') ;
	    var temp = document.getElementById(dataDetail[2]);
	    if(temp)
	    {
        if (temp.checked == true) {
		  // disable by BK
          //LoadingStart("PS", "Please wait... Loading data " + dataDetail[0]);
          //setTimeout("eval('" + temp.Function + "');", ((i*5000)+10));
        }
      }
    } 
  }
  
  if(this.mapObj.flagAusway == 1 && this.mapObj.ausway == 1 && (this.mapObj.level == 12 || this.mapObj.level == 13 || this.mapObj.level == 14) )
  { 
	//  ajax_getMapReference(); //genmap.mapreference			
  } else {
	  hide_auswayMap(); //genmap.mapreference
  }
  this.mapObj.showhideButtonAusway();
}

genMap.prototype.stopDrag = function (e)	// function yang dipanggil saat melepaskan klik mouse (onmouseup)
{ 
// disable by BK
// if (AjaxProcess > 0) return; 
  var mapDiv = document.getElementById("mapDiv_" + this.obj.divName) ;
//  mapDiv.style.cursor = (window.stop)?"-moz-grab":"/CommonLibrary/move.cur" ;
  mapDiv.style.cursor = (window.stop)?"-moz-grab":"" ;

 if ( this.obj.ObjClickTarget )
 {
   if ( this.obj.ObjClickTarget.id.indexOf("imgMap") >= 0 )
   {  
     if (this.obj.genmapClickAction == 1)
     {
    	  this.obj.isValidToLoadData();
      	this.obj.flagMove = 0 ;  	
      	this.obj.setCenter () ;
      	this.obj.getRowCol () ;    
      }
   }
 }     
	return false ;
}

genMap.prototype.changePositionX = function (sX, mX)	// function yang mengembalikan nilai pixelX saat ini
{
	this.xCurr = parseInt(this.xCurr) + (parseInt(sX,10) - parseInt(mX,10)) ;
	return this.xCurr ;
}

genMap.prototype.changePositionY = function (sY, mY)	// function yang mengembalikan nilai pixelY saat ini
{		
	this.yCurr = parseInt(this.yCurr) + (parseInt(sY,10) - parseInt(mY,10)) ;
	return this.yCurr ;
}

genMap.prototype.setCenter = function ()	// menyesuaikan xcenter 'n ycenter map ke posisi saat ini (posisi setelah berubah)
{
	this.xCenter = Math.round(this.width / 2) - parseInt(this.xCurr) + this.xStart ;		
	this.yCenter = Math.round(this.height / 2) - parseInt(this.yCurr) + this.yStart ;			
	//this.longitude = this.getCurrLong();
	//this.latitude  = this.getCurrLat();
}

genMap.prototype.setType = function ()	
{
	var mapDivN = document.getElementById("mapDivNormal_" + this.divName) ;
	var mapDivS = document.getElementById("mapDivSatelite_" + this.divName) ;
	var mapDivH = document.getElementById("mapDivHybrid_" + this.divName) ;
	var mapDivA = document.getElementById("mapDivAusway_" + this.divName) ;

	if (this.viewType == "normal")
	{
		mapDivS.style.visibility = "hidden" ;
		mapDivH.style.visibility = "hidden" ;
		mapDivN.style.visibility = "visible" ;
		mapDivA.style.visibility = "hidden" ;
		mapDivS.style.zIndex = "0" ;		
		mapDivH.style.zIndex = "0" ;
		mapDivN.style.zIndex = "1000" ;
    mapDivA.style.zIndex = "0" ;	
	}
	else if (this.viewType == "satelite")
	{
		mapDivS.style.visibility = "visible" ;
		mapDivA.style.visibility = "hidden" ;
		mapDivH.style.visibility = "hidden" ;
		mapDivN.style.visibility = "hidden" ;
		mapDivS.style.zIndex = "1000" ;
		mapDivH.style.zIndex = "0" ;
		mapDivN.style.zIndex = "0" ;	
    mapDivA.style.zIndex = "0" ;		
	}
	else if (this.viewType == "hybrid")
	{
		if (parseInt(this.level) > 4)
		{
			mapDivS.style.visibility = "visible" ;
			mapDivH.style.visibility = "visible" ;
			mapDivN.style.visibility = "hidden" ;
			mapDivS.style.zIndex = "999" ;
			mapDivH.style.zIndex = "1000" ;
			mapDivN.style.zIndex = "0" ;						
		}
		else
		{
			mapDivS.style.visibility = "visible" ;
			mapDivH.style.visibility = "hidden" ;
			mapDivN.style.visibility = "visible" ;
			mapDivS.style.zIndex = "1000" ;
			mapDivH.style.zIndex = "0" ;
			mapDivN.style.zIndex = "9999" ;				
		}		
	}
	else if(this.viewType == "ausway")
	{
		mapDivS.style.visibility = "hidden" ;
		mapDivH.style.visibility = "hidden" ;
		mapDivN.style.visibility = "hidden" ;
		mapDivA.style.visibility = "visible" ;
		mapDivS.style.zIndex = "0" ;
		mapDivH.style.zIndex = "0" ;
		mapDivN.style.zIndex = "0" ;	
    mapDivA.style.zIndex = "1000" ; 
  }
}

/*=====================================================A N I M A S I===============================================================*/

genMap.prototype.recenter = function (lon, lat)	// function yang digunakan objek lain untuk mendapatkan titik perpindahan dan melakukan animasi saat terjadi perubahan posisi di objek sendiri
{
  var pix = this.changeToPixel(lon, lat) ;
	this.xNext = pix[0] - this.xStart ;
	this.yNext = pix[1] - this.yStart;	
	this.checkAnimation () ;
}

genMap.prototype.checkAnimation = function ()	// function untuk menyeleksi apakah perpindahan ke titik tengah masih menggunakan animasi ato langsung
{	
	if((this.xCenter -  this.xStart) != this.xNext || (this.yCenter - this.yStart) != this.yNext)
	{		
		var lineX = (this.xCenter - this.xStart)  - this.xNext ;
		var lineY = (this.yCenter - this.yStart) - this.yNext ;
		var line = Math.ceil( Math.sqrt( (lineX*lineX) + (lineY*lineY) ) ) ;
    if( line < (2 * this.width) )	// kalo masih di bawah 2 * lebar canvas, di masih animasi, kalo ga, dia move langsung tanpa animasi
		{		  
			this.startAnimation ((this.xCenter - this.xStart), (this.yCenter - this.yStart), this.xNext, this.yNext) ;
		}
		else
		{
			this.moveMap () ;
		}
	} else {    
    this.flagAnim = 0;    
  }
}

genMap.prototype.startAnimation = function (x, y, x1, y1)	// mengambil titik2/point2 garis yang diperlukan untuk animasi baru menjalankan animasinya
{	
	this.vpoint = this.getVirtualLine(x, y, x1, y1) ;	
	this.animation() ;
}

genMap.prototype.getVirtualLine = function (x1, y1, x2, y2)	// mengembalikan titik2/point2 garis dari titik awal ke titik berikutnya untuk animasi
{
	var baseOnX = true ;
	var animbuf = 1 ;	// ini supaya jarak yang dibuat bervariasi, jadi semakin jauh dia semakin cepet animasinya, ga monoton
	var newXY = "" ;

	if(Math.abs(x2-x1) <= Math.abs(y2-y1)) // tuker posisi, supaya jarak titik2 pixel ga langsung jauh2 amat
	{
		baseOnX = false ;
		var temp = x1 ;
		x1 = y1 ;
		y1 = temp ;

		temp = x2 ;
		x2 = y2 ;
		y2 = temp ;
	}

	var point = new Array () ;
	if(x2 - x1 > 0)
	{
		var lineM = (y2 - y1) / (x2 - x1) ;
	}
	else
	{
		var lineM = (y2 - y1) / (x2 - x1 + 0.00000000000000001);
	}
	var lineC = y1 - lineM * x1 ;
	var lineY = y1 ;

	if(x2 > x1)
	{
		for(var i = x1 ; i <= x2 ; i+=animbuf)
		{
			if(i % 2 == 0)
			{
				animbuf ++ ;
			}
			var lnX =  i ;
			var lnFy = lineM * lnX + lineC ;
			var lnY = Math.round(lnFy + 0.5) ;
			if(baseOnX)
			{
				var curXY = lnX + "\t" + lnY ;
			}
			else
			{
				var curXY = lnY + "\t" + lnX ;
			}

			if(newXY != curXY)
			{
				point.push(curXY) ;
			}
			newXY = curXY ;
		}
	}
	else
	{
		for(var i = x1; i >= x2 ; i -= animbuf)
		{
			if(i % 2 == 0)
			{
				animbuf ++ ;
			}
			var lnX = i ;
			var lnFy = lineM * lnX + lineC ;
			var lnY = Math.round(lnFy+0.5) ;			
			if(baseOnX)
			{
				curXY = lnX + "\t" + lnY ;
			}
			else
			{
				curXY = lnY + "\t" + lnX ;
			}

			if(newXY != curXY)
			{
				point.push (curXY) ;
			}
			newXY = curXY ;
		}
	}
	return point ;
}

genMap.prototype.animation = function ()	// function untuk melakukan perulangan perpindahan (animasi)
{
	var mapDiv = document.getElementById("mapDiv_" + this.divName) ;
	if(this.vpoint.length > 0)
	{
		var ppoint = this.vpoint[0].split("\t") ;
		this.vpoint.shift() ;	// membuang kolom array yang paling depan (index yang ke-0)
		this.xCurr = parseInt(-ppoint[0]) + Math.round(this.width/2) ;
		this.yCurr = parseInt(-ppoint[1]) + Math.round(this.height/2) ;
		mapDiv.style.left = this.xCurr + 'px' ;
		mapDiv.style.top = this.yCurr + 'px' ;
		if(this.vpoint.length > 0)
		{
			window.mapObj = this ;
			window.setTimeout(function () { this.mapObj.animation(); } , 3) ;
		}
		else	// pas udah mau berhenti
		{
			this.xCurr = parseInt(-this.xNext) + Math.round(this.width/2) ;		
			this.yCurr = parseInt(-this.yNext) + Math.round(this.height/2) ;
			mapDiv.style.left = this.xCurr + 'px' ;
			mapDiv.style.top = this.yCurr + 'px' ;

			this.isValidToLoadDataAfterAnim();
			this.flagAnim = 0;
			this.setCenter () ;
			this.getRowCol () ;	     			       		
		}
	}
   return false ;	
}

genMap.prototype.moveMap = function ()	// function untuk memindahkan map tanpa animasi (jika jarak sudah dianggap jauh dari titik tengah (center) )
{
	var mapDiv = document.getElementById("mapDiv_" + this.divName) ;
	this.xCurr = - this.xNext  + Math.round(this.width/2) ;
	this.yCurr = - this.yNext  + Math.round(this.height/2) ;
	mapDiv.style.left = this.xCurr + 'px' ;
	mapDiv.style.top = this.yCurr + 'px' ;
	this.isValidToLoadDataAfterAnim();
	this.flagAnim = 0;
	this.setCenter () ;	
	this.getRowCol() ;	
	
	/*
	if (this.flagMove > 2)
	{ 
	  checkLayers();
  }	
  this.flagMove = 0 ;
	*/
	/*
	if(this.myCommand)
	{
		eval (this.myCommand) ;		
	}*/
}

genMap.prototype.callBack = function (command)	
{	
	this.myCommand = command ;
	//alert (oMap.divName) ;
}

genMap.prototype.getTop = function ()
{
  var canvasDiv = document.getElementById("canvasDiv_" + this.divName) ;
	var TopLeft = this.changeToLongLat(this.xCenter - ( parseInt(canvasDiv.style.width) / 2 ), this.yCenter - ( parseInt(canvasDiv.style.height) / 2 )) ;
  return TopLeft[1] ;							  
}

genMap.prototype.getLeft = function ()
{
  var canvasDiv = document.getElementById("canvasDiv_" + this.divName) ;
	var TopLeft = this.changeToLongLat(this.xCenter - ( parseInt(canvasDiv.style.width) / 2 ), this.yCenter - ( parseInt(canvasDiv.style.height) / 2 )) ;
  return TopLeft[0] ;  
}

genMap.prototype.getBottom = function ()
{
  var canvasDiv = document.getElementById("canvasDiv_" + this.divName) ;
  var BottomRight = this.changeToLongLat(this.xCenter + ( parseInt(canvasDiv.style.width) / 2 ), this.yCenter + ( parseInt(canvasDiv.style.height) / 2 )) ;
  return BottomRight[1] ;  
}

genMap.prototype.getRight = function ()
{
  var canvasDiv = document.getElementById("canvasDiv_" + this.divName) ;
  var BottomRight = this.changeToLongLat(this.xCenter + ( parseInt(canvasDiv.style.width) / 2 ), this.yCenter + ( parseInt(canvasDiv.style.height) / 2 )) ;
  return BottomRight[0] ;
}

/*************************************************************************
/* Cursor Mouse
/************************************************************************/

// function bayu untuk debug
function toText(obj, delcontent, targetElem) {
  var e = document.getElementById(targetElem || "debug") || (function() {
    var dbg = document.createElement("div");
    dbg.id = "debug";
    document.body.insertBefore(dbg, document.body.lastChild);
    return dbg;
  })();
  var res = document.createElement("pre");
  res.gettext = function(str) {
    if(typeof(res.textContent) != "undefined") res.textContent = str;
    else res.innerText = str;
  };
  var txt = "";
  if(typeof(obj) == "undefined") txt = "Object undefined!!!\n";
  else if(typeof(obj) == "string") txt = obj + "\n";
  else {
    for(var i in obj) txt += (obj.id || "") + "[" + i + "] = " + obj[i] + "\n";
  }
  res.gettext(txt); /*"------------------------------"*/
  if(delcontent) e.innerHTML = "";
  e.appendChild(res);
}
