﻿var objEditor;
var objIframe = null;
var objBodyTop = null;
var objText = null;
var objDiv = null;
var objbodyMain;
var objdivLockScreen;
var selWindow;
var templWindow;
var contentWindow;
var cusWindow;
var strWinmetrics = "resizable=1,width=600,height=400,left=100,top=100,scrollbars=1,toolbar=0,menubar=0,modal=yes,alwaysRaised=yes"


function LoadRad(){
    AttachToolEvents();
    ResizeColorDropDowns(objEditor);
    objText = objBodyTop.getElementsByName("thehtmlbody");

    if(objText[0]!=null){
        objEditor.SetHtml(objText[0].value);
    }

    //Handle FireFox Delete Bug
    objEditor.SetMode(3);
    objEditor.SetMode(1);
    //attachResizeEvents(objEditor)
    //objEditor.SetSize(objIframe.document.body.clientWidth,objIframe.document.body.clientHeight);
    //remove the screen blocker
    objbodyMain = objBodyTop.getElementById("bodyMain");
    if(objbodyMain!=null){
        objdivLockScreen = objBodyTop.getElementById("divLockScreen");
        objbodyMain.removeChild(objdivLockScreen);
    }
    
    var customFilter = new RadEditorLeftBracket(); 
    objEditor.FiltersManager.Add(customFilter); 
    
    var customFilter = new RadEditorRightBracket(); 
    objEditor.FiltersManager.Add(customFilter);
    
     var customFilter = new RadEditorAmp(); 
    objEditor.FiltersManager.Add(customFilter);
}

function AttachToolEvents(){

    RadEditorCommandList["Clear"] = function(commandName, editor, oTool){
		return ClearEditor(editor, oTool);
	}

    RadEditorCommandList["Image Library"] = function(commandName, editor, oTool){
		return InsertClubPicture(editor, oTool);
	}

    RadEditorCommandList["Global Library"] = function(commandName, editor, oTool){
		return InsertGlobalPicture(editor, oTool);
	}

    RadEditorCommandList["Templates"] = function(commandName, editor, oTool){
		return InsertTemplate(editor, oTool);
	}

    RadEditorCommandList["Content Hyperlink"] = function(commandName, editor, oTool){
		return InsertContent(editor, oTool);
	}

    RadEditorCommandList["Custom Directory"] = function(commandName, editor, oTool){
		return InsertCustomDir(editor, oTool);
	}
	
	RadEditorCommandList["Preview Page"] = function(commandName, editor, oTool){
		return ShowPreview(editor, oTool);
	}

	RadEditorCommandList["IncreaseSize"] = function(commandName, editor, oTool){
        SizeIncrease(editor, oTool);
    }
    
    RadEditorCommandList["DecreaseSize"] = function(commandName, editor, oTool){
        SizeDecrease(editor, oTool);
    }
    /*
    RadEditorCommandList["ForeColor"] = function(commandName, editor, oTool){      
        var tool = oTool.GetSelectedValue(); //returns the color value selected in the dropdown
        //make a check whether the value is of HEX format and supply it as an argument to the execCommand function of the browser
        editor.Document.execCommand("foreColor",false, tool);
    } 
    RadEditorCommandList["BackColor"] = function(commandName, editor, oTool){      
        var tool = oTool.GetSelectedValue();
        editor.Document.execCommand("backColor",false, tool);
    } 
    RadEditorCommandList["ImageMapDialog"] = function(commandName, editor, oTool){
    }
    /*
    RadEditorCommandList["ModuleManager"] = function(commandName, editor, oTool){
       return SizeFrame(editor, oTool);
    }
    RadEditorCommandList["GSConfig Colors"] = function(commandName, editor, oTool){
        var selection = editor.GetSelectionHtml();
        editor.PasteHtml('<span style="color: '+oTool.GetSelectedValue()+';">' + selection + '</span>'); 
    }
    */
    
    objEditor.AttachEventHandler ("RADEVENT_SEL_CHANGED", function (e){
        SelectionChanged();
    });
    objEditor.AttachEventHandler ("RADEVENT_MODE_CHANGED", function (e){
        DisplayModeChanged();
    });
    objEditor.AttachEventHandler ("RADEVENT_SIZE_CHANGED", function (e){
        EditorSizeChanged();
    });
    objEditor.AttachEventHandler ("onclick", function (e){
        EditorClicked();
    });
    objEditor.AttachEventHandler ("ondblclick", function (e){
        EditorDoubleClicked();
    });
}

function RadEditorLeftBracket() { 
    this.GetHtmlContent = function (content){ 
        re = /%5B/gi
        newContent = content.replace(re, "["); 
        return newContent; 
    }; 
} 

function RadEditorRightBracket() { 
    this.GetHtmlContent = function (content){ 
        re = /%5D/gi 
        newContent = content.replace(re, "]"); 
        return newContent; 
    }; 
} 

function RadEditorAmp() { 
    this.GetHtmlContent = function (content){ 
        re = /&amp;/gi 
        newContent = content.replace(re, "&"); 
        return newContent; 
    }; 
} 


function SelectionChanged(){
    //alert("RADEVENT_SEL_CHANGED");
}

function DisplayModeChanged(){
    //alert("Display Mode Changed");
}

function EditorSizeChanged(){
    //alert("Size Changed");
}

function EditorClicked(){
    //alert("editor Click");
    //document.getElementById("radEditorInsertImage"+ "<%=mfEditor.ClientID%>").click();
    //objEditor.ShowDialog("SetImageProperties");
}

function EditorDoubleClicked(){
    //alert("editor double click");
}
 
function ResizeColorDropDowns(editor){
    var objFore = editor.GetToolByName("ForeColor"); 
    var objBack = editor.GetToolByName("BackColor");

    objFore.PopupWidth = 200;
    objFore.PopupHeight = 200;
    objBack.PopupWidth = 200;
    objBack.PopupHeight = 200;
}

function ClearEditor(sender, e){
    var objResponse = confirm("Do you really want to clear the editor?");

    if (objResponse !=0){ 
        sender.SetHtml("");
    } 
}

function CatchSubmit(sender, e){
//alert("submit " + e);
}

function CatchEvent(sender, e){
    //debugger;
    //alert("event " + e);
    switch(e){
    case "ImageMapDialog":
      //SizeForImageMap(sender, e);
      //var objImageMap = sender.GetToolByName("ImageMapDialog"); 
      
      //objImageMap.Element.style.top = 220;
     // alert("Event");
     
      //alert(objImageMap);
      break
    default:
   case "LinkManager":
     //var objCss = sender.GetToolByName("LinkManager");
      break       
    }
    SizeFrame(sender, e);
}

function CatchExecution(sender, e){
    //alert("execution " + e);
    switch(e){
    case "ImageMapDialog":
        var objSelected = sender.GetSelection();
        var objElement = objSelected.GetParentElement();
        if(objElement.tagName!="IMG"){
            alert("Please select an image to map first");
            return false;
        }
        
      break  
    default:
    }
}

function SizeFrame(sender, e){
    var currentWidth = sender.GetWidth();
    var currentHeight = sender.GetHeight();
    objBodyTop.getElementById('edit1').height = sender.GetHeight() + "px;";
}

function SizeIncrease(sender, e){
    var currentWidth = sender.GetWidth();
    var currentHeight = sender.GetHeight();
    sender.SetSize(currentWidth, currentHeight + (currentHeight / 3));
    //objBodyTop.getElementById('edit1').height = sender.GetHeight() + 10 + "px;";
}

function SizeForImageMap(sender, e){
    var currentWidth = sender.GetWidth();
    var currentHeight = sender.GetHeight();
    sender.SetSize(currentWidth, 600);
    //objBodyTop.getElementById('edit1').height = sender.GetHeight() + 10 + "px;";
}

function SizeDecrease(sender, e){
    var currentWidth = sender.GetWidth();
    var currentHeight = sender.GetHeight();
    if(currentHeight > 250){
        sender.SetSize(currentWidth, currentHeight - (currentHeight / 3));
        objBodyTop.getElementById('edit1').height = sender.GetHeight() + 10 + "px;";
    }
}

function resizeContentArea(editor){
    if (editor.InitialHeight == -1){
        editor.InitialHeight = editor.Document.body.clientHeight;
    }
    var targetHeight = editor.Document.body.scrollHeight;
    if (targetHeight > editor.InitialHeight){
        var theIFrame = document.getElementById("edit1" + editor.Id);
        objBodyTop.getElementById('edit1').height = parseInt(targetHeight) + "px";
        editor.SetSize(currentWidth, parseInt(targetHeight));
    }
}

function attachResizeEvents(editor){
    editor.InitialHeight = -1;
    editor.GetContentArea().style.overflow = "hidden";
    editor.Document.body.scroll = "no";
    var resizeFnRef = function anon(){resizeContentArea(editor)};
    editor.AttachEventHandler("RADEVENT_SEL_CHANGED", resizeFnRef)
    editor.AttachEventHandler("keydown", resizeFnRef)
}

/*Window popups*/

function InsertClubPicture(sender, e){ 

	var strOpen 
    strOpen = '/admin/scripts/imagelib/imagelib_select.asp?TYPE=1';

    if (selWindow == null ||  selWindow.closed){
        selWindow = open(strOpen,"",strWinmetrics);
    }
    else{
        selWindow.location.href = strOpen;
    }
        selWindow.focus();

    return false;
}

function InsertGlobalPicture(sender, e){  
    var strOpen 
    strOpen = '/admin/scripts/imagelib/imagelib_select.asp?GT=G&TYPE=1';

	if (selWindow == null ||  selWindow.closed){
         selWindow = open(strOpen,"",strWinmetrics);
    }
	else{
		selWindow.location.href = strOpen;
	}
        selWindow.focus();
         return false;
}

function InsertTemplate(sender, e){
	var bModal = true;
	var strOpen 
	strOpen = '/admin/scripts/templates/templ_select.asp?TYPE=1';

	if (templWindow == null ||  templWindow.closed){
        templWindow = open(strOpen,"",strWinmetrics);
	}
	else{
	    templWindow.location.href = strOpen;
    }
     templWindow.focus();
	 return false;
}

//function InsertContent(inMail){
function InsertContent(sender, e){

	var strOpen 
	strOpen = '/admin/scripts/content/content_select.asp?TYPE=1';

	//if (inMail==true){
	//	strOpen = strOpen + '&MAIL=1'
	//}

	if (contentWindow == null ||  contentWindow.closed){
       contentWindow = open(strOpen,"",strWinmetrics);
	}
	else{
	    contentWindow.location.href = strOpen;
    }

     contentWindow.focus();
    return false;
}

function InsertCustomDir(sender, e){
    var strOpen 
    strOpen = '/admin/scripts/custom/custom_select.asp?GT=G&TYPE=1';

	if (cusWindow == null ||  cusWindow.closed){
       cusWindow = open(strOpen,"",strWinmetrics);
    }
	else{
		cusWindow.location.href = strOpen;
	}
    cusWindow.focus();
    return false;
}

function ShowPreview(sender, e){
  
    var strHeader;
    var strControls;
    var strContent;
    var strFooter;
    //<input type=\"button\" style=\"width:100%\" onclick=\"window.close();\" value=\"Close\" /><br />
	strControls = "<table style=\"width:100%;white-space:nowrap;\"><tr><td style=\"text-align:left;width:50%;\"><input onclick=\"window.print();\" type=\"button\" value=\"Print\" /></td><td style=\"text-align:right;width:50%;\"><input onclick=\"window.close();\" type=\"button\" value=\"Close\" /></td></tr></table>";
	strHeader = "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\" />";
    strHeader = strHeader + "<html xmlns=\"http://www.w3.org/1999/xhtml\"><head><title>Preview</title></head><body style=\"margin: 0px 0px 0px 0px; padding: 0px 0px 0px 0px;\">"+strControls+"<div style=\"margin: 8px 8px 8px 8px\">";
    strFooter = "</div></body></html>";
	strContent = sender.GetHtml();
	
	if (cusWindow == null ||  cusWindow.closed){
       cusWindow = open("about:blank","",strWinmetrics);
    }
	else{
		cusWindow.location.href = "about:blank";
	}
   
    cusWindow.document.write(strHeader + strContent + strFooter);
    cusWindow.focus();
    return false;
}

function PutImage(inVal){
    if (window.opener && !window.opener.closed){
	     alert(document.forms["frmMain"]['IMG_URL_'+inVal].value);
	     //window.opener.doInsertPictureReturn(document.forms["frmMain"]['IMG_URL_'+inVal].value, document.forms["frmMain"]['IMG_SIZE_WIDTH_'+inVal].value, document.forms["frmMain"]['IMG_SIZE_HEIGHT_'+inVal].value);
	}
	else{
		alert ("Original Opener Window not available...");
    }
	window.opener.focus();
	window.close();
}

function doInsertPictureReturn(inFilename, inWidth, inHeight){
	var tmpSize = "";

	if (arguments[1] != null && arguments[2] !=null){
		tmpSize = " WIDTH=" + inWidth + " HEIGHT=" + inHeight;
	}
	objEditor.PasteHtml("<img border='0' alt='img' src='" + inFilename + "' " + tmpSize + " />"); 
}

function doTemplateReturn(inText){
	objEditor.PasteHtml(inText);
}

function doContentReturn(inType, inID, inText, inTarget, inPopup){

	var tmpLink = 'http://[' + inType + '_hyperlink:' + inID + ']/'
    var objSelected = objEditor.GetSelection();
    var strSelectedText = objSelected.GetText();
    var strSelectedHtml = objSelected.GetHtmlText();
    var strSelectedElement = objSelected.GetParentElement();

    if(strSelectedElement.tagName =="img" || strSelectedElement.tagName =="IMG"){
        //inText = strSelectedElement.outerHTML;  bombs in Ffox outerHTML not available so rebuild it
        strImg = "<img ";
        strImg = strImg + "alt='" +strSelectedElement.alt +"' ";
        strImg = strImg + "src='" +strSelectedElement.src +"' ";
        
        if(strSelectedElement.height >0){
            strImg = strImg + "height='" + strSelectedElement.height +"' ";
        }
        
        if(strSelectedElement.width >0){
            strImg = strImg + "width='" + strSelectedElement.width +"' ";
        }
        strImg = strImg + "border='0'  />"
        inText = strImg; 
    }
    else if(strSelectedText.length>0){
        inText = strSelectedText;
    }

	if (inTarget.length>0){
		doInsertHyperlinkSimple (tmpLink, inText, arguments[3]);
	}
	else{
		if (inPopup){
			doInsertHyperlinkPopup (tmpLink, inText);
		}
		else{
			doInsertHyperlinkSimple (tmpLink, inText);
		}
	}
}

function doCustomReturn(inPath, inFilename){
	if (inFilename != ''){
		doInsertHyperlinkSimple (inPath, inFilename)
	}
}
/*
function doInsertHyperlink(){
	var linkURL = prompt("Enter the Hyperlink URL: ", "http://");
	if (linkURL != null){	
		var linkText = prompt("Enter the Hyperlink Text: " , linkURL);
		if (linkText != null){
			edit1.doInsertHyperlink(linkURL, linkText);
		}
	}
}
*/
function doInsertHyperlinkSimple(inLink, inText){
	objEditor.PasteHtml("<a href='" + inLink + "'>"+ inText + "</a>"); 
}

function FullScreen(){
    objDiv = objBodyTop.getElementById("tfullpagetext");
    objDiv.style.position = "absolute";
    objDiv.style.top="0px";
    objDiv.left="0px";
}