/**
 * MyAjax
 * By Arnaud BOUTIN
 * All rights reserved
 **/

var	MyAjax =	{
	Version: '0.1Beta'
}

/*
 * Add multiple onLoad Events
 * Copied from the AjaxTags
 * http://ajaxtags.sourceforge.net/
 */
function addOnLoadEvent(func) {
	var oldonload = window.onload;
	if (typeof window.onload != 'function') {
		window.onload = func;
	} else {
		window.onload = function() {
		oldonload();
		func();
		}
	}
}

//	Loading function
var	MyAjaxPageLoaded = false;
addOnLoadEvent(MyAjaxSetPageLoaded);

function	MyAjaxSetPageLoaded(){
	//	Set the the page is loaded
	MyAjaxPageLoaded = true;
}

//	This function is used to either enqueue to the load event or execute now
function	AddToLoadOrExecute( toexec ){
	if( MyAjaxPageLoaded ){
		toexec();
	}else{
		addOnLoadEvent( toexec );
	}
}

//	Function called in order to do an update
function	DoUpdate( target, url ){
	//	Check that the target exists
	if( window.document.getElementById( target ) == null ){
		alert( "The target ("+target+") does not exists");
		return	false;
	}
	
	//	Request the updater
	new	Ajax.Updater( target, url );
	//	Return false for an easier chaining
	return	false;
}

//	Function called in order to show an 'hidden' item
function	ShowElement( element, displaytype ){
	element.style.display	= ((arguments.length>=2)?displaytype:'');
	element.style.overflow	= "visible";
}

//	Function called in order to hide an item based on its name
function	HideElement( element ){
	//var	olddisplay = element.style.display;
	element.style.display = "none";
	element.style.overflow = "hidden";
	
	//return	olddisplay;
}



//----------- TabPanel
MyAjax.TabPanel = Class.create();

MyAjax.TabPanel.prototype = {

	initialize: function(options) {
		this.tabpages		= new Array();
		this.setOptions(options);
	},
	
	setOptions: function(options) {
      this.options = {
         expandedTextColor   : '#ffffff',
         hoverTextColor      : '#ffffff',
         collapsedTextColor  : '#ced7ef',
         hoverTextColor      : '#ffffff',
         onHideTab           : null,
         onShowTab           : null
      }.extend(options || {});
   },
   
   AddPanel: function( titlediv, contentdiv, options ){
		//	Check for an existing content div
		var	content = window.document.getElementById( contentdiv );
		var title = window.document.getElementById( titlediv );
		if( content == null || title == null )
			return;
		//	Add the tab
		var	newtab = new MyAjax.TabPanel.Tab( this, title, content, options );
		this.tabpages.push( newtab );
		//	Check what to do
		if( this.tabpages.length > 1 )
			newtab.showCollapsed();
		else
			newtab.showExpanded();
		//	Finished
		return;
   },
   
   showTab:	function( toshow ){
		//	Process the tab array
		for( var i=0 ; i< this.tabpages.length ; i++ ){
			if( this.tabpages[i].expanded )
				this.tabpages[i].showCollapsed();
		}
		toshow.showExpanded();   
   }
   
};


MyAjax.TabPanel.Tab = Class.create();

MyAjax.TabPanel.Tab.prototype = {

	initialize: function(tabpanel, titlebar, content, options) {
		this.tabpanel	= tabpanel;
		this.titlebar	= titlebar;
		this.content	= content;
		this.setOptions(options);
		//this.defaultdisplay = this.content.style.display;
		
		//	Set the expanded flag
		this.expanded = false;
		
		//	Init the style of the title
		this.titlebar.style.backgroundColor		= this.options.backgroundColor;
		this.titlebar.style.border				= "1px solid " + this.options.borderColor;
		this.titlebar.style.borderTopWidth		= "1px";
		this.titlebar.style.borderBottomWidth	= "1px";
		this.titlebar.style.borderRightWidth	= "1px";
		this.titlebar.style.borderLeftWidth		= "1px";
		this.titlebar.onclick					= this.titleBarClicked.bindAsEventListener(this);
		this.titlebar.onmouseover				= this.hover.bindAsEventListener(this);
		this.titlebar.onmouseout				= this.unhover.bindAsEventListener(this);
		//	Init the style of the content
		this.content.style.backgroundColor		= this.options.backgroundColor;
		this.content.style.border				= "1px solid " + this.options.borderColor;
		this.content.style.borderTopWidth		= "1px";
		this.content.style.borderBottomWidth	= "1px";
		this.content.style.borderRightWidth		= "1px";
		this.content.style.borderLeftWidth		= "1px";
	},
	
	setOptions: function(options) {
      this.options = {
         backgroundColor     : '#6b79a5',
         borderColor         : '#1f669b'
      }.extend(options || {});
	},
	
	showCollapsed: function()	{
		this.content.style.display			= "none";
		this.content.style.overflow			= "hidden";
		this.titlebar.style.color           = this.tabpanel.options.collapsedTextColor;
		this.expanded = false;
	},
	
	showExpanded: function()	{
		this.content.style.display			= "";//this.defaultdisplay;
		this.content.style.overflow			= "visible";
		this.titlebar.style.color           = this.tabpanel.options.expandedTextColor;
		this.expanded = true;
	},
	
	titleBarClicked: function(e) {
		if( this.expanded == true )
			return;
		this.tabpanel.showTab(this);
	},

	hover: function(e) {
		this.titlebar.style.color				= this.tabpanel.options.hoverTextColor;
	},

	unhover: function(e) {
		if ( this.expanded ) {
			this.titlebar.style.color           = this.tabpanel.options.expandedTextColor;
		}
		else {
			this.titlebar.style.color           = this.tabpanel.options.collapsedTextColor;
		}
	}
};

//-------------------- Accordion
// Widely inspired from Rico : http://openrico.org/

MyAjax.Accordion = Class.create();

MyAjax.Accordion.prototype = {

   initialize: function(container, options) {
      this.container            = $(container);
      this.accordionTabs        = new Array();
      this.setOptions(options);
      
      this._attachBehaviors();

	  if( this.options.borderColor != '' )
		this.container.style.borderBottom = '1px solid ' + this.options.borderColor;

      // set the initial visual state...
      for ( var i=1 ; i < this.accordionTabs.length ; i++ )
      {
         this.accordionTabs[i].showCollapsed();
      }
      if( this.options.initialExpand )
		this.accordionTabs[0].showExpanded();
	  else
		this.accordionTabs[0].showCollapsed();
   },

   setOptions: function(options) {
      this.options = {
         mode                : 'accordion',
         titleCursor         : 'pointer',
         expandedClass		 : '',
         collapsedClass		 : '',
         hoverClass			 : '',
         contentClass		 : '',
         borderColor         : '',
         initialExpand		 : true,
         onShowTab           : null
      }.extend(options || {});
   },

   showTab: function( toshow ) {
		//	Check for the accordion mode
		if( this.options.mode == 'accordion' ){
			for( var i = 0 ; i<this.accordionTabs.length ; i++ )
				if( this.accordionTabs[i].expanded )
					this.accordionTabs[i].showCollapsed();
			if( !toshow.expanded )
				toshow.showExpanded();
		}
		//	Check for the expandable mode
		if( this.options.mode == 'expandable' ){
			if( toshow.expanded )
				toshow.showCollapsed();
			else
				toshow.showExpanded();
		}
      
      if ( this.options.onShowTab )
         this.options.onShowTab( toshow );
   },

   _attachBehaviors: function() {
      var panels = this._getDirectChildrenByTag(this.container, 'DIV');
      for ( var i = 0 ; i < panels.length ; i++ ) {

         var tabChildren = this._getDirectChildrenByTag(panels[i],'DIV');
         if ( tabChildren.length != 2 )
            continue; // unexpected

         var tabTitleBar   = tabChildren[0];
         var tabContentBox = tabChildren[1];
         this.accordionTabs.push( new MyAjax.Accordion.Tab(this,tabTitleBar,tabContentBox) );
      }
   },

   _getDirectChildrenByTag: function(e, tagName) {
      var kids = new Array();
      var allKids = e.childNodes;
      for( var i = 0 ; i < allKids.length ; i++ )
         if ( allKids[i] && allKids[i].tagName && allKids[i].tagName == tagName )
            kids.push(allKids[i]);
      return kids;
   }

};

MyAjax.Accordion.Tab = Class.create();

MyAjax.Accordion.Tab.prototype = {

   initialize: function(accordion, titleBar, content) {
      this.accordion = accordion;
      this.titleBar  = titleBar;
      this.content   = content;
      this.expanded = false;
      this._attachBehaviors();
   },
	
   showCollapsed: function() {
      this.expanded = false;
      this.titleBar.className = this.accordion.options.collapsedClass;
      HideElement( this.content );
   },

   showExpanded: function() {
      this.expanded = true;
      this.titleBar.className = this.accordion.options.expandedClass;
      ShowElement( this.content );
   },

   titleBarClicked: function(e) {
		this.accordion.showTab(this);
   },

   hover: function(e) {
		if( !this.expanded || this.accordion.options.mode != 'accordion' )
			this.titleBar.className = this.accordion.options.hoverClass;
		
   },

   unhover: function(e) {
      if ( this.expanded ) {
		this.titleBar.className = this.accordion.options.expandedClass;
      }
      else {
		this.titleBar.className = this.accordion.options.collapsedClass;
      }
   },

   _attachBehaviors: function() {
	  this.content.className			= this.accordion.options.contentClass;
      this.titleBar.onclick				= this.titleBarClicked.bindAsEventListener(this);
      this.titleBar.onmouseover			= this.hover.bindAsEventListener(this);
      this.titleBar.onmouseout			= this.unhover.bindAsEventListener(this);
   }

};

//-------------------- RadioElements

MyAjax.RadioElements = Class.create();

MyAjax.RadioElements.prototype = {
	initialize: function( _unselectedclass, _selectedclass ){
		this.unselectedclass = _unselectedclass;
		this.selectedclass = _selectedclass;
		this.elements = new Array();
	},
	
	AddItem: function( _element, _unselectfunction, _selectfunction, _initialselect ){
		//	Create the new item
		var	newitem = new MyAjax.RadioElements.RadioItem( this, _element, _unselectfunction, _selectfunction );

		//	Add it to the list
		this.elements.push( newitem );
		
		//	Check for initial selection
		if( _initialselect )
			newitem.setSelected( newitem );
	},
	
	doSelect: function( _item ){
		for( var i = 0 ; i < this.elements.length ; i++ )
			if( this.elements[i].selected )
				this.elements[i].setUnselected();
		_item.setSelected();      
	}
}

MyAjax.RadioElements.RadioItem = Class.create();

MyAjax.RadioElements.RadioItem.prototype = {
	initialize: function( _parent, _element, _unselectfunction, _selectfunction ){
		this.parent = _parent;
		this.element = _element;
		this.selectfunction = _selectfunction;
		this.unselectfunction = _unselectfunction;
		
		this.selected = false;
		this.setUnselected();
		
		this.element.onclick = this.elementClicked.bindAsEventListener(this);
	},
	
	elementClicked: function(e){
		if( this.selected == true )
			return;
		this.parent.doSelect(this);
	},
	
	setUnselected: function(){
		this.element.className = this.parent.unselectedclass;
		this.selected = false;
		this.unselectfunction();
	},
	
	setSelected: function(){
		this.element.className = this.parent.selectedclass;
		this.selected = true;
		this.selectfunction();
	}
}


//-------	Image slide show
MyAjax.ImagesSlide = Class.create();

MyAjax.ImagesSlide.prototype = {

	initialize: function( _imagedest, _options ){
		this.imagedest = _imagedest;
		this.waitsrc = this.imagedest.src;
		
		this.setOptions(_options);
		
		if( this.options.previouselement != null ){
			this.options.previouselement.onclick = this._onprevious.bindAsEventListener(this);
			HideElement(this.options.previouselement);
		}
		
		if( this.options.nextelement != null ){
			this.options.nextelement.onclick = this._onnext.bindAsEventListener(this);
			HideElement(this.options.nextelement);
		}
		
		if( this.options.firstelement != null ){
			this.options.firstelement.onclick = this._onfirst.bindAsEventListener(this);			
		}
		
		if( this.options.lastelement != null ){
			this.options.lastelement.onclick = this._onlast.bindAsEventListener(this);
		}
		
		this.images = new Array();
		this.currentImageIdx = 0;
		
		this.imagedest.onclick = this._onclickonimage.bindAsEventListener(this);
	},
	
	_onnext: function(e) {
		if( this.currentImageIdx < (this.images.length-1) ){
			this.currentImageIdx++;
			this._postindexchanged();
		}	
	},
	
	_onprevious: function(e) {
		if( this.currentImageIdx > 0 ){
			this.currentImageIdx--;
			this._postindexchanged();
		}
	},
	
	_onfirst: function(e) {
		if( this.currentImageIdx > 0 ){
			this.currentImageIdx = 0;
			this._postindexchanged();
		}
	},
	
	_onlast: function(e) {
		if( this.currentImageIdx < (this.images.length-1) ){
			this.currentImageIdx = (this.images.length-1);
			this._postindexchanged();
		}
	},
	
	_postindexchanged: function() {
		this.imagedest.src = this.waitsrc;
		this.images[this.currentImageIdx].getImage();
		this._checkVisibilityAndPreload();
	},
	
	_onclickonimage: function(e) {
		
	},

	setOptions: function(options) {
		this.options = {
			updatestatus		 : true,
			preload				 : true,
			previouselement		 : null,
			nextelement			 : null,
			firstelement		 : null,
			lastelement			 : null,
			textelement			 : null
		}.extend(options || {});
	},

	
	AddImage: function( _lowres, _hires, _comment ){
		var	newim = new MyAjax.ImagesSlide.Image( this, _lowres, _hires, _comment );
		this.images.push( newim );
		
		//	Check if this image is the first one
		if( this.currentImageIdx == (this.images.length-1) ){
			newim.getImage();
		}
		//	Update the next and previous
		this._checkVisibilityAndPreload();
	},
	
	_checkVisibilityAndPreload: function(){
		//	Check for previous
		if( this.options.previouselement != null ){
			if( this.currentImageIdx > 0 ){
				ShowElement( this.options.previouselement );
			}else{
				HideElement( this.options.previouselement );
			}
		}
		//	Check for the next element
		if( this.options.nextelement != null ){
			if( this.currentImageIdx < (this.images.length-1) ){
				ShowElement( this.options.nextelement );
			}else{
				HideElement( this.options.nextelement );
			}
		}
		//	Check for preload
		if( this.options.preload ){
			//	Check for the preload of the previous
			if( this.currentImageIdx > 0 )
				this.images[this.currentImageIdx-1].preloadImage();
			//	Check for the preload of the next
			if( this.currentImageIdx < (this.images.length-1) )
				this.images[this.currentImageIdx+1].preloadImage();
		}
		//	Finished
		return;	
	},
	
	updateCallback: function( _updated ){
		//	Check for no shift between loading and displaying
		if( this.images[this.currentImageIdx] != _updated )
			return;
		//	Now update the display
		this.imagedest.src = _updated.lowres;
		//	Check for a text to display
		if( this.options.textelement != null ){
			this.options.textelement.innerHTML = (_updated.comment != null)?_updated.comment:''; 
		}
		//	Finished
		return;
	}
}

MyAjax.ImagesSlide.Image = Class.create();

MyAjax.ImagesSlide.Image.prototype = {
	//	Create a new image
	initialize: function( _parent, _lowres, _hires, _comment ){
		this.parent = _parent;
		this.lowres = _lowres;
		this.highres = _hires;
		this.comment = _comment;
		//	No image loaded
		this.image = null;
		this.finished = false;
		this.error = false;
		this.docallback = false;
	},
	
	getImage: function(){
		if( this.image != null && this.finished == true ){
			//	Do not wait
			this.parent.updateCallback(this);
			return;
		}
		
		//	Set the flag for callback request
		this.docallback = true;
		
		//	Check for an image being loaded
		if( this.image != null && this.finished == false ){
			return;
		}
		
		//	Load the image
		this.preloadImage();
	},
	
	preloadImage: function(){
		if( this.image != null )
			return;
		
		this.image = new Image;
		this.image.onload = this._onimageloaded.bindAsEventListener(this);
		this.image.onabort = this._onimageerrored.bindAsEventListener(this);
		this.image.onerror = this._onimageerrored.bindAsEventListener(this);
		this.image.src = this.lowres;
	},
	
	_onimageloaded: function(e){
		this.error = false;
		this.finished = true;
		
		if( this.docallback )
			this.parent.updateCallback(this);
		this.docallback = false;
	},
	
	_onimageerrored: function(e){
		this.error = true;
		this.finished = true;
		
		if( this.docallback )
			this.parent.updateCallback(this);
		this.docallback = false;
	}
}

//-------- Hover image

MyAjax.ImageHover = Class.create();

MyAjax.ImageHover.prototype = {
	//	Create a new image
	initialize: function( _parent, _imgsrc ){
		this.parent = _parent;
		this.originalimgsrc = this.parent.src;
		this.imgsrc = _imgsrc;
		//	Set the status
		this.finished = false;
		this.ismouseover = false;
		//	Load the image
		this.image = new Image;
		this.image.onload = this._onimageloaded.bindAsEventListener(this);
		this.image.onabort = this._onimageerrored.bindAsEventListener(this);
		this.image.onerror = this._onimageerrored.bindAsEventListener(this);
		this.image.src = this.imgsrc;
		//	Hook to the parent
		this.parent.onmouseover = this._onmouseover.bindAsEventListener(this);
		this.parent.onmouseout = this._onmouseout.bindAsEventListener(this);
	},
	
	_updateDisplay: function(){
		if( this.error == true || this.finished == false ){
			return;
		}
		if( this.ismouseover ){
			this.parent.src = this.imgsrc;
		}else{
			this.parent.src = this.originalimgsrc;
		}
	},
		
	_onimageloaded: function(e){
		this.error = false;
		this.finished = true;
		this._updateDisplay();
	},
	
	_onimageerrored: function(e){
		this.error = true;
		this.finished = true;
		this._updateDisplay();
	},
	
	_onmouseover: function(e){
		this.ismouseover = true;
		this._updateDisplay();
	},
	
	_onmouseout: function(e){
		this.ismouseover = false;
		this._updateDisplay();
	}
}

//--------	Late image loading

MyAjax.LateImage = Class.create();

MyAjax.LateImage.prototype = {
	//	Create a new image
	initialize: function( _parent, _imgsrc ){
		this.parent = _parent;
		this.imgsrc = _imgsrc;
		//	Load the image
		this.image = new Image;
		this.image.onload = this._onimageloaded.bindAsEventListener(this);
		this.image.onabort = this._onimageerrored.bindAsEventListener(this);
		this.image.onerror = this._onimageerrored.bindAsEventListener(this);
		this.image.src = this.imgsrc;
	},
	
	_updateDisplay: function(){
		this.parent.src = this.imgsrc;
	},
		
	_onimageloaded: function(e){
		this._updateDisplay();
	},
	
	_onimageerrored: function(e){
	}
}


//---------- Mouse Over class switcher

MyAjax.MouseClassSwitcher = Class.create();

MyAjax.MouseClassSwitcher.prototype = {
	initialize: function( _element, _mouseInClass, _mouseOutClass )
	{
		this.element = _element;
		this.mouseInClass = _mouseInClass;		
		this.mouseOutClass = _mouseOutClass;
		
		this.element.onmouseover = this._onEnter.bindAsEventListener(this);
		this.element.onmouseout = this._onExit.bindAsEventListener(this);
	},
	
	_onEnter: function(){
		if( this.element.className == this.mouseOutClass )
			this.element.className = this.mouseInClass;
	},
	
	_onExit: function(){
		if( this.element.className == this.mouseInClass )
			this.element.className = this.mouseOutClass;
	}
}


//---------- Mouse Over image switcher

MyAjax.MouseImageSwitcher = Class.create();

MyAjax.MouseImageSwitcher.prototype = {
	initialize: function( _element, _imgsrc )
	{
		this.element = _element;
		this.imgsrc = _imgsrc;
		this.orgscr = this.element.src;
		
		this.image = new Image;
		this.image.src = this.imgsrc;
		
		this.element.onmouseover = this._onEnter.bindAsEventListener(this);
		this.element.onmouseout = this._onExit.bindAsEventListener(this);
	},
	
	_onEnter: function(){
		this.element.src = this.imgsrc;
	},
	
	_onExit: function(){
		this.element.src = this.orgscr;
	}
}


//---------- List tree

MyAjax.ListTree = Class.create();

MyAjax.ListTree.prototype = {
	initialize: function( _ulelement, _expandedclass, _collapsedclass )
	{
		this.ulelement = _ulelement;
		this.expandedclass = _expandedclass;
		this.collapsedclass = _collapsedclass;
		this._doInit( this.ulelement );
	},
	
	_doInit: function( ul ){
		//	Process every item to look for a LI
		var allKids = ul.childNodes;
		//alert(allKids.length);
      	for( var i = 0 ; i < allKids.length ; i++ )
		{
         	if ( allKids[i] && allKids[i].tagName  )
			{
				if(allKids[i].tagName == "LI")
				{
					var	li = allKids[i];
					var	containedUL = new Array();
					var	liKids = li.childNodes;
					var	liKidsCount = (liKids)?liKids.length:0;
					var	spanElement = null;
					
					for( var k = 0 ; k<liKidsCount ; k++ )
					{
						if ( liKids[k] && liKids[k].tagName  )
						{
							if(liKids[k].tagName == "UL")
							{
								//	Add the child
								containedUL.push( liKids[k] );
								//	Continue processing
								this._doInit( liKids[k] );
							}
							if(liKids[k].tagName == "SPAN")
							{
								spanElement = liKids[k];
							}
						}
						
					}
					
					//	Now check for some ULs
					if( containedUL.length>0 )
					{
						//	Create the span
						if( spanElement == null )
						{
							spanElement = document.createElement("SPAN");
							var t = '\u00A0';
							if( li.firstChild.nodeName == "#text" ){
								t += li.firstChild.nodeValue;
								li.removeChild(li.firstChild);
							}
							spanElement.appendChild( document.createTextNode(t) );
							li.insertBefore( spanElement, li.firstChild );
						}
						new MyAjax.ListTreeListener( li, spanElement, containedUL, this.expandedclass, this.collapsedclass );
					}
				}
			}
		}
		return;
	}
}

MyAjax.ListTreeListener = Class.create();

MyAjax.ListTreeListener.prototype = {
	initialize: function( _parentil, _spanelement, _containedul, _expandedclass, _collapsedclass )
	{
		this.parentil = _parentil;
		this.containedul = _containedul;
		this.expandedclass = _expandedclass;
		this.collapsedclass = _collapsedclass;
		_spanelement.onclick = this._onClick.bindAsEventListener(this);
		this.expanded = (this.parentil.className == this.expandedclass);
	},
	
	_onClick: function(){
		this.expanded = !this.expanded;
		this.parentil.className = (this.expanded)?this.expandedclass:this.collapsedclass;
		
		for( var i = 0 ; i<this.containedul.length ; i++ )
		{
			if( this.expanded )
				ShowElement( this.containedul[i], "block" );
			else
				HideElement( this.containedul[i] );
		}
		return	false;
	}
	
}


//---------- Text editor

MyAjax.TextEditor = Class.create();

MyAjax.TextEditor.prototype = {
	initialize: function( _textelement, options  )
	{
		//	Save the text element
		this.textelement = _textelement;
		//	Set the option
		this.setOptions( options );
		//	Do the requested bindings
		if( this.options.boldbutton != null )
			this.options.boldbutton.onclick = this._doBold.bindAsEventListener(this);
		if( this.options.italicbutton != null )
			this.options.italicbutton.onclick = this._doItalic.bindAsEventListener(this);
		if( this.options.underlinebutton != null )
			this.options.underlinebutton.onclick = this._doUnderline.bindAsEventListener(this);
		if( this.options.biggerbutton != null )
			this.options.biggerbutton.onclick = this._doBigger.bindAsEventListener(this);
		if( this.options.smallerbutton != null )
			this.options.smallerbutton.onclick = this._doSmaller.bindAsEventListener(this);
		if( this.options.centerbutton != null )
			this.options.centerbutton.onclick = this._doCenter.bindAsEventListener(this);
		//	Finished
		return;
	},
	
	setOptions: function(options) {
      this.options = {
         boldbutton        	: null,
         italicbutton		: null,
         underlinebutton	: null,
         biggerbutton		: null,
         smallerbutton		: null,
         centerbutton		: null,
         codeson			: new Array('[b]','[i]','[u]','[A]','[a]','[center]'),
         codesoff			: new Array('[/b]','[/i]','[/u]','[/A]','[/a]','[/center]')
      }.extend(options || {});
   },
   
   //	Function called in bold
   _doBold: function(){
   		this._doProcess(0);
   },
   
   //	Function called on italic
   _doItalic: function(){
   		this._doProcess(1);
   },
   
   //	Function called in underline
   _doUnderline: function(){
   		this._doProcess(2);
   },

   //	Function called in underline
   _doBigger: function(){
   		this._doProcess(3);
   },
   
   //	Function called in underline
   _doSmaller: function(){
   		this._doProcess(4);
   },
   
   //	Function called in underline
   _doCenter: function(){
   		this._doProcess(5);
   },
   
   //	Function called in order to do the process
   _doProcess: function( code ){
		this.textelement.focus();
		
		//if (document.all) {IEWrap(lft, rgt);}
		//else if (document.getElementById) {mozWrap(txtarea, lft, rgt);}
		
		//if ((clientVer >= 4) && is_ie && is_win)
		if (document.all)
		{
			theSelection = document.selection.createRange().text; // Get text selection
			if (theSelection) {
				// Add tags around selection
				document.selection.createRange().text = this.options.codeson[code] + theSelection + this.options.codesoff[code];
				this.textelement.focus();
				theSelection = '';
				return;
			}
		}
		else if (this.textelement.selectionEnd && (this.textelement.selectionEnd - this.textelement.selectionStart > 0))
		{
			this.mozWrap(this.textelement, this.options.codeson[code], this.options.codesoff[code]);
			return;
		}
   	
   },
   
   	// From http://www.massless.org/mozedit/
	mozWrap: function(txtarea, open, close)
	{
		var selLength = txtarea.textLength;
		var selStart = txtarea.selectionStart;
		var selEnd = txtarea.selectionEnd;
		if (selEnd == 1 || selEnd == 2) 
			selEnd = selLength;
	
		var s1 = (txtarea.value).substring(0,selStart);
		var s2 = (txtarea.value).substring(selStart, selEnd)
		var s3 = (txtarea.value).substring(selEnd, selLength);
		txtarea.value = s1 + open + s2 + close + s3;
		return;
	}
	
}
