// Copyright 2003 Digital Kreasi. All rights reserved.
	

/**
	class PropertiesLink
*/


function PropertiesLink(owner) {
	this.owner = owner;
	var name = owner.name;
	this.a = 'Properties_a_' + name;
	this.none = 'Properties_none_' + name;
	this.a_name = "Properties_a_name_"+name;
	this.a_href = "Properties_a_href_"+name;
	this.a_target = "Properties_a_target_"+name;
	
}

// public methods
PropertiesLink.prototype.showNone = PropertiesLink_showNone;
PropertiesLink.prototype.hideNone = PropertiesLink_hideNone;
PropertiesLink.prototype.update = PropertiesLink_update;
PropertiesLink.prototype.href_changed = PropertiesLink_href_changed;
PropertiesLink.prototype.name_changed = PropertiesLink_name_changed;
PropertiesLink.prototype.target_changed = PropertiesLink_target_changed;
PropertiesLink.prototype.chooseFileLink = PropertiesLink_chooseFileLink;
PropertiesLink.prototype.selectOption = PropertiesLink_selectOption;
PropertiesLink.prototype.clear = PropertiesLink_clear;
PropertiesLink.prototype.hndlr_keyDown = PropertiesLink_hndlr_keyDown;

PropertiesLink.prototype.name_focus = PropertiesLink_name_focus;
PropertiesLink.prototype.href_focus = PropertiesLink_href_focus;

function PropertiesLink_clear() {
	document.getElementById(this.a).style.display='none';
}

/**
* update properties for a specific tag in property editor
*
* @param
*	obj - ktml object
*/
function PropertiesLink_showNone() {
	document.getElementById(this.none).style.display='block';
}
function PropertiesLink_hideNone() {
	document.getElementById(this.none).style.display='none';
}

function PropertiesLink_update(){
	this.clear();
	if (this.owner.selectableNodes && this.owner.selectableNodes[0]) {
		try {
			if (
				this.owner.selectableNodes[0].tagName == 'A' ||
				(this.owner.useIntrospection && this.owner.inspectedNode != null) 
			) {
				this.hideNone();
				var tooperate = this.owner.selectableNodes[0];
			} else {
				this.showNone();
			}
		} catch (e) {
		}	
		
		if (!tooperate) 
			return;
			
		this.owner.inspectedNode = tooperate;
			
		if (tooperate.tagName == 'A') {
			document.getElementById(this.a).style.display='block';
			//this.a_name.value = tooperate.getAttribute("name");
			//this.a_href.value = tooperate.getAttribute("href");
			util_setInput(document.getElementById(this.a_href), tooperate.getAttribute("href"));
			util_setInput(document.getElementById(this.a_name), tooperate.getAttribute("name"));
			this.selectOption(document.getElementById(this.a_target), tooperate.getAttribute("target"));
		}
	}
}

/**
	check if the href has some value. If yes ask the user to confirm the change.
	Called when Name field got focus
	
	@params
	msg - message for the user
	
	@return
		none
*/
function PropertiesLink_name_focus(msg) {
	Logging.entering(3);

	var hrefNode = document.getElementById(this.a_href);
	
	if (hrefNode.value) {
		var change = false;
		if (hrefNode.value == "#") {
			change = true;
		} else {
			change = window.confirm(msg);
		}
		if (change) {
			hrefNode.value = '';
		} else {
			hrefNode.focus();
		}
	}
	
	Logging.exiting(3);
}

/**
	check if the name has some value. If yes ask the user to confirm the change.
	Called when href field got focus
	
	@params
	msg - message for the user
	
	@return
		none
*/
function PropertiesLink_href_focus(msg) {
	Logging.entering(3);
	
	var nameNode = document.getElementById(this.a_name);
	
	if (nameNode.value) {
		if (window.confirm(msg)) {
			nameNode.value = '';
		} else {
			nameNode.focus();
		}
	}
	Logging.exiting(3);
}

/**
* change the href property
*
* @param
*	propValue - property value
*/
function PropertiesLink_href_changed(propValue){
	Logging.entering(3);

	//var nameNode = document.getElementById(this.a_name);

	
	if (this.owner.selectableNodes && this.owner.selectableNodes[0]) {
		if (this.owner.selectableNodes[0].tagName == 'A') {
				var tooperate = this.owner.selectableNodes[0];
		}
		if (tooperate.tagName == 'A') {
			if (propValue != '' && propValue != 'false') {
				propValue = String(propValue);
				propValue = propValue.replace(/'/g, "\\'");
				if (Ktml_mozilla) {
					tooperate.setAttribute("href", propValue);
					tooperate.removeAttribute("name");
				} else {
					tooperate.setAttribute("href", propValue);
					tooperate.setAttribute("name", ''); // work around a bug in IE
					tooperate.removeAttribute('name');
					tooperate.removeAttribute('NAME');
				}
			} else {
				tooperate.removeAttribute('href');
				if ((tooperate.getAttribute("name") == undefined) || (tooperate.getAttribute("name") == '')) {
					if (this.owner.svdSelection) {
						this.owner.util_restoreSelection();
					} else {
						this.owner.cw.focus();
					}
					//this.owner.util_restoreSelection();
					this.owner.logic_updateDOMHierarchy(true, 0);
					tooperate.outerHTML = tooperate.innerHTML;
					this.owner.logic_removeTag(1);
					
					this.owner.logic_updateDOMHierarchy(true, 0);
					this.owner.propertieslink.update();

				}

			}
		}
		
		//LA CE FOLOSESTE ASTA?
		//Comentat de MPR ca sa prinda focusul Name-ul.
/*  		if (this.owner.svdSelection) {
			this.owner.util_restoreSelection();
		} else {
			this.owner.cw.focus();
		}
 */				
 		if(this.owner.undo) {
			this.owner.undo.addEdit();
		}
  	}
	Logging.exiting(3);
}

/**
* change the name property
*
* @param
*	propValue - property value
*/
function PropertiesLink_name_changed(propValue){
	
	Logging.entering(3);
	
	if (this.owner.selectableNodes && this.owner.selectableNodes[0]) {
		if (this.owner.selectableNodes[0].tagName == 'A') {
				var tooperate = this.owner.selectableNodes[0];
		}
		Logging.log(4,tooperate.outerHTML);
		if (tooperate.tagName == 'A') {
			if (propValue != '' && propValue != 'false') {
				propValue = String(propValue);
				propValue = propValue.replace(/'/g, "\\'");
				if (Ktml_mozilla) {
					tooperate.setAttribute("name", propValue);
					tooperate.name = propValue;
					tooperate.removeAttribute("href");
				} else {
					//LA CE FOLOSESTE ASTA?
					//Comentat de MPR ca sa prinda focusul Href-ul.
					//tooperate.removeAttribute("href");
/* 					if (false) {
						//this.owner.util_restoreSelection();
					} else {
 						var r = this.owner.edit.body.createTextRange();
						try {
							r.moveToElementText(tooperate);
							r.select();
						} catch(e) {
						}
 					}
 */	
					// comentat de RST
					//this.owner.edit.execCommand(DECMD_UNLINK, false, null);
					//this.owner.edit.execCommand('CreateBookmark', false, propValue);
					
					
					tooperate.removeAttribute('href');
					// dirty trick for a workaround of a IE bug (that doesn't set attribute name)
					tooperate.setAttribute('NAME', propValue); // this will set the text
					tooperate.setAttribute('name', propValue); // this will set the dom 
				}
			} else {
				tooperate.removeAttribute('name');
				tooperate.removeAttribute('NAME');
				if (tooperate.getAttribute('href') == '') {
					if (this.owner.svdSelection) {
						this.owner.util_restoreSelection();
					} else {
						this.owner.cw.focus();
					}
					this.owner.logic_updateDOMHierarchy(true, 0);
					tooperate.outerHTML = tooperate.innerHTML;
					this.owner.logic_removeTag(1);
				}
				/*
				if (Ktml_mozilla) {
					this.owner.edit.execCommand(DECMD_UNANCHOR, OLECMDEXECOPT_DODEFAULT, null);
				} else {
					this.owner.edit.execCommand(DECMD_UNANCHOR);
				}
				*/
			}
		}
		//LA CE FOLOSESTE ASTA?
		//Comentat de MPR ca sa prinda focusul Href-ul.
/* 		if (this.owner.svdSelection) {
			this.owner.util_restoreSelection();
		} else {
			this.owner.cw.focus();
		}
 */				
		if(this.owner.undo) {
			this.owner.undo.addEdit();
		}
	}
	Logging.exiting(3);
}

/**
* change the href property
*
* @param
*	propValue - property value
*/
function PropertiesLink_target_changed(propValue){
	propName = 'target';
	if (this.owner.selectableNodes && this.owner.selectableNodes[0]) {
		if (this.owner.selectableNodes[0].tagName == 'A') {
				var tooperate = this.owner.selectableNodes[0];
		}
		if (propValue != '' && propValue != 'false') {
			propValue = String(propValue);
			propValue = propValue.replace(/'/g, "\\'");
			tooperate.setAttribute(propName, propValue);
			if (propName == 'name' && tooperate.tagName == 'A') {
				tooperate.setAttribute(propName, propValue);
			}
		} else {
			tooperate.removeAttribute(propName);
		}
		if (this.owner.svdSelection) {
			this.owner.util_restoreSelection();
		} else {
			this.owner.cw.focus();
		}
		
		if(this.owner.undo) {
			this.owner.undo.addEdit();
		}
	}
}


function PropertiesLink_chooseFileLink(element, idu, counter) {
	var pth = this.owner.pathToFileDir;
	util_openwnd("uploadImage",NEXT_ROOT+"includes/ktedit/dirbrowser.php?mode=img&submode=file&elname="+idu+"&ktmlid="+this.owner.pageId + '&counter=' + counter, 700, 540);
}

/**
* selects an option in a html dropdown
*
* @param
*	select - dropdown object
*	option - the value to be selected
*/
function PropertiesLink_selectOption(select, option) {
	for (i=0; i<select.options.length; i++) {
		if(select.options[i].value==option) {
			select.options[i].selected = true;
			return;
		}
	}
	// select default option
	select.options[0].selected = true;
}

function PropertiesLink_hndlr_keyDown(e) {
	var keycode = e.keyCode;
	if (keycode == 0) {
		keycode = e.charCode;
	}
	if (keycode == 13) {
		e.preventDefault();
		e.stopPropagation();
		return false;
	}
}
