/*********************************************/
/************* Simple Text Javascript ********/
/*********************************************/
YAHOO.namespace('simpleText');
if (!YD) var YD = YAHOO.util.Dom;
if (!YE) var YE = YAHOO.util.Event;
if (!$) var $ = YD.get;
// Shortcut to the main URL of the webservice, so that each call to that URL only needs the final path (ease of use for file path changing)
var subURL = 'http://webservices.sitebuilder.customerstreet.com/rapidsite/xstandard/';
var clientID;

/**
 * createParamElement
 * Because Internet Explorer cannot create some elements properly (especially with certain attributes), this function is required
 * to create the parameters for the XStandard Editor, which caters for all variations of the creation process
 *
 * @access public
 * @return element
 **/
document.createParamElement = function(type, name, value) {
  var element;
  try {
    element = document.createElement('<'+type+' name="'+name+'" value="'+value+'">');
  } catch (e) { }
  if (!element || !element.name) { // Not in IE, then
    element = document.createElement(type)
    element.name = name;
    element.value = value;
  }
  return element;
}

YAHOO.simpleText.xsEdit = {
	xsDialog: null,
	submitUrl: "/plugins/simple_text/extras/simple_text_ajax.php",
	imageHeight : null,
	imageWidth : null,
	self : this,

	/**
	 * Setup
	 * Finds all the editable elements by class "client_edit"
	 * Attaches an event handler to each editable elements click event with a callback to editElement
	 * Adds the tooltips to the editable elements to show they are editable
	 * Attaches an event handler to the forms submit event with a callback to catchXSSubmit
	 *
	 * @access public
	 * @return void
	 **/
	setup: function(o){
//		self = o;
		if (document.getElementById('admin_toolbar')) {
			$('simple_text_plugin').style.display = 'none';
			var clientEdit = YD.getElementsByClassName('client_edit', 'div', 'content_inner_wrapper');
			YE.addListener(clientEdit, 'click', this.editElement);
			var clientEdit = YD.getElementsByClassName('client_edit', 'h2', 'content_inner_wrapper');
			YE.addListener(clientEdit, 'click', this.editElement);
//			var imageEdit = document.getElementById('simple_text').getElementsByTagName('img');
//			YE.addListener(imageEdit, 'click', this.editImage);
			this.addTooltips(clientEdit);
			/**
			 * Capture the output of the xStandard object and insert it into a hidden input field
			 *
			 **/
			var xStandard = YD.getElementsByClassName('simple_text_plugin', 'form', 'extra_2')[0];
			YE.on(xStandard, 'submit', this.catchXSSubmit);
		}
	},

	/**
	 * CatchXSSubmit
	 * Catches the submit event of the form containing the xStandard editor

	 * @access public
	 * @return void
	 **/
	catchXSSubmit: function(e){
		var pageCopy = document.getElementById('page_copy');
		pageCopy.EscapeUnicode = true;
	   	document.getElementById('xhtml').value = pageCopy.value;
	},

	/**
	 * getNodeName
	 * Takes a XHTML tag name and returns a descriptive string for each
	 * @access public
	 * @return void
	 **/
	getNodeName: function (tag){
		tag = tag.toLowerCase();
		switch(tag){
			case 'h2':
				var tagName = 'Main Heading';
				break;
			case 'h3':
				var tagName = 'Sub-Heading';
				break;
			case 'h4':
				var tagName = 'Minor Heading';
				break;
			case 'p':
				var tagName = 'Paragraph';
				break;
			case 'li':
			case 'ul':
			case 'ol':
				var tagName = 'List';
				break;
			case 'img':
				var tagName = 'Image';
				break;
			default:
				var tagName = false;
		} // switch
		return tagName;
	},

	/**
	 *
	 * @access public
	 * @return void
	 **/
	getEditNode: function(queryNode){
		while(queryNode.nodeType != 3 && queryNode.nodeName.toLowerCase() != 'img'){
			queryNode = queryNode.hasChildNodes? queryNode.firstChild : queryNode.nextSibling;
		} // while
		if (queryNode.parentNode.nodeName.toLowerCase() == 'strong'
		|| queryNode.parentNode.nodeName.toLowerCase() == 'em'
		|| queryNode.nodeType == 3) {
			queryNode = queryNode.parentNode;
		}
		return queryNode;
	},

	/**
	 * addTooltips
	 * Adds a title attribute to each of the editable elements
	 * @access public
	 * @return void
	 **/
	addTooltips: function (elementList){
		if (elementList.length) {
			var wrapper = document.getElementById("content_inner_wrapper");
			if (!YD.hasClass(wrapper, 'yui-skin-sam')) YD.addClass(wrapper, 'yui-skin-sam');
			var tagList = ['h3', 'p', 'img', 'ul'];
			var idList = [];
			var thisNode;
			var contentType;
			for (var i=0; i<elementList.length; i++){
				if (!elementList[i].getElementsByTagName) continue;
				for (var n=0; n<tagList.length; n++){
					var nodes = elementList[i].getElementsByTagName(tagList[n]);
					for (var x=0; x<nodes.length; x++){
						var thisNode = nodes[x];
						if (!thisNode.hasAttribute('id')) {
							thisNode.setAttribute('id', 'tooltip_node_'+x);
						}
						contentType = YX.getNodeName(thisNode.nodeName);
						content = 'Edit this '+contentType+' by clicking on it.';
						thisNode.setAttribute('title', content);
						idList[idList.length] = thisNode.getAttribute('id');
					}
				}
			}
			YX.toolTip = new YAHOO.widget.Tooltip("simple_tooltip", {context: idList, container: wrapper});
		}else{
			return false;
		}
	},

	/**
	 * closeDialog a tidy up function called after the xStandard editor has finished
	 * Removes the dropsheet used to fade out non-edited elements and the editor
	 * itself from the DOM
	 * active
	 * @access public
	 * @return void
	 **/
	closeDialog: function(){
		YX.xsDialog.destroy();
		YX.xsDialog = null;
		return true;
	},

	/**
	 * UpdateDom
	 * Finds the node specified by element_id, removes it's child nodes
	 * then replaces them with the child nodes of dom_node, and copies over
	 * the attributes of each node
	 * active
	 * @access public
	 * @return void
	 **/
	updateDom: function(elementId, domNode){
		var oldNode = document.getElementById(elementId);
		if (oldNode.hasChildNodes()) {
			var oldChild = oldNode.firstChild;
			while(oldChild){
				var nextChild = oldChild.nextSibling;
				oldChild.parentNode.removeChild(oldChild);
				oldChild = nextChild;
			} // while
		}
		if (domNode.hasChildNodes()) {
			newChild = domNode.firstChild;
			while(newChild){
				var nextChild = newChild.nextSibling;
				oldNode.appendChild(newChild);
				newChild = nextChild;
			} // while
		}
/*		var parentTag = oldNode.parentNode;
		oldNode = parentTag.replaceChild(domNode, oldNode);
		var editNode = YD.getAncestorByClassName('client_edit');
		YE.addListener(editNode, 'click', simpleText.editElement);*/
		return true;
	},


	/**
	 * getXmlFromString
	 * Converts a string of xml data into a dom element and returns the element
	 * active
	 * @access public
	 * @return void
	 **/
	getXmlFromString: function(xml_string){
		if (window.ActiveXObject) {
			var xml_doc = new ActiveXObject('Microsoft.XMLDOM');
			xml_doc.async = 'false';
			xml_doc.loadXML(xml_string);
		} else {
			var xml_parser = new DOMParser();
			var xml_doc = xml_parser.parseFromString(xml_string, 'text/xml');
			if (xml_doc.documentElement.nodeName == 'parsererror') {
				return false;
			}
		}
		var xml_root = xml_doc.documentElement;
		return xml_root;
//		var copyNode = YD.getElementsByClassName('client_edit', null, xml_root);
//		if (copyNode.length > 0) {
//			return copyNode[0];
//		}else{
//			return false;
//		}
	},

	/**
	 * getXmlFromDom converts a DOM element into an XML string
	 * active
	 * @access public
	 * @return void
	 **/
	getXmlFromDom: function(domNode){
		if (domNode.hasAttribute("style")) domNode.removeAttribute('style');
		var children = YD.getChildren(domNode);
		for( var i=0; i< children.length; i++){
			if (children[i].hasAttribute('style')) children[i].removeAttribute('style');
		} // for
		if (domNode.xml) {
			var xmlValue = domNode.xml;
		}else{
			var newDom = document.implementation.createDocument("http://www.w3.org/1999/xhtml", "html", null);
		//	var newDom = document.implementation.createDocument("", "page_copy", null);
			var newNode = newDom.importNode(domNode, true);
			newDom.documentElement.appendChild(newNode);
			var xmlSerialiser = new XMLSerializer();
			var xmlValue = xmlSerialiser.serializeToString(newDom.documentElement);
		}
		return xmlValue;
	},

	/**
	 * createEditor
	 * Creates the XStandard Editor passing the correct parameters and returning it inside a form
	 * active
	 * @access public
	 * @return void
	 **/
	createEditor: function (editNode, xsId, xsForm, dimxy, buttons, tagName){
		this.method;
		this.newParam = [];
		this.newParam.toolbar = {'name': 'ToolbarWysiwyg', 'value': buttons};
		this.newParam.styles =	{'name': 'ShowStyles', 'value': 'yes'};
		this.newParam.victim = {'name': 'Victim', 'value': editNode.id};
		this.newParam.timestamp = {'name': 'EnableTimestamp', 'value': 'no'};
		/**
		 * Clone the pages xstandard form (not just the object)
		 *
		 **/
		var xsEditor = xsForm.cloneNode(true);
		/**
		 * Set the private properties
		 *
		 **/
		var id = xsId;
		var width = dimxy[0];
		var height = dimxy[1];
		if (editNode.hasChildNodes()) {
			var child = editNode.firstChild;
			while(child){
				if (child.nodeType != 3) {
					var xmlValue = editNode.innerHTML;
					break;
					}
				child = child.nextSibling;
			} // while
		}
		if (typeof xmlValue == 'undefined') {
			if(navigator.appVersion.indexOf("MSIE")!== -1){
				var xmlValue = '<h2>' + $('header_1').innerHTML + '</h2>';
			} else {
				var xmlValue = YX.getXmlFromDom(editNode);
			}
		}
/*		var tagName = simpleText.getNodeName(editNode.nodeName);*/
		var self = this;
		var xsObject = xsEditor.getElementsByTagName('object')[0];
		var editor = document.createElement('div');
		var editHead = document.createElement('div');
		var editHeadText = document.createTextNode('Edit this '+tagName);
		var editBody = document.createElement('div');
		/**
		 * Set the parameters of the xstandard object that need changing
		 *
		 **/
		var setParameters = function (xs){
			if(navigator.appVersion.indexOf("MSIE")!== -1){
				var objParametersOut = document.getElementById('page_copy');
				var objParameters = objParametersOut.getElementsByTagName("param");
			} else {
				var objParameters = xs.getElementsByTagName("param");
			}
			/*var xParam;
			var objName;
			var objValue;*/
			for (var i=0; i<objParameters.length; i++){
				var paramName = objParameters[i].getAttribute('name');
				var paramValue = objParameters[i].getAttribute('value');
				switch(paramName){
					case 'width':
						objParameters[i].value = width;
						break;
					case 'height':
						objParameters[i].value = height;
						break;
					case 'Value':
						if(navigator.appVersion.indexOf("MSIE")!= -1){
							xmlValue = str_replace('"', '&quot;', xmlValue);
						}
						objParameters[i].value = xmlValue;
						break;
					case 'ClientID':
						// Needed fix for IE to get the URl of the current domain for the document library path
						clientID = objParameters[i].value;
						break;
					default:
						continue;
				} // switch
			}
		};
		/**
		 * Create the xstandard parameters that are missing
		 *
		 **/
		var createParameters = function (xs){
			if(navigator.appVersion.indexOf("MSIE")!= -1){
				/**
				 *  Unfortunately, because of IE's shocking handling of object when stored in a variable the parameters
				 *  do not get passed to the new object properly so each one has to be built 'manually' rather than dynamically
				 *
				 **/
				subURL = $('xstandardBase').value;
				paramNode = document.createParamElement('param', 'ClientID', clientID);
				xs.appendChild(paramNode);
				paramNode = document.createParamElement('param', 'License', subURL+'license.txt');
				xs.appendChild(paramNode);
				paramNode = document.createParamElement('param', 'Styles', subURL+'styles.xml');
				xs.appendChild(paramNode);
				paramNode = document.createParamElement('param', 'ImageLibraryURL', subURL+'image_library/imagelibrary.php '+subURL+'image_library/publiclibrary.php');
				xs.appendChild(paramNode);
				paramNode = document.createParamElement('param', 'Base', subURL+'document_library');
				xs.appendChild(paramNode);
				paramNode = document.createParamElement('param', 'AttachmentLibraryURL', subURL+'document_library/attachmentlibrary.php');
				xs.appendChild(paramNode);
				paramNode = document.createParamElement('param', 'SpellCheckerURL', subURL+'spellchecker/spellchecker.php');
				xs.appendChild(paramNode);
				paramNode = document.createParamElement('param', 'Options', '66054');
				xs.appendChild(paramNode);
				paramNode = document.createParamElement('param', 'Value', xmlValue);
				xs.appendChild(paramNode);
			}
			for(var param in self.newParam){
				// Again, IE cannot create some elements in the normal way so a function has been written to cater for this problem
				paramNode = document.createParamElement('param', self.newParam[param].name, self.newParam[param].value);
				/*paramNode = document.createElement('param');
				paramNode.setAttribute('name', self.newParam[param].name);
				paramNode.setAttribute('value', self.newParam[param].value);*/
				xs.appendChild(paramNode);
			}
		};
		/**
		 * Set the attributes of the xstandard object
		 *
		 **/
		if(xsObject){
			xsObject.setAttribute("id", "xsedit");
			xsObject.setAttribute("width", width);
			xsObject.setAttribute("height", height);
			setParameters(xsObject);
			createParameters(xsObject);
		}

		/**
		 * Create the divs that will hold the editor
		 *
		 **/
		editor.setAttribute("id", xsId);
		editHead.setAttribute("class", "hd");
		editHead.setAttribute("className", "hd");
		editHead = editor.appendChild(editHead);
		editHead.appendChild(editHeadText);
		editBody.setAttribute("class", "bd");
		editBody.setAttribute("className", "bd");
		editBody = editor.appendChild(editBody);
		/**
		 * Set the action attribute for the form
		 * and attach the editor to the div
		 **/
		xsEditor.setAttribute("action", YX.submitUrl);
		xsEditor = editBody.appendChild(xsEditor);

		var keyWordHead = document.createElement('h3');
		keyWordHead.setAttribute('id', 'keyWordHead');
		editor.appendChild(keyWordHead);
		keyWordHead.appendChild(document.createTextNode('Your Page Key Phrases'));

		var keyWords = document.getElementById('keyphraseList');
		keyWords = keyWords.cloneNode(true);
		keyWords.id = 'keyphraseListClone';
		editor.appendChild(keyWords);
		keyWords.style.display = 'block';

		var containerSave = document.createElement('div');
		editor.appendChild(containerSave);
		containerSave.setAttribute('id', 'container-save');
		YE.on(containerSave, 'click', saveButtonClicked);

		return editor;
	},

	/**
	 *
	 * active
	 * @access public
	 * @return void
	 **/
	getEditor: function(element, buttons, tagName){
		var editExists = YD.getElementsByClassName('yui-dialog', 'div', 'extra_2');
		if (editExists.length > 0) {
			if (YX.xsDialog != null) YX.xsDialog.destroy();
			if (YX.imgDialog != null) YX.imgDialog.destroy();
		}
		editExists = null;
		width = 600;
		height = 400;
		if (tagName == 'Image') {
			width = parseInt(YD.getStyle(element, 'width')) + 30;
			height = parseInt(YD.getStyle(element, 'height')) + 60;
		}
		/* Clone the standard xStandard editor */
		if (document.forms["simple_text_plugin"] == 'undefined') {
			return false;
		}
		var xsForm = document.forms["simple_text_plugin"];
		var xsEditor = YX.createEditor(element, 'editor', xsForm, [width, height], buttons, tagName);
		/* Append the editor div to one of the spare divs */
		var parent = document.getElementById('extra_2');
		parent.appendChild(xsEditor);
		YX.xsDialog = new YAHOO.widget.Dialog(xsEditor,
			{	fixedcenter:true,
				modal: true,
				close: true,
				width: "620px",
				zIndex: 1000
			});
		YX.xsDialog.render();
/*		if (tagName == 'Image') {
			var xsObject = xsEditor.getElementsByTagName('object')[0];
			xsObject.CallToolbarButton('image');
		}*/
		YX.xsDialog.show();
		var editorMask = $('editor_mask');
		if(editorMask){
			maskHeight = editorMask.style.cssText;
			maskHeight = maskHeight.split('; ');
			newMaskHeight = new Array();
			var j = 0;
			for(var i=0;i<maskHeight.length;i++){
				newMaskHeight[j] = maskHeight[i].split(': ');
				j++;
			}
			for(var i=0;i<newMaskHeight.length;i++){
				theMaskHeight = newMaskHeight[i].toString();
				theMaskHeight = theMaskHeight.split(',');
				if(theMaskHeight[0].toLowerCase() == 'height'){
					i++;
					var theRealMaskHeight = theMaskHeight[1];
				}
			}
			maskHeight = theRealMaskHeight.split('px');
			maskHeight = maskHeight[0];
			maskHeight = parseFloat(maskHeight);
			maskHeight = maskHeight - 520;
			editorMask.style.cssText = 'z-index: 1001; height: '+ maskHeight +'px; width: 1263px; display: block;';
		}
		return true;
	},

	/**
	 * saveContent main ajax call
	 * Gets the whole user edited content div and uses an ajax post call to push this together with
	 * the site id, page id, object id and object type id up to the server.
	 * active
	 * @access public
	 * @return void
	 **/
	saveContent : {
		copy : null,
		siteId : null,
		pageId : null,
		nodeId : null,
		submitUrl : null,
		update : function (){
			this.siteId = document.getElementById("siteid_simple_text_plugin").value;
			this.pageId = document.getElementById("pageid_simple_text_plugin").value;
			this.copy = escape(this.copy);
			if(this.copy == ''){
				this.copy = '<p style="padding: 5px;"></p>';
			}
			this.copy = str_replace('+', '#105;', this.copy);
			this.submitUrl = "plugins/simple_text/extras/simple_text_ajax.html";
			var postData = 'siteid='+this.siteId+'&pageid='+this.pageId+'&nodeid='+this.nodeId+'&copy='+this.copy;
			this.copy = YAHOO.util.Connect.asyncRequest('POST', this.submitUrl, updateComplete, postData);

		},
		success : function (objResponse){
			//alert("Ajax returned: "+objResponse.statusText);
			YX.closeDialog();
			var existingAnalyserToolbar = document.getElementById('analyser_toolbar');
			var responseDiv = document.createElement('div');
			var wrapper = document.getElementById('wrapper');
			wrapper.appendChild(responseDiv);
			var newHTML = objResponse.responseText;

			var newAnalyserToolbar = document.getElementById('analyser_toolbar');
			newAnalyserToolbar.innerHTML = newHTML;

			//responseDiv.innerHTML = objResponse.responseText;
			existingAnalyserToolbar.innerHTML = newAnalyserToolbar.innerHTML;
			var newStuff = Dom.getFirstChild(existingAnalyserToolbar);
			existingAnalyserToolbar.parentNode.appendChild(newStuff);
			existingAnalyserToolbar.parentNode.removeChild(existingAnalyserToolbar);

			var showAnalLink = document.createElement('a');
			newStuff.appendChild(showAnalLink);
			showAnalLink.appendChild(document.createTextNode('Hide Analyser'));
			showAnalLink.setAttribute('href', '');
			showAnalLink.setAttribute('title', 'Hide The Analyser Toolbar');
			showAnalLink.setAttribute('id', 'hideAnalLink');
			showAnalLink.style.cssText = 'position:absolute;top:2px;left:4px;color: #FFF;width: 40px;';

			var toTopLinks = Dom.getElementsByClassName('toTop');
			for(var i=0;i<toTopLinks.length;i++){
				toTopLinks[i].style.display = 'none';
			}

			var theImagesForMatt = Dom.getElementsByClassName('seo_rank_image');
			for(var i=0;i<theImagesForMatt.length;i++){
				theImagesForMatt[i].style.display = 'none';
			}

			var descriptions = Dom.getElementsByClassName('section_info');
//					var analyserTabs = new YAHOO.widget.TabView(analyserToolbar);
			for(var i=0;i<descriptions.length;i++){
				var description = descriptions[i];
//						description.style.display = 'none';

				var firstHead = Dom.getFirstChild(description);
				var firstHeadImg = Dom.getFirstChild(firstHead);
				/*if(firstHeadImg){
					var newDiv = document.createElement('div');
					description.appendChild(newDiv);
				}*/
				var firstPara = Dom.getNextSibling(firstHead);
				if(description.id == 'keyphrase_position_description' || description.id == 'keyphrase_density_description'){
					var anotherList = Dom.getNextSibling(firstPara);
					anotherList.parentNode.removeChild(anotherList);
				}
				var secondPara = Dom.getNextSibling(firstPara);
				var seoList = Dom.getNextSibling(secondPara);
				finalPara = Dom.getNextSibling(seoList);

				if(firstHead){
					if(description.id == 'keyphrase_density_description'){
						firstHead.style.cssText = 'padding-top: 2px;';
					}
					//firstHead.parentNode.removeChild(firstHead);
				}
				if(firstPara){
					firstPara.parentNode.removeChild(firstPara);
				}
				if(secondPara){
					secondPara.parentNode.removeChild(secondPara);
				}
				if(seoList){
					seoList.parentNode.removeChild(seoList);
				}
				switch(description.id){
					case 'keyphrase_density_description':
						if(finalPara){
							finalPara.parentNode.removeChild(finalPara);
						}
					break;
					case 'title_description':
						if(finalPara){
							finalPara.parentNode.removeChild(finalPara);
						}
					break;
					case 'description_description':
						if(finalPara){
							finalPara.parentNode.removeChild(finalPara);
						}
					break;
					case 'key_phrases_description':
						if(finalPara){
							finalPara.parentNode.removeChild(finalPara);
						}
					break;
				}
			}
			var siteid = getURLParam('siteid');
			var pageid = getURLParam('pageid');
			var linkToPage = '/content_analyser/analyse_page?siteid=' + siteid + '&pageid=' + pageid;
			newStuff.innerHTML += '<a href="' + linkToPage + '" title="View The Full Result For This Page" onclick="window.open(\''+ linkToPage +'\');return false;">View Full Result</a>';

			var analyserLink = document.getElementById('hideAnalLink');
			YE.on(analyserLink, 'click', hideAnalLinkNow);
			//alert("Content Updated Successfully");

		},
		failure : function (objResponse){
			//alert("Ajax returned: "+objResponse.statusText);
			//alert("Content Not Updated Successfully. Please Try Again");
		}
	},

	contentChanged : function(xsEditor){
		xsContent = xsEditor.value;
		/*if (xsContent.indexOf('<img ') != -1) {
			xsDom = YX.getXmlFromString(xsContent);
			if (xsDom) {
				xsDom = document.importNode(xsDom, true);
				xsDom = document.body.appendChild(xsDom);
				var imageTags = xsDom.getElementsByTagName('img');
				if (imageTags.length > 1) {
					var classValue = 'none';
					for( var i=0; i<imageTags.length; i++){
						if (imageTags[i].hasAttribute('class')){
							classValue = String(imageTags[i].getAttribute('class'));
						}
						if (classValue.indexOf('client_edit') != -1){
							var oldImage = imageTags[i];
						}else{
							var newImage = imageTags[i];
						}
					} // for
					if (typeof oldImage != 'undefined' && typeof newImage != 'undefined') {
						oldImage.setAttribute('src', newImage.getAttribute('src'));
						if (newImage.getAttribute('src') != '') {
							oldImage.setAttribute('src', newImage.getAttribute('src'));
						}
						xsEditor.value = YX.getXmlFromDom(oldImage);
					}
				}else{
					if (imageTags[0].hasAttribute('height')) {
						var imgHeight = parseInt(imageTags[0].getAttribute('height'));
						if (typeof(YX.imageHeight) != 'number') {
							YX.imageHeight = imgHeight;
						}else{
							if (YX.imageHeight != imgHeight) {
								imageTags[0].setAttribute('height', YX.imageHeight);
							}
						}
					}
					if (imageTags[0].hasAttribute('width')) {
						var imgWidth = parseInt(imageTags[0].getAttribute('width'));
						if (typeof(YX.imageWidth) != 'number') {
							YX.imageWidth = imgWidth;
						}else{
							if (YX.imageWidth != imgWidth) {
								imageTags[0].setAttribute('width', YX.imageWidth);
							}
						}
					}
			//		if (xsDom.tagName != 'img') xsDom = xsDom.getElementsByTagName('img')[0];
					xsEditor.value = YX.getXmlFromDom(imageTags[0]);
				}
				xsDom.parentNode.removeChild(xsDom);
			}
		}*/
		return true;
	},

	/**
	 *
	 * @access public
	 * @return void
	 **/
	editElement: function(e, objTag){
		if (objTag == null) objTag = this;
		if (typeof objTag.nodeName == 'string') {
			var tagType = objTag.nodeName.toLowerCase();
			switch(tagType){
				case "h2" :
					YX.editHeading(objTag);
					break;
				case "h3":
				case "h4":
				case "ul":
				case "ol":
				case "li":
//					simpleText.editList(objTag);
//					break;
				case "p":
				case "span":
				case "strong":
				case "em":
					YX.editParagraph(objTag);
					break;
				case "div":
					if (objTag.firstChild.nodeName.toLowerCase() == 'img') {
						YX.editImage(objTag);
					}else{
						YX.editParagraph(objTag);
					}
					break;
				/*case "img":
					YX.editImage(objTag);
					break;*/
				default:
					alert("Tag Select Failed:"+objTag.nodeName);
			} // switch
		}else{
		 alert("Tag node name is undefined");
		}
	},

	/**
	 *
	 * active
	 * @access public
	 * @return void
	 **/
	editHeading: function(objHead){
/*		objDetails = simpleText.getTagdetails(objHead);*/
		//var buttons = 'spellchecker, copy, paste,,help,save';
		var buttons = 'spellchecker, copy, paste,,help';
		var editor = YX.getEditor(objHead, buttons, 'Heading');
	},

	/**
	 *
	 * active
	 * @access public
	 * @return void
	 **/
	editList: function(objList){
		if (objList.nodeName.toLowerCase() == "li") {
			objList = YD.getAncestorByClassName(objList, 'client_edit');
		}
/*		objDetails = simpleText.getTagdetails(objList);*/
		//var buttons = 'strong, em, underline, hyperlink, attachment,, spellchecker, copy, paste,,help,save';
		var buttons = 'strong, em, underline, sup, sub, hyperlink, attachment,, spellchecker, copy, paste,,help';
		var editor = YX.getEditor(objList, buttons, 'List');
	},

	/**
	 *
	 * active
	 * @access public
	 * @return void
	 **/
	editParagraph: function(objPara){
		if (objPara.nodeName.toLowerCase() != 'div') {
			itemNode = objPara;
			while(itemNode.parentNode.nodeName.toLowerCase() != 'div'){
				itemNode = itemNode.parentNode;
			} // while
			objPara = itemNode;
		}
/*		objDetails = simpleText.getTagdetails(objPara);*/
		//var buttons = 'strong, em, underline,, hyperlink, attachment, unordered-list, ordered-list,, spellchecker, copy, paste,,help,save';
		var buttons = 'strong, em, underline, sup, sub,, hyperlink, attachment, unordered-list, ordered-list, draw-data-table,, spellchecker, copy, paste,,help';
		var editor = YX.getEditor(objPara, buttons, 'Text');
	}
};
if (!YX) var YX = YAHOO.simpleText.xsEdit;
YE.onDOMReady(YX.setup, YX, true);

/**
 * Ajax callback object
 *
 **/
updateComplete = {
	success : YX.saveContent.success,
	failure: YX.saveContent.failure,
	scope: YX.saveContent
};

/**
 * xsButtonClicked listens for the button click event on the xStandard editor
 * If the save button has been clicked, a dom update process is followed:
 * 1. Get the content of the editor and convert it into a DOM node
 * 2. Find the Id of the node being edited (stored as parameter 'Victim')
 * 3. Update the node being edited with the new data
 * @access public
 * @return void
 **/
xsButtonClicked = function(id, button, state){
	document.getElementById(id).EscapeUnicode = true;
	var xsEditor = document.getElementById(id);
	switch(button) {
		case 'save':
			var xsParameters = xsEditor.getElementsByTagName('param');
			for (var i = 0; i < xsParameters.length; i++){
				if (xsParameters[i].getAttribute('name') == 'Victim') {
					var oldNodeId = xsParameters[i].getAttribute('value');
				}
				if(xsParameters[i].getAttribute('name') == 'Value'){
					var startText = xsParameters[i].getAttribute('value');
				}
			}
			//xsEditor.EscapeUnicode = true;
			//YX.saveContent.copy = xsEditor.value;
			//alert(YX.saveContent.copy);
			YX.saveContent.nodeId = oldNodeId;
			var victimType = oldNodeId.substring(0, oldNodeId.indexOf('_'));
			var victim = document.getElementById(oldNodeId);

			if(xsEditor.value !== ''){
				switch(victimType){
					case 'header':
						if(navigator.appVersion.indexOf("MSIE")!== -1){
							newValue = xsEditor.value;
							newValue = newValue.toString();
							newValue = stripTags(newValue, null);
							victim.innerHTML = newValue;
							YX.saveContent.copy = '<h2>' + newValue + '</h2>';
						} else {
							var domHeader = YX.getXmlFromString(xsEditor.value);
							if (domHeader) {
								domHeader = document.importNode(domHeader, true);
								domHeader = document.body.appendChild(domHeader);
								var headerTag = domHeader.getElementsByTagName('h2')[0];
								if (headerTag) victim.textContent = headerTag.textContent;
							}
							domHeader.parentNode.removeChild(domHeader);
							YX.saveContent.copy = xsEditor.value;
						}
						break;
					case 'text':
						victim.innerHTML = xsEditor.value;
						YX.saveContent.copy = xsEditor.value;
						break;
					default:
						;
				} // switch
			} else {
				var youSure = confirm("Warning: removing all content will remove this section from the page completely and cannot be recovered");
				if(youSure){
					switch(victimType){
						case 'header':
							victim.innerHTML = '<h2>This Is Major Heading 1</h2>';
							YX.saveContent.copy = '<h2>This Is Major Heading 1</h2>';
							break;
						case 'text':
							victim.innerHTML = '<p style="padding: 5px;"></p>';
							YX.saveContent.copy = '<p style="padding: 5px;"></p>';
							break;
						default:
							break;
					} // switch
				} else {
					//alert("I'm here!" + startText);
					xsEditor.value = startText;
					return false;
				}
			}
			/* CAUSES EDITOR TO COMPLETELY CRASH FIREFOX AND SAFARI (AND OPERA)
			YX.closeDialog();*/
			YX.saveContent.update();
			break;
		case 'image':
			break;
	}// switch
	/*	activeElements = YAHOO.util.Dom.getElementsByClassName('clickable', null, 'content_inner_wrapper');
		for (var i=0; i<activeElements.length; i++){
			YAHOO.util.Dom.removeClass(activeElements[i], 'clickable');
		}*/
	return true;
};

/**
 *
 * @access public
 * @return void
 **/
xsTagListChanged = function (id){
	xsEditor = document.getElementById(id);
	var update = YX.contentChanged(xsEditor);
	if (!update) alert('Could not change content');
	return true;
}

/**
 *
 * @access public
 * @return void
 **/
xsContentChanged = function (id){
	xsEditor = document.getElementById(id);
	var update = YX.contentChanged(xsEditor);
	//alert(update);
	if (!update) alert('Could not change content');
	return true;
};

function saveButtonClicked(){
	xsButtonClicked('xsedit', 'save', null);
}

function strip_tags(str, allowed_tags) {
	// http://kevin.vanzonneveld.net
	// +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
	// +   improved by: Luke Godfrey
	var key = '', tag = '';
	var matches = allowed_array = [];
	var allowed_keys = {};
	// Build allowes tags associative array
	if (allowed_tags) {
		allowed_tags  = allowed_tags.replace(/[\<\> ]+/g, '');;
		allowed_array = allowed_tags.split(',');

		for (key in allowed_array) {
			tag = allowed_array[key];
			allowed_keys['<' + tag + '>']   = true;
			allowed_keys['<' + tag + ' />'] = true;
			allowed_keys['</' + tag + '>']  = true;
		}
	}
	// Match tags
	matches = str.match(/(<\/?[^>]+>)/gi);
	// Is tag not in allowed list? Remove from str!
	for (key in matches) {
		// IE7 Hack
		if (isNaN(key)) {
			tag = matches[key].toString();
			if (!allowed_keys[tag]) {
				// Looks like this is
				// reg = RegExp(tag, 'g');
				// str = str.replace(reg, '');
				str = str.replace(tag, "");
			}
		}
	}
	return str;
}