/* Functions for the advlink plugin popup */

tinyMCEPopup.requireLangPack();

function preinit() {
  var url;
  if (url = tinyMCEPopup.getParam("external_link_list_url"))
    document.write('<script language="javascript" type="text/javascript" src="' + tinyMCEPopup.editor.documentBaseURI.toAbsolute(url) + '"></script>');
}

function init() {
  var formObj = document.forms[0];
  var ed = tinyMCEPopup.editor;
  var node = ed.selection.getNode()
  var sel = ed.selection.getContent();
  var pnode = ed.dom.getParent(node, function(n) {return ed.dom.hasClass(n, "snInfoBox");});

  if(pnode){
      formObj.insert.value = tinyMCEPopup.getLang('update', 'Insert', true);
      var _pwv = parseInt(pnode.style.width);
      formObj.size.value = isNaN(_pwv) ? 100: _pwv;
      var f = pnode.style.cssText.indexOf("float: left") != -1 ? "left":"right"; // workroung becouse pnode.style.float return undefined
      var o = formObj.float.options;
      for(var i = 0; i < o.length;i++){
        if(o[i].value == f){
          o.selectedIndex = i;
          break;
        }
      }
  }
 }

function insertAction() {
  var formObj = document.forms[0];
  var ed = tinyMCEPopup.editor;
  var node = ed.selection.getNode()
  var sel = ed.selection.getContent();
  var pnode = ed.dom.getParent(node, function(n) {return ed.dom.hasClass(n, "snInfoBox");});
  
  var s = parseInt(formObj.size.value);
  var f = formObj.float.value;
  var args = {style: "width:"+s+"px; float:"+f+";", class:"snInfoBox"};

  if(!pnode && sel.length == 0){ sel = tinyMCEPopup.getLang('snstuff_dlg.ibox_defaulttext', 'Text....', true);}

  tinyMCEPopup.execCommand("mceBeginUndoLevel");
  if(sel.length > 0){
    ed.execCommand('mceInsertRawHTML', false, '<span id="__snInfoBoxTmp" >'+sel+'</span>', {skip_undo : 1});
    ed.dom.setAttribs('__snInfoBoxTmp', args);
    ed.dom.setAttrib('__snInfoBoxTmp', 'id', '');
    ed.undoManager.add();
  }else{
    ed.dom.setAttribs(pnode, args);
  }
  tinyMCEPopup.execCommand("mceEndUndoLevel");
  ed.execCommand('mceRepaint'); 
  tinyMCEPopup.close();
}

// While loading
preinit();
tinyMCEPopup.onInit.add(init);

