var layerCenterMap;
var map;
var markersize = new OpenLayers.Size(49,32);
var markeroffset = new OpenLayers.Pixel(-17, -markersize.h);
var zoomcontrol;

mapinit = function() {

	zoomcontrol = new OpenLayers.Control.PanZoomBar();
	loadjscssfile("http://api.mapwithyou.com/mwy.css", "css");	

        map = new OpenLayers.Map ("map", {
                                controls:[
                                        new OpenLayers.Control.Navigation(),
					zoomcontrol
				],
                                maxExtent: new OpenLayers.Bounds(-20037508.34,-20037508.34,20037508.34,20037508.34),
                                maxResolution: 156543.0399,
                                numZoomLevels: 19,
                                units: 'm',
                                projection: new OpenLayers.Projection("EPSG:900913"),
                                displayProjection: new OpenLayers.Projection("EPSG:4326")
        } );

	    /*	
	    var mwybutton1 = new OpenLayers.Control.Button({title:'Zoom In',
                      displayClass: "ZoomInButton",
                      trigger: function(){
                        map.zoomIn();
                      }
                });
	    var mwybutton2 = new OpenLayers.Control.Button({title:'Zoom Out',
                      displayClass: "ZoomOutButton",
                      trigger: function(){
                        if(map.getZoom()>10)
                                map.zoomOut();
                      }
                });
	    var mwydummy1 = new OpenLayers.Control.Button();
	    var mwypanel = new OpenLayers.Control.Panel({});
	    mwypanel.addControls([mwydummy1,mwybutton1,mwybutton2]);
	    map.addControl(mwypanel);

	    var mwylogo = new OpenLayers.Control.Button({title:'mapwithyou',
                      displayClass: "mwylogo"
                });

	    map.addControl(mwylogo);
	    */

        layerMapnik = new OpenLayers.Layer.OSM.Mapnik("Mapnik");
        map.addLayer(layerMapnik);
        layerCenterMap = new OpenLayers.Layer.Markers("Markers");
        map.addLayer(layerCenterMap);
}

function loadjscssfile(filename, filetype){
 if (filetype=="js"){ //if filename is a external JavaScript file
  var fileref=document.createElement('script')
  fileref.setAttribute("type","text/javascript")
  fileref.setAttribute("src", filename)
 }
 else if (filetype=="css"){ //if filename is an external CSS file
  var fileref=document.createElement("link")
  fileref.setAttribute("rel", "stylesheet")
  fileref.setAttribute("type", "text/css")
  fileref.setAttribute("href", filename)
 }
 if (typeof fileref!="undefined")
  document.getElementsByTagName("head")[0].appendChild(fileref)
}


function addmarker(lonlat,mcolor){
     var icon;
     if(mcolor=='B')
        icon = new OpenLayers.Icon('http://api.mapwithyou.com/img/m_blue.png',markersize,markeroffset);
     else if(mcolor=='G')
        icon = new OpenLayers.Icon('http://api.mapwithyou.com/img/m_green.png',markersize,markeroffset);
     else if(mcolor=='O')
        icon = new OpenLayers.Icon('http://api.mapwithyou.com/img/m_orange.png',markersize,markeroffset);
     else
        icon = new OpenLayers.Icon('http://api.mapwithyou.com/img/m_red.png',markersize,markeroffset);
     layerCenterMap.addMarker(new OpenLayers.Marker(lonlat,icon));
}

function clearmarkers(){
	layerCenterMap.clearMarkers();
}


function getlonlat(lon,lat){
	var alat=-0.0001142639; 
	var alon=0.00004023311;
        return new OpenLayers.LonLat(lon+alon, lat+alat).transform(new OpenLayers.Projection("EPSG:4326"), map.getProjectionObject());
}


/**
 * Namespace: Util.OSM
 */
OpenLayers.Util.OSM = {};


OpenLayers.Util.getImagesLocation = function() {
        return "http://api.mapwithyou.com/img/";
}

/**
 * Constant: MISSING_TILE_URL
 * {String} URL of image to display for missing tiles
 */
OpenLayers.Util.OSM.MISSING_TILE_URL = "http://openstreetmap.org/openlayers/img/404.png";

/**
 * Property: originalOnImageLoadError
 * {Function} Original onImageLoadError function.
 */
OpenLayers.Util.OSM.originalOnImageLoadError = OpenLayers.Util.onImageLoadError;

/**
 * Function: onImageLoadError
 */
OpenLayers.Util.onImageLoadError = function() {
    if (this.src.match(/^http:\/\/[abc]\.[a-z]+\.openstreetmap\.org\//)) {
        this.src = OpenLayers.Util.OSM.MISSING_TILE_URL;
    } else if (this.src.match(/^http:\/\/[def]\.tah\.openstreetmap\.org\//)) {
        // do nothing - this layer is transparent
    } else {
        OpenLayers.Util.OSM.originalOnImageLoadError;
    }
};

/**
 * @requires OpenLayers/Layer/TMS.js
 *
 * Class: OpenLayers.Layer.OSM
 *
 * Inherits from:
 *  - <OpenLayers.Layer.TMS>
 */
OpenLayers.Layer.OSM = OpenLayers.Class(OpenLayers.Layer.TMS, {
    /**
     * Constructor: OpenLayers.Layer.OSM
     *
     * Parameters:
     * name - {String}
     * url - {String}
     * options - {Object} Hashtable of extra options to tag onto the layer
     */
    initialize: function(name, url, options) {
        options = OpenLayers.Util.extend({
            attribution: "",
            maxExtent: new OpenLayers.Bounds(-20037508.34,-20037508.34,20037508.34,20037508.34),
            maxResolution: 156543.0339,
            units: "m",
            projection: "EPSG:900913",
            transitionEffect: ""
        }, options);
        var newArguments = [name, url, options];
        OpenLayers.Layer.TMS.prototype.initialize.apply(this, newArguments);
    },

    /**
     * Method: getUrl
     *
     * Parameters:
     * bounds - {<OpenLayers.Bounds>}
     *
     * Returns:
     * {String} A string with the layer's url and parameters and also the
     *          passed-in bounds and appropriate tile size specified as
     *          parameters
     */
    getURL: function (bounds) {
        var res = this.map.getResolution();
        var x = Math.round((bounds.left - this.maxExtent.left) / (res * this.tileSize.w));
        var y = Math.round((this.maxExtent.top - bounds.top) / (res * this.tileSize.h));
        var z = this.map.getZoom();
        var limit = Math.pow(2, z);
        if (y < 0 || y >= limit)
        {
            return OpenLayers.Util.OSM.MISSING_TILE_URL;
        }
        else
        {
            x = ((x % limit) + limit) % limit;
		
            var path;
            path = 'http://api.mapwithyou.com/img/err.png';
            return path;
        }
    },

    CLASS_NAME: "OpenLayers.Layer.OSM"
});

/**
 * Class: OpenLayers.Layer.OSM.Mapnik
 *
 * Inherits from:
 *  - <OpenLayers.Layer.OSM>
 */
OpenLayers.Layer.OSM.Mapnik = OpenLayers.Class(OpenLayers.Layer.OSM, {
    /**
     * Constructor: OpenLayers.Layer.OSM.Mapnik
     *
     * Parameters:
     * name - {String}
     * options - {Object} Hashtable of extra options to tag onto the layer
     */
    initialize: function(name, options) {
        var url = [
            "http://a.tile.openstreetmap.org/",
            "http://b.tile.openstreetmap.org/",
            "http://c.tile.openstreetmap.org/"
        ];
        options = OpenLayers.Util.extend({ numZoomLevels: 19 }, options);
        var newArguments = [name, url, options];
        OpenLayers.Layer.OSM.prototype.initialize.apply(this, newArguments);
    },

    CLASS_NAME: "OpenLayers.Layer.OSM.Mapnik"
});


