﻿var iloadCommand = new Object();
iloadCommand.Name = "ILoad";

iLoadGallery = function(editor, node)
{
    window.parent.editorFrame = window;
    var url = "./ILoadGallery.aspx?fr=" + escape(Date());
    var w = 350;
    var h = 500;
    url += "&width=" + escape(w);
    url += "&height=" + escape(h);
    window.parent.frames["ILoad1Frame"].document.location.href = url;
}

iLoadDisplay = function(editor, node)
{
    window.parent.editorFrame = window;
    var url = "./ILoad.aspx?fr=" + escape(Date());

    var o = node;
    if (o)
    {
        if (o.tagName == "IMG")
        {
            var w = o.clientWidth;
            var h = o.clientHeight;
            
            url += "&edit=1";
            url += "&width=" + escape(w);
            url += "&height=" + escape(h);
        }
    }
    if (window.parent.frames["ILoad1Frame"])
    {
        window.parent.frames["ILoad1Frame"].document.location.href = url;
    }
    else if (window.frames["ILoad1Frame"])
    {
         window.frames["ILoad1Frame"].document.location.href = url;
    }
    else
        alert ("failed!");
}

function GetAttribute( element, attName, valueIfNull )
{
	var oAtt = element.attributes[attName] ;

	if ( oAtt == null || !oAtt.specified )
		return valueIfNull ? valueIfNull : '' ;

	var oValue = element.getAttribute( attName, 2 ) ;
	
	if ( oValue == null )
		oValue = oAtt.nodeValue ;

	return ( oValue == null ? valueIfNull : oValue ) ;
}

function SetAttribute( element, attName, attValue )
{
	if ( attValue == null || attValue.length == 0 )
		element.removeAttribute( attName, 0 ) ;			// 0 : Case Insensitive
	else
		element.setAttribute( attName, attValue, 0 ) ;	// 0 : Case Insensitive
}

function ILoad1_ValueChanged(controlId, webImage)
{
    if (!webImage)
    {
        return;
    }
    var imageResize = webImage.SelectedImage();
    var retVal = oboutGetEditor("editor").InsertHTML("<img src='" + imageResize.FileUrl() + "' width='" + imageResize.Size.Width + "' height='" + imageResize.Size.Height + "' alt='' />"); 
}

function ILoadGallery_ValueChanged(controlId, webImage)
{
    if (!webImage)
    {
        return;
    }
}

function UploadSWF(editor)
{
    UploadFile(editor, "SWF")
}

function UploadPDF(editor)
{
    UploadFile(editor, "PDF")
}

function UploadWMV(editor)
{
    UploadFile(editor, "WMV")
}

function UploadFile(editor, extension)
{
  var imageFileName = null;
  var imageFileTitle = null;

  function postback(doc,popup_iframe)
  {
    if(doc != null) // Upload clicked
    {
      if(doc.getElementById("path").value != "")
      {
        var frm = doc.getElementById("fraExecute");

        function onLoad()
        {
          setTimeout(function()
                    {
                     if(frm.detachEvent)
                        frm.detachEvent("onload", onLoad);
                     else
                     if(frm.removeEventListener)
                        frm.removeEventListener("load", onLoad, true);

                     imageFileName = frm.contentWindow.imageFileName;
                     imageFileTitle = frm.contentWindow.imageFileTitle;

                     if(imageFileName == null)
                        alert(frm.contentWindow.imageSaved);
                        
                     popup_iframe.contentWindow.document.getElementById("cancel").onclick();  // emulate Cancel pressed
                    },0);
        }

        if(frm.attachEvent)
           frm.attachEvent("onload", onLoad);
        else
        if(frm.addEventListener)
           frm.addEventListener("load", onLoad, true);

        try{doc.getElementById("frmFile").submit();}
        catch(e)
        {
          alert(e.message);

          if(frm.detachEvent)
             frm.detachEvent("onload", onLoad);
          else
          if(frm.removeEventListener)
             frm.removeEventListener("load", onLoad, true);

         popup_iframe.contentWindow.document.getElementById("cancel").onclick();
        }
        return false;
      }
    }
    else  // Cancel clicked or emulated
    {
      if(imageFileName != null)
      {
        //editor.focusEditor();
        //editor .InsertHTML("<img src=\""+imageFileName+"\" alt=\""+imageFileTitle+"\" title=\""+imageFileTitle+"\" />");
      }
    }
  }
  
  function init(doc,popup_iframe)
  {
    if(doc != null)
    {
        doc.getElementById("title").value = "";
        if(document.all)
        {
          popup_iframe.style.display = "none";
          doc.getElementById("path").click();
          popup_iframe.style.display = "";
          if(doc.getElementById("path").value == "")
            popup_iframe.contentWindow.document.getElementById("cancel").onclick();
        }
    }
  }

  oboutGetEditor("editor").customPopup("popup_image_upload", "upload-" + extension,"./resources/obout/editor/ed_custom/_UploadFile.aspx?f=" + extension, postback, init, false, false);
}  

function addParagraph(editor)
{
 oboutGetEditor('editor').SurroundHTML("<p>","</p>");
}

function enableMenuItem(){ return true; }

function doNothing(){}
