// HEADER START
// This file is part of the wdss system.
// Author: Eduard Gode
// Copyright by the author
// Copying, distribution, inclusion (execution) without explicit permission is denied.
// Permissions granted to <wdss> websites of customers of:
//    Weitblick Internetwerkstatt, Erhard Klein or
//    Weitblick <wdss> Services, Eduard Gode
//
// Coding conventions:
// * All functions, methods and data has to be coded within the global object WDSS
// * All functions, methods and data named with a stating '_' is internal and may be change in future revisions 
if(null==window.WDSS)window.WDSS={module:{},requireModule:function(m){if(!this.module[m]){this.module[m]=1;document.write('<script type="text/javascript" src="/js-pool/wdss/'+m+'.js"></script>');}}};
// HEADER END

WDSS.requireModule('popup');

WDSS.module.editor = '$Revision: 1.39 $';


WDSS._createCallbacks = function ( frame, form, field, argsIn, values ) { // intern function
  var callback;
  if ( null != argsIn && null != argsIn.callback ) {
    callback = argsIn.callback;
  } else {
    var multiple = false;
    var getField;
    var getFields;
    if ( null == frame ) frame = window;
    if ( null != form && typeof(form) == 'string' ) form = frame.document.forms[form];
    if ( null == field ) {
    } else if ( 'string' == typeof(field) ) {
      var _field = form[field];
      if ( null == form ) form = _field.form;
      getField = function () { return _field; };
    } else if ( null != field.length ) {
      multiple = true;
      if ( null == form ) {
        for ( var i = 0; null == form && i < field.length; i++ ) {
          form = field[i].form;
        }
      }

      var _fields = [];
      var _form = form;
      for ( var i = 0; i < field.length; i++ ) {
        var _field = field[i];
        if ( 'string' == typeof(_field) ) _field = _form[_field];
        _form = _field.form;
        _fields.push(_field);
      }
      getFields = function () { return _fields; };
    } else if ( null != field.form ) {
      form = field.form;
      getField  = function () { return field; };
    }
    getForm = function () { return form; };

    var getValues;
    if ( values != null ) {
      getValues = function () { return values; };
    } else if ( multiple ) {
      getValues = function () { var _fields = getFields();
				var _values = [];
			        for ( var i = 0; i < _fields.length; i++ ) {
                                  var _field = _fields[i];
                                  _values.push( ( null == _field ) ? null : _field.value );
				}
				return _values;
			      };
    } else {
      getValues = function () { var _field = getField(); return _field.value; };
    }

    var setValues;
    if ( null == field ) {
    } else if ( multiple ) {
      setValues = function (arg) { var _fields = getFields();
			           for ( var i = 0; i < _fields.length; i++ ) {
                                     var _field = _fields[i];
                                     if ( null != _field ) {
				       _field.value = arg[i];
                                       if ( _field.onchange ) _field.onchange();
                                     }
				   }
                                   return true;
			         };
    } else {
      setValues = function (arg) { var _field = getField();
                                   if ( null == arg ) return;
                                   _field.value = arg;
                                   if ( _field.onchange ) _field.onchange();
                                   return true;
                                 };
    }
    callback = { multiple: multiple };
    if ( null != getValues ) callback.getValues = getValues;
    if ( null != setValues ) callback.setValues = setValues;
    if ( null != getFields ) callback.getFields = getFields;
    if ( null != getField  ) callback.getField  = getField;
    if ( null != getForm   ) callback.getForm   = getForm;
  } 
  return callback;
};


WDSS._addArguments = function ( dest, source, argnames ) {
  if ( !argnames ) return;
  for ( var i = 0; i < argnames.length; i++ ) {
    var name = argnames[i];
    if ( null != source[name] ) dest[name] = source[name];
  }
};


/* embedded dialogs */

WDSS._editorEmbedded = { id: 0, data: {} };


WDSS._openEmbeddedDialogs = function () {
  for ( var id in WDSS._editorEmbedded.data ) {
    var node  = document.getElementById(id);
    if ( null == node ) return;
    var data = WDSS._editorEmbedded.data[id]; WDSS._editorEmbedded.data[id] = null;
    if ( null != data ) {
      var frame = document.createElement('iframe');
      frame.setAttribute('width', data.width );
      frame.setAttribute('height',data.height);
      frame.setAttribute('border',data.border);
      node.parentNode.replaceChild(frame,node);
      frame.setAttribute('src',data.src);
      frame.setAttribute('id',id);
    }
  }
  window.clearInterval(WDSS._editorEmbedded.handler);
  WDSS._editorEmbedded.handler = null;
};


WDSS._isEmbedded = function ( argsIn ) { return null != argsIn && ( 'string' == typeof(argsIn.embed) || argsIn.embed ); };


WDSS._openDialogOrEmbed = function ( win, url, name, args, info ) {
  if ( WDSS._isEmbedded( args ) ) {
    var id = 'string' == typeof(args.embed) ? args.embed : 'wdss_embed_'+(WDSS._editorEmbedded.id++);
    name = WDSS.setDialogCallInfo( window, null, info );
    url += '?name=' + escape(name);
    if ( null == WDSS._editorEmbedded.handler ) WDSS._editorEmbedded.handler = window.setInterval('WDSS._openEmbeddedDialogs()',300);
    WDSS._editorEmbedded.data[id] = { width: args.width, height: args.height, border: args.border, src: url };
    document.write('<span id="'+id+'">&nbsp;</span>');
  } else {
    WDSS.openDialog( window, url, name, args, info ); 
  }
};


// functions called by special links in the form

function wdss_openEditor( form, field, argsIn, info, url, target ) { return WDSS.openEditor( form, field, argsIn, info, url, target ); }
WDSS.openEditor = function ( form, field, argsIn, info, url, target ) {
  var args = wdss_mergeAttributes( argsIn, { width: 800, height: 640, scrollbars: false, resizable: true } );
  if ( !info ) info = {};
  info.callback = _createCallbacks(window,form,field,null);
  WDSS.openDialog( window, url, target, args, info );
};


function wdss_designEditor( form, field, code, argsIn ) { return WDSS.designEditor( form, field, code, argsIn ); }
WDSS.designEditor = function ( form, field, code, argsIn ) {
  var editor = (argsIn != null && argsIn.editor != null) ? argsIn.editor.toLowerCase() : 'wdss';
  var url    = '/js-pool/wdss/editor/editor.design.'+editor+'.html';
  var args;
  var info;
  if ( WDSS._isEmbedded( argsIn ) ) {
    args = wdss_mergeAttributes( argsIn, { width: 300, height: 200, border: 1 } );
    info = { callback: WDSS._createCallbacks(window,form,field,argsIn), code: code };   
  } else {
    args = wdss_mergeAttributes( argsIn, { width: 800, height: 640, scrollbars: false, resizable: true } );
    info = { callback: WDSS._createCallbacks(window,form,field,argsIn), code: code };
  }
  if ( argsIn ) WDSS._addArguments(info,argsIn,['noFontAttrs','noLink','noImage','getImgUrl','getLnkUrl','helpUrl','baseHref','customStyles','style','tags','experimental','designMode','embed']);
  WDSS._openDialogOrEmbed( window, url, 'designEditor', args, info );
};


function wdss_codeEditor( form, field, lineNrPrefix ) { return WDSS.codeEditor( form, field, lineNrPrefix ); }
WDSS.codeEditor = function ( form, field, lineNrPrefix ) {
  var args = { width: 800, height: 640, scrollbars: false, resizable: true };
  var info = { callback: WDSS._createCallbacks(window,form,field,null), lineNrPrefix: lineNrPrefix }
  WDSS.openDialog( window, '/js-pool/wdss/editor/editor.code.html', 'codeEditor', args, info );
};


function wdss_urlEditor( form, field, argsIn ) { WDSS.urlEditor( form, field, argsIn ); }
WDSS.urlEditor = function ( form, field, argsIn ) {
  var args = WDSS.combineHash( { width: 800, height: 640, scrollbars: false, resizable: true }, argsIn );
  var info = { callback: WDSS._createCallbacks(window,form,field,argsIn) }
  WDSS._addArguments(info,argsIn,['nonLocal','local']);
  WDSS.openDialog( window, '/js-pool/wdss/editor/editor.url.html', 'urlEditor', args, info );
};


function wdss_htmlEditor( form, field, argsIn ) { WDSS.htmlEditor( form, field, argsIn ); }
WDSS.htmlEditor = function ( form, field, argsIn ) {
  var args = WDSS.combineHash( { width: 800, height: 640, scrollbars: false, resizable: true, reusePopup: null }, argsIn );
  var info = { callback: WDSS._createCallbacks(window,form,field,argsIn) }
  WDSS._addArguments(info,argsIn,['lineNrPrefix']);
  WDSS.openDialog( window, '/js-pool/wdss/editor/editor.html.html', 'htmlEditor', args, info );
};


function wdss_textileEditor( form, field, argsIn ) { WDSS.textileEditor( form, field, argsIn ); }
WDSS.textileEditor = function ( form, field, argsIn ) {
  var args = WDSS.combineHash( { width: 800, height: 640, scrollbars: false, resizable: true, reusePopup: null }, argsIn );
  var info = { callback: WDSS._createCallbacks(window,form,field,argsIn) }
  WDSS.openDialog( window, '/js-pool/wdss/editor/editor.textile.html', 'textileEditor', args, info );
};


function wdss_colorEditor( form, field, argsIn ) { WDSS.colorEditor( form, field, argsIn ); }
WDSS.colorEditor = function ( form, field, argsIn ) {
  var args = WDSS.combineHash( { width: 400, height: 360, scrollbars: false, resizable: true, reusePopup: true }, argsIn );
  var info = { callback: WDSS._createCallbacks(window,form,field,argsIn) }
  if ( argsIn ) WDSS._addArguments(info,argsIn,['showRGB','noHash']);
  WDSS.openDialog( window, '/js-pool/wdss/editor/editor.color.html', 'colorEditor', args, info );
};


function wdss_dateEditor( form, field, argsIn ) { WDSS.dateEditor( form, field, argsIn ); }
WDSS.dateEditor = function ( form, field, argsIn ) {
  var height = 132;
  var url = '/js-pool/wdss/editor/editor.date.html';
  if ( null != argsIn && argsIn.tfield ) {
    if ( 'string' == typeof( field ) || null == field.lenght ) field = [ field, null ];
    if ( null == field[1] ) field[1] = argsIn.tfield;
  }
  var args;
  if ( WDSS._isEmbedded( argsIn ) ) {
    height = 126;
    if ( null != argsIn.tfield ) height += 22;
    args = WDSS.combineHash( { width: 215, height: height, border: 1 }, argsIn );
  } else {
    args = WDSS.combineHash( { width: 215, height: height, scrollbars: false, resizable: true, reusePopup: true }, argsIn );
  }
  var info = { callback: WDSS._createCallbacks(window,form,field,argsIn) };

  if ( argsIn ) WDSS._addArguments(info,argsIn,['showOtherDays','showWeek','firstWDay','deFormat','tfield','embed']);

  WDSS._openDialogOrEmbed( window, url, 'dateEditor', args, info );
};


function wdss_geoposEditor( form, field, values ) { WDSS.geoposEditor( form, field, values ); }
WDSS.geoposEditor = function ( form, field, values ) {
  var args = { width: 705, height: 530, scrollbars: false, resizable: true };
  var info = { callback: WDSS._createCallbacks(window,form,field,null,values) };
  if ( null == values ) values = info.callback.getValues();
  WDSS.openDialog( window, '/js-pool/wdss/editor/editor.geopos.html?geo='+escape(values), 'geoposEditor', args, info );
};


WDSS._int_parseAttrString = function ( str ) {
  var attrs = {};
  if ( null != str ) {
    if ( 'string' == typeof(str) ) {
      var attrList = str.split(/\s*,\s*/);
      for ( var i = 0; i < attrList.length; i++ ) {
        if ( attrList[i].match(/^(\w+)=(.*)$/) ) {
          var name = RegExp.$1;
          var val  = RegExp.$2;
          if      ( val.match(/^\d+$/) ) val = parseInt(val);
          else if ( val.match(/yes/,i) ) val = true;
          else if ( val.match(/no/,i)  ) val = false;
          attrs[name] = val;
        } else {
          alert("illegal attribute '"+attrList[i]+"' in '"+str+"' (editor.js)");
        }
      }
    } else {
      attrs = str;
    }
  }
  return attrs;
};


WDSS._int_getSingleField = function ( frame, form, field ) {
  if ( 'string' != typeof(field) ) return field;
  if ( 'string' == typeof(form) ) form = frame.document.forms[form];
  return form[field];
};


function wdss_configuredExtension( form, field, arg ) { WDSS.configuredExtension( form, field, arg ); }
WDSS.configuredExtension = function ( form, field, arg ) {
  field = WDSS._int_getSingleField ( window, form, field );
  var args = { field: field, arg: arg };
  var cgi = arg.CGI; if (null == cgi) cgi = {};
  var params = [ '_field='+encodeURIComponent(field.name), 'ref='+arg.ref ];
  for ( var p in cgi ) {
    params.push( encodeURIComponent(p)+'='+encodeURIComponent(cgi[p]) ); 
  }
  var url = '/admin/'+arg.CONFIG_KEY+'/extension.popup/simple.html?'+params.join('&');
  if ( !arg.CONFIG_KEY ) url = '/admin/editor.indiv/simple.html';
  if ( !arg.target ) arg.target = "wdssPopup";
  var geo = wdss_mergeAttributes( WDSS._int_parseAttrString( arg.geo ), { width: 600, height: 400 } );
  wdss_openPopup( window, url, arg.target, geo, args );
};


function wdss_anchorEditor( form, field, argsIn ) { WDSS.anchorEditor( form, field, argsIn ); }
WDSS.anchorEditor = function ( form, field, argsIn ) {
  field = WDSS._int_getSingleField ( window, form, field );
  var upwinname = 'upload.'+field.name;
  var info = { callback: _createCallbacks(window,form,field,argsIn), arg: argsIn };
  info.upload = window[upwinname];
  var cgi  = argsIn.CGI; if (null == cgi) cgi = {};
  var params = [ '_field='+encodeURIComponent(field.name), 'ref='+argsIn.ref ];
  for ( var p in cgi ) { params.push( encodeURIComponent(p)+'='+encodeURIComponent(cgi[p]) ); }
  var url = '/admin/'+argsIn.CONFIG_KEY+'/editor.anchor/simple.html?'+params.join('&');
  var target = (null != argsIn.target) ? argsIn.target : 'anchorPopup';
  var geo = WDSS.combineHash( { width: 520, height: 400, scrollbars: true }, WDSS._int_parseAttrString( argsIn.geo ) );
  var win = WDSS.openDialog( window, url, target, geo, info );

  var upload = window[upwinname];
  if ( null != upload ) {
    var name = win.name;
    info.callback.upload = function () {
      //popupClose(name);
      WDSS.subEntryAdmin( form, field, upload );
    };
  }
};


// callback has to return true on success
function wdss_dataImport( callback, argsIn ) { WDSS.dataImport( callback, argsIn ); }
WDSS.dataImport = function ( callback, argsIn ) {
  var args = { width: 290, height: 75, scrollbars: false, resizable: true };
  var info = { callback: { getValues: function () {}, setValues: callback }  };
  WDSS.openDialog( window, '/js-pool/wdss/editor/editor.import.html', 'dataImport', args, info );
};


function subEntryAdmin( form, field, argsIn ) { WDSS.subEntryAdmin( form, field, argsIn ); }
WDSS.subEntryAdmin = function ( form, field, argsIn ) {
  field = WDSS._int_getSingleField ( window, form, field );
  var info = { callback: WDSS._createCallbacks(window,form,field,argsIn) };

  var feature = argsIn.FEATURE;
  if ( !feature ) feature = 'file';
  var ref     = argsIn.ref;
  if ( argsIn.key ) ref += ',H'+argsIn.key;

  var url = '/dynamic/secure/admin/entry.phtml?_frame=content&_feature='+feature+'&_field='+escape(field.name)+'&ref='+ref;
  if ( argsIn.CODE        ) url += '&_code='+escape(argsIn.CODE);
  if ( argsIn.urlParamStr ) url += argsIn.urlParamStr;

  var geo = WDSS.combineHash( { width: 700, height: 540 }, WDSS._int_parseAttrString( argsIn.geo ) );
  WDSS.openDialog( window, url, 'subEntry', geo, info );
};

