/**
* 	This file is used for YUI functionality for sending/deleting quick
* 	actions.  It creates handlers to catch the required user operations.  
*	It also performs AJAX requests to update the database and redisplay
*	the results.
*/

// Declare the Global Variables YUI variables
var send_qa;
var confirm_delete;
var panel_success;
var dialog_success;

//Variables to pass static values around the script
var action_name;
var user_to_name;
var action_message;
var user_to;
var user_from;
var qa_user_id;
var del_type;
var user_id;



/**************************************************************************************
*
*	FUNCTIONS TO SUBMIT QUICK ACTION
*
***************************************************************************************/

//-------------------------------------------------------------------------------------
/*
*	Function: handleSubmit
*	
*	When a user submits a quick action form, this function will be called.  It collects
*	the quick_action value from the form, the current url, and uses the global variables
*	populated from the user click to create the post request through AJAX.
*
*	Params:
*		none
*
*	Returns:
*		none
*/
var handleSubmit = function() {

	var data = this.getData();
	var post_data = "qa_id=" + data.action + "&user_to=" + data.user_to + "&user_from=" + data.user_from + "&action=send";
	var URL = unescape(location.href);
	var end_domain_index = URL.indexOf('/',7);
	var post_path = URL.substring(0,end_domain_index) + "/blog/submit_quick_action.one";
	
	var request = YAHOO.util.Connect.asyncRequest('POST', post_path, callback, post_data);
	
};

//-------------------------------------------------------------------------------------
/*
*	Function: handleCancel
*	
*	This will cancel the YUI dialog when a user clicks cancel
*
*	Params:
*		none
*
*	Returns:
*		none
*/
var handleCancel = function() {

	this.cancel();
};

//-------------------------------------------------------------------------------------
/*
*	Function: handleSuccess
*	
*	If the AJAX submission is successful, this function will hide the dialog box, generate
*	the parameters for the success panel, and finally show the success panel to the user
*
*	Params:
*		@param o		- The response from the server AJAX request
*
*	Returns:
*		none
*/
var handleSuccess = function(o) {
	
	send_qa.hide();
	send_qa = null;
	
	if (dialog_success == null) {
		
		dialog_success = new YAHOO.widget.SimpleDialog("dialog_success", { 
				width : "250px",
				fixedcenter : true,
				visible : false,
				constraintoviewport : true,
				draggable : false,
				modal : true,
				close : false,
				buttons : [ { text:"OK", handler:handleClose, isDefault:true } ]			
			}
		);		
	}
	
	dialog_success.setHeader("Status");
	dialog_success.setBody(o.responseText);
		
	dialog_success.render(document.body);
	dialog_success.show(); //Show the dialog box	
	

	
};

//-------------------------------------------------------------------------------------
/*
*	Function: handleFailure
*	
*	If the AJAX submission fails, this function will hide the dialog box, generate
*	the parameters for the success panel, and finally show the success panel to the user
*
*	Params:
*		@param o		- The response from the server AJAX request
*
*	Returns:
*		none
*/
var handleFailure = function(o) {

	alert("Submission failed: " + o.status);
};


/**************************************************************************************
*
*	FUNCTIONS TO DELETE QUICK ACTION
*
***************************************************************************************/

//-------------------------------------------------------------------------------------
/*
*	Function: handleSubmit
*	
*	When a user submits a quick action form, this function will be called.  It collects
*	the quick_action value from the form, the current url, and uses the global variables
*	populated from the user click to create the post request through AJAX.
*
*	Params:
*		none
*
*	Returns:
*		none
*/
var handleYes = function() {
	
	//Set up the URL for submission
	var URL = unescape(location.href);
	var end_domain_index = URL.indexOf('/',7);
	var post_path = URL.substring(0,end_domain_index) + "/blog/submit_quick_action.one";

	if (del_type == 1) {
		var post_data = "qa_user_id=" + qa_user_id + "&action=del";
		var request = YAHOO.util.Connect.asyncRequest('POST', post_path, callbackDelete, post_data);
	}
	else if (del_type == 2) {
		var post_data = "user_id=" + user_id + "&action=all";
		var request = YAHOO.util.Connect.asyncRequest('POST', post_path, callbackDelete, post_data);		
	}
};

//-------------------------------------------------------------------------------------
/*
*	Function: handleCancel
*	
*	This will cancel the YUI dialog when a user clicks cancel
*
*	Params:
*		none
*
*	Returns:
*		none
*/
var handleNo = function() {

	this.cancel();
};

//-------------------------------------------------------------------------------------
/*
*	Function: handleDeleteSuccess
*	
*	If the AJAX submission is successful, this function will hide the dialog box, generate
*	the innner html for the side and main panels, and set the new values for innerHTML
*
*	Params:
*		@param o		- The response from the server AJAX request
*
*	Returns:
*		none
*/
var handleDeleteSuccess = function(o) {

	confirm_delete.hide();
	confirm_delete = null;
	
	//Variables to hold different site div htmls
	var for_menu;
	var for_list;
	
	//Split the response text into the two divs
	var array_list = o.responseText.split("|NEXT|");
	
	//Set the variables	
	for_menu = array_list[0];
	for_list = array_list[1];
	
	//Set the innerHTML of the given divs
	document.getElementById("quick_action").innerHTML = for_list;
	document.getElementById("qa_on_menu").innerHTML = for_menu;
	
	
};

//-------------------------------------------------------------------------------------
/*
*	Function: handleDeleteFailure
*	
*	If the AJAX submission fails, this function will hide the dialog box, generate
*	the parameters for the success panel, and finally show the success panel to the user
*
*	Params:
*		@param o		- The response from the server AJAX request
*
*	Returns:
*		none
*/
var handleDeleteFailure = function(o) {

	alert("Unable to delete: " + o.status);
};


//-------------------------------------------------------------------------------------
/*
*	Function: handleClose
*	
*	Close the success dialog and set it to null
*
*	Params:
*		@param o		- The response from the server AJAX request
*
*	Returns:
*		none
*/
var handleClose = function() {

	dialog_success.hide();
	dialog_success = null;
};

/**************************************************************************************
*
*	UNIVERSAL PAGE LOAD
*
***************************************************************************************/


//The callback functions for the new action AJAX request
var callback = { 
	success: handleSuccess,
	failure: handleFailure 
};

//The callback functions for the delete action AJAX request
var callbackDelete = { 
	success: handleDeleteSuccess,
	failure: handleDeleteFailure 
};


/**************************************************************************************
*
*	LINK CLICK EVENT HANDLERS
*
***************************************************************************************/

//-------------------------------------------------------------------------------------
/*
*	Function: quick_action_click
*	
*	Populate the global variables based on user input.  Params generated in smarty template
*	based on the user specific quick actions
*
*	Params:
*		@param in_user_to		- The recipient of the new quick action
*		@param in_user_from		- The sender of the new quick action
*		@param in_user_name		- The name of recipient (for display)
*		@param in_action_noun	- The noun/verb used to describe quick action (for display)
*
*	Returns:
*		none
*/
var quick_action_click = function(in_html,in_member_to) {

		
	if (send_qa == null) {
		//Initialize the send_qa object
		send_qa = new YAHOO.widget.Dialog("send_qa", {   //Dialog Settings 
				width : "250px",
				fixedcenter : true,
				visible : false,
				constraintoviewport : true,
				draggable : false,
				modal : true,
				close : true,
				buttons : [ { text:"Submit", handler:handleSubmit, isDefault:true },
						  { text:"Cancel", handler:handleCancel } ]
			} 
		);
	}

	send_qa.setHeader("To: " + unescape(in_member_to));
	send_qa.setBody(unescape(in_html));


	if(dialog_success != null) {
		dialog_success.hide();
		dialog_success = null;
	}


		
	send_qa.render(document.body);
	send_qa.show(); //Show the dialog box
};


//-------------------------------------------------------------------------------------
/*
*	Function: quick_action_click_delete
*	
*	Populate the global variables based on user input.  Params generated in smarty template
*	based on the user specific quick actions
*
*	Params:
*		@param in_qa_user_id	- The unique ID of the quick action
*		@param in_action_noun	- The noun/verb used to describe quick action (for display)
*
*	Returns:
*		none
*/
var quick_action_click_delete = function(in_qa_user_id,in_action_noun) {

	if (confirm_delete == null) {
		
		confirm_delete = new YAHOO.widget.SimpleDialog("confirm_diag", { 
				width : "250px",
				fixedcenter : true,
				visible : false,
				constraintoviewport : true,
				draggable : false,
				modal : true,
				close : false,
				buttons : [ { text:"Yes", handler:handleYes, isDefault:true },
						  { text:"No", handler:handleNo } ]			
			}
		);		
	}
	
	qa_user_id = in_qa_user_id;
	action_name = in_action_noun;
	del_type = 1;
	
	confirm_delete.setHeader("Warning!");
	confirm_delete.setBody("Are you sure you want to delete this " + action_name + "?");
	confirm_delete.cfg.setProperty("icon",YAHOO.widget.SimpleDialog.ICON_WARN);
	
	confirm_delete.render(document.body);
	confirm_delete.show(); //Show the dialog box
};

//-------------------------------------------------------------------------------------
/*
*	Function: quick_action_click_delete_all
*	
*	Populate the global variables based on user input.  Params generated in smarty template
*	based on the user specific quick actions
*
*	Params:
*		@param in_qa_user_id	- The unique ID of the quick action
*		@param in_action_noun	- The noun/verb used to describe quick action (for display)
*
*	Returns:
*		none
*/
var quick_action_click_delete_all = function(in_user_id,in_action_noun) {


	if (confirm_delete == null) {
		
		confirm_delete = new YAHOO.widget.SimpleDialog("confirm_diag", { 
				width : "250px",
				fixedcenter : true,
				visible : false,
				constraintoviewport : true,
				draggable : false,
				modal : true,
				close : false,
				buttons : [ { text:"Yes", handler:handleYes, isDefault:true },
						  { text:"No", handler:handleNo } ]			
			}
		);		
	}
	
	user_id = in_user_id;
	action_name = in_action_noun;
	del_type = 2;
	
	confirm_delete.setHeader("Warning!");
	confirm_delete.setBody("Are you sure you want to delete all " + action_name + "s?");
	confirm_delete.cfg.setProperty("icon",YAHOO.widget.SimpleDialog.ICON_WARN);
	
	confirm_delete.render(document.body);
	confirm_delete.show(); //Show the dialog box
};


