/* MGC Chatbox EVO created by MGC, Version 2.0.1, Chatbox Archives Functions Script, Copyright 2008 all rights reserved */

var loading = 0;
var chat_in_process = 0;
var prompt_callback_fct = null;
var alreadyEditing = 0;
var currentEditObj = null;
var currentEditContent = null;
var currentEditId = 0;
var isovermenuitem = 0;
var opened_menu = 0;
var channel_id = 0;
var chatprompt;
var enter_key_listener;
var chatprompt_kl = null;

YAHOO.util.Event.onDOMReady(function()
{
	YAHOO.initChatbox=new function()
	{		
		// Define various event handlers for Dialog
		var handleSubmit = function() {
			this.cancel();
			mgc_cb_js_showprompt_submit();
		};
		var handleCancel = function() {
			this.cancel();
		};		
		
		// Instantiate the Dialog
		chatprompt =
			new YAHOO.widget.Dialog("chatpopupinput",
					{ width : "30em",
					  fixedcenter : true,
					  visible : false,
					  constraintoviewport : true,
					  buttons : [ { text: phrase_save, handler:handleSubmit, isDefault:true },
							  { text: phrase_cancel, handler:handleCancel } ]
					});
		
		// Set the enter key as dialog 
		chatprompt_kl = new YAHOO.util.KeyListener(
							document,
							{ keys:13 },
							{
								fn:mgc_cb_evo_handle_enter_button,
								scope:chatprompt,
								correctScope:true 
							}
						);					
		chatprompt.cfg.queueProperty("keylisteners", chatprompt_kl);

		// Render the Dialog
		chatprompt.render();
	};
});

function mgc_cb_evo_handle_enter_button()
{
	chatprompt.cancel();
	mgc_cb_js_showprompt_submit();
}

function mgc_cb_js_showpopup(message,callback_fct)
{
	var handleOK = function() { 
		this.hide();
		eval(callback_fct + '();')
	}; 
	
	var chatpopup = 
		new YAHOO.widget.SimpleDialog("simpledialog", 
				 { width: "500px",
				   fixedcenter: true,
				   visible: false,
				   draggable: false,
				   close: false,
				   text: message,
				   icon: YAHOO.widget.SimpleDialog.ICON_WARN,
				   constraintoviewport: true,
				   buttons: [ { text: phrase_ok, handler:handleOK, isDefault:true }]
				 } );
				 
	chatpopup.render("mgc_cb_evo_chat_popup"); 
	chatpopup.show();     
	
	return false;
}

function mgc_cb_js_showpopup_yesno(message,callback_fct)
{
	var handleYes = function() { 
		this.hide(); 
		eval(callback_fct + '();');
	}; 
	
	var handleNo = function() { 
		this.hide(); 
	}; 	
	
	var chatpopup = 
		new YAHOO.widget.SimpleDialog("simpledialog", 
				 { width: "500px",
				   fixedcenter: true,
				   visible: false,
				   draggable: false,
				   close: true,
				   text: message,
				   icon: YAHOO.widget.SimpleDialog.ICON_WARN,
				   constraintoviewport: true,
				   buttons: [ { text: phrase_yes, handler:handleYes, isDefault:true },
				   			  { text: phrase_no, handler:handleNo}]
				 } );
				 
	chatpopup.render("mgc_cb_evo_chat_popup"); 
	chatpopup.show(); 
	
	return false;   
}

function mgc_cb_js_showprompt(message,inputval,prompt_callback)
{
	prompt_callback_fct = prompt_callback;

	fetch_object('mgc_cb_evo_chat_popup_content').innerHTML	= message;
	fetch_object('id_mgc_cb_evo_prompt_input').value		= inputval;
	chatprompt.show();
}

function mgc_cb_js_showprompt_submit()
{
	eval(prompt_callback_fct + '("' + chatprompt.getData().mgc_cb_evo_prompt_input + '");');
}


function mgc_cb_evo_force_page_refresh()
{
	window.location.reload();
}

function chatbox_refresh(variable)
{
	mgc_cb_evo_force_page_refresh();
}

function exec_chatbox_clear(variable)
{
	mgc_cb_evo_force_page_refresh();
}

function wait_send_chat(sendAjax)
{
   if(sendAjax.responseXML)
   {
      /* Get xml answer content */
		chatbox_result = sendAjax.responseXML.getElementsByTagName('sendchat_result')[0].firstChild.nodeValue;

      chat_in_process = 0;
      
      /* Execute ajax call return function */
      eval(chatbox_result);
   }
}
function mgc_cb_evo_inline_edit(obj,chatid)
{
   if (!alreadyEditing)
   {
		// Preparing context
		alreadyEditing = 1;
		// Useful info
		currentEditObj = obj;
		currentEditId = chatid;
		// Ajax query
		YAHOO.util.Connect.asyncRequest("POST", mgc_cb_evo_jsloc + 'mgc_cb_evo_ajax.php', {
			success: wait_mgc_cb_evo_inline_edit,
			failure: vBulletin_AJAX_Error_Handler,
			timeout: vB_Default_Timeout
		}, SESSIONURL + "securitytoken=" + SECURITYTOKEN + "&do=ajax_edit_getchat&chatid=" + currentEditId);   
	}
   else
   {
		mgc_cb_js_showpopup(phrase_already_editing);
	}	
}

function wait_mgc_cb_evo_inline_edit(retrieveDataEditAjax)
{
	if(retrieveDataEditAjax.responseXML)
	{
		/* Get xml answer content */
		chat_content = retrieveDataEditAjax.responseXML.getElementsByTagName('chat_content')[0].firstChild.nodeValue;;
      
		// Résultat ok ?
		if (chat_content != '')
		{
			currentEditContent = chat_content;
   			currentEditObj.innerHTML = '<input type="text" id="chatedit" value="' + PHP.htmlspecialchars(currentEditContent) + '" class="bginput" style="width: 75%" maxlength="500" />';
   	
   		fetch_object('chatedit').focus();
   		fetch_object('chatedit').select();
   
   		fetch_object('chatedit').onblur = function leave_edit()
   		{		
   			mgc_cb_evo_save_edit();			
   		};
   
   		// Enter key pushed
   		fetch_object('chatedit').onkeypress = function keyDown(e)
   		{
            var characterCode;
            
            if(e && e.which)
            {
               e = e;
               characterCode = e.which;
            }
            else
            {
               e = event;
               characterCode = e.keyCode;
            }
            
            if(characterCode == 13)
            {
               mgc_cb_evo_save_edit();
            }
   		};
      }
      else
      {
         mgc_cb_js_showpopup(phrase_edit_forbidden);
      }
   }	
}

function mgc_cb_evo_save_edit()
{
	if(alreadyEditing)
	{
		chat_urlencoded = PHP.urlencode(fetch_object('chatedit').value);				
		YAHOO.util.Connect.asyncRequest("POST", mgc_cb_evo_jsloc + 'mgc_cb_evo_ajax.php', {
			success: wait_mgc_cb_evo_save_edit,
			failure: vBulletin_AJAX_Error_Handler,
			timeout: vB_Default_Timeout
		}, SESSIONURL + "securitytoken=" + SECURITYTOKEN + "&do=ajax_save_edit&chatid=" + currentEditId + "&chat=" + chat_urlencoded);	
	}
}

function wait_mgc_cb_evo_save_edit(saveEditAjax)
{
	if(saveEditAjax.responseXML)
	{
		/* Get xml answer content */
		edit_result = saveEditAjax.responseXML.getElementsByTagName('edit_result')[0].firstChild.nodeValue;
		
		alreadyEditing = 0;
		eval(edit_result);
	}	
}

function show_hide_select(select)
{
    var objSelect = fetch_object(select);
    fetch_object(select).style.display = (objSelect.style.display == 'block') ? 'none' : 'block';
}

function valid_selection_and_hide_select(txt, obj, input, select)
{
    fetch_object(input).value = txt;
    obj.parentNode.parentNode.style.display = 'none';
    fetch_object(select).innerHTML = obj.innerHTML;
}

function change_archives_page()
{
	/* Channel id */
	if (fetch_object('channel_id'))
	{
		var chanid = fetch_object('channel_id').options[fetch_object('channel_id').selectedIndex].value;
	}
	else
	{
		var chanid = 0;
	}
	
	/* Dlimit */
	var dlimit = fetch_object('channel_dlimit').options[fetch_object('channel_dlimit').selectedIndex].value;
	
	window.location = 'mgc_cb_evo.php?' + sessionurl + 'do=view_archives&channel_id=' + chanid + '&dlimit=' + dlimit;
}

function inline_select_chat(chatid)
{
   /* Check */
   if (fetch_object('check_' + chatid).value == 0)
   {
      fetch_object('check_' + chatid).value = 1;
      fetch_object('chat_' + chatid).className = 'inlinemod';
      fetch_object('date_' + chatid).className = 'inlinemod';
      fetch_object('uname_' + chatid).className = 'inlinemod';
      fetch_object('text_' + chatid).className = 'inlinemod';
      fetch_object('del_' + chatid).className = 'inlinemod';
      
      /* Avatar line also if on */
      if (avataron)
      {
      	fetch_object('avatar_' + chatid).className = 'inlinemod';
      }
   }
   /* Uncheck */
   else
   {
      fetch_object('check_' + chatid).value = 0;
      fetch_object('date_' + chatid).className = 'alt1';
      fetch_object('uname_' + chatid).className = 'alt2';
      fetch_object('text_' + chatid).className = 'alt1';
      fetch_object('del_' + chatid).className = 'alt2'; 
      
      /* Avatar line also if on */
      if (avataron)
      {
      	fetch_object('avatar_' + chatid).className = 'alt2';
      	fetch_object('chat_' + chatid).className = 'alt1';
      }
      else
      {
	      fetch_object('chat_' + chatid).className = 'alt2';  
      } 
   }
}

function inline_select_all_chat()
{
	/* Select all */
	if (fetch_object('check_all_chatrows').value == 0)
	{
		fetch_object('check_all_chatrows').value = 1;
		
		for (cnt=0; cnt < chatids_array.length; cnt++)
		{
			fetch_object('check_' + chatids_array[cnt]).value = 1;
			fetch_object('chat_' + chatids_array[cnt]).className = 'inlinemod';
			fetch_object('date_' + chatids_array[cnt]).className = 'inlinemod';
			fetch_object('uname_' + chatids_array[cnt]).className = 'inlinemod';
			fetch_object('text_' + chatids_array[cnt]).className = 'inlinemod';
			fetch_object('del_' + chatids_array[cnt]).className = 'inlinemod';
      
			/* Avatar line also if on */
			if (avataron)
			{
				fetch_object('avatar_' + chatids_array[cnt]).className = 'inlinemod';
			}	
		}
	}
	else
	{
		fetch_object('check_all_chatrows').value = 0;
	
		for (cnt=0; cnt < chatids_array.length; cnt++)
		{
			fetch_object('check_' + chatids_array[cnt]).value = 0;
			fetch_object('date_' + chatids_array[cnt]).className = 'alt1';
      		fetch_object('uname_' + chatids_array[cnt]).className = 'alt2';
      		fetch_object('text_' + chatids_array[cnt]).className = 'alt1';
      		fetch_object('del_' + chatids_array[cnt]).className = 'alt2'; 
      
      		/* Avatar line also if on */
		    if (avataron)
      		{
      			fetch_object('avatar_' + chatids_array[cnt]).className = 'alt2';
      			fetch_object('chat_' + chatids_array[cnt]).className = 'alt1';
      		}
      		else
      		{
	      		fetch_object('chat_' + chatids_array[cnt]).className = 'alt2';  
      		} 
		}	
	}
}

function confirm_menu_action(chatid,phrase_confirm,fct_callback)
{
   	hide_chat_menu(chatid,1);
   
   	mgc_cb_js_showpopup_yesno(phrase_confirm,fct_callback);
   	
   	return false;
}

function show_chat_menu(chatid)
{
	if (!opened_menu)
	{
		opened_menu = 1;
		fetch_object('chat_' + chatid + '_menu').style.display = '';	
	}
}

function hide_chat_menu(chatid,forced)
{
   if ((!isovermenuitem || forced) && opened_menu)
	{
		opened_menu = 0;
		fetch_object('chat_' + chatid + '_menu').style.display = 'none';		
  	}
}

function hilite_chat_menu_item(menuitem)
{
   menuitem.className = 'vbmenu_hilite';
   isovermenuitem = 1;
   
   return true;
}

function unhilite_chat_menu_item(menuitem)
{
   menuitem.className = 'vbmenu_option';
   isovermenuitem = 0;
   
   return true;
}

function mgc_cb_evo_edit_bymenu(chatid)
{
	hide_chat_menu(chatid,1);
	mgc_cb_evo_inline_edit(document.getElementById('cspan_' + chatid),chatid);
	return false;
}

function mgc_cb_evo_delete_bymenu(chatid)
{
	hide_chat_menu(chatid,1);
	YAHOO.util.Connect.asyncRequest("POST", mgc_cb_evo_jsloc + 'mgc_cb_evo_ajax.php', {
		success: wait_delete_chat,
		failure: vBulletin_AJAX_Error_Handler,
		timeout: vB_Default_Timeout
	}, SESSIONURL + "securitytoken=" + SECURITYTOKEN + "&do=ajax_delete_chat&chatid=" + chatid);
	return false;
}

function wait_delete_chat(getAnswer)
{
	if(getAnswer.responseXML)
	{
		chatbox_result = getAnswer.responseXML.getElementsByTagName('answer_result')[0].firstChild.nodeValue;
      	eval(chatbox_result);
	}
}