/* Functions for the advlink plugin popup */

tinyMCEPopup.requireLangPack();

function preinit() {
/*	var url;

	if (url = tinyMCEPopup.getParam("external_link_list_url"))
		document.write('<script language="javascript" type="text/javascript" src="' + tinyMCEPopup.editor.documentBaseURI.toAbsolute(url) + '"></script>');*/
}


function init() {
  
	var formObj = document.forms[0];
	var ed = tinyMCEPopup.editor;
	var enode = ed.selection.getNode();

	var node = ed.dom.getParent(enode, "A");
	var selTxt = ed.selection.getContent();

	if(enode.nodeName == 'IMG'){
	    document.getElementById("urlName").style.visibility = "hidden";
	  }
	
	if(node != null && typeof node != 'undefined' && node.nodeName == 'A'){
	  formObj.insert.value = tinyMCEPopup.getLang('update', 'Insert', true); 
	  
	  var url = ed.dom.getAttrib(node,"href");
	  var txt = node.firstChild.nodeValue;
	  var desc = ed.dom.getAttrib(node,"title");
	  formObj.urlField.value = url;
	  formObj.nameField.value = txt;
	  formObj.descField.value = desc;
	  
	}else{
	  
	  if(isURL(selTxt)){
	    formObj.urlField.value = selTxt.replace(/^\s*/,"");
	  }
	  if(enode.nodeName != 'IMG'){
	    formObj.nameField.value = selTxt;
	  }
	  if(selTxt.length > 0){
	    document.getElementById("urlName").style.visibility = "hidden";
	  }
	  
	}
}

function insertAction() {
	var formObj = document.forms[0];
	var ed = tinyMCEPopup.editor;
	var enode = ed.selection.getNode();

	var node = ed.dom.getParent(enode, "A");

	// Remove element if there is no href
	if (!formObj.urlField.value) {
		tinyMCEPopup.execCommand("mceBeginUndoLevel");
		i = ed.selection.getBookmark();
		ed.dom.remove(node, 1);
		ed.selection.moveToBookmark(i);
		tinyMCEPopup.execCommand("mceEndUndoLevel");
		tinyMCEPopup.close();
		return;
	}
	tinyMCEPopup.execCommand("mceBeginUndoLevel");

	var lnk = formObj.urlField.value;//ed.baseURI.toAbsolute(formObj.urlField.value);
	var title = formObj.descField.value;
	if(!title || title.length == 0){
	  title = lnk;
	}
	if (node && node.nodeName == 'A') {		
		ed.dom.setAttrib(node, "href", lnk);
	        ed.dom.setAttrib(node, "title", title);
		node.firstChild.nodeValue = formObj.nameField.value;

	}else {
// 	  if(ed.selection.getContent().length > 0){
// 		ed.getDoc().execCommand("unlink", false, null);
// 		tinyMCEPopup.execCommand("CreateLink", false, "#mce_temp_url#", {skip_undo : 1});
// 
// 		elementArray = tinymce.grep(ed.dom.select("a"), function(n) {return ed.dom.getAttrib(n, 'href') == '#mce_temp_url#';});
// 		for (i=0; i<elementArray.length; i++){
// 			var elm = elementArray[i];
// 			ed.dom.setAttrib(elm, "href", formObj.urlField.value);
// 		}
// 	  }else{
	    ed.execCommand('mceInsertContent', false, '<a href="'+lnk+'" title="'+title+'">'+formObj.nameField.value+'</a>', {skip_undo : 1});
// 	  }

	}

	tinyMCEPopup.execCommand("mceEndUndoLevel");
	tinyMCEPopup.close();

}


function isURL(str){
  if(str.match(/^http(s){0,1}:\/\//) || str.match(/^\s*www.*\..+/)){
    return true;
  }
  
  return false;
}

// While loading
preinit();
tinyMCEPopup.onInit.add(init);
