﻿//
//
// TextEditorProvider
//
// -------------------------------------------------------------------------
function TextEditorProvider(objectName){
    this.name = objectName;
    this.width = 500;
    this.height = 500;
    this.EditContent=function(e, txtEl, hidEl, onShowHandlerName){
        if (onShowHandlerName != '') eval(onShowHandlerName);
        this.textElement = txtEl;
        this.hiddenElement = hidEl;
        
        var textElement = document.getElementById(this.textElement);
        var hiddenElement = document.getElementById(this.hiddenElement);
        
        var blockId = this.name + "_Div";
        var block = document.getElementById(blockId);
        
        if (!e) e = window.event;

        if (e) {
            if (e.pageX || e.pageY) {
                mousex = e.pageX;
                mousey = e.pageY;

            } else if (e.clientX || e.clientY) {
                mousex = e.clientX + document.body.scrollLeft;
                mousey = e.clientY + document.body.scrollTop;
            }
        }
        
        if (document.all) {
            windowHeight = document.body.offsetHeight ;
            windowWidth = document.body.offsetWidth ;
        } else {
            windowHeight = window.innerHeight;
            windowWidth = window.innerWidth;
        }                    
        
        if (block){
            block.style.display = "block";
            //block.style.visibility="visible";
            
            var left = 0; 
            var top = 0;
            
            if (document.documentElement && document.documentElement.scrollLeft)
				left = document.documentElement.scrollLeft;
			else if (document.body)
				left = document.body.scrollLeft;
            
			if (document.documentElement && document.documentElement.scrollTop)
				top = document.documentElement.scrollTop;
			else if (document.body)
				top = document.body.scrollTop;
			
			left += 70;
			top += 70;
            
            if (left + this.width > windowWidth)
                left = windowWidth - this.width;
            
            if (left < 1)
                left = 1;
            if (top < 1)
                top = 1;
                
            block.style.left = left + "px";
            block.style.top = top + "px";
        }
        
        
        var editorId = this.name + "_Editor";
        var editor = document.getElementById(editorId);
        editor.value = textElement.innerHTML;
    };
    
    this.SetData=function(){
        var textElement = document.getElementById(this.textElement);
        var hiddenElement = document.getElementById(this.hiddenElement);
        
        var editorId = this.name + "_Editor";
        var editor = document.getElementById(editorId);
        
        textElement.innerHTML = editor.value;
        hiddenElement.value = editor.value;
        
        this.CloseEditor();
    };
    
    this.MouseOver=function(element){element.style.border="1px solid black";};
    
    this.MouseOut=function(element){element.style.border="1px solid #eeeeee";};
    
    this.CloseEditor=function(){
        var blockId = this.name + "_Div";
        var block = document.getElementById(blockId);
        if (block){
            block.style.display = "none";                   
        }
    };
    
}

//
//
// RichTextEditorProvider
//
// -------------------------------------------------------------------------
function RichTextEditorProvider(objectName){
    this.name = objectName;
    this.width = 500;
    this.height = 500;
    this.EditContent=function(e, txtEl, hidEl, onShowHandlerName){
        if (onShowHandlerName != '') eval(onShowHandlerName);
        this.textElement = txtEl;
        this.hiddenElement = hidEl;
        
        var textElement = document.getElementById(this.textElement);
        var hiddenElement = document.getElementById(this.hiddenElement);
        
        var blockId = this.name + "_Div";
        var block = document.getElementById(blockId);
        
        if (!e) e = window.event;

        if (e) {
            if (e.pageX || e.pageY) {
                mousex = e.pageX;
                mousey = e.pageY;

            } else if (e.clientX || e.clientY) {
                mousex = e.clientX + document.body.scrollLeft;
                mousey = e.clientY + document.body.scrollTop;
            }
        }
        
        if (document.all) {
            windowHeight = document.body.offsetHeight ;
            windowWidth = document.body.offsetWidth ;
        } else {
            windowHeight = window.innerHeight;
            windowWidth = window.innerWidth;
        }                    
        
        if (block){
            var isMozilla = (document.all) ? 0 : 1;
            if (isMozilla == 1) {block.style.display = "block"; block.style.visibility = "visible";}
            else
                block.style.display = "block";
            
            
            var left = 0; 
            var top = 0;
            
            if (document.documentElement && document.documentElement.scrollLeft)
				left = document.documentElement.scrollLeft;
			else if (document.body)
				left = document.body.scrollLeft;
            
			if (document.documentElement && document.documentElement.scrollTop)
				top = document.documentElement.scrollTop;
			else if (document.body)
				top = document.body.scrollTop;
			
			left += 70;
			top += 70;
				
            
            if (left + this.width > windowWidth)
                left = windowWidth - this.width;
            
            if (left < 1)
                left = 1;
            if (top < 1)
                top = 1;
            block.style.left = left + "px";
            block.style.top = top + "px";
        }
        
        
        var editorId = this.name + "_Editor";
        var editor = document.getElementById(editorId);
        
        FTB_API[editorId].SetHtml(textElement.innerHTML);
        
        
    };
    
    this.SetData=function(){
        var textElement = document.getElementById(this.textElement);
        var hiddenElement = document.getElementById(this.hiddenElement);
        
        var editorId = this.name + "_Editor";
        
        textElement.innerHTML = FTB_API[editorId].GetHtml();
        hiddenElement.value = FTB_API[editorId].GetHtml();
        
        this.CloseEditor();
    };
    
    this.MouseOver=function(element){element.style.border="1px solid black";};
    
    this.MouseOut=function(element){element.style.border="1px solid #eeeeee";};
    
    this.CloseEditor=function(){
        var blockId = this.name + "_Div";
        var block = document.getElementById(blockId);
        if (block){
            block.style.display = "none";
//            var isMozilla = (document.all) ? 0 : 1;
//            if (isMozilla == 1) block.style.display = "none";// block.style.visibility = "hidden";
//            else
//                block.style.display = "none";
        }
    };
    
}

//
// ImageEditorProvider
//
// -------------------------------------------------------------------------
function ImageEditorProvider(objectName, appPath, onShowHandlerName){
    this.name = objectName;
    this.width = 300;
    this.height = 200;
    this.ApplicationPath = appPath;
    
    this.EditContent=function(e, imageEl, uri1El, widthEl, heightEl, altEl, titleEl, uri2El, targetEl, onShowHandlerName){
        if (onShowHandlerName != '') eval(onShowHandlerName);
        this.imageElement = imageEl;
        this.uri1Element = uri1El;
        
        this.widthElement = widthEl;
        this.heightElement = heightEl;
        this.altElement = altEl;
        this.titleElement = titleEl;
        
        this.uri2Element = uri2El;
        this.targetElement = targetEl;
        
        var uri1Element = document.getElementById(this.uri1Element);
        
        var widthElement = document.getElementById(this.widthElement);
        var heightElement = document.getElementById(this.heightElement);
        var altElement = document.getElementById(this.altElement);
        var titleElement = document.getElementById(this.titleElement);
        
        var uri2Element = document.getElementById(this.uri2Element);
        var targetElement = document.getElementById(this.targetElement);
        
        var blockId = this.name + "_Div";
        var block = document.getElementById(blockId);
        
        if (!e) e = window.event;

        if (e) {
            if (e.pageX || e.pageY) {
                mousex = e.pageX;
                mousey = e.pageY;

            } else if (e.clientX || e.clientY) {
                mousex = e.clientX + document.body.scrollLeft;
                mousey = e.clientY + document.body.scrollTop;
            }
        }
        
        if (document.all) {
            windowHeight = document.body.offsetHeight ;
            windowWidth = document.body.offsetWidth ;
        } else {
            windowHeight = window.innerHeight;
            windowWidth = window.innerWidth;
        }                    
        
        if (block){
            block.style.display = "block";
            
            var left = 0; 
            var top = 0;
            
            if (document.documentElement && document.documentElement.scrollLeft)
				left = document.documentElement.scrollLeft;
			else if (document.body)
				left = document.body.scrollLeft;
            
			if (document.documentElement && document.documentElement.scrollTop)
				top = document.documentElement.scrollTop;
			else if (document.body)
				top = document.body.scrollTop;
			
			left += 70;
			top += 70;

            
            if (left + this.width > windowWidth)
                left = windowWidth - this.width;
            
            if (left < 1)
                left = 1;
            if (top < 1)
                top = 1;
                
            
            block.style.left = left + "px";
            block.style.top = top + "px";
        }
        
        var uri1EditorId = this.name + "_uri1Box";
        var uri1Editor = document.getElementById(uri1EditorId);
        uri1Editor.value = uri1Element.value;
        
        var widthEditorId = this.name + "_widthBox";
        var widthEditor = document.getElementById(widthEditorId);
        widthEditor.value = widthElement.value;
        
        var heightEditorId = this.name + "_heightBox";
        var heightEditor = document.getElementById(heightEditorId);
        heightEditor.value = heightElement.value;
        
        var altEditorId = this.name + "_altBox";
        var altEditor = document.getElementById(altEditorId);
        altEditor.value = altElement.value;
        
        var titleEditorId = this.name + "_titleBox";
        var titleEditor = document.getElementById(titleEditorId);
        titleEditor.value = titleElement.value;
        
        var uri2EditorId = this.name + "_uri2Box";
        var uri2Editor = document.getElementById(uri2EditorId);
        uri2Editor.value = uri2Element.value;
        
        var targetEditorId = this.name + "_targetList";
        var targetEditor = document.getElementById(targetEditorId);
        var opts = targetEditor.options.length;
        for (i = 0; i < opts; i++)
            if (targetEditor.options[i].value == targetElement.value){
                targetEditor.selectedIndex = i;
                break;
            }
    };
    
    this.SetData=function(){
        var imageElement = document.getElementById(this.imageElement);
        var uri1Element = document.getElementById(this.uri1Element);
        
        var widthElement = document.getElementById(this.widthElement);
        var heightElement = document.getElementById(this.heightElement);
        var altElement = document.getElementById(this.altElement);
        var titleElement = document.getElementById(this.titleElement);
        
        var uri2Element = document.getElementById(this.uri2Element);
        var targetElement = document.getElementById(this.targetElement);
        
        var uri1EditorId = this.name + "_uri1Box";
        var uri1Editor = document.getElementById(uri1EditorId);
        uri1Element.value = uri1Editor.value;
        
        if (uri1Element.value.indexOf("~/") >= 0)
            imageElement.src = this.ApplicationPath + "/" + uri1Element.value.substring("~/".length, uri1Element.value.length);
        else{
            if (uri1Element.value.indexOf(this.ApplicationPath) < 0)
                imageElement.src = this.ApplicationPath + "/" + uri1Element.value;
            else
                imageElement.src = uri1Element.value;                
        }
            
        
        var widthEditorId = this.name + "_widthBox";
        var widthEditor = document.getElementById(widthEditorId);
        
            widthElement.value = widthEditor.value;
            imageElement.style.width = (widthEditor.value != "0") ? widthEditor.value : "";
        
        
        var heightEditorId = this.name + "_heightBox";
        var heightEditor = document.getElementById(heightEditorId);
        
            heightElement.value = heightEditor.value;
            imageElement.style.height = (heightEditor.value != "0") ? heightEditor.value : "";
        
        
        var altEditorId = this.name + "_altBox";
        var altEditor = document.getElementById(altEditorId);
        altElement.value = altEditor.value;
        imageElement.alt = altEditor.value;
        
        var titleEditorId = this.name + "_titleBox";
        var titleEditor = document.getElementById(titleEditorId);
        titleElement.value = titleEditor.value;
        imageElement.title = titleEditor.value;
        
        var uri2EditorId = this.name + "_uri2Box";
        var uri2Editor = document.getElementById(uri2EditorId);
        uri2Element.value = uri2Editor.value;
        
        var targetEditorId = this.name + "_targetList";
        var targetEditor = document.getElementById(targetEditorId);
        targetElement.value = targetEditor.options[targetEditor.selectedIndex].value;
        
        this.CloseEditor();
    };
    
    this.SetImageProperties=function(arg){
            // URI1, Width, Height, Alt, Title
            var values = arg.split(';');
            if (values.length >= 5){
            var uri1EditorId = this.name + "_uri1Box";
            var uri1Editor = document.getElementById(uri1EditorId);
            var incSrc = values[0];
            var startIndex = 0;
            var resSrc = "";
            startIndex = incSrc.toLowerCase().indexOf(this.ApplicationPath.toLowerCase());
            /*if (startIndex >= 0){
                resSrc = incSrc.substring(startIndex + this.ApplicationPath.length + 1, incSrc.length);
                resSrc = "~/" + resSrc;
            }
            else*/{
                resSrc = incSrc;
            }
            //var startIndex = incSrc.toLowerCase().indexOf(this.ApplicationPath.toLowerCase());
            // resSrc = incSrc;//.substring(incSrc.toLowerCase().indexOf(this.ApplicationPath.toLowerCase()) + this.ApplicationPath.length + 1, incSrc.length);
            uri1Editor.value = resSrc;//"~/" + resSrc;
            
            var widthEditorId = this.name + "_widthBox";
            var widthEditor = document.getElementById(widthEditorId);
            widthEditor.value = values[1];
            
            var heightEditorId = this.name + "_heightBox";
            var heightEditor = document.getElementById(heightEditorId);
            heightEditor.value = values[2];
            
            var altEditorId = this.name + "_altBox";
            var altEditor = document.getElementById(altEditorId);
            altEditor.value = values[3];
            
            var titleEditorId = this.name + "_titleBox";
            var titleEditor = document.getElementById(titleEditorId);
            titleEditor.value = values[4];
        }       
    };
    
    this.MouseOver=function(element){element.style.border="1px solid black";};
    
    this.MouseOut=function(element){element.style.border="1px solid #eeeeee";};
    
    this.CloseEditor=function(){
        var blockId = this.name + "_Div";
        var block = document.getElementById(blockId);
        if (block){
            block.style.display = "none";
        }
    };
}


//
// LinkEditorProvider
//
// -------------------------------------------------------------------------
function LinkEditorProvider(objectName){
    this.name = objectName;
    this.width = 300;
    this.height = 200;
    
    this.EditContent=function(e, linkEl, uriEl, textEl1, textEl2, classEl, onShowHandlerName){
        if (onShowHandlerName != '') eval(onShowHandlerName);
        this.linkElement = linkEl;
        this.uriElement = uriEl;
        this.classElement = classEl;
        
        this.textElement1 = textEl1;
        this.textElement2 = textEl2;
        
        
        var uriElement = document.getElementById(this.uriElement);
        var classElement = document.getElementById(this.classElement);
        
        var textElement1 = document.getElementById(this.textElement1);
        var textElement2 = document.getElementById(this.textElement2);
        
        
        var blockId = this.name + "_Div";
        var block = document.getElementById(blockId);
        
        if (!e) e = window.event;

        if (e) {
            if (e.pageX || e.pageY) {
                mousex = e.pageX;
                mousey = e.pageY;

            } else if (e.clientX || e.clientY) {
                mousex = e.clientX + document.body.scrollLeft;
                mousey = e.clientY + document.body.scrollTop;
            }
        }
        
        if (document.all) {
            windowHeight = document.body.offsetHeight ;
            windowWidth = document.body.offsetWidth ;
        } else {
            windowHeight = window.innerHeight;
            windowWidth = window.innerWidth;
        }                    
        
        if (block){
            block.style.display = "block";
            var left = 0; 
            var top = 0;
            
            if (document.documentElement && document.documentElement.scrollLeft)
				left = document.documentElement.scrollLeft;
			else if (document.body)
				left = document.body.scrollLeft;
            
			if (document.documentElement && document.documentElement.scrollTop)
				top = document.documentElement.scrollTop;
			else if (document.body)
				top = document.body.scrollTop;
			
			left += 70;
			top += 70;
            
            if (left + this.width > windowWidth)
                left = windowWidth - this.width;
            
            if (left < 1)
                left = 1;
            if (top < 1)
                top = 1;
                
            
            block.style.left = left + "px";
            block.style.top = top + "px";
        }
        
        var uriEditorId = this.name + "_uriBox";
        var uriEditor = document.getElementById(uriEditorId);
        uriEditor.value = uriElement.value;
        
        var textEditorId = this.name + "_textBox";
        var textEditor = document.getElementById(textEditorId);
        textEditor.value = textElement1.value;
        
        var classEditorId = this.name + "_classBox";
        var classEditor = document.getElementById(classEditorId);
        classEditor.value = classElement.value;
    };
    
    this.SetData=function(){
        var linkElement = document.getElementById(this.linkElement);
        var uriElement = document.getElementById(this.uriElement);
        var classElement = document.getElementById(this.classElement);
        
        var textElement1 = document.getElementById(this.textElement1);
        var textElement2 = document.getElementById(this.textElement2);
        
        var uriEditorId = this.name + "_uriBox";
        var uriEditor = document.getElementById(uriEditorId);
        uriElement.value = uriEditor.value;
        
        var classEditorId = this.name + "_classBox";
        var classEditor = document.getElementById(classEditorId);
        classElement.value = classEditor.value;
        textElement2.className = classEditor.value;
        
        var textEditorId = this.name + "_textBox";
        var textEditor = document.getElementById(textEditorId);
        textElement1.value = textEditor.value;
        textElement2.innerHTML = textEditor.value;
        
        //uriElement = textEditor.value;
        
        this.CloseEditor();
    };
    
    this.MouseOver=function(element){element.style.border="1px solid black";};
    
    this.MouseOut=function(element){element.style.border="1px solid #eeeeee";};
    
    this.CloseEditor=function(){
        var blockId = this.name + "_Div";
        var block = document.getElementById(blockId);
        if (block){
            block.style.display = "none";
        }
    }
}
