// TWITSTEPS.home
TWITSTEPS.home = function(){

	// shortcuts
	var _d = YAHOO.util.Dom,
		_e = YAHOO.util.Event,
		_c = YAHOO.util.Connect,
		_s = _d.setStyle;
	
	// is user logged in
	var bLoggedIn = null;

	// unsername input 
	var elInputUN = _d.get('twitsteps_login_un_input');

	// password input 
	var elInputPW = _d.get('twitsteps_login_pw_input');

	// the login gif container
	var elLoginLoading = _d.get('twitsteps_login_loading');
	
	// the 'sign in' button
	var elLoginSignin = _d.get('twitsteps_login_signin');

	// will represent the request object
	var oRequest = null;

	// will repreesent the username & password
	var sUserName = null,
		sPassword = null;

	/** 
	* @description Is executed regardless of 
	* success or failure
	* @method doLoginStart
	* @private
	*/
	var doLoginStart = function(){

		// reveal the anim gif
		_s( elLoginLoading, 'display', 'block' );

		// disable form elements
		elInputUN.disabled = true;
		elInputPW.disabled = true;
		elLoginSignin.disabled = true;

	}

	/** 
	* @description Is executed regardless of 
	* success or failure
	* @method doLoginComplete
	* @private
	*/
	var doLoginComplete = function(){

		// clear password
		sPassword = null;

		// kill the password field value
		elInputUN.value = '';
		elInputPW.value = '';

		// hide the anim gif
		_s( elLoginLoading, 'display', 'none' );

		// re-enable elements
		elInputUN.disabled = false;
		elInputPW.disabled = false;
		elLoginSignin.disabled = false;

	}

	/** 
	* @description Success handler
	* @method handleSuccess
	* @private
	*/
	var handleSuccess = function(o){

		// get the ret val
		try{
			bLoggedIn = o.responseXML.documentElement.getElementsByTagName('result')[0].firstChild.nodeValue;
			bLoggedIn = (bLoggedIn == 1) ? true : false;
		}
		catch(e){
			bLoggedIn = false;
		}

		// if the user is logged in, 
		if (bLoggedIn){

			// forward to admin
			window.location.href = 'admin_home';

			// execute login done steps
			doLoginComplete();

		}
		else{

			// alert user to err
			handleFailure();

		}

	}

	/** 
	* @description Failure handler
	* @method handleFailure
	* @private
	*/
	var handleFailure = function(){

		// execute login done steps
		doLoginComplete();

		// alert user
		alert(TWITSTEPS.strings.loginFailed);

	}

	var doSignIn = function(e){

		// stop the click event from bubbling
		_e.stopEvent(e)

		// get out if call in progress (in case
		// of multiple clicking)
		if (_c.isCallInProgress(oRequest)){
			return;
		}

		// get the input values
		sUserName = elInputUN.value;
		sPassword = elInputPW.value;

		// only move forward if neither val is 
		// empty
		if ((sUserName) && (sPassword)){

			// execute start steps
			doLoginStart();
		
			// make the call
			oRequest = _c.asyncRequest(
				'GET',
				g_sBasePath + 'services/verify?un=' + sUserName + '&pw=' + sPassword,
				{success:handleSuccess,	failure:handleFailure}
			);

		}

	}

	_e.on( elLoginSignin, 'click', doSignIn );
	_e.on( 'login', 'submit', doSignIn );

}();



