if (MGCCbEvoNS.autocomplete_on)
{
	dojo.require("dijit.form.ComboBox");
	dojo.require("dojo.data.ItemFileReadStore");
}

MGCCbEvoNS.shift_key_pushed		= 0;
MGCCbEvoNS.temporary_url		= null;
MGCCbEvoNS.input_initial_style	= new Array();

dojo.addOnLoad(function() { MGCCbEvoNS.onload_editor(); });

MGCCbEvoNS.onload_editor = function()
{
	// Register init style
	this.input_initial_style["b"] = this.ubbcode_b;
	this.input_initial_style["i"] = this.ubbcode_i;
	this.input_initial_style["u"] = this.ubbcode_u;
	
	if (this.ubbcode_size != null)
	{
		this.input_initial_style["size"] = this.ubbcode_size;
	}

	if (this.ubbcode_color != null && this.ubbcode_color != "#") // Default from user
	{
		this.input_initial_style["color"] = this.ubbcode_color;
	}
	else // Default from usergroup
	{
		this.input_initial_style["color"] = this.ugpbbcode_color;
	}

	if (this.ubbcode_font != null)
	{
		this.input_initial_style["font"] = this.ubbcode_font;
	}

	// Apply input style if active
	if (this.wysiwyg)
	{
		this.init_input_style();
	}

	// Prepare autocomplete if ON
	if (MGCCbEvoNS.autocomplete_on)
	{	
		var myStore = new dojo.data.ItemFileReadStore(this.autocomplete_jsondata);
		
		var autocompleteinput = dijit.byId('mgc_cb_evo_input');
		autocompleteinput.set('autoComplete', false);
		autocompleteinput.set('hasDownArrow', false);
		autocompleteinput.set('selectOnClick', true);
		autocompleteinput.set('store', myStore);
		autocompleteinput.set('searchAttr', "cmd");
		autocompleteinput.set('labelType', "html");
		autocompleteinput.set('labelFunc', function(item, store) {
		        return [
		        	"<div style='font-size: 10px;'>",
					"<div style='float: right;vertical-align: middle;'>",
					"<b>" + phrase_mgc_cb_evo_cmd_syntax + "</b> " + store.getValue(item, "desc"),
					"</div>",
					"<span style='padding-right: 20px;'>" + store.getValue(item, "cmd") + "</span>",
					"</div>"
		        ].join("")
		    });
	}
};

MGCCbEvoNS.init_input_style = function()
{
	if (this.ubbcode_b)
	{
		dojo.style('mgc_cb_evo_input','fontWeight','bold');
	}

	if (this.ubbcode_i)
	{
		dojo.style('mgc_cb_evo_input','fontStyle','italic');
	}

	if (this.ubbcode_u)
	{
		dojo.style('mgc_cb_evo_input','textDecoration','underline');
	}

	if (this.ubbcode_size != null  && this.ubbcode_size != 0)
	{
		var fontsizes = ['xx-small','x-small','small','medium','large','x-large','xx-large'];
		dojo.style('mgc_cb_evo_input','fontSize',fontsizes[this.ubbcode_size - 1]);
	}

	if (this.ubbcode_color != null && this.ubbcode_color != "#") // Default from user
	{
		dojo.style('mgc_cb_evo_input','color',this.ubbcode_color);
	}
	else // Default from usergroup
	{
		dojo.style('mgc_cb_evo_input','color',this.ugpbbcode_color);
	}

	if (this.ubbcode_font != null)
	{
		dojo.style('mgc_cb_evo_input','fontFamily',this.ubbcode_font);
	}
};

MGCCbEvoNS.detect_shift_key_push = function(e)
{
	var characterCode;

	if(e && e.which)
	{
		e = e;
		characterCode = e.which;
	}
	else
	{
		e = event;
		characterCode = e.keyCode;
	}

	if (characterCode == 16)
	{
		this.shift_key_pushed = 1;
	}
	
	return true;
};

MGCCbEvoNS.detect_shift_key_release = function(e)
{
	var characterCode;

	if(e && e.which)
	{
		e = e;
		characterCode = e.which;
	}
	else
	{
		e = event;
		characterCode = e.keyCode;
	}

	if (characterCode == 16)
	{
		this.shift_key_pushed = 0;
	}
	
	return true;
};

MGCCbEvoNS.detect_enter_key_press = function(e)
{
	var characterCode;

	if(e && e.which)
	{
		e = e;
		characterCode = e.which;
	}
	else
	{
		e = event;
		characterCode = e.keyCode;
	}

	if(characterCode == 13 && this.shift_key_pushed == 0)
	{
		this.send_chat();
	}
	
	return true;
};

MGCCbEvoNS.toggle_sound = function()
{
	if (this.sound == 1) // mute
	{
		this.sound = 0;
		dojo.byId('mgc_cb_evo_sound_button').src = this.imgdir_misc + '/mgc_cb_evo/mgc_cb_evo_sound_off.gif';	
	}
	else // unmute
	{
		this.sound = 1;
		dojo.byId('mgc_cb_evo_sound_button').src = this.imgdir_misc + '/mgc_cb_evo/mgc_cb_evo_sound_on.gif';
	}
	return false;
};

MGCCbEvoNS.insert_smiley = function(smileytext)
{
	this.insert_at_cursor(dojo.byId('mgc_cb_evo_input'),smileytext);
	return false;
};

MGCCbEvoNS.insert_at_cursor = function(myField, myValue)
{
	// Clear chat input if prompt still present
	if (this.input_prompt_msg)
	{
		this.remove_chat_prompt();
	}


	// In case of autocompletion active, always add at the end of the line
	if (this.autocomplete_on)
	{
		myField.value += myValue;
	}
	//IE support
	else if (document.selection)
	{
		myField.focus();
		sel 		= document.selection.createRange();
		sel.text 	= myValue;
	}
	//MOZILLA/NETSCAPE support
	else if (myField.selectionStart || myField.selectionStart == 0)
	{
		var startPos 	= myField.selectionStart;
		var endPos 		= myField.selectionEnd;
		myField.value 	= myField.value.substring(0, startPos) + myValue + myField.value.substring(endPos, myField.value.length);
	}
	else
	{
		myField.value += myValue;
	}
};

MGCCbEvoNS.text_insert = function(repdeb, repfin)
{
	// Selection of the input's focus
  	var input = dojo.byId('mgc_cb_evo_input');
  	input.focus();
  	
  	// IE support
	if(typeof document.selection != 'undefined')
	{
		// Formatting code insert
		var range = document.selection.createRange();
		var insText = range.text;
	    range.text = repdeb + insText + repfin;
	    // Cursor position adjustment
	    range = document.selection.createRange();
	    if (insText.length == 0)
		{
	      range.move('character', -repfin.length);
	    }
		else
		{
	      range.moveStart('character', repdeb.length + insText.length + repfin.length);
	    }
	    range.select();
  	}
  	// Newer browsers based on Gecko
  	else
  	{
	    // Insertion du code de formatage
	    var start = input.selectionStart;
	    var end = input.selectionEnd;
	    var insText = input.value.substring(start, end);
	    input.value = input.value.substr(0, start) + repdeb + insText + repfin + input.value.substr(end);
	    // Cursor position adjustment
	    var pos;
	    if (insText.length == 0)
		{
	      pos = start + repdeb.length;
	    }
		else
		{
	      pos = start + repdeb.length + insText.length + repfin.length;
	    }
	    input.selectionStart = pos;
	    input.selectionEnd = pos;
  	}
  	return false;
};

MGCCbEvoNS.enhanced_format = function(select_item,tag)
{
	bbcode_option = select_item.options[select_item.selectedIndex].value;

	if (tag == 'size')
	{
		var fontsizes = ['xx-small','x-small','small','medium','large','x-large','xx-large'];
		this.ubbcode_size = bbcode_option;
		if (this.wysiwyg) dojo.style('mgc_cb_evo_input','fontSize',fontsizes[bbcode_option - 1]);
	}

	if (tag == 'color')
	{
		this.ubbcode_color = bbcode_option;
		if (this.wysiwyg) dojo.style('mgc_cb_evo_input','color',bbcode_option);
	}

	if (tag == 'font')
	{
		this.ubbcode_font = bbcode_option;
		if (this.wysiwyg) dojo.style('mgc_cb_evo_input','fontFamily',bbcode_option);
	}

	return false;
};

MGCCbEvoNS.simple_format = function(tag)
{
	var currentState = null;

	if (tag == 'b')
	{
		currentState = this.ubbcode_b;
		if (currentState)
		{
			this.ubbcode_b = 0;
			if (this.wysiwyg) dojo.style('mgc_cb_evo_input','fontWeight','');
			dojo.byId('bbcode_image_b').src = this.imgdir_misc + '/mgc_cb_evo/mgc_cb_evo_b_off.gif';
		}
		else
		{
			this.ubbcode_b = 1;
			if (this.wysiwyg) dojo.style('mgc_cb_evo_input','fontWeight','bold');
			dojo.byId('bbcode_image_b').src = this.imgdir_misc + '/mgc_cb_evo/mgc_cb_evo_b_on.gif';
		}
	}

	if (tag == 'i')
	{
		currentState = this.ubbcode_i;
		if (currentState)
		{
			this.ubbcode_i = 0;
			if (this.wysiwyg) dojo.style('mgc_cb_evo_input','fontStyle','');
			dojo.byId('bbcode_image_i').src = this.imgdir_misc + '/mgc_cb_evo/mgc_cb_evo_i_off.gif';
		}
		else
		{
			this.ubbcode_i = 1;
			if (this.wysiwyg) dojo.style('mgc_cb_evo_input','fontStyle','italic');
			dojo.byId('bbcode_image_i').src = this.imgdir_misc + '/mgc_cb_evo/mgc_cb_evo_i_on.gif';
		}
	}

	if (tag == 'u')
	{
		currentState = this.ubbcode_u;
		if (currentState)
		{
			this.ubbcode_u = 0;
			if (this.wysiwyg) dojo.style('mgc_cb_evo_input','textDecoration','');
			dojo.byId('bbcode_image_u').src = this.imgdir_misc + '/mgc_cb_evo/mgc_cb_evo_u_off.gif';
		}
		else
		{
			this.ubbcode_u = 1;
			if (this.wysiwyg) dojo.style('mgc_cb_evo_input','textDecoration','underline');;
			dojo.byId('bbcode_image_u').src = this.imgdir_misc + '/mgc_cb_evo/mgc_cb_evo_u_on.gif';
		}
	}
};

MGCCbEvoNS.clear_chat_input = function()
{
	dojo.byId('mgc_cb_evo_input').value = '';
	return false;
};

MGCCbEvoNS.focus_chat_input = function()
{
	dojo.byId('mgc_cb_evo_input').focus();
}

MGCCbEvoNS.reset_bbcodes = function()
{
	// Remove all bbcode from form input
	dojo.byId('mgc_cb_evo_input').value = stripcode(dojo.byId('mgc_cb_evo_input').value, false, false);
	
	// Reset style
	this.ubbcode_b = this.input_initial_style["b"];
	this.ubbcode_i = this.input_initial_style["i"];
	this.ubbcode_u = this.input_initial_style["u"];
	
	if (this.input_initial_style["size"] != null)
	{
		this.ubbcode_size = this.input_initial_style["size"];
	}

	if (this.input_initial_style["color"] != null)
	{
		this.ubbcode_color = this.input_initial_style["color"];
	}

	if (this.input_initial_style["font"] != null)
	{
		this.ubbcode_font = this.input_initial_style["font"];
	}	
	
	this.init_input_style();
	
	return false;
};

MGCCbEvoNS.insert_bbcode_url = function()
{
	/* First get url */
   this.show_promptdialog(phrase_enter_url,"http://",'MGCCbEvoNS.check_url');
};

MGCCbEvoNS.check_url = function(url)
{
	if (url != null && PHP.trim(url) != "")
   	{
   		// URL ok, ask for an url text after a given timeout (to ensure dialog 1st dialog disappearance
		MGCCbEvoNS.temporary_url = url;
		setTimeout("MGCCbEvoNS.ask_urltext()", 500);
	}
};

MGCCbEvoNS.ask_urltext = function()
{
	this.show_promptdialog(phrase_enter_urltext,"",'MGCCbEvoNS.exec_insert_bbcode_url');
};

MGCCbEvoNS.exec_insert_bbcode_url = function(urltext)
{
	if (urltext != null && PHP.trim(urltext))
	{
		this.insert_at_cursor(dojo.byId('mgc_cb_evo_input'),"[url=" + this.temporary_url + "]" + urltext + "[/url]");
	}
	else
	{
		this.insert_at_cursor(dojo.byId('mgc_cb_evo_input'),"[url=" + this.temporary_url + "]" + this.temporary_url + "[/url]");
	}
};

MGCCbEvoNS.insert_bbcode_email = function()
{
	this.show_promptdialog(phrase_enter_email,"@",'MGCCbEvoNS.exec_insert_bbcode_email');
};

MGCCbEvoNS.exec_insert_bbcode_email = function(email)
{
	if (email != null && PHP.trim(email) != "")
   	{
		this.insert_at_cursor(dojo.byId('mgc_cb_evo_input'),"[email]" + email + "[/email]");
	}
};

MGCCbEvoNS.insert_bbcode_img = function()
{
   this.show_promptdialog(phrase_enter_imgurl,"http://",'MGCCbEvoNS.exec_insert_bbcode_img');
};

MGCCbEvoNS.exec_insert_bbcode_img = function(imgurl)
{
	if (imgurl != null && PHP.trim(imgurl) != "")
   {
		this.insert_at_cursor(dojo.byId('mgc_cb_evo_input'),"[img]" + imgurl + "[/img]");
	}
};

MGCCbEvoNS.remove_chat_prompt = function()
{
   if (this.input_prompt_msg)
   {
      this.clear_chat_input();
      this.input_prompt_msg = 0;
   }
};

MGCCbEvoNS.show_rules = function()
{
	dijit.byId('mgc_cb_evo_rules').show();
	return false;
};

MGCCbEvoNS.show_help = function()
{
	dijit.byId('mgc_cb_evo_help').show();
	return false;
};

MGCCbEvoNS.open_smilies_window = function()
{
   window.open(MGCCbEvoNS.bburl + "/" + MGCCbEvoNS.filename + "?" + SESSIONURL + "do=showsmilies" , "showsmilies", "toolbar=no,scrollbars=yes,resizable=yes,left=0 top=0,width=850,height=630");
   return false;
};

MGCCbEvoNS.insert_atusername = function(username)
{
	if(this.input_prompt_msg)
	{
		this.remove_chat_prompt();
	}
	
	dojo.byId("mgc_cb_evo_input").value = "@" + username + ": " + dojo.byId("mgc_cb_evo_input").value;

	return false;
};

MGCCbEvoNS.check_char_limit = function(input_field,editmode)
{
	if (input_field.value.length >= this.char_limit)
	{
		input_field.value = input_field.value.substring(0, this.char_limit);
	}
	
	// Limit on edit
	if (editmode)
	{
		if (dojo.byId('mgc_cb_evo_edit_char_counter') != null)
		{
			dojo.byId('mgc_cb_evo_edit_char_counter').value = this.char_limit - input_field.value.length;
		}
	}
	else
	{
		if (dojo.byId('mgc_cb_evo_char_counter') != null)
		{
			dojo.byId('mgc_cb_evo_char_counter').value = this.char_limit - input_field.value.length;
		}
	}
};

MGCCbEvoNS.collapse_item = function(item_id) // TOaction : one day move this to default js code instead of vB based
{
	var obj  = dojo.byId('collapseobj_' + item_id);
	var icon = dojo.byId('icon_' + item_id);
	
	if (this.isvb4)
	{
		// Object management
		if (obj.style.display == 'none')
	   	{
			icon_re 			= new RegExp("\\.gif$");
			icon.src 			= icon.src.replace(icon_re, '_collapsed.gif');
			obj.style.display 	= '';
			removeFromCookie	= 0;
		}
	   	else
	   	{
			icon_re 			= new RegExp("_collapsed\\.gif$");
			icon.src 			= icon.src.replace(icon_re, '.gif');
			obj.style.display 	= 'none';
			removeFromCookie	= 1;
		}	
	
		// Cookie save
		var vBCookie = fetch_cookie("vbulletin_collapse");
		var TableauCollapsed = new Array();
		if(vBCookie != null)
		{
			vBCookie = vBCookie.split("\n");
			for(var vBCookieElement in vBCookie)
			{
				if(YAHOO.lang.hasOwnProperty(vBCookie,vBCookieElement) && vBCookie[vBCookieElement] != item_id && vBCookie[vBCookieElement] != "")
				{
					TableauCollapsed[TableauCollapsed.length] = vBCookie[vBCookieElement];
				}
			}
		}
		
		if (removeFromCookie == 1) // Remove from collapsed
		{
			TableauCollapsed[TableauCollapsed.length] = item_id;
		}
		
		expires=new Date();
		expires.setTime(expires.getTime()+(1000*86400*365));
		set_cookie("vbulletin_collapse",TableauCollapsed.join("\n"),expires);
		return false;	
	}
	else
	{
		if (obj.style.display == 'none')
	   	{
			icon_re = new RegExp("\\.gif$");
			icon.src = icon.src.replace(icon_re, '_collapsed.gif');
		}
	   	else
	   	{
			icon_re = new RegExp("_collapsed\\.gif$");
			icon.src = icon.src.replace(icon_re, '.gif');
		}
		return toggle_collapse(item_id);
	}
};

MGCCbEvoNS.hide_editor = function(msg)
{
	/* Show no editor line */
	dojo.byId('mgc_cb_evo_row_noeditor').style.display 	= '';
	dojo.byId('mgc_cb_evo_user_warn').style.display  	= '';
	dojo.byId('mgc_cb_evo_user_warn').innerHTML 			= msg;

	/* If editor is on side, change width and margins to resize */
	if (this.isvb4 && this.editor_onside)
	{
		dojo.byId('mgc_cb_evo_inner_side').style.width	= this.editor_side_size;
		if (this.editor_onside == 1) // left
		{
			dojo.byId('mgc_cb_evo_inner_content').style.marginLeft = this.editor_side_size;
		}
		else // right
		{
			dojo.byId('mgc_cb_evo_inner_content').style.marginRight = this.editor_side_size;
		}
	}

	/* Remove elements */
	var el = dojo.byId('mgc_cb_evo_row_editor');
	el.parentNode.removeChild(el);

	el = dojo.byId('collapseobj_mgc_cb_evo_bbcodes');
	el.parentNode.removeChild(el);

	el = dojo.byId('collapseobj_mgc_cb_evo_smilies');
	el.parentNode.removeChild(el);

	if (this.disable_refresh_upon_postlimit)
	{
		clearTimeout(this.refresh_timeout);
		this.autorefresh_on = 0;
	}
};
