/*
  util.js
  include file with helper functions for browser specific handling
  Author: TheGismo
  Copyright (c)2001-2003 by OmanBros.com. All rights reserved.
  Last update: 16.05.2003

  Contents:
	variable NO_NS4x_RELOAD
	object is
	function CheckBrowser
	function Reload_NS4_Page
	function NewImage
	function NewMouseOverImage
	function PreloadImages
	function PreloadMouseOverImages
	function SetImg
	function SetExImg
	function Popup
	function SetMenu
	function CleanUpMenu
	function GoURI
*/

/* -------------------------------------------------------------------
  variable NO_NS4x_RELOAD

  Because of a bug in Netscape 4.x a webpage sometimes does not redraw
  properly when resizing the window. In fact, the browser tries to
  reload the page, which sometimes does not work properly. To come
  around this bug a special reload procedure is built into this script
  include file. It does the same what the reload button in the menu bar
  of the browser does, but just for the specific page.

  Because it reload's the page from the server (or cache) serious
  problems may arise when resizing dynamic pages, which are created
  by scripts like PHP:
  
  1) The specific page is loaded at least twice from the server.
     Especially, when working with databases, queries are done
     twice. All this leads at least to more traffic and more server
     processing -> worse interaction.
     
  2) Additional data to be POSTed or transfered via GET from one
     document to another (or itself) is lost. Using a session
     management or ordering in a webshop may lead to unpredictable
     results.
  
  This variable determines wether a webpage in Netscape 4.x browser
  is reloaded on resizing the browser window or not. Normally, this
  is done automatically (when using this script include file). For
  some reasons, like mentioned above, it might be a good idea to
  prevent the automatic reload of webpages.
  To prevent reload the undefined NO_NS4x_RELOAD variable must be
  assigned a value <> 0 before this script include file is loaded.
  
  Example:
  <script language="JavaScript1.2">NO_NS4x_RELOAD=1</script>
  <script language="JavaScript1.2" src="util.js"></script>
*/
var NO_NS4x_RELOAD

/* -------------------------------------------------------------------
  function CheckBrowser

  Assigns and returns a CheckBrowser object to distinguish between
  different browsers and operating systems.
  The is object can be used to refer to the following properties:
  b		browser name "ns", "ie" or other name
  version	version of browser
  v		version representing the integer portion
  ns		true if browser is Netscape Navigator
  ns4		true if browser is Netscape Navigator 4.x
  ns5		true if browser is Netscape Navigater >= 5.x
  ie		true if browser is Microsoft Internet Explorer
  ie4		true if browser is Microsoft Internet Explorer 4.x
  ie5		true if browser is Microsoft Internet Explorer 5.x
  min		true if ie4 or ns4 is true (minimum configuration)
  win		true if browser runs on Microsoft Windows
  mac		true if browser runs on Apple Macintosh OS
*/
function CheckBrowser() {
  var b=navigator.appName
  if (b=="Netscape") this.b="ns"
  else if (b=="Microsoft Internet Explorer") this.b="ie"
  else this.b=b
  this.version=navigator.appVersion
  this.v=parseInt(this.version)
  this.ns=(this.b=="ns")
  this.ns4=(this.b=="ns"&&this.v==4)
  this.ns5=(this.b=="ns"&&this.v>=5)
  this.ie=(this.b=="ie")
  this.ie4=(this.version.indexOf('MSIE 4')>0)
  this.ie5=(this.version.indexOf('MSIE 5')>0)
  this.min=(this.ie4||this.ns4)
  this.win=(this.version.indexOf('Win')>0)
  this.mac=(this.version.indexOf('Mac')>0)
}
is=new CheckBrowser()

/* -------------------------------------------------------------------
  function Reload_NS4_Page

  Reloads the window if Netscape Navigator 4 is resized (due to an error
  in Netscape Navigator 4.x, which doesn't display pages properly on resize).
  init must be true on load of the webpage. On resize the innerWidth and
  innerHeight properties are checked before the page is reloaded.
*/
function Reload_NS4_Page(init) {
  if (init==true) with (navigator) {
    if (is.ns4) {
      document.stored_Width=innerWidth
      document.stored_Height=innerHeight
      onresize=Reload_NS4_Page
    }
  }
  else if (innerWidth!=document.stored_Width||innerHeight!=document.stored_Height) location.reload()
}
if (!NO_NS4x_RELOAD) Reload_NS4_Page(true)

/* -------------------------------------------------------------------
  function NewImage

  Helper function to create a new image object used for
  onMouseOver functionality. Given an imgname <xxx> the images must
  have the file name <xxx>.gif, .png or .jpg.
  If ext is 1, then a .jpg extention is assumed otherwise a .gif.
  If ext is 2, then a .png extention is assumed.
  It is stored in the document as pre_<xxx>.
  imgpath represents the (relative) path to the images to be loaded.
*/
function NewImage(imgpath,imgname,ext) {
  if (document.images) {
    eval("obj=document.pre_"+imgname+"=new Image()")
    txt=imgpath+imgname
    txt+=(ext>0)?((ext<2)?".jpg":".png"):".gif"
    obj.src=txt
  }
}

/* -------------------------------------------------------------------
  function NewMouseOverImage

  Helper function to create a pair of new image objects used for
  onMouseOver functionality. Given an imgname <xxx> the images must
  have the file name <xxx>.gif, .png or .jpg and <xxx>_o.gif, .png or .jpg.
  If ext is 1, then a .jpg extention is assumed otherwise a .gif.
  If ext is 2, then a .png extention is assumed.
  They are stored in the document as pre_<xxx> and pre_<xxx>_o.
  imgpath represents the (relative) path to the images to be loaded.
*/
function NewMouseOverImage(imgpath,imgname,ext) {
  if (document.images) {
    // normal image
    NewImage(imgpath,imgname,ext)
    // onMouseOver image
    NewImage(imgpath,imgname+"_o",ext)
  }
}

/* -------------------------------------------------------------------
  function PreloadImages

  Used to preload all images used for OnMouseOver events.
  The first argument imgpath represents the path the images are
  loaded from. The second one ext, if 1, indicates that the images
  have .jpg extentions, otherwise (0) .gif is assumed. If ext is 2 then
  .png is assumed. Further, one or more arguments might be handled over,
  which indicate the simple names of the images (without extentions and
  path). This function uses the function NewImage to load the appropriate
  files.
*/
function PreloadImages(imgpath,ext) {
var i,a=PreloadImages.arguments
  if (document.images) for (i=2;i<a.length;i++) NewImage(imgpath,a[i],ext)
}

/* -------------------------------------------------------------------
  function PreloadMouseOverImages

  Used to preload all images used for OnMouseOver events.
  The first argument imgpath represents the path the images are
  loaded from. The second one ext, if 1, indicates that the images
  have .jpg extentions, otherwise (0) .gif is assumed. If ext is 2 then
  .png is assumed. Further, one or more arguments might be handled over,
  which indicate the simple names of the images (without extentions and
  path). This function uses the function NewMouseOverImage to load the
  appropriate files.
*/
function PreloadMouseOverImages(imgpath,ext) {
var i,a=PreloadMouseOverImages.arguments
  if (document.images) for (i=2;i<a.length;i++) NewMouseOverImage(imgpath,a[i],ext)
}

/* -------------------------------------------------------------------
  function SetImg

  Used to exchange an image with another one corresponding to the over
  parameter. If over is true then the onMouseOver-image, otherwise the
  normal image will be used. Images subject to be used for exchange
  have to be name-tagged as img_<xxx>, where <xxx> is the simple name
  of the respective image. imgname represents the simple name of the
  image.
  menu_hl is a global variable, which is set when a menu was selected
  to permanently highlight the respective menu.
*/
var menu_hl = ""
function SetImg(imgname,over) {
var i,x,is
is=new CheckBrowser()
  if (document.images) {
    x=eval("document.img_"+imgname)
    if (is.ns4) for (i=0;!x&&document.layers&&i<document.layers.length;i++) x=eval("document.layers[i].document.img_"+imgname)
    if (menu_hl == imgname) {
      txt="x.src=document.pre_"+imgname+"_o.src"
    } else {
      txt="x.src=(over)?document.pre_"+imgname+"_o.src:document.pre_"+imgname+".src"
    }
    if (x) eval(txt)
  }
}

/* -------------------------------------------------------------------
  function SetExImg

  Used to exchange an image with another one corresponding to the imgid
  parameter. imgid represents the simple name of the name parameter.
  Images subject to be used for exchange
  have to be name-tagged as img_<xxx>, where <xxx> is the simple name
  of the respective image. imgname represents the simple name of the
  image.
*/
function SetExImg(imgid,imgname) {
var i,x
  if (document.images) {
    x=eval("document.img_"+imgid)
    if (is.ns4) for (i=0;!x&&document.layers&&i<document.layers.length;i++) x=eval("document.layers[i].document.img_"+imgid)
    txt="x.src=document.pre_"+imgname+".src"
    if (x) eval(txt)
  }
}

/* -------------------------------------------------------------------
  function Popup

  Hides or shows a layer. The layername represents the layer. Setting
  show to true makes it visible, otherwise the layer gets hidden.
  It is possible to show or hide a layer, which is defined in another frame.
  For this case frame is used to be set to the frame which contains
  the popup and where the layer should be shown.
*/
function Popup(layername,show,frame) {
  if (layername) {
    if (frame) frame = frame.document;
    else frame = document;
    with (frame)
      if (is.ns5) getElementById(layername).style.visibility=(show)?"visible":"hidden"
      else if (is.ns4) layers[layername].visibility=(show)?"show":"hide"
      else all[layername].style.visibility=(show)?"visible":"hidden"
  }
}

/* -------------------------------------------------------------------
  function SetMenu

  Is used to popup a sub menu, which is normally a set of vertical
  positioned images. imgname represents the simple name of the main
  menu image, menuname is the simple name of the popup menu layer (and the
  clearing layer, which is used to close the popup menu when the mouse
  pointer leaves the menu), frame is used to use a different frame where
  the popup menu resides (so it is possible to use popup menus across
  different frames).
  If menuname is kept empty, then the active menu is closed, if one is
  open.
*/
var menu_popup = ""
function SetMenu(imgname,menuname,frame) {
  if (imgname) SetImg(imgname,1)
  // make sure that we are at the top of the frame-page
  // where the menu should popup
  if (frame) frame.scrollTo(0,0);
  if (menuname) {
    Popup(eval("'menu"+menuname+"'"),1,frame)
    Popup(eval("'menu"+menuname+"clear'"),1,frame)
  }
  if (menu_popup&&menu_popup!=menuname) CleanUpMenu(frame)
  menu_popup=(menuname)?menuname:""
}

/* -------------------------------------------------------------------
  function CleanUpMenu

  Is almost the same as SetMenu without a menuname, but it only closes
  an eventually active menu without highlighting the main menu image.
  It is also called by SetMenu to close a former opened sub menu.
  frame is used to close a sub menu in a different frame.
*/
function CleanUpMenu(frame) {
  if (menu_popup) {
    Popup(eval("'menu"+menu_popup+"'"),0,frame)
    Popup(eval("'menu"+menu_popup+"clear'"),0,frame)
  }
  menu_popup=""
}

/* -------------------------------------------------------------------
  function GoURI
  
  This function is helper function to be used e.g. if OnMouseDown-Events
  are handled by a hook function (like window.onmousedown = CleanUpMenu)
  and in this case the normal href-link of a sub menu is not acting. Then
  this function can be called in a onmousedown event handler of a link
  (like onmousedown=GoURI(this)).
*/
function GoURI(uri) {
  location.href = uri.href
}
