/********************************************
(c) 2005-present, uTime Games
Despite the fact that you can see this, 
you may NOT use or copy this code for any reason.
If you'd like to use this code, please contact:
	Geoffrey Benson at
	sales@utimegames.com

Licensing terms are generally reasonable -
ranging from free to cheap - but you MUST
ask and obtain permission first.

Thank you for not being a jerk.
  --The Management
********************************************/

// The client-side game state (old style)

// Handles user actions to relay to the server, as well as maintaining internal state that can be used to save trips.

var PQGameState = Class.create({
	initialize: function()
	{
		this._window_drag_bonus = 10000;	// How much higher than this window should it be when dragged?
		this._in_combat = false;
		this._avatar_cache = [];
		this._is_facebook = false;
		this._player_stats = false;
		this._player_avail_skills = {};
	},
	
	ShowWindow: function( window_id )
	{
		if( !g_window_cache[window_id] )
			return;
		g_window_cache[window_id].show();
	},
	
	CloseWindow: function( window_id )
	{
		if( !g_window_cache[window_id] )
			return;
		g_window_cache[window_id].hide();
	},
	
	SetCombatMode: function()
	{
		this._in_combat = true;
		
		this.ShowWindow( 'combatmap' );
		this.ShowWindow( 'combatpanel' );
		this.CloseWindow( 'targetselectionpanel' );
		this.CloseWindow( 'pvppanel' );
	},
	
	SetNormalMode: function()
	{
		this._in_combat = false;
		
		this.CloseWindow( 'combatmap' );
		this.CloseWindow( 'combatpanel' );
		this.CloseWindow( 'targetselectionpanel' );
	},
	
	ExtractStateInformation: function( response )
	{
		// Pull out data from the response for internal use later
		var temp = response.getAttribute( 'in_combat' );
		if( temp != undefined )
		{
			if( temp == true )
			{
				this.SetCombatMode();
			}
			else
			{
				this.SetNormalMode();
			}
		}
	},
	
	SetClothing: function( clothing )
	{
		this._clothing_layers = clothing;
	},
	
	// Action Response Handler relay
	HandleResponse: function( action, response )
	{
		// Pull out data from the response for internal use later
		this.ExtractStateInformation( response );
		
		// Pick an action handler
		switch( action )
		{
			case 'show_prefs':
				this._HandleShowPrefs();
				break;
			case 'enter_area':
				this._HandleEnterArea();
				break;
			case 'quests':
				this._HandleQuests();
				break;
			case 'show_make_banner':
				this._HandleShowMakeBanner();
				break;
			case 'show_customization':
				this._HandleShowCustomization();
				break;
			case 'show_fb_invite':
				this._HandleShowFacebookInvite();
				break;
			case 'complete_quest':
				this._HandleCompleteQuest();
				break;
			case 'player_shop':
				this._HandlePlayerShop();
				break;
			case 'show_select_target':
				this._HandleShowSelectTarget();
				break;
			case 'show_target_player':
				this._HandleShowTargetPlayer();
				break;
			case 'show_customize_shop':
				this._HandleShowShopCustomization();
				break;
			case 'builder_tools':
				this._HandleBuilderTools();
				break;
			case 'builder_tiles_refresh':
				this._HandleBuilderTilesRefresh();
				break;
			case 'customize_player_house':
				this._HandleCustomizeHouse();
				break;
			case 'admintools':
				this._HandleAdminTools();
				break;
			case 'refresh_character':
				this._HandleRefreshCharacter();
				break;
			default:
				// No need to do anything - message simply doesn't require a response handler.
				// TODO: handle these the same as anything else
//				if( action != "load_tooltip" )
//				{
//					alert( "Hello!  Sorry for the intrusion, but it seems that Xaroth missed one during his conversion.\n\nPlease post in the forums to let him know he missed '" + action + "', along with a note about what you were doing just now.\n\nThanks!" );
//				}
				break;
		}
	},
	
	// Actions / Action Response Handlers
	ShowPrefs: function()
	{
		var temp = g_window_cache['characterprefs'];
		if( temp )
			this._HandleShowPrefs();
		else
			SendRequest( 'show_prefs' );
	},
	
	_HandleShowPrefs: function()
	{
		this.ShowWindow( 'characterprefs' );
	},
	
	AcceptQuest: function( the_event, questid )
	{
		SendRequest( 'accept_quest', { 'questid': questid } );
	},
	
	EnterArea: function()
	{
		if( this._in_combat == true )
			return;

		SendRequest( 'enter_area' );
	},
	
	_HandleEnterArea: function()
	{
		this.CloseWindow( 'pvppanel' );
		this.CloseWindow( 'playershop' );
		this.CloseWindow( 'customizeshop' );
		this.CloseWindow( 'customizehouse' );
	},
	
	ShowMakeBanner: function()
	{
		SendRequest( 'show_make_banner' );
	},
	
	_HandleShowMakeBanner: function()
	{
		this.ShowWindow( 'makebanner' );
	},
	
	ShowCustomization: function()
	{
		SendRequest( 'show_customization' );
	},
	
	_HandleShowCustomization: function()
	{
		this.ShowWindow( 'customization' );
	},

	ShowFacebookInvite: function()
	{
		SendRequest( 'show_fb_invite' );
	},
	
	_HandleShowFacebookInvite: function()
	{
		this.ShowWindow( 'facebookinvite' );
	},
	
	BuyGoldStyle: function( the_event, itemid, confirmed )
	{
		SendRequest( 'buy_gold_style', { 'item': itemid, 'confirmed': confirmed } );
	},
	
	SetCSS: function( typeid, value )
	{
		// NOTE: "this" refers to the generator of the event, not game_state!
		SendRequest( 'set_css', { 'type': this.getAttribute( 'typeid' ), 'value': this.value } );
	},
	
	SetChatPref: function( the_event )
	{
		// NOTE: "this" refers to the generator of the event, not game_state!
		SendRequest( 'set_chatpref', { 'value': this.value } );
	},
	
	CompleteQuest: function( the_event, questid )
	{
		SendRequest( 'complete_quest', { 'questid': questid } );
	},
	
	_HandleCompleteQuest: function()
	{
	},
	
	_HandlePvPPanel: function()
	{
		this.ShowWindow( 'pvppanel' );
	},
	
	PlayerShop: function()
	{
		SendRequest( 'player_shop' );
	},
	
	_HandlePlayerShop: function()
	{
		this.ShowWindow( 'playershop' );
	},
	
	PreviewStyle: function( the_event, styleid )
	{
		SendRequest( 'preview_style', { 'styleid': styleid } );
	},
	
	MakeMail: function( the_event, touser )
	{
		var temp = {};
		if( touser != '' )
			temp['touser'] = touser;
		SendRequest( 'make_mail', temp );
	},
	
	ReplyMail: function( the_event, post_officeid )
	{
		SendRequest( 'reply_mail', { 'messageid': post_officeid } );
	},
	
	ShowShopCustomization: function()
	{
		SendRequest( 'show_customize_shop' );
	},
	
	_HandleShowShopCustomization: function()
	{
		this.ShowWindow( "customizeshop" );
	},
	
	ShowSelectTarget: function( the_event, name, effectid, targets, skillgem )
	{
		SendRequest( "show_select_target", { 'name': name, 'effectid': effectid, 'targets': targets, 'skillgem': skillgem } );
	},
	
	_HandleShowSelectTarget: function()
	{
		this.ShowWindow( "targetselectionpanel" );
	},
	
	ShowTargetPlayer: function( the_event, effectid )
	{
		SendRequest( "show_target_player", { 'sge': effectid } );
	},
	
	_HandleShowTargetPlayer: function()
	{
		this.ShowWindow( 'targetplayer' );
	},

	// Registered event handlers
	// NOTE: Due to weird scoping rules, 'this' will refer to the object that fired the event, not game_state.
	EventShowTooltip: function()
	{
		ShowTooltip( this );
	},
	
	EventClearTooltip: function()
	{
		ClearTooltip( this );
	},
	
	EventCloseWindow: function( the_event, window_id )
	{
		game_state.CloseWindow( window_id );
	},
	
	ShowBuilderTools: function()
	{
		SendRequest( "builder_tools" );
	},
	
	_HandleBuilderTools: function()
	{
		this.ShowWindow( 'builder_tiles' );
		this.ShowWindow( 'builder_tile_details' );
		this.ShowWindow( 'builder_scope_details' );
		this.ShowWindow( 'builder_map_generate' );
		this.ShowWindow( 'builder_scope_map' );
		
		// Enable the hotkeys to switch modes
		var body = document.getElementsByTagName( 'body' );
		body = body[0];
		Event.observe( body, 'keyup', game_state.HotkeyPressed.bindAsEventListener( game_state ) );
	},
	
	BuilderChangeTileCategory: function( the_event, the_select)
	{
		SendRequest( "builder_tiles_refresh", { 'categoryid': the_select.value } );
	},
	
	_HandleBuilderTilesRefresh: function()
	{
		this.ShowWindow( 'builder_tiles' );
	},
		
	BuilderSetTooltip: function( the_event )
	{
		var the_window = GetWindow( "BuilderTileDetails" );
		var tooltip = the_window.GetActiveTooltip();
		var mapid = the_window.GetActiveMapid();
		SendRequest( "builder_set_tooltip", { 'tooltip': tooltip, 'mapid': mapid } );
	},
	
	BuilderToggleImpassable: function( the_event )
	{
		var the_window = GetWindow( "BuilderTileDetails" );
		var mapid = the_window.GetActiveMapid();
		SendRequest( "builder_toggle_impassable", { 'mapid': mapid } );
	},
	
	BuilderDeleteTile: function( the_event )
	{
		var the_window = GetWindow( "BuilderTileDetails" );
		var mapid = the_window.GetActiveMapid();
		
		SendRequest( "builder_delete_tile", { 'mapid': mapid } );
	},
	
	BuilderDeleteOverlays: function( the_event )
	{
		var the_window = GetWindow( "BuilderTileDetails" );
		var mapid = the_window.GetActiveMapid();
		var tileid = the_window.GetActiveTileid();
		var zindex = the_window.GetActiveZindex();
		
		SendRequest( "builder_delete_overlay", { 'mapid': mapid, 'tileid': tileid, 'zindex': zindex } );
	},
	
	HotkeyPressed: function( the_event )
	{
		if( enableKeyboardMove == 0 )
			return;
		
		switch( the_event.keyCode )
		{
			case 77:
				GetWindow( 'BuilderTiles' ).SetMode( 'move' );
				break;
			case 69:
				GetWindow( 'BuilderTiles' ).SetMode( 'edit' );
				break;
			case 65:
				GetWindow( 'BuilderTiles' ).SetMode( 'add' );
				break;
		}
		
		the_event.preventDefault();
	},
	
	BuilderEditTile: function( x, y )
	{
		// NOTE: x, y are offsets from the character's current position
		SendRequest( "builder_edit_tile", { 'offset_x': x, 'offset_y': y } );
	},
	
	BuilderAddTile: function( x, y )
	{
		var builder_window = GetWindow( "BuilderTiles" );
		if( builder_window == false )
			return;
		
		var tileid = builder_window.GetActiveTileid();
		if( tileid == 0 )
			return;
		
		// NOTE: x, y are offsets from the character's current position
		SendRequest( 'builder_set_tile', { 'x': x, 'y': y, 'tileid': tileid } );
	},
	
	BuilderUpdateTileOrder: function( mapid, new_order )
	{
		SendRequest( 'builder_update_tile_order', { 'mapid': mapid, 'new_order': new_order } );
	},
	
	BuilderNewScope: function( the_event )
	{
		SendRequest( 'builder_new_scope', 
						{	'name': $( 'builder_scope_name' ).value,
							'description': $( 'builder_scope_description' ).value,
							'defaulttileid': $( 'builder_scope_defaulttileid' ).value,
							'ishealer': $( 'builder_scope_ishealer' ).checked,
							'isshop': $( 'builder_scope_isshop' ).checked,
							'isquests': $( 'builder_scope_isquests' ).checked,
							'ispostoffice': $( 'builder_scope_ispostoffice' ).checked
						}
					);
	},
	
	BuilderAutoEdge: function( the_event )
	{
		SendRequest( 'builder_edgeit', { 'groupid': $('builder_edginggroupid').value } );
	},
	
	EnterPlayerHouse: function( the_event, userid )
	{
		SendRequest( 'enter_player_house', { 'owner': userid } );
		this.CloseWindow( 'customizehouse' );
	},
	
	BuilderUpdateScopeDetails: function()
	{
		// Gather the form data, and send it along
		var formdata = collectFormData( 'builder_scope_details_form' );
		SendRequest( 'builder_update_scope_details', formdata );
	},
	
	CustomizeHouse: function()
	{
		SendRequest( 'customize_player_house' );
	},
	
	_HandleCustomizeHouse: function()
	{
		this.ShowWindow( 'customizehouse' );
	},
	
	SetFacebookMode: function( mode )
	{
		this._is_facebook = mode;
	},
	
	IsFacebook: function()
	{
		return this._is_facebook;
	},
	
	GetAvailSkillById: function( skillid )
	{
		// TODO: look at available skills, and return an array with appropriate data about this skill
		return false;
	},

	GetPlayerStats: function()
	{
		return this._player_stats;
	},
	
	UpdateAvailSkills: function( skills )
	{
		this._player_avail_skills = skills;
	},
	
	GetAvailSkillById: function( skillid )
	{
		return this._player_avail_skills[skillid];
	},
	
	ShowAdminTools: function()
	{
		SendRequest( 'admintools' );
	},
	
	_HandleAdminTools: function()
	{
		this.ShowWindow( 'admintools' );
	},
	
	FindAvatarTileForUser: function( userid )
	{
		for( var i = 0, len = this._avatar_cache.length; i < len; i++ )
		{
			var av_tile = this._avatar_cache[i];
			
			if( av_tile.getAttribute( 'userid' ) == userid )
				return av_tile;
		}
		
		return null;
	},
	
	_HandleRefreshCharacter: function()
	{
		var char_window = GetWindow( 'CharGraph' );
		char_window.Handler( null );
	}
});

