///////////////////////////
//  ajax functions for tags
///////////////////////////


//Global vars for YUI/Params
var flag_dialog;
var response_dialog;

//-------------------------------------------------------------------------------------
/*
*	Function: handle_submit_flag
*	
*	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 handle_submit_flag = function() {

	var data = this.getData();
	var post_data = '';

	//Loop through all post data and create the post string
	for (key in data) {
		post_data += key + "=" + data[key] + "&";
	}
	
	//Add the action to the post data
	post_data += 'action=post_flag';
	
	var post_path = "/resources/ajax/_content_flagging.one";
	
	var request = YAHOO.util.Connect.asyncRequest('POST',post_path,{success: handle_flag_success,failure: ajax_failure}, post_data);
	

};

//-------------------------------------------------------------------------------------
/*
*	Function: handle_flag_success
*	
*	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 handle_flag_success = function(o) {

	//Hide and destroy the flag dialog form
	flag_dialog.hide();
	flag_dialog = null;
	
	if (response_dialog == null) {
		
		response_dialog = new YAHOO.widget.SimpleDialog("response_dialog", { 
				width : "250px",
				fixedcenter : true,
				visible : false,
				constraintoviewport : true,
				draggable : false,
				modal : true,
				close : false,
				buttons : [ { text:"OK", handler:handle_close, isDefault:true } ]			
			}
		);		
	}
	
	//Set the title and content of the YUI window
	response_dialog.setHeader("Flag Status...");
	response_dialog.setBody(o.responseText);
	
	//Render and display the YUI dialog
	response_dialog.render(document.body);
	response_dialog.show(); 
	
};

//-------------------------------------------------------------------------------------
/*
*	Function: display_content_form
*	
*	Initializes the post vars and path from the onClick call, then makes ajax call to get the formatted
*	content reporting form
*
*	Params:
*		@param in_content		- The ID of the content to flag
*		@param in_content_type	- The type of content being flagged
*		@param in_uid			- The user_id of the reporter
*		@param in_flag_type		- The flag type being sent
*
*	Returns:
*		none
*/
var display_content_form = function(in_content,in_content_type,in_uid,in_flag_type)
{	
	/*
	//Set the post data and path
	var post_data = 'content_id=' + in_content + '&user_id=' + in_uid + '&flag_type=' + in_flag_type + '&content_type=' + in_content_type + '&action=display_form';
	var post_path = "/resources/ajax/_content_flagging.one";
	
	//Make the AJAX call
	var request = YAHOO.util.Connect.asyncRequest('POST',post_path,{success: display_form_success,failure: ajax_failure}, post_data);	
	
	*/
	
	OneScript.exec('Moderation_Flag','flagContent',in_content,in_uid);
	
};


//-------------------------------------------------------------------------------------
/*
*	display_form_success
*		On success, initialize the flag_dialog YUI window, and set the form based on the AJAX response.
*
*	@param	o		The response from the AJAX call
*/
var display_form_success = function(o) {

	if (flag_dialog == null) {
		//Yes - Initialize the flag_dialog object
		flag_dialog = new YAHOO.widget.Dialog("flag_dialog", {   //Dialog Settings 
				width : "500px",
				fixedcenter : true,
				visible : false,
				constraintoviewport : true,
				draggable : false,
				modal : true,
				close : true,
				buttons : [ { text:"Submit Flag", handler:handle_submit_flag, isDefault:true },
						  { text:"Cancel", handler:handle_cancel } ]
			} 
		);
	}	
	
	//Set the title and content of the YUI window
	flag_dialog.setHeader("<span>Report Content:</span>");
	flag_dialog.setBody(unescape(o.responseText));
	
	//Render and display the YUI dialog
	flag_dialog.render(document.body);
	flag_dialog.show();	
	
};


//-------------------------------------------------------------------------------------
/*
*	Default AJAX failure function - displays the error message to the user
*/
var ajax_failure = function(o) {

	alert(o.responseText);
	
};

//-------------------------------------------------------------------------------------
/*
*	Default window closing function
*/
var handle_close = function() {

	response_dialog.hide();
	response_dialog = null;
};

//-------------------------------------------------------------------------------------
/*
*	Default handler for cancelling a dialog
*/
var handle_cancel = function() {

	this.cancel();
};


var update_flag_response = function(flagdata) {
		
	//pass the return string to a local variable
	flag_response = flagdata.responseText;
	
	//strip out the content id and the blog id from the return string - [$number]($content_id)<$uid>
	var content_id = flag_response.substring(flag_response.search(/\[/)+1,flag_response.search(/\]/));
	//var content_type = flag_response.substring(flag_response.search(/\(/)+1,flag_response.search(/\)/));
	
	
	//now that we have the content id remove them from the response string 
	flag_response = flag_response.replace(/\[\w+\]/,'');	
	
	if(flag_response == '') {					
			return false;
	} else {
		document.getElementById('content_flag_span'+ content_id).innerHTML = flag_response;	
		//alert('This content has been reported as containing objectionable material');
		//document.getElementById('content_message'+content_id).innerHTML='Content reported as objectionable';
		return false;
	}	
};



// Javascript for adding favorites.

var sendfavorite = function(content,uid,add_remove)
{	
	
	
	document.getElementById('favorite' + content).blur();
	
	url = "/admin/content_favorites_ajax.one";
	
	prama = 'content_id=' + content + '&user_id=' + uid + '&add_remove=' + add_remove;
	
	YAHOO.util.Connect.asyncRequest(
		'GET',
		url + '?' + prama,
		{
			success : update_favorites_response
		}
	);
	
};

var update_favorites_response = function(flagdata) {
		
	//pass the return string to a local variable
	flag_response = flagdata.responseText;
	
	//strip out the content id and the blog id from the return string - [$number]($content_id)<$uid>
	var content_id = flag_response.substring(flag_response.search(/\[/)+1,flag_response.search(/\]/));
		
	//now that we have the content id remove them from the response string 
	flag_response = flag_response.replace(/\[\w+\]/,'');	
	
	if(flag_response == '') 
	{					
			return false;
	} 
	else 
	{
		document.getElementById('content_favorites_span'+ content_id).innerHTML = flag_response;	
		//alert('This content has been reported as containing objectionable material');
		//document.getElementById('content_message'+content_id).innerHTML='Content reported as objectionable';
		return false;
	}
};

//
// FLOATING TOOL TIP JAVASCRIPT
//
// ------------------------
// Browser / OS Detection -
// ------------------------

var IE = (document.all) ? 1 : 0;
var DOM = (document.getElementById) ? 1 : 0;

// ----------------------
// Mouse-over Tool-tips -
// ----------------------

var iconTop=0;
var iconLeft=0;
//var = whereami('tooltip');


var getMousePosDOM = function(e) {
    iconTop=e.clientY;
    iconLeft=e.clientX;
};

var getMousePosIE = function() {
    iconTop=event.clientY;
    iconLeft=event.clientX;
//  iconY=event.scrollX;
};
 
var tooltipOn = function(message,flagID,shift) {
// if called from the child, then pass second argument

   
   if (IE) {
   		var flag_span = document.all['content_message'+flagID];
   	
       	flag_span.innerHTML=message;
       	
        // get shifts for when called from child
        var shiftItX=arguments.length==2?sekritLeft-document.body.scrollLeft:0;
        var shiftItY=arguments.length==2?sekritTop-document.body.scrollTop:0;

               
        flag_span.style.top=10;//document.body.scrollTop+iconTop+shift+shiftItY;
        flag_span.style.left=70;//document.body.scrollLeft+iconLeft+shift+shiftItX;
        
        flag_span.style.visibility="visible";

  }  else if (DOM) {

        document.getElementById('content_message'+flagID).innerHTML=message;

        // get shifts for when called from child 

        var shiftItX=arguments.length==2?sekritLeft-window.pageXOffset:0;

        var shiftItY=arguments.length==2?sekritTop-window.pageYOffset:0;

        // else pos

        document.getElementById('content_message'+flagID).style.top= window.pageYOffset+iconTop+shift+shiftItY;
        document.getElementById('content_message'+flagID).style.left=window.pageXOffset+iconLeft+shift+shiftItX;
        document.getElementById('content_message'+flagID).style.visibility="visible";
  }
  return;
};

var tooltipOff = function(flagID) {

 	if (IE) { 		
 		var flag_span = document.all['content_message'+flagID]; 				
 		flag_span.style.visibility="hidden";
 	} else if (DOM) {
 		document.getElementById('content_message'+flagID).style.visibility="hidden";
 	} else {
 		return;
 	}
}; 


// Tool Tip for Favorites

var tooltipOnF = function(message,favID,shift) {
// if called from the child, then pass second argument

   
   if (IE) {
   		var fav_span = document.all['content_messageF'+favID];
   	
       	fav_span.innerHTML=message;
       	
        // get shifts for when called from child
        var shiftItX=arguments.length==2?sekritLeft-document.body.scrollLeft:0;
        var shiftItY=arguments.length==2?sekritTop-document.body.scrollTop:0;

               
        fav_span.style.top=10;//document.body.scrollTop+iconTop+shift+shiftItY;
        fav_span.style.left=70;//document.body.scrollLeft+iconLeft+shift+shiftItX;
        
        fav_span.style.visibility="visible";

  }  else if (DOM) {

        document.getElementById('content_messageF'+favID).innerHTML=message;

        // get shifts for when called from child 

        var shiftItX=arguments.length==2?sekritLeft-window.pageXOffset:0;

        var shiftItY=arguments.length==2?sekritTop-window.pageYOffset:0;

        // else pos

        document.getElementById('content_messageF'+favID).style.top= window.pageYOffset+iconTop+shift+shiftItY;
        document.getElementById('content_messageF'+favID).style.left=window.pageXOffset+iconLeft+shift+shiftItX;
        document.getElementById('content_messageF'+favID).style.visibility="visible";
  }
  return;
};

var tooltipOffF = function(favID) {

 	if (IE) { 		
 		var fav_span = document.all['content_messageF'+favID]; 				
 		fav_span.style.visibility="hidden";
 	} else if (DOM) {
 		document.getElementById('content_messageF'+favID).style.visibility="hidden";
 	} else {
 		return;
 	}
}; 


if (IE) {document.onmousemove=getMousePosIE;}
else if (DOM) {document.onmousemove=getMousePosDOM;}

//##################################################################################

// -->

