/***

SchmapKit.Maps__GStatic 0.2

(c) 2007 Schmap ltd.  All rights Reserved.

***/
var b_version=navigator.appVersion;
var isIE6 = b_version.search(/MSIE 6/i) != -1;
if (typeof(SchmapKit) == 'undefined') {
    SchmapKit = {};
}
if (typeof(SchmapKit.Base) == 'undefined') {
    SchmapKit.Base = {};
}
if (typeof(SchmapKit.DOM) == 'undefined') {
    SchmapKit.DOM = {};
}
if (typeof(SchmapKit.Maps__GStatic) == 'undefined') {
    SchmapKit.Maps__GStatic = {};
}

if (typeof(SchmapKit.Maps) == 'undefined') {
    // provide default implementation
    SchmapKit.Maps = SchmapKit.Maps__GStatic;
}
SchmapKit.Maps__GStatic.NAME = "SchmapKit.Maps__GStatic";
SchmapKit.Maps__GStatic.VERSION = "0.2";
SchmapKit.Maps__GStatic.__repr__ = function () {
    return "[" + this.NAME + " " + this.VERSION + "]";
};
SchmapKit.Maps__GStatic.toString = function () {
    return this.__repr__();
};

SchmapKit.Maps__GStatic.Map = function(element, options) {
    this.parentElement = $(element);
    this.element = SchmapKit.DOM.DIV({id: 'SchmapKit.Maps__GStatic.Map.' +
                            SchmapKit.Maps__GStatic.Map._id_pool + '.container',
                        style: 'position: absolute; height: 100%; width:100%; top: 0px; bottom: 0px; right: 0px; left: 0px;' +
                               'overflow: hidden; margin: 0px; padding: 0px; ' +
                               'background: white;'});
    this.imgElement = SchmapKit.DOM.IMG({id: 'SchmapKit.Maps__GStatic.Map.' +
                               SchmapKit.Maps__GStatic.Map._id_pool + '.img',
                           style: 'z-index: 1; position: absolute; ' +
                                  'top: 0px; left: 0px;','class': 'staticmapimg'});
    this.markersElement = SchmapKit.DOM.DIV({id: 'SchmapKit.Maps__GStatic.Map.' +
                                   (SchmapKit.Maps__GStatic.Map._id_pool++) +
                                   '.markers',
                               style: 'z-index: 2; height: 100%; ' +
                                      'margin: 0px; padding: 0px;' +
                                      'position: relative;' + 
                                      'font-size: 10px; font-weight:bold'});
    this.parentElement.appendChild(this.element);
    this.element.appendChild(this.markersElement);
	this.element.appendChild( this.imgElement);
    this.provider = 'c';
    this.bounds = this.imgBounds = null;
    this.images = [];
    this._items = [];
	this.google_api_key = google_api_key;
	this.mutiMarker = true;
	this.zindex = 23;
};
 
SchmapKit.Maps__GStatic.Map._id_pool = 0;
SchmapKit.Maps__GStatic.Map.datafiles = {};
SchmapKit.Maps__GStatic.Map.newDataArrived = true;
SchmapKit.Maps__GStatic.Map.prototype.setBackground = function(style) {
    SchmapKit.DOM.setStyle(this.element, {background: style});
};
SchmapKit.Maps__GStatic.Map.prototype.actualBoundsFromIdealBounds = function (bounds)
{
    return (this.imgBounds);
}

SchmapKit.Maps__GStatic.Map.prototype.setDisplay = function(point, zoom) {
 	var d = SchmapKit.DOM.getElementDimensions(this.element);
 	this.imgElementSize = new SchmapKit.Maps__GStatic.ElementSize(d.w,d.h);
    this.centerPoint = point;
	x = point.latitude;
	y = point.longitude;
    this.imgElement.src = "/images/iSchmap/null.png";
	this.imgElement.src = "http://maps.google.com/staticmap?center="+x+","+y+"&zoom="+zoom+"&size="+d.w+"x"+d.h+"&key="+this.google_api_key;
    var map = this;
	this.imgElement.onload=function(){
			map.visibleMap();
		};
	if(this.imgElement.complete){
		map.visibleMap();
	}
    this.zoom = zoom;
    this.imgBounds = null;
	this.__display__();	
};
 
SchmapKit.Maps__GStatic.Map.prototype.visibleMap = function() {
	this.imgElement.style.visibility = 'visible';
}

SchmapKit.Maps__GStatic.Map.prototype.inVisibleMap = function(point, zoom) {
	this.imgElement.style.visibility = 'hidden';
}

SchmapKit.Maps__GStatic.Map.prototype.caculatBounds = function(point, zoom) {
    var d = this.imgElementSize;
    this.GMercatorProjection = new SchmapKit.Maps__GStatic.GMercatorProjection(17);
    this.zoomFactors = {x: this.GMercatorProjection.pixelsPerLonDegree[zoom],
                 y: this.GMercatorProjection.pixelsPerLonRadian[zoom]};
    var halfLng = d.width/this.zoomFactors.x/2;
    var halfLat = d.height/this.zoomFactors.y/2*180/Math.PI;
    this.imgBounds = new SchmapKit.Maps__GStatic.Bounds(); 
    this.imgBounds.west = point.longitude - halfLng;
    this.imgBounds.east = point.longitude + halfLng;
    this.imgBounds.north = point.latitude + halfLat;
    this.imgBounds.south = point.latitude - halfLat;
    this.imgElmentOpoint = this.GMercatorProjection.fromLatLngToPixel(point,zoom);	
    return this.imgBounds;
}

SchmapKit.Maps__GStatic.Map.prototype.zoomToBounds = function(bounds) {	
	var d = SchmapKit.DOM.getElementDimensions(this.element);
	var centergeo = bounds.getCenter();
	x = centergeo.latitude;
	y = centergeo.longitude;
    this.centerPoint = centergeo;
	//var ppl = d.w/(bounds.east-bounds.west);
	//var zoom = Math.round(Math.log(ppl/(256/360)) / Math.log(2));
	this.GMercatorProjection = new SchmapKit.Maps__GStatic.GMercatorProjection(17);
	this.imgElementSize = new SchmapKit.Maps__GStatic.ElementSize(d.w,d.h);
	var zoom = this.GMercatorProjection.getBoundsZoomLevel(bounds,this.imgElementSize);
    this.zoom = zoom;
    this.inVisibleMap();
    this.imgElement.src = "/images/iSchmap/null.png";
	this.imgElement.src = "http://maps.google.com/staticmap?center="+x+","+y+"&zoom="+zoom+"&size="+d.w+"x"+d.h+"&key="+this.google_api_key;
    if(this.imgElement.complete) {
        this.visibleMap();
    } else {
        var map = this;
        this.imgElement.onload = function() {
			map.visibleMap();
		};
    }
    this.caculatBounds(centergeo,zoom);
	
};

SchmapKit.Maps__GStatic.Map.prototype.getBounds = function() {
    if(this.imgBounds)
    return SchmapKit.Base.clone(this.imgBounds);
    return this.caculatBounds(this.centerPoint,this.zoom);
};

SchmapKit.Maps__GStatic.Map.prototype.setMapType = function(maptype) {
};

SchmapKit.Maps__GStatic.Map.prototype.getPosOfPoint = function(point, icon) {
    var p = SchmapKit.DOM.getElementPosition(
            this.convert(point, true), this.markersElement);
    p.x -= icon.anchor.x; 
    p.y -= icon.anchor.y;
    return p;
};

SchmapKit.Maps__GStatic.Map.prototype.convert = function(item, _with_data) {
    var c = SchmapKit.DOM.getElementPosition(this.imgElement);
    if(_with_data) {
        if(item instanceof SchmapKit.Maps__GStatic.Point) { 
            c.x += Math.round((item.longitude - this.imgBounds.west) * this.zoomFactors.x);
            g = this.GMercatorProjection.fromLatLngToPixel(item,this.zoom);
            c.y += (g.y-this.imgElmentOpoint.y+this.imgElementSize.height/2);
	        return c;
        } else {
            return undefined;
        }
    }
    c.x += this.imgElementSize.width/2;
    c.y += this.imgElementSize.height/2;
    return c;
};

SchmapKit.Maps__GStatic.Map.prototype.addItem = function(item) {
    if(item.__added__ !== undefined)
        item.__added__(this);
    else
        item.map = this;
    if(item.__display__ !== undefined)
        this._items.push(item);
};

SchmapKit.Maps__GStatic.Map.prototype.hideMarkers = function() {
    this.markersElement.style.zIndex = 0;	
	this.markersElement.style.visibility = "hidden";
};

SchmapKit.Maps__GStatic.Map.prototype.__display__ = function() {
    if(this.markersHidden) return;
    SchmapKit.Base.map(function(item) {item.__display__();}, this._items);
    this.markersElement.style.zIndex = 2;
    this.markersElement.style.visibility = "visible";
};

SchmapKit.Maps__GStatic.Map.prototype.hideAll = function(hide) {
    if(hide) {
        this.markersHidden = true;
        this.hideMarkers();
    } else {
        this.markersHidden = false;
        this.__display__();
    }
};
 
SchmapKit.Maps__GStatic.Point = function(lat, lon) {
    this.latitude = Number(lat);
    this.longitude = Number(lon);
};

SchmapKit.Maps__GStatic.Point.prototype.lat = function(){
   return this.latitude
};

SchmapKit.Maps__GStatic.Point.prototype.lng = function(){
   return this.longitude
};
 
SchmapKit.Maps__GStatic.Bounds = function(south, west, north, east) {
    if(south > north) {
        var tmp = south;
        south = north;
        north = tmp;
    }
    if(west > east) {
        var tmp = west;
        west = east;
        east = tmp;
    }
    this.west = west;
    this.south = south;
    this.east = east;
    this.north = north;
	this.southWest = new SchmapKit.Maps__GStatic.GLatLng(south,west);
	this.northEast = new SchmapKit.Maps__GStatic.GLatLng(north,east);
	this.northWest = new SchmapKit.Maps__GStatic.GLatLng(north,west);
};

SchmapKit.Maps__GStatic.Bounds.fromGeoRSS = function(georss) {
    // extract from xml node
	
    if(georss.textContent && georss.nodeName &&
       georss.nodeName.match(/box$/)){
        georss = georss.textContent;}
    else if(georss.object){
        georss = georss.object;}
    var box = SchmapKit.Base.map(Number, georss.split(/ /));
   
    return new SchmapKit.Maps__GStatic.Bounds(box[0], box[1], box[2], box[3]);
};

SchmapKit.Maps__GStatic.Bounds.prototype.__repr__ = function () {
    return "[Map bounds: (" + this.south + ", " + this.east +
                   ") to (" + this.north + ", " + this.west + ")]";
};

SchmapKit.Maps__GStatic.Bounds.prototype.getCenter = function() {
    return new SchmapKit.Maps__GStatic.Point((this.north + this.south) / 2,
                                   (this.west + this.east) / 2);
};

SchmapKit.Maps__GStatic.Bounds.prototype.contains = function(pb) {
    if(pb instanceof SchmapKit.Maps__GStatic.Bounds) {
        return (pb.west >= this.west) && (pb.east <= this.east) &&
               (pb.south >= this.south) && (pb.north <= this.north);
    } else if(pb instanceof SchmapKit.Maps__GStatic.Point) {
        return (pb.longitude >= this.west) && (pb.longitude <= this.east) &&
               (pb.latitude >= this.south) && (pb.latitude <= this.north);
    }
};

SchmapKit.Maps__GStatic.Bounds.prototype.equals = function(b) {
    return (b.west == this.west) && (b.east == this.east) &&
           (b.south == this.south) && (b.north == this.north);
};

SchmapKit.Maps__GStatic.Bounds.prototype.growTo = function(pb) {
    if(pb instanceof SchmapKit.Maps__GStatic.Bounds) {
        if(pb.west < this.west)
            this.west = pb.west;
        if(pb.east > this.east)
            this.east = pb.east;
        if(pb.south < this.south)
            this.south = pb.south;
        if(pb.north > this.north)
            this.north = pb.north;
    } else if(pb instanceof SchmapKit.Maps__GStatic.Point) {
        if(pb.longitude < this.west)
            this.west = pb.longitude;
        if(pb.longitude > this.east)
            this.east = pb.longitude;
        if(pb.latitude < this.south)
            this.south = pb.latitude;
        if(pb.latitude > this.north)
            this.north = pb.latitude;
    }
	this.southWest = new SchmapKit.Maps__GStatic.GLatLng(this.south,this.west);
	this.northEast = new SchmapKit.Maps__GStatic.GLatLng(this.north,this.east);
	this.northWest = new SchmapKit.Maps__GStatic.GLatLng(this.north,this.west);
};

SchmapKit.Maps__GStatic.Bounds.prototype.getWidth = function() {
    return Math.abs(this.east - this.west);
};

SchmapKit.Maps__GStatic.Bounds.prototype.getHeight = function() {
    return Math.abs(this.north - this.south);
};

SchmapKit.Maps__GStatic.Bounds.prototype.northwest = function() {
    return new SchmapKit.Maps__GStatic.GLatLng(this.north,this.west);
};

SchmapKit.Maps__GStatic.Bounds.getBestBounds = function(points) {
    var top = points[0].latitude;
	var left = points[0].longitude;
	var bottom = top;
	var right = left;
	for(var i = 1; i < points.length; i++){
	    var p =  points[i];
		if(p.latitude > top){
		    top = p.latitude;
		}
		if(p.latitude < bottom){
		    bottom = p.latitude;
		}
		if(p.longitude > right){
		    right = p.longitude;
		}
		if(p.longitude < left){
		    left = p.longitude;
		}
	}
	var factor = (top - bottom) * 0.1;
	top += factor;
	bottom -= factor;
	left -= factor;
	right += factor;
	return new SchmapKit.Maps__GStatic.Bounds(bottom,left,top,right);
};

SchmapKit.Maps__GStatic.Icon = function(url, dimensions, anchor,dom) {
    this.image = url;
    this.dimensions = dimensions;
    this.anchor = anchor;
	this.dom = dom;
};

SchmapKit.Maps__GStatic.Icon.prototype.create = function() {
    if(this.dom)
    return this.dom;
    e = SchmapKit.DOM.IMG({src:this.image, suppress:"TRUE"});
    //e.src = this.image;
    //e.width = this.dimensions.w; 
    //e.height = this.dimensions.h;
    e.style.position = 'absolute';
    //e.style.zIndex = '23';
    return e;
};

SchmapKit.Maps__GStatic.Marker = function(icon, point) {
    this.point = point;
    this.icon = icon;
    this.map = null;
    this.element = SchmapKit.DOM.DIV(null,this.icon.create());
        this.element.style.position = 'absolute';
    this.element.style.zIndex = '23';
    this.hidden = false;
    this.added = false;
};

SchmapKit.Maps__GStatic.Marker._nextId = 1;
 
SchmapKit.Maps__GStatic.Marker.prototype.__added__ = function (map) {
    this.map = map;
    this.map.markersElement.appendChild(this.element); 
    this.__display__();
};

SchmapKit.Maps__GStatic.Marker.prototype.__display__ = function () {
    if(!this.map) return;
    if(!this.hidden)
        this.element.style.display = 'block'; 
    p = SchmapKit.DOM.getElementPosition(this.map.convert(this.point, this.map.mutiMarker), this.map.markersElement);
	p.x -= this.icon.anchor.x; p.y -= this.icon.anchor.y;
    SchmapKit.DOM.setElementPosition(this.element, p);
};

SchmapKit.Maps__GStatic.Marker.prototype.setMarkerText = function(text) {
    if(!this.markerText){
        this.markerText = SchmapKit.DOM.SPAN(null,text);
        this.element.appendChild(this.markerText);
        SchmapKit.DOM.setStyle(this.markerText,{position: 'absolute',
                                                'width': this.icon.dimensions.w + 'px',
                                                'height': this.icon.dimensions.h + 'px',
                                                'text-align': 'center',
                                                'color': 'white',
                                                'padding-top': '1px',
                                                'margin-left': '-2px'});
    }
    this.markerText.innerHTML = text;
};

SchmapKit.Maps__GStatic.Marker.prototype.setImage = function(url) {
    this.element.firstChild.src = url;
};

SchmapKit.Maps__GStatic.Marker.prototype.show = function() {
    this.map.zindex++;
	this.element.style.zIndex = (this.map.zindex);
	this.element.style.display = 'block';
    this.hidden = false;
};

SchmapKit.Maps__GStatic.Marker.prototype.hide = function() {
    this.element.style.display = 'none';
    this.hidden = true;
};

 

SchmapKit.Maps__GStatic.Marker.prototype.listen = function(events) {
    logWarning('Marker.listen is deprecated');
};

SchmapKit.Maps__GStatic.Marker.prototype.unlisten = function() {
    logWarning('Marker.unlisten is deprecated');
};
SchmapKit.Maps__GStatic.Marker.prototype.getElement = function() {
        return this.element;
};

SchmapKit.Maps__GStatic.GMercatorProjection= function(a)
  {
   var b=this;
   b.pixelsPerLonDegree=[];
   b.pixelsPerLonRadian=[];
   b.bitmapOrigo=[];
   b.numTiles=[];
   var c=256;
   for(var d=0;d<a;d++)
   {
    var e=c/2;
    b.pixelsPerLonDegree.push(c/360);
    b.pixelsPerLonRadian.push(c/(2*Math.PI));
    b.bitmapOrigo.push(new SchmapKit.Maps__GStatic.GPoint(e,e));
    b.numTiles.push(c);
    c*=2
   }
  };
  
SchmapKit.Maps__GStatic.GMercatorProjection.prototype.fromLatLngToPixel=function(point,zoom){
   var a = point;
   var b = zoom;
   var c = this;
   var d = c.bitmapOrigo[b];
   var e = Math.round(d.x+a.lng()*c.pixelsPerLonDegree[b]);
   var f = GetNumberInRange(Math.sin(this.getRadianByDegree(a.lat())),-0.9999,0.9999);
   var g = Math.round(d.y+0.5*Math.log((1+f)/(1-f))*-c.pixelsPerLonRadian[b]);
   return new SchmapKit.Maps__GStatic.GPoint(e,g)
  };
  
SchmapKit.Maps__GStatic.GMercatorProjection.prototype.fromPixelToLatLngDim=function(point,zoom){
   var d = this;
   var e = d.bitmapOrigo[zoom];
   var f = (point.x)/d.pixelsPerLonDegree[zoom];
   var g = (point.y)/-d.pixelsPerLonRadian[zoom];
   var h = d.getDegreeByRadian(2*Math.atan(Math.exp(g))-Math.PI/2);
   return new SchmapKit.Maps__GStatic.GLatLng(h,f)
};

SchmapKit.Maps__GStatic.GMercatorProjection.prototype.getDegreeByRadian=function(a) {
   return a/(Math.PI/180)
}

SchmapKit.Maps__GStatic.GMercatorProjection.prototype.getWrapWidth=function(a)
  {
   return this.numTiles[a]
  };  
SchmapKit.Maps__GStatic.GMercatorProjection.prototype.getBoundsZoomLevel=function (bounds,b)
  {
   var c=this;
   var d=16;
   var e=0;
   var f=bounds.southWest;
   var g=bounds.northEast;
   for(var h=d;h>=e;--h)
   {
    var i=c.fromLatLngToPixel(f,h);
    var l=c.fromLatLngToPixel(g,h);
    if(i.x>l.x)
    {
     i.x-=c.getWrapWidth(h)
    }
    if(Math.abs(l.x-i.x)<=b.width&&Math.abs(l.y-i.y)<=b.height)
    {
     return h
    }
   }
   return 0
  };  
 
SchmapKit.Maps__GStatic.GMercatorProjection.prototype.getRadianByDegree = function (d){
	return d*Math.PI/180
};

SchmapKit.Maps__GStatic.GLatLng = function (a,b,c){
	if(!c){
		a=GetNumberInRange(a,-90,90);
		b=this.getLoopNumber(b,-180,180)
	}
	this.latitude=a;
	this.longitude=b;
	this.x=b;
	this.y=a
} 

SchmapKit.Maps__GStatic.GLatLng.prototype.lat=function(){
	return this.latitude
};
SchmapKit.Maps__GStatic.GLatLng.prototype.lng=function(){
	return this.longitude
};
SchmapKit.Maps__GStatic.GLatLng.prototype.getLoopNumber = function(a,b,c){ 
   while(a>c){
    a-=c-b
   }
   while(a<b){
    a+=c-b
   }
   return a
};

SchmapKit.Maps__GStatic.GPoint=function (a,b){
	this.x=a;
	this.y=b
};

SchmapKit.Maps__GStatic.ElementSize=function (a,b){
	this.width=a;
	this.height=b
};

GetNumberInRange = function(a,b,c){
	  if(a>b&&a<c){
	  return a;
	  }
	  if(a<b) return b;
	  if(a>c) return c;
};
