/*************************************************************************
  dw_Popup_sel.js, dw_Popup.js with integrated select list overlay 
  requires: dw_event.js and dw_viewport.js
  version date: March 14, 2005 
  
  This code is from Dynamic Web Coding at dyn-web.com
  Copyright 2003-5 by Sharon Paine 
  See Terms of Use at www.dyn-web.com/bus/terms.html
  regarding conditions under which you may use this code.
  This notice must be retained in the code as is!
  
  *** NOTE *** This file has been edited extensively for Singapore Airlines
               and cannot just be replaced by a new version from DynWeb.
*************************************************************************/
function Popup(tipID) {
	this.followMouse = true;
	this.overlaySelects = false;  // iframe shim for select lists (ie win)
	this.offX = 8;
	this.offY = 12;
	this.tipID = tipID;
	this.showDelay = 0;
	this.hideDelay = 0;
	this.ovTimer = 0;
	this.ready = false;
	this.timer = null;
	this.tip = null;
	this.shim = null;
	this.timerId = 0;
	this.supportsOverlay = false;
	this.debug = false;
}

Popup.prototype.init = function() {
	if ( document.createElement && document.body && typeof document.body.appendChild != "undefined" ) {
		this.supportsOverlay = this.checkOverlaySupport();
		this.ready = true;
	}
}

Popup.prototype.show = function(e, url) {
	if(this.debug) csLog.debug("BEGIN SHOW");
	
	if (this.timer) { clearTimeout(this.timer);	this.timer = 0; }
	this.tip = getObj( this.tipID );
	if (this.followMouse) // set up mousemove 
		dw_event.add( document, "mousemove", this.trackMouse, true );

	viewport.getAll();
	this.handleOverlay(1, this.showDelay);
	this.positionTip(e);
	this.timer = setTimeout("toggleVis('" + this.tipID + "', 'visible')", this.showDelay);

	if (url != "undefined" && url != null && url != "") {
		// IE likes the window.frame, Mozilla likes getElementById
		if (window.frames["puContent"] != null) {
			window.frames["puContent"].location = url;
		} else {
			if (getObj("puContent") != null) getObj("puContent").src = url;	
		}
		if (getObj("puContent") != null) getObj("puContent").style.display = "block";
	}
	if(this.debug) csLog.debug("END SHOW");
}
Popup.prototype.hide = function() {
	if (this.timer) { clearTimeout(this.timer);	this.timer = 0; }
	this.handleOverlay(0, this.hideDelay);
	this.timer = setTimeout("toggleVis('" + this.tipID + "', 'hidden')", this.hideDelay);
	if (getObj("puContent") != null) getObj("puContent").style.display = "none";
	if (getObj("popupDiv") != null) getObj("popupDiv").style.display = "none";
	if (window.frames["puContent"] != null) {
		window.frames["puContent"].location = popupDefaultURL;
	} else {
		if (getObj("puContent") != null) getObj("puContent").src = popupDefaultURL;	
	}
	if (this.followMouse) // release mousemove
		dw_event.remove( document, "mousemove", this.trackMouse, true );
	this.tip = null; 
}

Popup.prototype.writeTip = function(msg) {
	if ( this.tip && typeof this.tip.innerHTML != "undefined" ) this.tip.innerHTML = msg;
}

Popup.prototype.positionTip = function(e) {
	if(this.debug) csLog.debug("BEGIN POSITION");
	if ( this.tip && this.tip.style ) {    
		// put e.pageX/Y first! (for Safari)
		var x = e.pageX? e.pageX: e.clientX + viewport.scrollX;
		var y = e.pageY? e.pageY: e.clientY + viewport.scrollY;

		if ( x + this.tip.offsetWidth + this.offX > viewport.width + viewport.scrollX ) {
			x = x - this.tip.offsetWidth - this.offX;
			if ( x < 0 ) x = 0;
		} else x = x + this.offX;

		if ( y + this.tip.offsetHeight + this.offY > viewport.height + viewport.scrollY ) {
			y = y - this.tip.offsetHeight - this.offY;
			if ( y < viewport.scrollY ) y = viewport.height + viewport.scrollY - this.tip.offsetHeight;
		} else y = y + this.offY;
		if(this.debug) csLog.debug("CURRENT:\nX: " + this.tip.style.left + " Y: " + this.tip.style.top + "\nNEW:\nX: " + x + " Y: " + y);
		this.tip.style.left = x + "px"; this.tip.style.top = y + "px";
	}

	this.positionOverlay();
	if(this.debug) csLog.debug("END POSITION");
}




Popup.prototype.trackMouse = function(e) {
	e = dw_event.DOMit(e);
	this.positionTip(e);
}

Popup.prototype.checkOverlaySupport = function() {
	if ( navigator.userAgent.indexOf("Windows") != -1 && 
		typeof document.body != "undefined" && 
		typeof document.body.insertAdjacentHTML != "undefined" && 
		!window.opera && navigator.appVersion.indexOf("MSIE 5.0") == -1 
		) return true;
	else return false;
}

Popup.prototype.handleOverlay = function(bVis, d) {
  	if(this.trace) csLog.debug("BEGIN HANDLE OVERLAY");
	if ( this.overlaySelects && this.supportsOverlay ) {
		if (this.ovTimer) { clearTimeout(this.ovTimer); this.ovTimer = 0; }
		switch (bVis) {
			case 1 :
				if ( !document.getElementById('tipShim') ) 
					document.body.insertAdjacentHTML("beforeEnd", '<iframe id="tipShim" src="about:blank" style="position:absolute; left:0; top:0; z-index:500; visibility:hidden" scrolling="no" frameborder="0"></iframe>');
				this.shim = document.getElementById('tipShim'); 
				if (this.shim && this.tip) {
					this.shim.style.width = this.tip.offsetWidth + "px";
					this.shim.style.height = this.tip.offsetHeight + "px";
				}
				this.ovTimer = setTimeout("toggleVis('tipShim', 'visible')", d);
			break;
			case 0 :
				this.ovTimer = setTimeout("toggleVis('tipShim', 'hidden')", d);
				if (this.shim) this.shim = null;
			break;
		 }
	}
	if(this.trace) csLog.debug("END HANDLE OVERLAY");
}
    
Popup.prototype.positionOverlay = function() {
  if(this.debug) csLog.debug("BEGIN POSITION OVERLAY");
	if ( this.overlaySelects && this.supportsOverlay && this.shim ) {
		this.shim.style.left = this.tip.style.left;
		this.shim.style.top = this.tip.style.top;
	}
  if(this.debug) csLog.debug("END POSITION OVERLAY");
}
   
Popup.prototype.tipOutCheck = function(e) {
  if(this.debug) csLog.debug("BEGIN TIP OUT CHECK");
	e = dw_event.DOMit(e);
	// is element moused into contained by Popup?
	var toEl = e.relatedTarget? e.relatedTarget: e.toElement;
	if ( this != toEl && !contained(toEl, this) ) this.hide();
  if(this.debug) csLog.debug("END TIP OUT CHECK");
}

Popup.prototype.doPopup = function(e, url, height, width) {
	if(document.getElementById("popupDiv")!=null){
		document.getElementById("popupDiv").style.display="block";
	}
  if(this.debug) csLog.debug("BEGIN DO POPUP" + height + " " + width);
  if ( typeof this == "undefined" || !this.ready ) return;
  this.clearTimer();
  var tip = document.getElementById? document.getElementById(this.tipID): null;
  if ( tip && tip.onmouseout == null ) {
      //tip.onmouseout = this.tipOutCheck; //comment out so popup doesn't go away
      tip.onmouseover = this.clearTimer;
  }
  tip.style.height = height + "px";
  tip.style.width = width + "px";
  if (getObj("puContent") != null) getObj("puContent").width = width;
  if (getObj("puContent") != null) getObj("puContent").height = height;
  
  if(this.debug) csLog.debug("URL:" + url);
  this.show(e, url);
  if(this.debug) csLog.debug("END DO POPUP");
}

Popup.prototype.hidePopup = function() {
  if(this.debug) csLog.debug("BEGIN HIDE POPUP");
  if ( typeof this == "undefined" || !this.ready ) return;

  //if (getObj("puContent") != null) getObj("puContent").style.display = "none";
  this.hide();

  if(this.debug) csLog.debug("END HIDE POPUP");
}
Popup.prototype.clearTimer = function() {
  if (this.timerId) { clearTimeout(this.timerId); this.timerId = 0; }
}

Popup.prototype.unHookHover = function () {
    var tip = document.getElementById? document.getElementById(this.tipID): null;
    if (tip) {
        tip.onmouseover = null;
        tip.onmouseout = null;
        tip = null;
    }
}


// OTHER POPUP/TOOLTIP RELATED FUNCTIONS

function doPopup(evt, url, height, width) {	
	if(document.getElementById("popupDiv")!=null){
		document.getElementById("popupDiv").style.display="block";
	}
    var e= (window.event) ? window.event : evt;
    var sUrl = SHOWPOPUPACTIONURL;
    if(sUrl.indexOf("?") > -1)
    	sUrl += "&";
    else
    	sUrl += "?";
    
    sUrl += "method=popup&popupUrl=" + url;

	popups.doPopup(e, sUrl, height, width);
}

function hidePopup() {
	popups.hidePopup();
}

function toggleVis(id, vis) { // to check for el, prevent (rare) errors
	var el = getObj(id);
	if(csLog.isDebugEnabled()) csLog.debug("TOGGLE VIS: " + el + " " + vis);
	if (el) el.style.visibiility = vis;
}

// returns true of oNode is contained by oCont (container)
function contained(oNode, oCont) {
  if (!oNode) return; // in case alt-tab away while hovering (prevent error)
  while ( oNode = oNode.parentNode ) if ( oNode == oCont ) return true;
  return false;
}
function setIframeHeight() {
  var iframeName = 'puContent';
  var iframeWin = window.frames[iframeName];
  var iframeEl = getObj(iframeName);
  if ( iframeEl && iframeWin ) {
    iframeEl.style.height = "auto"; // helps resize (for some) if new doc shorter than previous
    var docHt = getDocHeight(iframeWin.document);
    // need to add to height to be sure it will all show
    if (docHt) iframeEl.style.height = docHt + "px";
  }
}
function getDocHeight(doc) {
  if(csLog.isDebugEnabled()) csLog.debug("GET DOC HEIGHT");
  var docHt = 0, sh, oh;
  if (doc.height) docHt = doc.height;
  else if (doc.body) {
    if (doc.body.scrollHeight) docHt = sh = doc.body.scrollHeight;
    if (doc.body.offsetHeight) docHt = oh = doc.body.offsetHeight;
    if (sh && oh) docHt = Math.max(sh, oh);
  }
  if(csLog.isDebugEnabled()) csLog.debug("HEIGHT:" + docHt);
  return docHt;
}
function loadIframe(iframeName, url) {
  if ( window.frames[iframeName] ) {
    window.frames[iframeName].location = url;   
    return false;
  }
  else return true;
}

	// added for emex mkp90966

function doPopupemex(evt, url, height, width) {
	if(document.getElementById("popupDiv")!=null){
		document.getElementById("popupDiv").style.display="block";
	}
    var e= (window.event) ? window.event : evt;
    var sUrl = SHOWPOPUPACTIONURL;

    if(sUrl.indexOf("?") > -1)
    	sUrl += "&";
    else
		sUrl += "?";   
    sUrl += "method=popup&popupUrl=" + url;
	popups.doPopupemex(e, sUrl, height, width);
}


Popup.prototype.doPopupemex = function(e, url, height, width) {
	if(document.getElementById("popupDiv")!=null){
		document.getElementById("popupDiv").style.display="block";
	}
  if(this.debug) csLog.debug("BEGIN DO POPUP" + height + " " + width);
  if ( typeof this == "undefined" || !this.ready ) return;
  this.clearTimer();
  var tip = document.getElementById? document.getElementById(this.tipID): null;
  if ( tip && tip.onmouseout == null ) {
      //tip.onmouseout = this.tipOutCheck; //comment out so popup doesn't go away
      tip.onmouseover = this.clearTimer;
  }
  tip.style.height = height + "px";
  tip.style.width = width + "px";
  if (getObj("puContent") != null) getObj("puContent").width = width;
  if (getObj("puContent") != null) getObj("puContent").height = height;  
  if(this.debug) csLog.debug("URL:" + url);
  this.showemex(e, url);
  if(this.debug) csLog.debug("END DO POPUP");
}


Popup.prototype.showemex = function(e, url) {
	if(this.debug) csLog.debug("BEGIN SHOW");	
	if (this.timer) { clearTimeout(this.timer);	this.timer = 0; }
	this.tip = getObj( this.tipID );
	if (this.followMouse) // set up mousemove 
		dw_event.add( document, "mousemove", this.trackMouse, true );
	viewport.getAll();
	this.handleOverlay(1, this.showDelay);
	this.positionTipemex(e);
	this.timer = setTimeout("toggleVis('" + this.tipID + "', 'visible')", this.showDelay);
	if (url != "undefined" && url != null && url != "") {
		// IE likes the window.frame, Mozilla likes getElementById
		if (window.frames["puContent"] != null) {
			window.frames["puContent"].location = url;
		} else {
			if (getObj("puContent") != null) getObj("puContent").src = url;	
		}
		if (getObj("puContent") != null) getObj("puContent").style.display = "block";
	}
	if(this.debug) csLog.debug("END SHOW");
}

Popup.prototype.positionTipemex = function(e) {
	if(this.debug) csLog.debug("BEGIN POSITION");
	if ( this.tip && this.tip.style ) {    
	// put e.pageX/Y first! (for Safari)
		var x = e.pageX? e.pageX: e.clientX + viewport.scrollX;
		var y = e.pageY? e.pageY: e.clientY + viewport.scrollY ;
		x= 325;
		if ( x + this.tip.offsetWidth + this.offX > viewport.width + viewport.scrollX ) {
			x = x - this.tip.offsetWidth - this.offX;
			if ( x < 0 ) x = 0;
		} else x = x + this.offX;
		if ( y + this.tip.offsetHeight + this.offY > viewport.height + viewport.scrollY ) {
			y = y - this.tip.offsetHeight - this.offY - 70;
			if ( y < viewport.scrollY ) y = viewport.height + viewport.scrollY - this.tip.offsetHeight - 130;
		} else y = y + this.offY - 70;
		if(this.debug) csLog.debug("CURRENT:\nX: " + this.tip.style.left + " Y: " + this.tip.style.top + "\nNEW:\nX: " + x + " Y: " + y);
		this.tip.style.left =  x + "px"; this.tip.style.top = y + "px";
	}



	this.positionOverlay();

	if(this.debug) csLog.debug("END POSITION");

}

function doPopupDeepLink(evt, url, height, width) {
	if(document.getElementById("popupDiv")!=null){
		document.getElementById("popupDiv").style.display="block";
	}
var e= (window.event) ? window.event : evt;
var sUrl = SHOWPOPUPACTIONURL;

if(sUrl.indexOf("?") > -1)
sUrl += "&";
else
sUrl += "?"; 
sUrl += "method=popup&popupUrl=" + url;
popups.doPopupDeepLink(e, sUrl, height, width);
}


Popup.prototype.doPopupDeepLink = function(e, url, height, width) {
	if(document.getElementById("popupDiv")!=null){
		document.getElementById("popupDiv").style.display="block";
	}
if(this.debug) csLog.debug("BEGIN DO POPUP" + height + " " + width);
if ( typeof this == "undefined" || !this.ready ) return;
this.clearTimer();
var tip = document.getElementById? document.getElementById(this.tipID): null;
if ( tip && tip.onmouseout == null ) {

tip.onmouseover = this.clearTimer;
}
tip.style.height = height + "px";
tip.style.width = width + "px";
if (getObj("puContent") != null) getObj("puContent").width = width;
if (getObj("puContent") != null) getObj("puContent").height = height; 
if(this.debug) csLog.debug("URL:" + url);
this.showDeepLink(e, url);
if(this.debug) csLog.debug("END DO POPUP");
}


Popup.prototype.showDeepLink = function(e, url) {
if(this.debug) csLog.debug("BEGIN SHOW"); 
if (this.timer) { clearTimeout(this.timer); this.timer = 0; }
this.tip = getObj( this.tipID );
if (this.followMouse) 
dw_event.add( document, "mousemove", this.trackMouse, true );
viewport.getAll();
this.handleOverlay(1, this.showDelay);
this.positionTipDeepLink(e);
this.timer = setTimeout("toggleVis('" + this.tipID + "', 'visible')", this.showDelay);
if (url != "undefined" && url != null && url != "") {

if (window.frames["puContent"] != null) {
window.frames["puContent"].location = url;
} else {
if (getObj("puContent") != null) getObj("puContent").src = url; 
}
if (getObj("puContent") != null) getObj("puContent").style.display = "block";
}
if(this.debug) csLog.debug("END SHOW");
}

Popup.prototype.positionTipDeepLink = function(e) {
if(this.debug) csLog.debug("BEGIN POSITION");
if ( this.tip && this.tip.style ) { 

var x = (viewport.width)/2-170;
var y = 250;
this.tip.style.left = x + "px"; this.tip.style.top = y + "px";
}

this.positionOverlay();

if(this.debug) csLog.debug("END POSITION");

}