
// REFERENCE
// =========
_TYPE 	= 0;
_HIDDEN = -1;

INIT_TRUE = true;
INIT_FALSE = false;


TOTAL_FIELDS		= 7;
TOTAL_TEMPLATES		= 2;		// NOT USED?


// TEMPLATES
// =========
TEMPLATE_PASSIVE	= 0;
TEMPLATE_SPELL		= 1;

DEFAULT_TEMPLATE = TEMPLATE_PASSIVE;


// IDS
// ===
ID_NAME_RANK		= 0;
ID_COST_RANGE		= 1;
ID_TIME_COOLDOWN	= 2;
ID_REAGENT			= 3;
ID_REQUIRES			= 4;
ID_STANCE			= 5;
ID_DESCRIPTION		= 6;



// SIZE
// ====
SIZE_TITLE			= 100;


// TYPE
// ====
TYPE_NORMALWRAP		= 1;
TYPE_REQUIRES		= 13;

TYPE_NAME_RANK		= 51;
TYPE_COST_RANGE		= 52;
TYPE_TIME_COOLDOWN	= 53;
TYPE_REAGENT		= 54;


// STYLE TYPE
// ==========
STYLE_TYPE = new Array();
STYLE_TYPE[ ID_NAME_RANK ]			= TYPE_NAME_RANK+SIZE_TITLE;
STYLE_TYPE[ ID_COST_RANGE ]			= TYPE_COST_RANGE;
STYLE_TYPE[ ID_TIME_COOLDOWN ]		= TYPE_TIME_COOLDOWN;
STYLE_TYPE[ ID_REQUIRES ]			= TYPE_REQUIRES;
STYLE_TYPE[ ID_STANCE ]				= TYPE_REQUIRES;
STYLE_TYPE[ ID_REAGENT ]			= TYPE_REAGENT;
STYLE_TYPE[ ID_DESCRIPTION ]		= TYPE_NORMALWRAP;


// COLOR
// =====
COLOR_GREY 		= 0;
COLOR_WHITE 	= 1;
COLOR_GREEN 	= 2;
COLOR_BLUE 		= 3;
COLOR_PURPLE 	= 4;
COLOR_ORANGE 	= 5;
COLOR_RED 		= 6;
COLOR_YELLOW 	= 7;


// DROPDOWN VALUES
// ===============
DROP_CUSTOM	= "Custom..";
DROP_NONE 	= "None";
DROP_HIDDEN	= "Hidden";


// STATIC VALUES
DROP_SPELLTEMPLATE = new Array(
	"Passive Ability",
	"Castable Ability"
);

DROP_SPELLRANK = new Array(
	DROP_NONE,
	"Racial",
	"Number"
);

DROP_COST = new Array(
	DROP_NONE,
	"Mana",
	"Rage",
	"Energy",
	DROP_CUSTOM
);

DROP_CASTTIME = new Array(
	DROP_HIDDEN,
	"Channeled",
	"Next Melee",
	"Attack Speed",
	"Instant (ability)",
	"Instant Cast (spell)",
	"Duration (sec)"
);

DROP_COOLDOWN = new Array(
	DROP_NONE,
	"Sec",
	"Min"
);

DROP_REQUIRES = new Array(
	DROP_NONE,
	"Melee Weapon",
	"Ranged Weapon",
	"Shields",
	DROP_CUSTOM
);

DROP_STANCE = new Array(
	DROP_NONE,
	"Battle Stance",
	"Battle / Defensive",
	"Battle / Berserker",
	"Defensive Stance",
	"Berserker Stance",
	"Aquatic Form",
	"Bear Form",
	"Cat Form",
	"Moonkin Form",
	"Travel Form",
	"Stealth"
);

DROP_BACKGROUND = new Array(
	"Random zone image",
	"Arcane",
	"Fire",
	"Frost",
	"Holy",
	"Nature",
	"Shadow"
);


// FORM ELEMENTS
// =============
FORM_ELEMENTS = new Array(
	"frm-name",
	"frm-rank",
	"frm-cost",
	"frm-range",
	"frm-time",
	"frm-cooldown",
	"frm-require",
	"frm-stance",
	"frm-reagent",
	"frm-description"
);

g_HideElements = new Array();
g_HideElements[ TEMPLATE_SPELL ]	= new Array( "" );
g_HideElements[ TEMPLATE_PASSIVE ]	= new Array( "frm-cost", "frm-range", "frm-time", "frm-cooldown", "frm-reagent" );


// DEFAULTS
// ========
g_Defaults = new Array();
g_Defaults[ ID_NAME_RANK ] 				= new Array( TYPE_NAME_RANK+SIZE_TITLE, "Spell Name", COLOR_WHITE );
g_Defaults[ ID_DESCRIPTION ] 			= new Array( TYPE_NORMALWRAP, "Enter description", COLOR_YELLOW );


// TEMPLATE DEFAULTS
// =================
g_TemplateDefaults = new Array();
g_TemplateDefaults[ TEMPLATE_SPELL ]	= new Array( ID_NAME_RANK, ID_DESCRIPTION );
g_TemplateDefaults[ TEMPLATE_PASSIVE ]	= new Array( ID_NAME_RANK, ID_DESCRIPTION );


// TEMPLATE IGNORE
// ===============
g_TemplateIgnore = new Array();
g_TemplateIgnore[ TEMPLATE_SPELL ]		= new Array( null );
g_TemplateIgnore[ TEMPLATE_PASSIVE ]	= new Array( ID_COST_RANGE, ID_TIME_COOLDOWN, ID_REAGENT );


// DEFAULT FIELD VALUES
// ====================
g_DefaultField[ "name_val" ]			= g_Defaults[ ID_NAME_RANK ][ 1 ];
g_DefaultField[ "rank_val" ]			= 1;
g_DefaultField[ "cost_val" ]			= 10;
g_DefaultField[ "cost_opt" ]			= "<Custom Unit>";
g_DefaultField[ "time_val" ]			= 3;
g_DefaultField[ "cooldown_val" ]		= 1;
g_DefaultField[ "require_opt" ]			= "<Custom Requirement>";
g_DefaultField[ "description_val" ]		= g_Defaults[ ID_DESCRIPTION ][ 1 ];


ERROR_BLUR_DEFAULT[ "name_val" ]		= true;
ERROR_BLUR_DEFAULT[ "description_val" ]	= true;

ERROR_BLUR_EMPTY[ "description_val" ]	= true;
ERROR_BLUR_EMPTY[ "name_val" ]			= true;


// REGULAR EXPRESSIONS
// ===================
var g_RENumbersOnly 	= new Array( "rank_val", "cost_val", "range_val", "time_val", "cooldown_val" );

var REGEXP_RACIAL		= /^([1-9]|[1-9][0-9]+)?$/;							// allows 1-99
var REGEXP_RANGE		= /^([1-9]|[1-9][0-9]+)?-?([1-9]|[1-9][0-9]+)?$/;	// allows 1*-99+

// Numbers only
for ( var i=0; i<g_RENumbersOnly.length; i++ )
	ERROR_KEYUP_REGEXP[ g_RENumbersOnly[ i ] ] = REGEXP_NUMBERS_ONLY;

ERROR_KEYUP_REGEXP[ "rank_val" ]	= REGEXP_RACIAL;
ERROR_KEYUP_REGEXP[ "range_val" ]	= REGEXP_RANGE;


// GLOBAL VARIABLES
// ================
g_CurrentTemplate 	= null;
g_DropLastValue 	= new Array();

g_AllValues			= new Array();
g_TemplateValues	= new Array();


for ( i=0; i<TOTAL_FIELDS; i++ )
{
	g_AllValues[ i ] = new Array();
	g_TemplateValues[ i ] = new Array();
}


// EXTERNAL INTERFACE
// ==================
function wowTip_init()
{
	// make sure browser is capable of using creator
	if ( !JS_GETELEMENTBYID || !JS_GETELEMENTSBYTAGNAME )
		return;

	// enable global events
	form_events( "frm-create" );

	// show form (javascript only)
	setDisplay( "TOOLTIP", "block" );
	setDisplay( "frm-create", "block" );

	// hide extra fields
	setDisplay( "rank_val", "none" );
	setDisplay( "cost_val", "none" );
	setDisplay( "cost_opt", "none" );
	setDisplay( "time_val", "none" );
	setDisplay( "cooldown_val", "none" );
	setDisplay( "require_opt", "none" );

	// fill in dropdown boxes
	addOptions( "tpl_drp", DROP_SPELLTEMPLATE );
	addOptions( "rank_drp", DROP_SPELLRANK );
	addOptions( "cost_drp", DROP_COST );
	addOptions( "time_drp", DROP_CASTTIME );
	addOptions( "cooldown_drp", DROP_COOLDOWN );
	addOptions( "require_drp", DROP_REQUIRES );
	addOptions( "stance_drp", DROP_STANCE );
	addOptions( "background_drp", DROP_BACKGROUND );

	iTemplate = DEFAULT_TEMPLATE;

	if ( getSelected( "tpl_drp" ) != DEFAULT_TEMPLATE )
		iTemplate = getSelected( "tpl_drp" );

	// check dynamic fields to see if they should be open

	updateDrop( document.getElementById( "rank_drp" ) );
	updateDrop( document.getElementById( "cost_drp" ) );
	updateDrop( document.getElementById( "time_drp" ) );
	updateDrop( document.getElementById( "cooldown_drp" ) );
	updateDrop( document.getElementById( "require_drp" ) );

	// set form fields / fill in defaults
	loadTemplate( iTemplate );
}


// gets values from form/defaults based on field id
function setValues( fieldId, Arr, bInit )
{
	switch ( fieldId )
	{
		case ID_NAME_RANK :
		{
			// { "text", iRank }
			Arr[ 0 ] = getValue( "name_val" );

			// check if blank
			if ( Arr[ 0 ] == "" )
				Arr[ 0 ] = g_DefaultField[ "name_val" ];

			if ( DROP_SPELLRANK[ getValue( "rank_drp" ) ] == "Racial" )
				Arr[ 1 ] = 0;
			else
			{
				Arr[ 1 ] = ( getValue( "rank_val" ) == "" || DROP_SPELLRANK[ getValue( "rank_drp" ) ] == DROP_NONE ) ? "" : getValue( "rank_val" );
			}

			// draw on form if initializing
			if ( bInit == INIT_TRUE )
				setValue( "name_val", Arr[ 0 ] );

			break;
		}

		case ID_COST_RANGE :
		{
			// { num, "cost", iRange );
			Arr[ 0 ] = getValue( "cost_val" );
			Arr[ 1 ] = ( Arr[ 0 ] == "" ) ? "" : getValue( "cost_opt" );
			Arr[ 2 ] = ( getValue( "range_val" ) == "" ) ? 0 : getValue( "range_val" ) ;

			break;
		}

		case ID_TIME_COOLDOWN :
		{
			// { iCastNum, iCastType, iCooldownNum, iCooldownType );

			Arr[ 0 ] = getValue( "time_val" );
			Arr[ 1 ] = getSelected( "time_drp" );

			if ( DROP_CASTTIME[ Arr[ 1 ] ].indexOf( "Duration" ) != -1 && Arr[ 0 ] == "" )
				Arr[ 1 ] = 0;

			// set correct label for instant (cast)
			else if ( getSelected( "time_drp" ) == 4 && getValue( "cost_opt" ) == "Mana" )
			{
				setSelected( "time_drp", 5 );
				Arr[ 1 ] = 5;
			}

			else if ( getSelected( "time_drp" ) == 5 && getValue( "cost_opt" ) != "Mana" )
			{
				setSelected( "time_drp", 4 );
				Arr[ 1 ] = 4;
			}

			else if ( DROP_CASTTIME[ Arr[ 1 ] ].indexOf( "Duration" ) != -1 )
				Arr[ 1 ] = (DROP_CASTTIME.length-1);

			Arr[ 2 ] = getValue( "cooldown_val" );
			Arr[ 3 ] = ( Arr[ 2 ] == "" ) ? 0 : getSelected( "cooldown_drp" );

			break;
		}

		case ID_REAGENT :
		{
			// { "text", color }
			Arr[ 0 ] = getValue( "reagent_val" );
			Arr[ 1 ] = COLOR_RED;

			break;
		}

		case ID_REQUIRES :
		{
			// { "text", color }
			Arr[ 0 ] = ( getValue( "require_opt" ) == DROP_NONE ) ? "" : getValue( "require_opt" );
			Arr[ 1 ] = COLOR_RED;

			break;
		}

		case ID_STANCE :
		{
			// { "text", color }
			Arr[ 0 ] = ( DROP_STANCE[ getValue( "stance_drp" ) ] == DROP_NONE ) ? "" : DROP_STANCE[ getValue( "stance_drp" ) ];
			Arr[ 1 ] = COLOR_RED;

			break;
		}

		case ID_DESCRIPTION :
		{
			// { "text", iColor }
			Arr[ 0 ] = ( getValue( "description_val" ) == "" ) ? g_Defaults[ ID_DESCRIPTION ][ 1 ] : getValue( "description_val" );
			Arr[ 1 ] = COLOR_YELLOW;

			// draw on form if initializing
			if ( bInit == INIT_TRUE )
			{
				setValue( "description_val", Arr[ 0 ] );
			}

			break;
		}
	}

	// Add type to array if initializing
	if ( bInit == INIT_TRUE )
		Arr.unshift( STYLE_TYPE[ fieldId ] );
}


function loadTemplate( iTemplate )
{
	g_bTemplateReady = false;

	var bFirstLoad = ( g_CurrentTemplate == null ) ? true : false;
	g_CurrentTemplate = iTemplate;

	for ( var i=0; i<TOTAL_FIELDS; i++ )
	{
		// copy defaults/existing values to array(s)
		if ( bFirstLoad )
		{
			setValues( i, g_TemplateValues[ i ], INIT_TRUE );

			// copy values to master value list
			g_TemplateValues[ i ].copy( g_AllValues[ i ] );
		}
		else
		{
			g_AllValues[ i ].copy( g_TemplateValues[ i ] );
		}

		// check if we are ignoring field on current template
		if ( g_TemplateIgnore[ iTemplate ].find( i ) != -1 )
			g_TemplateValues[ i ][ _TYPE ] = _HIDDEN;
	}

	// update form fields

	for ( var i=0; i<FORM_ELEMENTS.length; i++ )
	{
		if ( g_HideElements[ iTemplate ].find( FORM_ELEMENTS[ i ] ) != -1 )
			setDisplay( FORM_ELEMENTS[ i ], "none" );
		else
		{
			setDisplay( FORM_ELEMENTS[ i ], "block" );
		}
	}

	g_bTemplateReady = true;

	// send to flash
	submitTemplate();
}


function submitTemplate()
{
	// send to flash
	if ( g_bFlashReady )
		findSWF( _SWF ).flash_init_values( g_TemplateValues );
}


// UPDATE INFO
// ===========
function submitInfo( infoId, iType, infoArr )
{
	if ( g_bFlashReady )
	{
		// send to flash
		findSWF( _SWF ).flash_update_values( infoId, iType, infoArr );

		// save values for later use
		infoArr.unshift( iType );
		infoArr.copy( g_AllValues[ infoId ] );
	}
}

function updateInfo( _this )
{
	var iField;
	var textArr = new Array();

	switch ( _this.id )
	{
		case "name_val" :
		case "rank_drp" :
		case "rank_val" :
		{
			iField = ID_NAME_RANK;
			break;
		}

		case "cost_drp" :
		case "cost_val" :
		case "cost_opt" :
		case "range_drp" :
		case "range_val" :
		{
			iField = ID_COST_RANGE;

			// modify instant label as well
			setValues( ID_TIME_COOLDOWN, textArr, INIT_FALSE );
			submitInfo( ID_TIME_COOLDOWN, STYLE_TYPE[ ID_TIME_COOLDOWN ], textArr );

			textArr.length = 0;

			break;
		}

		case "time_drp" :
		case "time_val" :
		case "cooldown_drp" :
		case "cooldown_val" :
		{
			iField = ID_TIME_COOLDOWN;
			break;
		}

		case "require_drp" :
		case "require_opt" :
		{
			iField = ID_REQUIRES;
			break;
		}

		case "stance_drp" :
		{
			iField = ID_STANCE;
			break;
		}

		case "reagent_val" :
		{
			iField = ID_REAGENT;
			break;
		}

		case "description_val" :
		{
			iField = ID_DESCRIPTION;
			break;
		}
	}

	// send to flash
	if ( iField != undefined )
	{
		setValues( iField, textArr, INIT_FALSE );
		submitInfo( iField, STYLE_TYPE[ iField ], textArr );
	}
}

// UPDATE DROPDOWN(S)
// ==================
function updateDrop( _this )
{
	switch ( _this.id )
	{
		case "tpl_drp" :
		{
			loadTemplate( getSelected( _this.id ) );
			break;
		}

		case "rank_drp" :
		{
			if ( DROP_SPELLRANK[ getValue( _this.id ) ] == "Number" )
			{
				setDisplay( "rank_val", "inline" );

				if ( getValue( "rank_val" ) == "" || getValue( "rank_val" ) == 0 )
					setValue( "rank_val", g_DefaultField[ "rank_val" ] );
			}
			else
			{
				setDisplay( "rank_val", "none" );
			}

			break;
		}

		case "cost_drp" :
		{
			if ( DROP_COST[ getValue( _this.id ) ] == DROP_NONE )
			{
				setDisplay( "cost_val", "none" );
				setDisplay( "cost_opt", "none" );

				setValue( "cost_val", "" );
				setValue( "cost_opt", "" );
			}
			else
			{
				setDisplay( "cost_val", "inline" );
				setDisplay( "cost_opt", "none" );

				if ( DROP_COST[ getValue( _this.id ) ] == DROP_CUSTOM )
				{
					setDisplay( "cost_opt", "inline" );

					if ( getValue( "cost_val" ) == "" )
						setValue( "cost_val", g_DefaultField[ "cost_val" ] );

					if ( getValue( "cost_opt" ) == "" || ( g_DropLastValue[ _this.id ] != null && DROP_COST[ g_DropLastValue[ _this.id ] ] != DROP_CUSTOM ) )
						setValue( "cost_opt", g_DefaultField[ "cost_opt" ] );
				}
				else
				{
					setValue( "cost_opt", DROP_COST[ getValue( "cost_drp" ) ] );

					if ( getValue( "cost_val" ) == "" )
						setValue( "cost_val", g_DefaultField[ "cost_val" ] );
				}
			}

			break;
		}

		case "time_drp" :
		{
			if ( DROP_CASTTIME[ getValue( _this.id ) ].indexOf( "Duration" ) != -1 )
			{
				setDisplay( "time_val", "inline" );

				if ( getValue( "time_val" ) == "" )
					setValue( "time_val", g_DefaultField[ "time_val" ] );
			}
			else
			{
				setDisplay( "time_val", "none" );
			}

			break;
		}

		case "cooldown_drp" :
		{
			if ( DROP_COOLDOWN[ getValue( _this.id ) ] == DROP_NONE )
				setDisplay( "cooldown_val", "none" );
			else
			{
				setDisplay( "cooldown_val", "inline" );

				if ( getValue( "cooldown_val" ) == "" )
					setValue( "cooldown_val", g_DefaultField[ "cooldown_val" ] );
			}

			break;
		}

		case "require_drp" :
		{
			if ( DROP_REQUIRES[ getValue( _this.id ) ] == DROP_CUSTOM )
			{
				setDisplay( "require_opt", "inline" );

				if ( getValue( "require_opt" ) == "" || ( g_DropLastValue[ _this.id ] != null && DROP_REQUIRES[ g_DropLastValue[ _this.id ] ] != DROP_CUSTOM ) )
					setValue( "require_opt", g_DefaultField[ "require_opt" ] );
			}
			else
			{
				setDisplay( "require_opt", "none" );
				setValue( "require_opt", DROP_REQUIRES[ getValue( _this.id ) ] );
			}

			break;
		}

		case "background_drp" :
		{
			if ( getSelected( _this.id ) == 0 )
			{
				var iRand = 0;
				while ( iRand < 6 )
					iRand = Math.round( 12*Math.random() )

 				document.getElementById( "create_bg" ).style.backgroundImage = "url('/img/view/bg/" + iRand + ".jpg')";
			}
			else
			{
				document.getElementById( "create_bg" ).style.backgroundImage = "url('/img/view/bg/" + (Number(getSelected( _this.id ))-1) + ".jpg')";
			}
		}
	}

	g_DropLastValue[ _this.id ] = getValue( _this.id );

	if ( g_bTemplateReady )
		updateInfo( _this );
}


function form_validate()
{
	if ( DROP_COST[ getSelected( "cost_drp" ) ] == DROP_CUSTOM && ( getValue( "cost_opt" ) == "" || getValue( "cost_opt" ) == g_DefaultField[ "cost_opt" ] ) )
	{
		setSelected( "cost_drp", 0 );
		setValue( "cost_opt", "" );
		setValue( "cost_val", "" );

		setDisplay( "cost_val", "none" );
		setDisplay( "cost_opt", "none" );
	}

	if ( DROP_COST[ getSelected( "require_drp" ) ] == DROP_CUSTOM && ( getValue( "require_opt" ) == "" || getValue( "require_opt" ) == g_DefaultField[ "require_opt" ] ) )
	{
		setSelected( "require_drp", 0 );
		setValue( "require_opt", "" );

		setDisplay( "require_opt", "none" );
	}

	if ( getValue( "time_val" ) && getSelected( "time_drp" ) != DROP_CASTTIME.length-1 )
		setValue( "time_val", "" );


	var bValidForm = true;

	// regular expressions / error checking
	for ( var i=0; i<document.forms[ "frm-create" ].elements.length; i++ )
	{
		if ( document.forms[ "frm-create" ].elements[i].type == "text" || document.forms[ "frm-create" ].elements[i].type == "textarea" )
		{
			var _this = document.forms[ "frm-create" ].elements[i];
			if ( !ERROR_KEYUP_test( _this ) || !ERROR_BLUR_test( _this ) )
				bValidForm = false;
		}
	}

	if ( !bValidForm )
		window.location.hash = "status_box";

	else
	{
		document.forms[ "frm-create" ].submit();
	}
}