var ActiveEditor = null; // Is used in call back function.

function GlobalSiteImage(CommandName, CurrentEditor, oTool) {
	ActiveEditor=CurrentEditor; // ActiveEditor is used in call back function.
	
	var imageElement = ActiveEditor.GetSelection().GetParentElement();
	if ( imageElement.tagName!="IMG" ) imageElement=null;

	var DialogParameters="";

	if ( typeof(ParentObjectId)=="string" ) {
		if ( DialogParameters ) DialogParameters+="&";
		DialogParameters+="ParentObjectId="+ParentObjectId+"&EnableCreate=True";
	}

	if ( imageElement ) {
		var ObjectId=GetUrlObjectId(imageElement.src);
		if ( ObjectId ) {
			if ( DialogParameters ) DialogParameters+="&";
			DialogParameters+="ObjectId="+ObjectId;
		 }
	}

	if ( DialogParameters ) DialogParameters="?"+DialogParameters;
    
	// Don't send found image element. Lots of extra attributes is added by: ActiveEditor.ShowDialog.
	var Element=null;
	if ( imageElement ) {
		Element={
			src:imageElement.src,
			width:imageElement.width,
			height:imageElement.height,
			hspace:imageElement.hspace,
			vspace:imageElement.vspace,
			border:imageElement.border,
			align:imageElement.align,
			alt:imageElement.alt,
			className:imageElement.className
		};
	}

	ActiveEditor.ShowDialog(
		dialogPath+"ImageDialog.aspx"+DialogParameters
		, Element //argument
		, 470
		, 300
		, GlobalSiteImageCallBack
		, null
		, "Image Manager");
}; // GlobalSiteImage

function GlobalSiteImageCallBack(ContentArray) {
    if (ContentArray) {
		var imageElement = ActiveEditor.GetSelection().GetParentElement();
		
		if ( imageElement.tagName!="IMG" ) {
			ActiveEditor.PasteHtml("<img id=\"NewGlobalSiteImage\" />");
			imageElement = ActiveEditor.Document.getElementById("NewGlobalSiteImage")
		}
		if ( imageElement.tagName!="IMG" ) {
			alert("Missing image element!");
		} else {
			for ( Key in ContentArray ) {
				eval("imageElement."+Key+"=\""+ContentArray[Key]+"\"");
			}
			if ( imageElement.id=="NewGlobalSiteImage" ) imageElement.removeAttribute("id");
			if ( imageElement.width=="0" ) imageElement.removeAttribute("width");
			if ( imageElement.height=="0" ) imageElement.removeAttribute("height");
			if ( imageElement.hspace=="0" ) imageElement.removeAttribute("hspace");
			if ( imageElement.vspace=="0" ) imageElement.removeAttribute("vspace");
		}
	}
}; // GlobalSiteImageCallBack

function TestGlobalSiteLink(CommandName, CurrentEditor, oTool) {
	var linkElement = CurrentEditor.GetSelection().GetParentElement();
	if ( linkElement.tagName!="A" ) existingLinkManager(CommandName, CurrentEditor, oTool);
	else {
		var ObjectId=GetUrlObjectId(linkElement.href);
		if ( ObjectId ) GlobalSiteLink(CommandName, CurrentEditor, oTool);
		else existingLinkManager(CommandName, CurrentEditor, oTool);
	}
}; // TestGlobalSiteLink

function GlobalSiteLink(CommandName, CurrentEditor, oTool) {
	ActiveEditor=CurrentEditor; // ActiveEditor is used in call back function.

	var ObjectId=null;
	var linkElement = ActiveEditor.GetSelection().GetParentElement();
	if ( linkElement.tagName!="A" ) linkElement=null;

	var DialogParameters="";

	if ( typeof(ParentObjectId)=="string" ) {
		if ( DialogParameters ) DialogParameters+="&";
		DialogParameters+="ParentObjectId="+ParentObjectId;
	}
	if ( linkElement ) ObjectId=GetUrlObjectId(linkElement.href);

	// If link exists but no ObjectId Then use the normal existing link editor.
	if ( linkElement && !ObjectId ) existingLinkManager(CommandName, CurrentEditor, oTool);
	else {
		if ( DialogParameters ) DialogParameters+="&";
		DialogParameters+="ObjectId="+ObjectId;
		DialogParameters="?"+DialogParameters;
		
		// Don't send found link element. Lots of extra attributes is added by: ActiveEditor.ShowDialog.
		var Element=null;
		if ( linkElement ) {
			Element={
				href:linkElement.href,
				target:linkElement.target,
				className:linkElement.className,
				title:linkElement.title
			};
		}

		ActiveEditor.ShowDialog(
			dialogPath+"LinkDialog.aspx"+DialogParameters
			, Element //argument
			, 470
			, 200
			, GlobalSiteLinkCallBack
			, null
			, "Document Manager");
	}
}; // GlobalSiteLink

function GlobalSiteLinkCallBack(ContentArray) {
    //ActiveEditor.SetEditable(true);

	if (ContentArray) {
		//ShowObjectProperties(ContentArray);
		var Element = ActiveEditor.GetSelection().GetParentElement();
		var SelectedText = ActiveEditor.GetSelection().GetText();
		
		if ( Element.tagName!="A" ) {
			if ( Element.tagName!="IMG" ) ActiveEditor.PasteHtml("<a id=\"NewGlobalSiteLink\"></a>");
			else ActiveEditor.PasteHtml('<a id="NewGlobalSiteLink">'+Element.outerHTML+'</a>');
			Element = ActiveEditor.Document.getElementById("NewGlobalSiteLink")
		}
		if ( Element.tagName!="A" ) {
			alert("Missing link element!");
		} else {
			for ( var Key in ContentArray ) eval("Element."+Key+"=ContentArray[Key];");
			
			if ( Element.id=="NewGlobalSiteLink" ) Element.removeAttribute("id");
			if ( Element.target=="" ) Element.removeAttribute("target");
			if ( Element.name=="" ) Element.removeAttribute("name");
			if ( Element.title=="" ) Element.removeAttribute("title");
			
			if ( !Element.innerHTML ) {
				SelectedText ? Element.innerHTML=SelectedText : ContentArray["title"] ? Element.innerHTML=ContentArray["title"] : Element.innerHTML=Element.href;
			}
		}
	}
}; // GlobalSiteLinkCallBack

function GlobalSiteEditorLoad(CurrentEditor) {
	//alert("GlobalSiteEditorLoad!");
	GlobalSiteEditorValidationUpdate(CurrentEditor);
}; // GlobalSiteEditorLoad

function GlobalSiteEditorValidationUpdate(CurrentEditor) {
	CurrentEditor.AttachEventHandler ("RADEVENT_KEYDOWN", function (e) { CurrentEditor.IsChange=true; });
	CurrentEditor.ContentHiddenTextarea.GsSave=function() { if ( CurrentEditor.IsChange ) RadEditorNamespace.SaveEditorValue(CurrentEditor); };
}; // GlobalSiteEditorValidationUpdate

function ShowObjectProperties(obj) {
	var count=0;
	var Functions="ShowObjectProperties:\n";
	var ShortFunctions="";
	for ( var Function in obj ) {
		var tf=Function.toLowerCase();
		//if ( tf.indexOf("CommonInternalParameters")>-1 ) {
		if ( tf.indexOf("skin")>-1 ) {
			count+=1;
			if ( (count%40)==0 ) {
				alert(Functions+ShortFunctions);
				Functions="";
				ShortFunctions="";
			}
			if ( Function.length<=3 ) ShortFunctions+=", "+Function;
			else {
				var FunctionType=typeof(eval("obj."+Function));
				if ( FunctionType=="boolean" ) {
					var FunctionValue=eval("obj."+Function);
					if ( FunctionValue ) 
						Functions+=Function+" ("+FunctionType+") = true\n";
					else 
						Functions+=Function+" ("+FunctionType+") = false\n";
				} else if ( FunctionType!="string" ) {
					Functions+=Function+" ("+FunctionType+")\n";
				} else {
					var FunctionValue=eval("obj."+Function);
					if ( FunctionValue.length>80 ) FunctionValue="#MAX 80 characters#";
					Functions+=Function+" ("+FunctionType+") = "+FunctionValue+"\n";
				}
			}
		}
	}
	alert(Functions+ShortFunctions);
}; // ShowObjectProperties

