var Yaina;
if (!Yaina) Yaina = OpenLayers.Class();

var permalink;

Yaina.Permalink = OpenLayers.Class(OpenLayers.Control.Permalink, {
	updateLink: function() {
		var params = OpenLayers.Control.Permalink.prototype.updateLink.apply(this);

		var poi=null;
		var img=document.getElementById("picture");
		if (img) poi=img.getAttribute("time");
		if (poi) this.element.href += "&poi="+poi;
	},

	CLASS_NAME: "Yaina.Permalink"
});

Yaina.BrowserCheck={};
Yaina.BrowserCheck.getMSIEVersion = function() {
	var id=navigator.appVersion.split(";");
	for (var k=0; k < id.length; k++) {
		var browserid=id[k];
		if (browserid.charAt(0) == " ") browserid=browserid.substr(1);
		if (browserid.substr(0,4) == "MSIE")
			return browserid.substr(5);
	}

	return 0;
};

Yaina.GetM4FUrl={};
Yaina.GetM4FUrl = function(bounds) {
    	bounds = this.adjustBounds(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();
	return this.url + "z" + z + "/row" + y + "/" + z + "_" + x + "-" + y + ".jpg";
};

Yaina.BrowserCheck.supportedVersion = function() {
	if ((OpenLayers.Util.getBrowserName() == "msie") && (this.getMSIEVersion() < 7))
		return false;
	return true;
};


Yaina.TrackMap = OpenLayers.Class( {
	lat: 37.806,
	lon: -122.4184,
	zoom: 12,
	width: 480,
	height: 320,
	requestlayer: null,
	trackURI: "http://yaina.de/~jreuter/openlayers/tiles/test/",

	initialize: function(args) {
		this.setArgs(args);
	},

	setArgs: function(args) {
		if (args.lon) this.lon=args.lon;
		if (args.lat) this.lat=args.lat;
		if (args.width) this.width=args.width;
		if (args.height) this.height=args.height;
		if (args.zoom) this.zoom=args.zoom;
		if (args.trackURI) this.trackURI=args.trackURI;
		if (args.layer) this.requestlayer=args.layer;
	},

	createMap: function() {
		var mapdiv=document.getElementById("map");
		if (!mapdiv) return null;

		if (!Yaina.BrowserCheck.supportedVersion()) {
			mapdiv.innerHTML="<p>This page needs at least Firefox 2, Konqueror 3.4, Safari 3.1, Google Chrome or Internet Explorer 7 to be viewed correctly.</p>";
			return null;
		}

		mapdiv.style.width=this.width;
		mapdiv.style.height=this.height;

		permalink=new Yaina.Permalink();

		map = new OpenLayers.Map ("map", {
			controls:[
				new OpenLayers.Control.Navigation(),
				new OpenLayers.Control.PanZoom(),
				new OpenLayers.Control.LayerSwitcher(),
				permalink,
				new OpenLayers.Control.Attribution()],
			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")
		} );


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

		layerTilesAtHome = new OpenLayers.Layer.OSM.Osmarender("Osmarender");
		map.addLayer(layerTilesAtHome);

		layerLandsat7= new OpenLayers.Layer.WMS("NASA Global Mosaic", "http://wms.jpl.nasa.gov/wms.cgi", {layers: "modis,global_mosaic", attribution: "Satellite Imaging Courtesy <a href='http://onearth.jpl.nasa.gov/'>NASA/JPL-Caltech</a>"});

//		layerLandsat7= new OpenLayers.Layer.WMS("NASA Global Mosaic", "http://t1.hypercube.telascience.org/cgi-bin/landsat7", {layers: "landsat7", attribution: "Satellite Imaging Courtesy <a href='http://onearth.jpl.nasa.gov/'>NASA/JPL-Caltech</a>"});
//		map.addLayer(layerLandsat7);

		layerMaps4Free= new OpenLayers.Layer.TMS("Maps-for-Free Relief", "http://maps-for-free.com/layer/relief/", { getURL: Yaina.GetM4FUrl, attribution: 'SRTM Relief Imaging by <a href="http://www.maps-for-free.com/">maps-for-free.com</a>' });
		map.addLayer(layerMaps4Free);

		layerTrack = new Yaina.Layer.Tracks("Track Layer", this.trackURI , {attribution: "GPS Track by <a href='http://gallery.yaina.de/album/jreuter/'>o'wolf</a>"});
		map.addLayer(layerTrack);
		
		if (!map.center) {
			var lonLat = new OpenLayers.LonLat(this.lon, this.lat).transform(new OpenLayers.Projection("EPSG:4326"), map.getProjectionObject());
			map.setCenter (lonLat, this.zoom);
		}


		return map;
	},

	CLASS_NAME: "Yaina.TrackMap"
});
