﻿// JScript File

var Map = 
{
	m_zoneNameTag: null, 
	m_mapMainTag : null, 
	m_mapBtnTag : null,
	m_zoneID: -1, 
	m_changefn: null,
	m_updatefn: null,

	Init:function( thechangefn, theupdatefn, theZoneNameTag, theMapMainTag, theMapBtnTag, theZone )
	{
		this.m_changefn = thechangefn;
		this.m_updatefn = theupdatefn;
		this.m_zoneID = theZone; 
		this.m_zoneNameTag = theZoneNameTag;
		this.m_mapMainTag = theMapMainTag;
		this.m_mapBtnTag = theMapBtnTag;
	},

	Load:function(  ) 
	{
		ServiceClasses.MapService.set_timeout(10000);
		$clearHandlers( $get("mapcontainer") );
		$addHandler( $get("mapcontainer"), "click", this.mapClick );
	},

	mapClick:function( eventElement )
	{
		var button = eventElement.target;
		
		if( button.className == "namemapbtn" || button.className == "zonemapbtn" )
			button = button.parentNode;

		if( (button.className.startsWith( "mapbtn" ) === true ) && ( button.childNodes[0].getAttribute( "value" ) > 0 ) )
		{
			Map.m_zoneID = button.childNodes[0].getAttribute( "value" );
			ret = ServiceClasses.MapService.GetZone( Map.m_zoneID, Map.zoneSuccessCallback, Map.zonefailedCallback   );
			Map.setButton( Map.m_zoneID );
			if( Map.m_changefn != null )
				Map.m_changefn( ); 
		}  
		else if(  (button.className === "mapmain") || 
						(button.nodeName === "IMG") ||
						(button.className.startsWith( "mapbtn" ) === true ) )
			Map.showModalMap( );
		else if( button.getAttribute("id") == "mysettings" )
		{
			ret = ServiceClasses.MapService.GetMapLocations( -1, Map.settingsSuccessCallback, Map.mapfailedCallback );
		}
		else
			return true;
			
		return Master.stopDefault( eventElement );
	},

	setButton:function ( zoneID )
	{
		var divs = $get("mapcontainer").getElementsByTagName("DIV" );
		for( var i = 0; i < divs.length; i++ )
		{
			if( divs[i].className.startsWith( "mapbtn" ) )
			{
				if( divs[i].childNodes[0].getAttribute( "value" ) == zoneID )
					divs[i].className = "mapbtn mapbtnselect";
				else
					divs[i].className = "mapbtn";
			}
		}
	},

	showModalMap:function( ) 
	{
		$addHandler( window, "resize", Map.resizeOverlay );
		$addHandler( window, "scroll", Map.scrollOverlay );

		var theframe = $get( "mapframe" );
		if( theframe != null )
			theframe.src = Master.m_path + "General/Zones.aspx" + this.buildQueryString( );

		var themodalmap = $get( "modalmap" );
		if( themodalmap != null )
		{
			themodalmap.style.display = "block";
			this.positionModalMap( false, themodalmap );
		}

		var overlay = $get( "overlay" );
		if( overlay != null )
		{
			overlay.style.height = Master.pageHeight( ) + "px";
			overlay.style.width = Master.pageWidth( ) + "px";
			overlay.style.display = "block";
		}
	},

	buildQueryString:function( )
	{
		var sb1 = new Sys.StringBuilder( "" );
		var zone = null;

		var divs = $get("mapcontainer").getElementsByTagName("DIV" );
		for( var i = 0; i < divs.length; i++ )
		{
			if( divs[i].className.startsWith( "mapbtn" ) )
			{
				if( !sb1.isEmpty( ) )
					sb1.append( "," );
				else
					sb1.append( "?locns=" );
				sb1.append( divs[i].childNodes[0].getAttribute( "value" ) );
			}
			if( divs[i].className == "mapbtn mapbtnselect" )
				zone = divs[i].childNodes[0].getAttribute( "value" );
		}
		if( zone !== null )
		{
			sb1.append( "&zone=" );
			sb1.append( zone );
		}
		if( sb1.isEmpty( ) )
			sb1.append( "?path=" );
		else
			sb1.append( "&path=" );
		return sb1.toString( );
	},

	saveMapSettings:function( obj ) 
	{
		this.m_zoneID = obj.SelectedZone.LocationID;
		var ctrl =  $get( this.m_mapBtnTag );
		if( ctrl != null ) ctrl.innerHTML = obj.HTML;
			
		this.updateMapUI( obj.SelectedZone );

		if( this.m_changefn != null )
			this.m_changefn( ); 
		else
			this.hideModalMap( null );
	},

	resizeOverlay:function( eventElement )
	{
		var overlay = $get( "overlay" );
		if( overlay != null && overlay.style.display === "block" )
		{
			overlay.style.width = Master.pageWidth( ) + "px";
			
			var themodalmap = $get( "modalmap" );
			if( themodalmap != null )
				Map.positionModalMap( false, themodalmap );
		}
	},

	scrollOverlay:function( eventElement )
	{
		var themodalmap = $get( "modalmap" );
		if( themodalmap != null && themodalmap.style.display === "block" )
			Map.positionModalMap( true, themodalmap );
	},

	positionModalMap:function( isScrolling, ctrl )
	{
		var pointVar = Sys.UI.DomElement.getLocation( ctrl );

		if( (Master.viewWidth( ) < 720) && !isScrolling ) 
		{
			if( (Master.scrollX( )+720) > Master.pageWidth( ) )
				pointVar.x = Master.pageWidth( ) - 720;
			else
				pointVar.x = Master.scrollX( );
		}
		else if( Master.viewWidth( ) >= 720 )
			pointVar.x = Math.floor((Master.viewWidth( ) - 720)/2) + Master.scrollX( );

		if( (Master.viewHeight( ) < 670) && !isScrolling )
			pointVar.y = Master.scrollY( );
		else if( Master.viewHeight( ) >= 670 )
			pointVar.y = Math.floor((Master.viewHeight( ) - 670)/2) + Master.scrollY( );

		Sys.UI.DomElement.setLocation( ctrl, pointVar.x, pointVar.y );
	},

	hideModalMap:function( eventElement ) 
	{
		$clearHandlers(window);
	
		var themodalmap = $get( "modalmap" );
		if( themodalmap != null )
			themodalmap.style.display = "none";

		var theoverlay = $get( "overlay" );
		if( theoverlay != null )
			theoverlay.style.display = "none";
	    
		var theframe = $get( "mapframe" );
		if( theframe != null )
			theframe.src = "";
		
		if( eventElement !== null )
			return Master.stopDefault( eventElement );
		return false;
	},

	zoneSuccessCallback:function( result ) 
	{
		if( result == null ) return;
		var obj = Sys.Serialization.JavaScriptSerializer.deserialize( result ); 
		Map.updateMapUI( obj );
	},

	settingsSuccessCallback:function( result ) 
	{
		if( result == null ) return;
		var obj = Sys.Serialization.JavaScriptSerializer.deserialize( result ); 

		Map.m_zoneID = obj.SelectedZone.LocationID;
		var ctrl =  $get( Map.m_mapBtnTag );
		if( ctrl != null ) ctrl.innerHTML = obj.HTML;
			
		Map.updateMapUI( obj.SelectedZone );
		if( Map.m_changefn != null )
			Map.m_changefn( ); 
	},

	updateMapUI:function( obj )
	{
		if( this.m_zoneNameTag != null )
		{	var ctrl =  $get( this.m_zoneNameTag ); if( ctrl != null ) ctrl.innerHTML = obj.Name; }
		var ctrl =  $get( this.m_mapMainTag );
		var label = ctrl.getElementsByTagName("SPAN" );
		if(  label[0] != null ) label[0].innerHTML = obj.LevelName;
		var img = ctrl.getElementsByTagName("IMG" );
		if(  img[0] != null ) 
		{ img[0].src = obj.CrosshairUrl; img[0].style.top = obj.yPixel + "px"; img[0].style.left = obj.xPixel + "px"; }
		
		if( Map.m_updatefn != null )
			Map.m_updatefn( ); 
	},

	mapfailedCallback:function( error )
	{
		Master.showoverlay( 2 );
	},

	zonefailedCallback:function( error )
	{
		// Ignore these..
	}
}

// Notify ScriptManager that this is the end of the script.
if ( typeof(Sys) !== 'undefined' ) Sys.Application.notifyScriptLoaded( );
