/**
 *	Master autoloading class to handle
 */
UserStatus_Master = {
	
	/**
	 *	The main YUI panel holding the div
	 *
	 *	@var YAHOO.widget.Panel
	 */
	mainWindow : null,
	
	/**
	 *	The target div to load the edit status menu in
	 *
	 *	@var string
	 */
	targetDiv : null,
	
	
	/**
	 *	Event that fires after the status is changed
	 *
	 *	@var YAHOO.util.CustomEvent
	 */	
	onUserStatusChange : new YAHOO.util.CustomEvent("userStatusChange"),	
	
	/**
	 * Function:	loadEditStatus
	 *	Purpose:		Load the template for the edit status form
	 * 
	 * @param string loadTarget ~Target to load - either popup or div
	 *	@return void
	 */
	loadEditStatus: function (loadTarget)	
	{
		
		//No target specified or popup - target is popup
		if(loadTarget == undefined || loadTarget == 'popup') {
			
			UserStatus_Master.targetDiv = 'popup';			
		}
		else {
			
			//Try to grab the div specified as target
			var elem = document.getElementById(loadTarget);

			//Check to see if the element is valid div			
			if(elem != undefined) {
			
				//Element exists - store as the target
				UserStatus_Master.targetDiv = loadTarget;
			}
			
			else {
				
				//Element is invalid - set display to popup
				UserStatus_Master.targetDiv = 'popup';
			}			
		}
		
		//Initialize the array and create the AJAX request parameters
		var params = new Array();

		params['responseFunction'] = UserStatus_Master.displayEditStatus;
		params['templateName'] = 'common_components/user_status/edit_status_autoload';
		params['action'] = 'loadTemplate';
		params['temType'] = 'file';
		params['responseType'] = 'JSON';
		params['handlerName'] = 'one_ajax_templateLoader';
		params['requestType'] = 'class';

		OneAjax.request(params);	
	},	
	
	/**
	 * Function:	displayEditStatus
	 *	Purpose:		Display the edit status template in the correct location
	 *
	 *	@param array resp ~The response from the AJAX request
	 *	@return void	 
	 */	
	displayEditStatus: function (resp)
	{	
		
		//Check what the target is
		if (UserStatus_Master.targetDiv == 'popup') {
			
			//The width to display
			var windowWidth = 300;
			var windowWidthString = windowWidth + 'px';		

			//Get the main region for the ticker
			var region = YAHOO.util.Dom.getRegion('editUserStatusLink');

			//Grab the current scroll location of the main page
			var currentY = region.bottom + 5;

			//Get the x value to center the window
			var currentX = region.left;			

			//Create the new YUI dialog
			UserStatus_Master.mainWindow = new YAHOO.widget.Panel('main_window', {
					modal : false,
					close : true,
					width : windowWidthString,
					zIndex : 1600,
					draggable : false,
					fixedCenter : false,
					y: currentY,
					x: currentX				
				}
			);

			//Set the header from parameter and set body from response text
			UserStatus_Master.mainWindow.setHeader('Edit Your Status');
			UserStatus_Master.mainWindow.setBody(resp['output']);

			//Display the Panel
			UserStatus_Master.mainWindow.render(document.body);
			UserStatus_Master.mainWindow.show(); //Show the dialog box		
		}
		else {
			
			//Try to grab the div specified as target
			var elem = document.getElementById(UserStatus_Master.targetDiv);
			
			//Validate and set the inner html
			if(elem != undefined) {
				
				elem.innerHTML = resp['output'];
			}			
		}
		
		return;		
	},
		
	/**
	 *	Function:	updateStatus
	 *	Purpose:		To make the ajax request to update the users status/mood
	 *
	 */
	updateStatus : function(statusId, type, moodId)
	{
		
		//check to ensure that the user actually did send a status
		var statusFields = document.getElementsByName(statusId);

		var status = window.currentStatus;
		var compare;

		for(var i = 0; i < statusFields.length; i++){
			if(statusFields[i].tagName == 'select'){
				var index = statusFields[i].selectedIndex;
				compare = statusFields[i].options[index].text;
				statusIndex = index;
			}
			else{
				compare = statusFields[i].value;
			}
			
			if(compare != currentStatus && compare != ''){
				status = statusFields[i].value;
				break;
			}
		}	
		
		//No status update - leave it be...
		if (!status){
			//do nothing:
			return false;
		}		

		
		var moodFields = document.getElementsByName(moodId);

		var mood;

		for(var i = 0; i < moodFields.length; i++){
			var moodOptIndex = moodFields[i].selectedIndex;
			mood = moodFields[i].value;

			if(moodFields[i].options[moodOptIndex].text != currentMood){
				mood = moodFields[i].value;
				moodIndex = moodOptIndex;
				break;
			}
		}		
		
		//Try to grab the type
		typeElem = document.getElementById('type');
		
		if(typeElem == undefined) {
			
			//no type specified
			return false;
		}				

		//Change the button during update				
		document.getElementById('status_update_button').style.display = 'none';
		document.getElementById('status_updating_button').style.display = 'inline';		
		
		//Set the listener to check if the status needs to be updated
		UserStatus_Master.onUserStatusChange.subscribe(UserStatus_Master.reloadStatusText);
		
		//Initialize the array and create the AJAX request parameters
		var params = new Array();

		params['responseFunction'] = UserStatus_Master.updateStatusResults;
		params['status'] = status;
		params['type'] = type;
		params['mood'] = mood;
		params['action'] = 'updateStatus';
		params['responseType'] = 'JSON';
		params['responseFormat'] = 'JSON';
		params['handlerName'] = 'one_ajax_statusHandler';
		params['requestType'] = 'class';
		params['sdb'] = 1;

		//Make the request
		OneAjax.request(params);		
		
		
	},
	
	/**
	 *	Function:	updateStatusResults
	 *	Purpose:		Try to update the status results...
	 *
	 */
	updateStatusResults : function(resp)
	{
		//Reset the buttons
		document.getElementById('status_updating_button').style.display = 'none';
		document.getElementById('status_update_button').style.display = 'inline';		
		
		if(resp['status'] == 1) {
			UserStatus_Master.closeWindow();
			UserStatus_Master.onUserStatusChange.fire(resp);
		}
		else {
			UserStatus_Master.onUserStatusChange.unsubscribeAll();
		}
				
		return;

	},	
	
	/**
	 *	Function:	reloadStatusText
	 *	Purpose:		Loop through the page and update the new status/mood
	 *
	 */
	reloadStatusText : function(type,response,me)
	{
		//Your here, now unsubscribe...
		UserStatus_Master.onUserStatusChange.unsubscribeAll();
		
		window.currentStatus = response[0].status_text;
		window.currentMood = response[0].mood_text;

		//Find all containers that have the name profile_status_text
		var statusTextDivs = OneUtil.getElementsByName('profile_status_text');

		for(var i = 0; i < statusTextDivs.length; i++){
			if(statusTextDivs[i]){
				statusTextDivs[i].innerHTML = response[0].status_text;
			}
		}

		var statusIcons = OneUtil.getElementsByName('status_icon');

		for(i = 0; i < statusIcons.length; i++){
			if(statusIcons[i] && response[0].status_icon != ''){
				statusIcons[i].src = response[0].status_icon;
			}
			else if(!statusIcons[i] && response[0].status_icon != ''){
				var img = new Image();
				img.src = response[0].status_icon;
				statusIcons[i].parentNode.appendChild(img);
			}
			else if(statusIcons[i] && response[0].status_icon == ''){
				statusIcons[i].parentNode.removeChild(statusIcons[i]);
			}
		}

		var moodTexts = OneUtil.getElementsByName('profile_mood_text');

		for(i = 0; i < moodTexts.length; i++){
			if(moodTexts[i]){
				moodTexts[i].innerHTML = response[0].mood_text;
			}
		}

		var moodIcons = OneUtil.getElementsByName('mood_icon');

		for(i = 0; i < moodIcons.length; i++){
			if(moodIcons[i]){
				moodIcons[i].src = response[0].mood_icon;
			}
		}

		var moodFields = OneUtil.getElementsByName('mood_text');

		for(i = 0; i < moodFields.length; i++){
			if(moodFields[i]){
				moodFields[i].selectedIndex = moodIndex;
			}
		}

		var statusFields = OneUtil.getElementsByName('status_text');
		var compare;

		for(var i = 0; i < statusFields.length; i++){
			if(statusFields[i].tagName == 'select'){
				statusFields[i].selectedIndex = statusIndex;
			}
			else{
				statusFields[i].value = response[0].status_text;
			}
		}
		
		return;		
	},

	/**
	 *	Function:	listenEnterPress
	 *	Purpose:		Wait to see if the user presses enter when filling out status
	 *
	 */
	listenEnterPress : function(e, el, type, mood) {

		//var to store the keycode
		var keycode;
		
		//Grab the keycode
		if (window.event) keycode = window.event.keyCode;
		else if (e) keycode = e.which;
		else return true;

		//Check if the keycode is 'enter'
		if (keycode == 13) {
			document.getElementById(el).blur();
			UserStatus_Master.updateStatus(el, type, mood);
			return false;
		}		
		
	},
	
	/**
	 *	Function:	closeWindow
	 *	Purpose:		Close and destroy the window
	 *
	 *	@return void
	 */
	closeWindow : function() {
		
		//Make sure the window exists
		if(UserStatus_Master.mainWindow != null) {
			//Hide and destroy the window
			UserStatus_Master.mainWindow.hide();
			UserStatus_Master.mainWindow = null;		
		}
	}	
}
