// MapFan api


var MapFan = {

	map: {},

	create : function(options){
		var self = this;
		$("#" + options.id).attr('src','/mapfan/map.html');
		$("#" + options.id).load(function(){
		
			var SCALE_MIN = 1;
			var SCALE_MAX = 13;
			var content = this.contentWindow;
			var map = content.CreateMap(options.fixed);
			
			if(options.fixed){
				map.OnChangedScale = function(){};
				map.OnChangedPosition = function(){};
			}else{
				var slider = new CScaleController( map.document, {
					DocRoot : './slider/',
					Left : 16,
					ZIndex : 10,
					Reverse : true,
					Grid : true,
					
					BaseSrc : 'base.gif',
					BaseLeft : 20,
					BaseTop : 29,
					BaseTopMargin : 5,
					
					ThumbSrc : 'thumb.gif',
					ThumbOffset : 2,
					ThumbMoveStep : 13,
					
					WideNormalSrc : 'widen.gif',
					WideDownSrc : 'wided.gif',
					DetailNormalSrc : 'detailn.gif',
					DetailDownSrc : 'detaild.gif',
					
					UpperButtonTop : 8,
					LowerButtonTop : 209
					
				});
				slider.OnClickWide = function(){
					if( map.Scale < SCALE_MAX ){
						map.Scale++;
					}
				};
				slider.OnClickDetail = function(){
					if( map.Scale > SCALE_MIN ){
						map.Scale--;
					}
				};
				slider.OnChangedScale = function(s){
					map.Scale = s;
				}
				
				
				map.OnChangedScale = function(scale){
					slider.SetScale(scale);
					if(options.onChangedScale){
						options.onChangedScale(scale);
					}
				};
				
				map.OnChangedPosition = function(longitude,latitude){
					if(options.onChangedPosition){
						options.onChangedPosition(self.getLongitude(),self.getLatitude());
					}
				}
			}
			
			var lonlat = map.LonLatStr10ToInt(options.longitude + options.latitude);
			map.Longitude = lonlat.Longitude;
			map.Latitude = lonlat.Latitude;
			map.follow = options.follow;
			if(options.starLongitude != null && options.starLatitude != null){
				var starLonlat = map.LonLatStr10ToInt(options.starLongitude + options.starLatitude);
				map.starLongitude = starLonlat.Longitude;
				map.starLatitude = starLonlat.Latitude;
			}
			
			var lUnitOffset = self.UnitOffset(options.scale);
			var strLonLat = self.LonLatToStr256( lonlat );
			var mapExistParam = "CPR=OFF&MAP=" + strLonLat + "&ZM=" + (13-options.scale) + "&" + map.ExtParams;

			if(parseInt(options.scale) < 3) {
				$.ajax({
					type: "POST",
					dataType: "json",
					data: {
						"queryString" : mapExistParam
					},
					url: XY.config.pageContext.ectx + "/entry/ajax/mapExist/",
					success: function(data){ 
						if(parseInt(data.result) < 2) {
							map.Scale = parseInt(options.scale) + 1;
						}
						else {
							map.Scale = options.scale;
						}
						map.AdjustToView();
						self.map = map;
					},
					error: function(){
					}
				});
			}
			else {
				map.Scale = options.scale;
				map.AdjustToView();
				self.map = map;
			}
		});
	},
	
	getLonLats : function(){
		var lonLat = this.map.CreateLonLatInt( this.map.Longitude, this.map.Latitude );
		var lonLat10 = this.map.LonLatIntToStr10( lonLat );
		return lonLat10.split("N");
	},
	
	getLongitude : function(){
		return this.getLonLats()[0];
	},
	getLatitude : function(){
		return 'N' + this.getLonLats()[1];
	},
	getScale : function(){
		return this.map.Scale;
	},
	LonLatToStr256: function( oLonLat )
	{
		return	"E" + this.IntToStr( oLonLat.Longitude, 256, "(", ")" ) +
				"N" + this.IntToStr( oLonLat.Latitude, 256, "(", ")" );
	},
	IntToStr: function( v, b, pr, ps )
	{
		var	s = pr + v%b + ps;
		v = parseInt( v/b );
		s = v%60 + "." + s;
		v = parseInt( v/60 );
		s = v%60 + "." + s;
		v = parseInt( v/60 );
		return v + "." + s;
	},
	UnitOffset: function( nScale )
	{
		return 0x2000 >> (13 - nScale);
	}
	
	
}



