/*
  dw_layer.js
  a few commonly-used functions for handling positioned divs
  used by Writing to Layers examples from www.dyn-web.com
  
  This code is from Dynamic Web Coding 
  at http://www.dyn-web.com/
  Copyright 2003 by Sharon Paine 
  See Terms of Use at http://www.dyn-web.com/bus/terms.html
  Permission granted to use this code 
  as long as this entire notice is included.
*/

layerHandler = {
  getRefs: function (id) {
    var el = (document.getElementById)? document.getElementById(id): (document.all)? document.all[id]: (document.layers)? document.layers[id]: null;
//    if (!el) el = (document.getElementById)? eval("window.frames."+id+".document.getElementById('body')" : (window.frames) ? window.frames[id].document.body : null;
    if (el) el.css = el.style? el.style: el;
    return el;
  },
  
  writeLayer: function (el, cntnt) {
      if (typeof el.innerHTML!="undefined") {
          el.innerHTML = cntnt;
      } else if (document.layers) {
    			el.document.write(cntnt);
    			el.document.close();
      }
  },
  
  shiftTo: function (el,x,y) {
    var px = (document.layers || window.opera)? 0: "px";
    if (x != null) el.css.left = x + px;
    if (y != null) el.css.top = y + px;
  },

  sizeTo: function (el,x,y) {
    var px = (document.layers || window.opera)? 0: "px";
    if (x != null) el.css.width = x + px;
    if (y != null) el.css.height = y + px;
  },

  show: function (el) { el.css.visibility = "visible"; },
  hide: function (el) { el.css.visibility = "hidden"; }
}

// adapted from act_api.js by Dan Pupius of www.13thparallel.org
var imageHandler = {
  imgs: new Array(),
  path: "",
  preload: function () {
    for (var i=0; i<arguments.length; i++) {
      var img = new Image();
      img.src = this.path + arguments[i];
      imageHandler.imgs.push(img);
    }
  }
}

// for browsers that don't support array push method
// from 13th parallel ( www.13thparallel.org )
if (Array.prototype && !Array.prototype.push) {
	Array.prototype.push = function() {
		for (var i=0; i<arguments.length; i++) this[this.length] = arguments[i];
		return this.length;
	};
}

// returns amount of vertical scroll
function getScrollY() {
	var sy = 0;
	if (document.documentElement && document.documentElement.scrollTop)
		sy = document.documentElement.scrollTop;
	else if (document.body && document.body.scrollTop) 
		sy = document.body.scrollTop; 
	else if (window.pageYOffset)
		sy = window.pageYOffset;
	else if (window.scrollY)
		sy = window.scrollY;
	return sy;
}

// returns amount of horizontal scroll
function getScrollX() {
	var sx = 0;
	if (document.documentElement && document.documentElement.scrollLeft)
		sx = document.documentElement.scrollLeft;
	else if (document.body && document.body.scrollLeft) 
		sx = document.body.scrollLeft; 
	else if (window.pageXOffset)
		sx = window.pageXOffset;
	else if (window.scrollX)
		sx = window.scrollX;
	return sx;
}

/* SPECIFC FUNCTIONS FOR FLEX TIPS */
var hint_old='';
var hint_timed=0;
function openTip(id,e,hint,offx,offy,w,h) {
  if (!page_loaded) return;
  if (id == null) id = 'hintDiv';
  var el = layerHandler.getRefs(id);
  if (!el) return;
  layerHandler.hide(el); // just in case ...

  // get position of onmouseover event and use it to position layer
        if (e == null)
        {
                layerHandler.shiftTo(el, offx, offy);
        }
        else
        {
            e = (window.event)? window.event: e;
            if (e) {
                    var x =  (e.pageX)? e.pageX: (e.clientX)? e.clientX + getScrollX(): 0;
                    var y =  (e.pageY)? e.pageY: (e.clientY)? e.clientY + getScrollY(): 0;
                    layerHandler.shiftTo(el, x+offx, y+offy);
            }
        }
        layerHandler.sizeTo(el, w, h);

  var cntnt = '';
  if (hint.length > 5 && (hint.substring(hint.length-5,hint.length) == '.html' || hint.substring(0,4) == 'http'))
  {
     if (hint_old != hint)
     {
          window.frames['hintFrm'].document.body.innerHTML = 'Loading ...' ;
	  loadIframe('hintFrm','help/'+hint);
/*
	  if (hint_timed==0)
	  {
	    hint_timed++;
	    window.setTimeout("showTip(id,e,hint,offx,offy,w,h)",5000);// ricircolo per caricamento !
	    return;
	  }
*/	  
	  hint_timed=0;
	  hint_old = hint;
          if (window.frames['hintFrm'].document.body.innerHTML == '') loadIframe('hintFrm','help/'+hint); // wait
     }
     cntnt = window.frames['hintFrm'].document.body.innerHTML ;
     cntnt = '<div class="hint">' + cntnt + '</div>';
//alert("inner="+window.frames['hintFrm'].document.body.innerHTML);
  }
  else
  {
     cntnt = '<div class="hint">' + hint + '</div>';
  }

  var win = window.open("","_blank","width="+w+",height="+h+",location=0,menubar=0,resizable=0,scrollbars=1,status=1,titlebar=1,toolbar=0,screenX=100,left=100,screenY=30,top=60");
  win.document.write(cntnt);
}

function showTip(id,e,hint,offx,offy,w,h) {
  if (!page_loaded) return;
  if (id == null) id = 'hintDiv';
  var el = layerHandler.getRefs(id);
  if (!el) return;
  layerHandler.hide(el); // just in case ...

  // get position of onmouseover event and use it to position layer
        if (e == null)
        {
                layerHandler.shiftTo(el, offx, offy);
        }
        else
        {
            e = (window.event)? window.event: e;
            if (e) {
                    var x =  (e.pageX)? e.pageX: (e.clientX)? e.clientX + getScrollX(): 0;
                    var y =  (e.pageY)? e.pageY: (e.clientY)? e.clientY + getScrollY(): 0;
                    layerHandler.shiftTo(el, x+offx, y+offy);
            }
        }
        layerHandler.sizeTo(el, w, h);

  var cntnt = '';
  if (hint.length > 5 && (hint.substring(hint.length-5,hint.length) == '.html' || hint.substring(0,4) == 'http'))
  {
     if (hint_old != hint)
     {
          window.frames['hintFrm'].document.body.innerHTML = 'Loading ...' ;
	  loadIframe('hintFrm','help/'+hint);
/*
	  if (hint_timed==0)
	  {
	    hint_timed++;
	    window.setTimeout("showTip(id,e,hint,offx,offy,w,h)",5000);// ricircolo per caricamento !
	    return;
	  }
*/	  
	  hint_timed=0;
	  hint_old = hint;
          if (window.frames['hintFrm'].document.body.innerHTML == '') loadIframe('hintFrm','help/'+hint); // wait
     }
     cntnt = window.frames['hintFrm'].document.body.innerHTML ;
     cntnt = '<div class="hint">' + cntnt + '</div>';
//alert("inner="+window.frames['hintFrm'].document.body.innerHTML);
  }
  else
  {
     cntnt = '<div class="hint">' + hint + '</div>';
  }
  
  layerHandler.writeLayer(el, cntnt);
  layerHandler.show(el);
}

function hideTip(id) {
  if (!page_loaded) return;
  if (id == null) id = 'hintDiv';
  var el = layerHandler.getRefs(id);
  if (!el) return;
  layerHandler.hide(el);
}

function hideTipTimed(id,time) {
  setTimeout("hideTip('"+id+"')", time);
}

function loadIframe(iframeName, url) {
  if ( window.frames[iframeName] ) {
    window.frames[iframeName].location = url;   
    return false;
  }
  else return true;
}

