// author: tilo hauke
// rights: eduard gode <eduard.gode@wdss.de>
// all rights reserved
//


function _wdss_layerwin_get_browser () {
  var items_= window.navigator.userAgent.toLowerCase().replace(/.*(mozilla|msie|opera|firefox|safari|konqueror|netscape)[^1-9]*([.0-9]+).*/g,'$1.$2').split('.');
  return items_[0]+items_[1];
}

function wdssLayerWin ( opener, my_id, my_src, attrs ) {
  var doc        = opener.document;
  var width      = attrs.width;  if ( null == width  ) width  = 400;
  var height     = attrs.height; if ( null == height ) height = 400;
  var top        = attrs.top;
  var left       = attrs.left;
  var title      = attrs.title;  if ( null == title  ) title  = my_src;
  var statustext = attrs.statustext;
  if  ( null == statustext && attrs.status ) statustext = ' ';

  if ( null == my_id || '_blank' == my_id || 0 == my_id.length )
    my_id = 'layerwin_'+wdssLayerWin.blank++;

  this.id = my_id;
  this._init();

  // insert templates copy into parent (document body)
  { var template = attrs.template;
    if ( null == template ) template = 'layerwin_template';
    if ( 'string' == typeof(template) ) template = doc.getElementById( template );
    this.node = template.cloneNode( true );
  }

  var browser  = _wdss_layerwin_get_browser();
  if ( browser.match( /msie[567]/ ) ) {
//  if ( _wdss_layerwin_get_browser().match( /msie[567]/ ) ) {
    // msie does not allow to set the window.name of the iframe contentWindow
    // the only way to set the window. name ist to create the iframe tag with innerHTML and name="myWindownameToSet" 
    var td = this.get_element( 'layerwin_td' );
    if ( null != td ) {
      td.innerHTML = '<iframe name="'+this.id+'" src="about:blank" frameborder="0" width="100%"; height="100%" marginheight="5" marginwidth="5"></iframe>';
      this.iframe = td.firstChild;
    }
  }
  if ( null == this.iframe ) {
    this.iframe = this.get_element( 'layerwin_iframe' );
    this.iframe.name = this.id;
    this.iframe.setAttribute( 'name', this.id );
  }

  //this.node.style.display = 'none';

  if ( browser.match( /msie[5678]/ ) ) {
    doc.body.insertBefore( this.node, doc.body.firstChild );
  } else {
    doc.body.appendChild( this.node );
  }
  this.node.setAttribute( 'id', this.id );

  // set content window name to post this info to the content window
  this._getWin().name = this.id;

  // to handle this as a node or window;
  this.style  = this.node.style;
  this.opener = opener;

  
  var box_title    = this.get_element( 'layerwin_title' );
  var close_button = this.get_element( 'layerwin_close_button' );

  if ( null == top  ) top  = doc.body.scrollTop+parseInt(this.style.top.replace(/px$/,''));
  if ( null == left ) left = doc.body.offsetWidth/2 - width/2 - 1;
  if ( top  < 0 ) top  = 0;
  if ( left < 0 ) left = 0;
  this.moveTo( left, top );

  wdssLayerWin.winmap[this.id] = this;
  var win = this;

  box_title.onmousedown = function ( myevent ) { win._moveStart(myevent);                   }
  box_title.onclick     = function ( myevent ) { wdssLayerWin.prototype.win_to_move = null; }

  this.setTitle(title);
  this.setStatus(statustext);

  close_button.onclick = function () { win.close(); };

  this.resizeTo( width, height );
}

wdssLayerWin.blank         = 0;
wdssLayerWin.winmap        = {};
wdssLayerWin.win_top_level = 10000;


wdssLayerWin.get_subelement = function ( box, name ) {
  if ( box.hasChildNodes() ) {
    for ( var c = 0; c < box.childNodes.length; c++ ) {
      var mybox = box.childNodes[c];
      if ( mybox.attributes && name == mybox.getAttribute('name') ) return mybox;
      if ( mybox.hasChildNodes() ) { 
        var child = wdssLayerWin.get_subelement( mybox, name);
        if ( null != child ) return child;
      }
    }
  }
  return null;
};


wdssLayerWin.prototype.get_element = function ( name ) { return wdssLayerWin.get_subelement( this.node, name ); };

wdssLayerWin.prototype._moveStart = function (myevent) {
  wdssLayerWin.prototype.win_to_move = this;
  this._toTop();
  if ( this.iframe ) this.iframe.style.display = 'none';
};

wdssLayerWin.prototype._moveEvent = function ( myevent ) {
  if ( this.iframe ) this.iframe.style.display = 'none';
  var coordinates = this.mousexy( myevent );
  if ( coordinates[0] > 3 && coordinates[1] > 3 ) {
    var global = wdssLayerWin.prototype;
    new_x = global.win_posxy['0']-coordinates[0];
    new_y = global.win_posxy['1']-coordinates[1];
    this.moveTo( this.node.offsetLeft-new_x, this.node.offsetTop-new_y );
  }
  wdssLayerWin.prototype.win_posxy = coordinates;
};

wdssLayerWin.prototype._moveEnd = function ( myevent ) {
  if ( this.iframe ) this.iframe.style.display = 'block';
  wdssLayerWin.prototype.win_to_move = null;
};


wdssLayerWin.prototype.mousexy = function (event_param) {
  my_event = event_param || window.event;
  if ( !my_event ) return [ 0, 0 ];
  if ( document.layers   )            { return [ my_event.pageX,   my_event.pageY   ]; }
  else if ( window.opera )            { return [ my_event.clientX, my_event.clientY ]; }
  else if ( document.all )            { return [ my_event.clientX, my_event.clientY ]; }
  else if ( document.getElementById ) { return [ my_event.pageX,   my_event.pageY   ]; }
}


wdssLayerWin.prototype.onmousedown = function ( myevent ) {
  wdssLayerWin.prototype.win_posxy = wdssLayerWin.prototype.mousexy ( myevent );
};
wdssLayerWin.prototype.onmouseup   = function ( myevent ) {
  if ( wdssLayerWin.prototype.win_to_move ) { wdssLayerWin.prototype.win_to_move._moveEnd( myevent ); }
};
wdssLayerWin.prototype.onmousemove = function ( myevent ) {
  if ( wdssLayerWin.prototype.win_to_move ) wdssLayerWin.prototype.win_to_move._moveEvent( myevent );
};


wdssLayerWin.prototype._init = function () {
  document.body.onmousedown = wdssLayerWin.prototype.onmousedown;
  document.body.onmouseup   = wdssLayerWin.prototype.onmouseup;
  document.body.onmousemove = wdssLayerWin.prototype.onmousemove;
}


wdssLayerWin._lookup = function (id) { return wdssLayerWin.winmap[id]; }
wdssLayerWin.prototype._toTop  = function ()   { this.style.zIndex = wdssLayerWin.win_top_level++; }
wdssLayerWin.prototype._getWin = function ()   { var frame = this.iframe; return frame.contentWindow ? frame.contentWindow : frame; }


wdssLayerWin._lookupWin = function (frameWin) {
  frameWin._wdssLayerWinFind = true;
  for ( var name in wdssLayerWin.winmap ) {
    var win = wdssLayerWin.winmap[name];
    if ( null != win ) {
      var w = win._getWin();
      if ( w._wdssLayerWinFind ) {
        w._wdssLayerWinFind = null;
        return win;
      }
    }
  }
  frameWin._wdssLayerWinFind = null;
  return null;
}


wdssLayerWin.prototype._parseAttrString = function (str) {
  var attrs = {};
  if ( null != str ) {
    if ( 'string' == typeof(str) ) {
      var attrList = str.split(/\s*,\s*/);
      var pat = /^(\w+)=(.*)$/;
      for ( var i = 0; i < attrList.length; i++ ) {
        var res;
        if ( (res = pat.exec(attrList[i]) ) ) {
          var name = res[1];
          var val  = res[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+"' (layerwin.js)");
        }
      }
    } else {
      attrs = str;
    }
  }
  return attrs;
}


wdssLayerWin.prototype._setInnerSize = function (width,height) {
  var lwin_td    = this.get_element( 'layerwin_td' );
  var lwin_table = this.get_element( 'layerwin_table' );

  if ( null != width ) {
    lwin_table.setAttribute( 'width', width );
    lwin_td.style.width = ''+width+'px';
  }
  if ( null != height ) {
    if ( false && navigator.userAgent.match( /MSIE 8\.0/ ) ) {
      this.iframe.setAttribute( 'height', height );
    } else {
      lwin_td.setAttribute( 'height', height );
    }
  }
}


// -------- public methods --------------------


wdssLayerWin.prototype.focus   = function ()        { this._getWin().focus();         }
wdssLayerWin.prototype.alert   = function (txt)     { this._getWin().alert(txt);      }
wdssLayerWin.prototype.confirm = function (txt)     { return this._getWin().confirm(txt);    }
wdssLayerWin.prototype.prompt  = function (txt,def) { return this._getWin().prompt(txt,def); }


wdssLayerWin.prototype.moveTo  = function (x,y) {
  var unit = 'px';
  this.style.left = x+unit;
  this.style.top  = y+unit;
}


wdssLayerWin.prototype.resizeTo = function (width,height) { this._setInnerSize(width,height); }

 
wdssLayerWin.prototype.setTitle = function (my_title) {
  var box_title = this.get_element( 'layerwin_title' );
  if ( null == my_title || my_title.match(/^\s*$/) ) {
    box_title.innerHTML = '&nbsp;';
  } else {
    box_title.firstChild.data = my_title;
  }
}


wdssLayerWin.prototype.setStatus = function (my_status) {
  var box_status = this.get_element( 'layerwin_status' );
  if ( null != my_status && my_status.length ) {
    if ( my_status.match(/^\s*$/) ) {
      box_status.innerHTML = '&nbsp;';
    } else {
      box_status.firstChild.data = my_status;
    }
    box_status.style.display   = 'block';
  } else {
    box_status.style.display   = 'none';
  }
}


wdssLayerWin.prototype.setBorderColors = function (b1t,b1r,b1b,b1l,b2t,b2r,b2b,b2l) {
  var frame1 = this.get_element( 'layerwin_frame1' );
  var frame2 = this.get_element( 'layerwin_frame2' );
  if ( null != frame1 ) {
    frame1.style.borderTopColor    = b1t;
    frame1.style.borderRightColor  = b1r;
    frame1.style.borderBottomColor = b1b;
    frame1.style.borderLeftColor   = b1l;
  }
  if ( null != frame2 ) {
    frame2.style.borderTopColor    = b2t;
    frame2.style.borderRightColor  = b2r;
    frame2.style.borderBottomColor = b2b;
    frame2.style.borderLeftColor   = b2l;
  }
}


wdssLayerWin.prototype.getTitleStyle = function () {
  var title  = this.get_element( 'layerwin_title' );
  return ( null == title ) ? null : title.style;   
}
wdssLayerWin.prototype.setTitleColors = function (titleBarColor,titleTextColor,closerBackgroundColor,closerTextColor) {
  var title  = this.get_element( 'layerwin_title' );
  var closer = this.get_element( 'layerwin_close_button' );
  if ( null != title ) {
    title.style.backgroundColor  = titleBarColor;
    title.style.color            = titleTextColor;
  }
  if ( null != closer ) {
    closer.style.backgroundColor = closerBackgroundColor;
    closer.style.color           = closerTextColor;
  }
}


wdssLayerWin.prototype.close = function () {
  if ( null != this.onclose ) {
    var ack = this.onclose(this);
    if ( 'undefined' != typeof(ack) && !ack ) return;
  }

  if ( this == wdssLayerWin.prototype.win_to_move ) wdssLayerWin.prototype.win_to_move = null;
  this.style.display = 'none';
  this.iframe.setAttribute('src','about:blank');
  document.body.removeChild(this.node);
  wdssLayerWin.winmap[this.id] = null;
  this.iframe  = null;
  this.style   = null;
  this.node    = null;
  this.onclose = null;
};


// ----------------------------------------------


function _layerwin_open( parent, name, my_src, attrs ) {
  var win;
  if ( null != name ) win = wdssLayerWin._lookup( name );
  if ( null == win )  win = new wdssLayerWin( parent, name, my_src, attrs );

  if ( null != my_src ) win.iframe.setAttribute('src',my_src);
  else if ( null != attrs.content ) {
    var doc = win._getWin().document;
    doc.open();
    doc.write( attrs.content );
    doc.close();
  }
  win.style.display = 'block'; 
  win._toTop();

  return win;
}


function layerwin_open( name, my_src, my_width, my_height, my_title, my_statustext ) {
  var attrs = { width: my_width, height: my_height, title: my_title, statustext: my_statustext };
  if ( 'object' == typeof(my_width) ) { attrs = my_width; }
  return _layerwin_open( window, name, my_src, attrs );
}


function layerwin_close ( name ) {
  var win = wdssLayerWin._lookup( name );
  if ( win ) win.close();
}


function layerwin_of_framewin ( win ) {
  return wdssLayerWin._lookupWin( win );
}


function LW_getLayerwinByName( name ) {
  return wdssLayerWin._lookup( name );
}


function LW_open( parent, my_src, name, my_attrs, my_title, my_statustext ) {
  var attrs  = wdssLayerWin.prototype._parseAttrString(my_attrs);
  if  ( null != my_title      ) attrs.title      = my_title;
  if  ( null != my_statustext ) attrs.statustext = my_statustext;
  return _layerwin_open( parent, name, my_src, attrs );
}

var _layerwin_defaultDefinition_style = ''
+'  .layerwin_table { background:#ddd; }\n'
+'  .layerwin_title {\n'
+'         font-size:80%;\n'
+'         font-weight:bold;\n'
+'         background:#5C85CC;\n'
+'         width:100%;\n'
+'         border:0;\n'
+'         color:#fff;\n'
+'         text-align:center;\n'
+'         padding:3px;\n'
+'  }\n'
+'  .layerwin_close_button {\n'
+'        font-size:80%;\n'
+'        font-weight:bold;\n'
+'        font-family: arial, helvetica;\n'
+'        display:block;\n'
+'        background:#ddd;\n'
+'        color:#000;\n'
+'        text-decoration: none;\n'
+'        text-align:center;\n'
+'        padding:4px 4px 5px 4px;\n'
+'  }\n'
+'  .layerwin_status{\n'
+'         text-align:left;\n'
+'         font-size:70%;\n'
+'         font-family: arial, helvetica;\n'
+'         color:#000;\n'
+'         padding:3px;\n'
+'         text-align:center;\n'
+'  }\n'
+'  .layerwin_design_element_frame_one {\n'
+'         border:solid 1px #000;\n'
+'         border-top-color:#ddd;\n'
+'         border-right-color:#000;\n'
+'         border-bottom-color:#000;\n'
+'         border-left-color:#ddd;\n'
+'  }\n'
+'  .layerwin_design_element_frame_two {\n'
+'         border:solid 2px #000;\n'
+'         border-top-color:#fff;\n'
+'         border-right-color:#999;\n'
+'         border-bottom-color:#999;\n'
+'         border-left-color:#fff;\n'
+'  }\n';

var _layerwin_defaultDefinition_template = ''
+'<div style="position:absolute;top:30px;left:0px;display:none" id="layerwin_template">\n'
+'  <div name="layerwin_frame1" class="layerwin_design_element_frame_one">\n'
+'    <div name="layerwin_frame2" class="layerwin_design_element_frame_two">\n'
+'      <table name="layerwin_table" class="layerwin_table" width="300"  border="0" cellpadding="0" cellspacing="0" >\n'
+'        <tr>\n'
+'          <td width="2000" style="overflow:hidden"><button title="Fenster verschieben" name="layerwin_title" class="layerwin_title" />no title</button></td>\n'
+'          <td class="layerwin_close" width="20"><a title="Fenster schlie&szlig;en" name="layerwin_close_button" class="layerwin_close_button">X</a></td>\n'
+'        </tr><tr>\n'
+'          <td colspan="2" name="layerwin_td"><iframe name="layerwin_iframe" frameborder="0" width="100%"; height="100%" marginheight="5" marginwidth="5" src="about:blank"></iframe></td>\n'
+'        </tr><tr>\n'
+'          <td class="layerwin_status" name="layerwin_status">no status</td><td></td>\n'
+'        </tr>\n'
+'      </table>\n'
+'    </div>\n'
+'  </div>\n'
+'</div>\n';

var _layerwin_navDefinition_style = ''
+'.nav_layerwin_table {\n'
+'   background:#ddd;\n'
+' }\n'
+' .nav_layerwin_title {\n'
+'   font-family: arial, helvetica;\n'
+'   font-size:8pt;\n'
+'   font-weight:bold;\n'
+'   color:#fff;\n'
+'   text-align:center;\n'
+' }\n'
+' .nav_layerwin_frame1 {\n'
+'   border: solid 1px #000;\n'
+'   border-color: #ddd #000 #000 #ddd;\n'
+' }\n'
+' .nav_layerwin_frame2 {\n'
+'   border:  solid 2px #000;\n'
+'   border-color:  #fff #999 #999 #fff;\n'
+' }\n'
+'.nav_layerwin_status {\n'
+'   font-family: arial, helvetica;\n'
+'   text-align:left;\n'
+'   font-size:70%;\n'
+'   color:#000;\n'
+'   padding:3px;\n'
+'   text-align:center;\n'
+'}\n';

var _layerwin_navDefinition_template = ''
+'<div style="position:absolute;top:150px;left:100px;z-index:100;display:none" id="layerwin_template">\n'
+'  <div name="layerwin_frame1" class="nav_layerwin_frame1">\n'
+'    <div name="layerwin_frame2" class="nav_layerwin_frame2">\n'
+'      <table class="nav_layerwin_table" name="layerwin_table" border="0" cellpadding="0" cellspacing="0" width="1">\n'
+'        <tr valign="middle">\n'
+'          <td bgcolor="#5C85CC" height="20" style="overflow:hidden; padding-right: 2px;">\n'
+'            <div style="float: right;"><a title="Fenster schliessen" class="nav_layerwin_close" onclick="document.getElementById(\'navtracks_layerwin1\').style.display = \'none\'"><img src="/js-pool/wdss/layerwin.close.gif" name="layerwin_close_button" alt="Fenster schlie&szlig;en" border="0" width="16" height="14" /></a></div>\n'
+'            <div class="nav_layerwin_title" name="layerwin_title">Testh&ouml;rer gesucht</div>\n'
+'          </td>\n'
+'        </tr>\n'
+'        <tr>\n'
+'          <td name="layerwin_td"><iframe name="layerwin_iframe" frameborder="0" width="100%"; height="100%" marginheight="0" marginwidth="0" src="about:blank"></iframe></td>\n'
+'        </tr><tr>\n'
+'          <td class="nav_layerwin_status" name="layerwin_status">no status</td>\n'
+'        </tr>\n'
+'      </table>\n'
+'    </div>\n'
+'  </div>\n'
+'</div>\n';

var _layerwin_Definition = { 
  _default: {  styleDefinitionString:    _layerwin_defaultDefinition_style,
	       templateDefinitionString: _layerwin_defaultDefinition_template
            },
  _nav:     {  styleDefinitionString:    _layerwin_navDefinition_style,
	       templateDefinitionString: _layerwin_navDefinition_template
            }
};

function _layerwin_getDefinition (name) {
  var data = _layerwin_Definition['_'+name];
  if ( null == data ) data = _layerwin_Definition._default;
  return data;
}

function LW_getDefaultLayerwinDefinition (name) {
  return _layerwin_getDefinition(name);
}

function LW_documentWriteDefaultDefinitions (name) {
  var def = LW_getDefaultLayerwinDefinition(name);
  document.write('<style>'+def.styleDefinitionString+'</style>');
  document.write(def.templateDefinitionString);
}
