var Maps = {};

Maps.addMap = function (map) {
	if ( !Maps.maps) {
		Maps.maps = new Array();
	}

	if (!Maps.maps[map.containerId]) { Maps.maps[map.containerId] = map; }
}


Maps.getMap = function (container) {
	return Maps.maps[container];
}


Maps.gpsFormat = function ( value ) {
           var deg = Math.floor(value);
           var min = (value - deg) * 60;
            return ( " " + deg.toFixed(0) + "<sup>0</sup>" + min.toFixed(2) + "0" );
}

Maps.gpsShow = function ( event) {
	for ( m in Maps.maps )
	{
		Maps.getMap(m).gpsShow ( Seadragon.Utils.getMousePosition(event) );
	}
}




// --------------------------------------------------------

function Map ( container, imgPath, baseSize, corelShift, corel2gps, gps2corel ) {

	Seadragon.Viewer.apply ( this, arguments );

	this.containerId = container;

	this.imgPath = imgPath;
	this.baseSize = new Seadragon.Point( baseSize[0], baseSize[1] );

	var strXML = "<Image TileSize='256' Overlap='1' Format='png' xmlns='http://schemas.microsoft.com/deepzoom/2008'><Size Width='"+
			baseSize[0]+
		"' Height='"+
			baseSize[1]+ 
		"'/></Image>";

        this.openDzi( imgPath+ ".xml", strXML );

        this.corelShift = new Seadragon.Point( corelShift[0], corelShift[1] );
        this.corel2gps = corel2gps;
        this.gps2corel = gps2corel;

        this.elmtLong = document.createElement("div");
			this.elmtLong.className="gps";
        this.elmtLatt = document.createElement("div");
			this.elmtLatt.className="gps";
        this.gpsBar = document.createElement("div");
                	this.gpsBar.appendChild( this.elmtLong);
			this.gpsBar.appendChild( this.elmtLatt);

        this.addControl( this.gpsBar,Seadragon.ControlAnchor.TOP_LEFT);

	Seadragon.Utils.addEvent( this.elmt, "mousemove", Maps.gpsShow  );   


	Maps.addMap(this);

}

Map.prototype = Seadragon.Viewer;

Map.prototype.toGps = function( corel ) {
           return new Seadragon.Point (
				this.corel2gps[0] * corel.x + this.corel2gps[2] * corel.y + this.corel2gps[4] ,
           			this.corel2gps[1] * corel.x + this.corel2gps[3] * corel.y + this.corel2gps[5]
				);

}

Map.prototype.toCorel = function( gps ) {
           return new Seadragon.Point (
				this.gps2corel[0] * gps.x + this.gps2corel[2] * gps.y + this.gps2corel[4] ,
           			this.gps2corel[1] * gps.x + this.gps2corel[3] * gps.y + this.gps2corel[5]
				);

}

Map.prototype.gpsToPoint = function( gps ) {
	var xy = this.toCorel(gps).minus(this.corelShift) ;
	return new Seadragon.Point(
			xy.x / this.baseSize.x,
			(this.baseSize.y - xy.y) / this.baseSize.x
		);
}


Map.prototype.gpsShow = function ( mouse ) {

        	if (!this.isOpen()) {
	          	return;
        	}

	        var pixel = mouse.minus(Seadragon.Utils.getElementPosition(this.elmt)); // позиция мыши в пикселах относительно карты 
	        var point = this.viewport.pointFromPixel(pixel);                                           // позиция в единицах Seadragon

        	var corelPoint = this.corelShift.plus( new Seadragon.Point( this.baseSize.x*point.x,this.baseSize.y-(this.baseSize.x*point.y))); // позиция в Corel
        	var gpsPoint = this.toGps(corelPoint);

		this.elmtLong.innerHTML = Maps.gpsFormat(gpsPoint.x) + " N";
        	this.elmtLatt.innerHTML = Maps.gpsFormat(gpsPoint.y) + " E";

}

Map.prototype.zoomTo = function ( gps, zoom) {

        var point = this.gpsToPoint( new Seadragon.Point(gps[0],gps[1]));
        this.viewport.zoomTo( zoom );
        this.viewport.panTo( point );
}

