﻿var dataHandlerIFrame;
var selectedControlID;

function saveSection( data, sectionID, pageName, subPageName, dataHandlerUrl ) 
{
    dataHandlerIFrame = document.createElement( "iframe" );
    dataHandlerIFrame.src = dataHandlerUrl;
    dataHandlerIFrame.style.position = "absolute";
    dataHandlerIFrame.style.top="0px";
    dataHandlerIFrame.style.width="100%";
    //dataHandlerIFrame.style.visibility = "hidden";
    document.body.appendChild( dataHandlerIFrame );
    $(dataHandlerIFrame).load( function() { dataHandlerIFrame.contentWindow.reciveData( data,  sectionID, pageName, subPageName ); $(dataHandlerIFrame).unbind(); } );
}

function closeDataHandlerIFrame() 
{
    document.body.removeChild( dataHandlerIFrame );
}

function plainTextEdit_Click(controlID) {
    Shadowbox.open({type:'html', content:"",height:436,width:506},{fadeDuration:0.1,resizeDuration:0.1,enableKeys:false,
    onFinish:function(){
        cont = document.createElement( "div" );
        cont.className="cms_plainTextEditor";
        ta = document.createElement("textarea");
        ta.className="cms_textedit";
        ta.value = $("#" + controlID +  "_txt")[0].value.replace(/<br \/>/g,"\n");
        bs = document.createElement("input");
        bs.value="Save";
        bs.type="button";
        bc = document.createElement("input");
        bc.value="Cancel";
        bc.type="button";

        cont.appendChild(ta);
        cont.appendChild(bs);
        cont.appendChild(bc);
        $("#shadowbox_content")[0].appendChild(cont);
        $(bs).click(function() { plainTextEditSave_Click(controlID); });
        $(bc).click(function() { Shadowbox.close(); });
    }});
}

function hyperLinkEdit_Click(controlID) {
    Shadowbox.open({type:'html', content:"",height:120,width:370},{fadeDuration:0.1,resizeDuration:0.1,enableKeys:false,
    onFinish:function(){
        cont = document.createElement( "div" );
        text = document.createElement("input");
        hyp = document.createElement( "input" );
        bs = document.createElement("input");
        bc = document.createElement("input");
        label1 = document.createElement("label");
        label2 = document.createElement("label");
        buttons = document.createElement( "div" );
        
        cont.className="cms_hyperLinkEdit";
        label1.innerHTML = "Text:";
        text.type = "text";
        text.className="textbox";
        text.value = $("#" + controlID +  "_hyp")[0].innerHTML;
        label2.innerHTML = "URL: <small>~/ is a shortcut for the root of this site.</small>";
        hyp.type = "text";
        var sRegExInput = new RegExp(absoluteApplicationPath, "i");
        hyp.value = $("#" + controlID +  "_hyp")[0].href.replace( sRegExInput, "~/" );
        hyp.className="textbox";
        bs.value="Save";
        bs.type="button";
        bc.value="Cancel";
        bc.type="button";
        buttons.appendChild(bs);
        buttons.appendChild(bc);

        cont.appendChild(label1);
        cont.appendChild(text);
        cont.appendChild(label2);
        cont.appendChild(hyp);
        cont.appendChild(buttons);
        $("#shadowbox_content")[0].appendChild(cont);
        $(bs).click(function() { hyperLinkEditSave_Click(controlID); });
        $(bc).click(function() { Shadowbox.close(); });
    }});
}

function imageEdit_Click(controlID) {
    selectedControlID = controlID;
    Shadowbox.open({type:'html', content:"",height:436,width:700},{fadeDuration:0.1,resizeDuration:0.1,enableKeys:false,
    onFinish:function(){
        try {
            cont = document.createElement( "div" );
            cont.className="cms_backgroundEditor";
            $(cont).css({height:"436px",  backgroundColor:"white", margin:"0px auto 0px auto", overflow:"hidden"});
            ifrm = document.createElement("iframe");
            ifrm.src = "FileManager/default.aspx";
            $(ifrm).css({height:"432px", width:"694px", border:"none"});

            ifrm.frameBorder = 0;
            cont.appendChild(ifrm);
            $("#shadowbox_content")[0].appendChild(cont);
            

           $(ifrm).load(function() {this.contentWindow.setFileSelectedFunction(imageChanged);$(ifrm.contentWindow.document.body).css({overflow:"hidden", border:"none" } );});
        }
        catch(exp) { alert(exp); }
    }});
}
