/* Lavidge Interactive Overlay Script
 *
 * This script contains the basic utility functions for managing
 * the overlays.  This has been assembled from cross-browser tested
 * scripting used since late 2006.
 * 
 * Key functions:
 *   function showResponse(url, div, source, modal, arrow, position, height, width, padding)
 *                         url = URL for ajax request 
 *						   div = id of the div tag used to display the results
 *						   source = id of the element used to trigger the display
 *                         modal = id of the div tag used to provide a background outside of the overlay
 *                                 send false to prevent background disable
 *                         arrow = id of the div tag used to provide an arrow or bubble from the source to the overlay
 *                                 send false if does not exist to prevent display
 *                         position = true : best position, false : center on screen
 *			   height = height of expected results
 *                         width = width of expected results
 *                         padding = width of padding for best fit
 * Dependencies:
 *		YUI 2.5.1  dom-min.js - Must be included along with this file
 *		YUI 2.5.1  yahoo-min.js - Must be included along with this file
 *
 * Created by:	Jessica Wilkinson, Stephen Heitz
 * Created on:	03/22/2008 - 
 * 
 * Modification Log
 * ----------------------------------------
 * 3/22/2008 - SWH - Function assembly
 */



 
// Function for testing with alert messages
window.onerror = function (sMessage, sURL, sLine){
	//alert("An error occured:\n" + sMessage + "\nURL:" + sURL + "\nLine Number:" + sLine);
	return true;
}

// Determine browser
var isIE = navigator.userAgent.indexOf("MSIE") > -1;
var version = 0;
if (navigator.appVersion.indexOf("MSIE")!=-1)
{
	temp=navigator.appVersion.split("MSIE");
	version=parseFloat(temp[1]);
}
var isSF = navigator.userAgent.indexOf("Safari") > -1;
// Other browsers for future use
// var isOP = navigator.userAgent.indexOf("Opera") > -1;
// var isFF = navigator.userAgent.indexOf("FireFox") > -1;
		
// AJAX Call
function createXMLHttp() {
	if(typeof XMLHttpRequest != "undefined") {
		return new XMLHttpRequest();
	}
	else if(window.ActiveXObject) {
		var aVersions = [ "MSXML2.XMLHttp.5.0", 
		"MSXML2.XMLHttp.4.0","MSXML2.XMLHttp.3.0", 
		"MSXML2.XMLHttp", "Microsoft.XXMLHttp"];
		
		for (var i = 0; i < aVersions.length; i ++) {
			try
			{
				var oXmlHttp = new ActiveXObject(aVersions[i]);
				return oXmlHttp;
			}
			catch(oError)
			{
			
			}
		}
	}
	throw new Error("XmlHttp object could not be created.");
}
function switchHTML(div1, div2)
{
    try
    {
	    var sHTML = div1.innerHTML;
	    div1.innerHTML = '';
	    div2.innerHTML = sHTML;
	}
	catch(e)
	{
	   // alert(e);
	}	
}
function closeHTMLOverlay(htmlControl, overlay, modal, arrow) {

    closableModalBackground = true;
    var dHTML = document.getElementById(htmlControl);
    var overlayDiv = document.getElementById(overlay);
    switchHTML(overlayDiv, dHTML);
    
    var overlayDiv = document.getElementById(overlay);
	var modalDiv = document.getElementById(modal);
	var arrowDiv = document.getElementById(arrow);
	if(overlayDiv)
	{
		if(modalDiv != null) {
			toggleSelects(true);
			modalDiv.style.display = "none";
		}
		if(arrowDiv != null) {
			arrowDiv.style.display = "none";			
		}
		overlayDiv.style.display = "none";
	}
}
function showHTMLOverlay(htmlControl, overlay, source, modal, arrow, position, height, width, padding){
    try
    {
        var dHTML = document.getElementById(htmlControl);
        var overlayDiv = document.getElementById(overlay);
        var arrowDiv = document.getElementById(arrow);
        $vW(modal).addClass('htmlOverlay');
           
        switchHTML(dHTML, overlayDiv);
        
        if(position) {
			var result = bestPosition(new positionInfo(document.getElementById(source)), overlayDiv, arrow, height, width, padding);
			overlayDiv.className = result;
			if(arrowDiv != null) {
				arrowDiv.className = result;
			}
		}
		else {
			centerOverlay(overlayDiv, height, width, padding);					
			overlayDiv.className = "center";
			if(arrowDiv != null) {
				arrowDiv.className = "center";
			}
		}
		showOverlay(overlay, modal, arrow);
    }
    catch(e)
    {
    
    }
}
function showResponse(url, overlay, source, modal, arrow, position, height, width, padding) {
	var oXmlHttp = createXMLHttp();
	var overlayDiv = document.getElementById(overlay);
	var arrowDiv = document.getElementById(arrow);


	oXmlHttp.open("GET", url, true);	
    oXmlHttp.setRequestHeader("If-Modified-Since", "Sat, 1 Jan 2005 00:00:00 GMT");
	oXmlHttp.onreadystatechange = function () {
		try {
			if(oXmlHttp.readyState == 4) {
				if(oXmlHttp.status == 200) {
					var oResponse = oXmlHttp.responseText;
					if(oResponse == null) {
						overlayDiv.innerHTML = "No response";	
					}
					else {
						var stringClose = "'" + overlay + "'";
						if(modal) stringClose += ",'" + modal + "'"; else stringClose += ",false";
						if(arrow) stringClose += ",'" + arrow + "'"; else stringClose += ",false";
						// Replace closeOverlay() with complete function values in inner HTML
						oResponse = oResponse.replace("closeOverlay()", "closeOverlay(" + stringClose + ")");
						// Provide HTML string between <!-- Begin Overlay Content --> and <!-- End Overlay Content --> 
						var iBegin = oResponse.indexOf("<!-- Begin Overlay Content -->") + 31;
						var iEnd = oResponse.indexOf("<!-- End Overlay Content -->");
						if(iBegin > 0 && iEnd > 0 && iBegin < iEnd)
							overlayDiv.innerHTML = oResponse.substring(iBegin, iEnd);														
						else
						{						
							oResponse = "<div class=\"message600\"><div class=\"messageHeader\"><h1>Sorry!</h1><div id=\"close\"><a href=\"javascript:void(closeOverlay())\">Close</a></div><!--End close--></div><!-- End messageHeader--><div class=messageContent><br />The page you requested can not be returned at this time.  Please try back later.  Or you can close this window, refresh the page and try again which may correct the issue.</div><!--End message600--></div><div class=message600Footer><div class=messageFooter></div></div>";
							overlayDiv.innerHTML = oResponse.replace("closeOverlay()", "closeOverlay(" + stringClose + ")");
                        }
						
						//check for onload event
						if(oResponse.indexOf("<!-- OnLoadEvent:") > -1)
						{
						    var onloadScript = oResponse.substring(oResponse.indexOf("<!-- OnLoadEvent:") + 17);
						    onloadScript = onloadScript.substring(0, onloadScript.indexOf("-->"));
						    eval(onloadScript);
						}
					}
					
					if(position) {
						var result = bestPosition(new positionInfo(document.getElementById(source)), overlayDiv, arrow, height, width, padding);
						overlayDiv.className = result;
						if(arrowDiv != null) {
							arrowDiv.className = result;
						}
					}
					else {
										    
						centerOverlay(overlayDiv, height, width, padding);					
						overlayDiv.className = "center";
						if(arrowDiv != null) {
							arrowDiv.className = "center";
						}
					}
					showOverlay(overlay, modal, arrow);
				}
			}
		}
		catch (oError) {
			//alert(oError);
		}
	}
	oXmlHttp.setRequestHeader("Cache-Control","no-cache");
	oXmlHttp.send(null);
}

// Show/Close Overlay
function showOverlay(overlay, modal, arrow) {
	var overlayDiv = document.getElementById(overlay);
	var modalDiv = document.getElementById(modal);
	var arrowDiv = document.getElementById(arrow);
	if(overlayDiv != null) {
		overlayDiv.style.display = "block";
		overlayDiv.style.position = "absolute";

		if(modalDiv != null) {
			prepareIE('100%', 'hidden');
			toggleSelects(false);
			modalDiv.style.display = "block";
		}
		if(arrowDiv != null) {
			arrowDiv.style.display = "block";			
			arrowDiv.style.position = "absolute";
		}
		$vW('#'+overlay).bgiframe();


	}
	else {
		overlayDiv.style.display = "none";
	}

}

function closeOverlay(overlay, modal, arrow) {
    closableModalBackground = true;
	var overlayDiv = document.getElementById(overlay);
	var modalDiv = document.getElementById(modal);
	var arrowDiv = document.getElementById(arrow);
	if(overlayDiv)
	{
		if(modalDiv != null) {
			toggleSelects(true);
			modalDiv.style.display = "none";
		}
		if(arrowDiv != null) {
			arrowDiv.style.display = "none";			
		}
		overlayDiv.style.display = "none";
	}
}

function prepareIE(height, overflow)
{
	try
	{
		if((isIE && (version < 7)) || isSF)
		{
			if(height == "auto"){
				toggleSelects(true);}
			else{
				toggleSelects(false);}
			var bod = document.getElementsByTagName('body')[0];
			bod.style.height = height;
			//bod.style.overflow = overflow;
			
			var htm = document.getElementsByTagName('html')[0];
			htm.style.height = height;
			//htm.style.overflow = overflow;
		}
	}
	catch(ex)
	{
	}
}

// Generic Helper functions

// TODO: Explore changing to do inline testing for document.all vs document.getElementById vs document.layers?
// Prefix codes for .NET control name integration
// var prefix1 = "availability_";
// var previx2 = "availability:";
function getElementByBaseId(oBase, prefix1, prefix2) {
	try {		
		var oControl = document.getElementById(oBase);
		if(oControl != null) {
			return oControl;
		}
		else {
			if(prefix1 != null) {
				var oControl1 = document.getElementById(prefix1 + oBase);
				if(oControl1 != null) {
					return oControl1;
				}
				else {
					if(prefix2 != null) {
						var oControl2 = document.getElementById(prefix2 + oBase);
						if(oControl2 != null) {
							return oControl2;
						}
					}					
				}
			}
			// Should have returned by now but if we are here then we need to send a null
			return null;
		}
	}
	catch (oError) {
		return null;
	}
	
}

function stringTrim(str)
{
	return str.replace(/^\s\s*/, '').replace(/\s\s*$/, '');
}

function toggleSelects(bOn)
{
//  This function needs to be updated to support select boxes in the overlay.
//	if(isIE && (version < 7))
//	{
//		var aSelects = document.getElementsByTagName("select");
//		for(var i = 0; i < aSelects.length; i++)
//		{
//			if(bOn)
//				aSelects[i].style.visibility = "visible";
//			else
//				aSelects[i].style.visibility = "hidden";
//		}
//	}
}

function positionInfo(object) {

  var p_elm = object;

  this.getElementLeft = getElementLeft;
  function getElementLeft() {
    var x = 0;
    var elm;
    if(typeof(p_elm) == "object"){
      elm = p_elm;
    } else {
      elm = document.getElementById(p_elm);
    }
    while (elm != null) {
      x+= elm.offsetLeft;
      elm = elm.offsetParent;
    }
    return parseInt(x);
  }

  this.getElementWidth = getElementWidth;
  function getElementWidth(){
    var elm;
    if(typeof(p_elm) == "object"){
      elm = p_elm;
    } else {
      elm = document.getElementById(p_elm);
    }
    return parseInt(elm.offsetWidth);
  }

  this.getElementRight = getElementRight;
  function getElementRight(){
    return getElementLeft() + getElementWidth(p_elm);
  }

  this.getElementTop = getElementTop;
  function getElementTop() {
    var y = 0;
    var elm;
    if(typeof(p_elm) == "object"){
      elm = p_elm;
    } else {
      elm = document.getElementById(p_elm);
    }
    while (elm != null) {
      y+= elm.offsetTop;
      elm = elm.offsetParent;
    }
    return parseInt(y);
  }

  this.getElementHeight = getElementHeight;
  function getElementHeight(){
    var elm;
    if(typeof(p_elm) == "object"){
      elm = p_elm;
    } else {
      elm = document.getElementById(p_elm);
    }
    return parseInt(elm.offsetHeight);
  }

  this.getElementBottom = getElementBottom;
  function getElementBottom(){
    return getElementTop(p_elm) + getElementHeight(p_elm);
  }
}

// From YUI DOM.getViewportWidth()
function viewWidth() {
	return YAHOO.util.Dom.getViewportWidth();
}

// From YUI DOM.getViewportHeight()
function viewHeight() {
	return YAHOO.util.Dom.getViewportHeight();
}

function bestPosition(source, overlay, arrow, height, width, padding) {
	var position = "none";
	var arrowDiv = document.getElementById(arrow);
	var offsetLeft = YAHOO.util.Dom.getDocumentScrollLeft();
	var offsetTop = YAHOO.util.Dom.getDocumentScrollTop();
	
	var vwPosition = new positionInfo('vW');
	var vwBottom = vwPosition.getElementTop();
	var vwTop = source.getElementTop() - vwPosition.getElementTop();
	var vwLeft = source.getElementLeft() - vwPosition.getElementLeft();
	
	
	// We want to display it to the right of the object if possible
	if((viewWidth() - source.getElementRight() ) > (width + padding)) {
		// Room on the right
		overlay.style.left = (vwLeft + padding) + "px";
		if(arrowDiv != null) arrowDiv.style.left = vwLeft + "px";
		if(viewHeight() > height) {
			// Plenty of room
			// Can we center?
			if(((source.getElementTop() - offsetTop) - (height / 2) - padding) > 0) {
				overlay.style.top = (vwTop - (height / 2)) + "px";
				if(arrowDiv != null) arrowDiv.style.top = (vwTop - (padding / 2)) + "px";
				position = "right-center";
			}
			else {
				// Make sure the top is displayed in the viewable area
				// So this is set the top to me plus the delta or space between me and the top
				overlay.style.top = (source.getElementTop() - (source.getElementTop() - offsetTop)) + "px";					
				if(arrowDiv != null) arrowDiv.style.top = (source.getElementTop() - (padding / 2)) + "px";
				position = "right-top";
			}
		}
		else {
			// Make sure the top is displayed in the viewable area
			// So this is set the top to me plus the delta or space between me and the top
			overlay.style.top = (source.getElementTop() - (source.getElementTop() - offsetTop)) + "px";					
			if(arrowDiv != null) arrowDiv.style.top = (source.getElementTop() - (padding / 2)) + "px";
			position = "right-top";
		}
	}
	else {
		// No room on the right
		if(source.getElementLeft() > (width + padding)) {
			// Room on the left
			overlay.style.left = ((source.getElementLeft() - offsetLeft - vwPosition.getElementLeft()) - width - padding) + "px";
			if(arrowDiv != null) arrowDiv.style.left = ((source.getElementLeft() - offsetLeft - vwPosition.getElementLeft()) - padding) + "px";
			if(viewHeight() > height) {
				// Plenty of room 
				// Can we center?
				if((source.getElementTop() - (height / 2) - padding) > 0) {
					overlay.style.top = (vwTop - (height / 2)) + "px";
					if(arrowDiv != null) arrowDiv.style.top = (vwTop - (padding / 2)) + "px";
					position = "left-center";
				}
				else {
					// Make sure the top is displayed in the viewable area
					overlay.style.top = (source.getElementTop() - (source.getElementTop() - offsetTop)) + "px";					
					if(arrowDiv != null) arrowDiv.style.top = (source.getElementTop() - (padding / 2)) + "px";
					position = "left-top";
				}
			}
			else {
				// Make sure the top is displayed in the viewable area
				overlay.style.top = (source.getElementTop() - (source.getElementTop() - offsetTop)) + "px";					
				if(arrowDiv != null) arrowDiv.style.top = (source.getElementTop() - (padding / 2)) + "px";
				position = "left-top";
			}
		}
		else {						
			// OK this thing is too big to fit left or right, let's try top or bottom center first
			if((source.getElementTop() - offsetTop) > (height + padding)) {
				// Great we can fit it above
				overlay.style.top = (vwTop - height) + "px";
				if(arrowDiv != null) arrowDiv.style.top = (vwTop  - padding) + "px";
				position = "top";
			}
			else {
				// Well now that's not good but its going to have to display below
				overlay.style.top = (vwTop + padding) + "px";
				if(arrowDiv != null) arrowDiv.style.top = vwTop + "px";
				position = "bottom";
			}
			
			// Is the screen big enough to display the entire width?
			if(viewWidth() > width) {
				// Yes it is so we can try for center, left, or right
				// Let's try to center it
				center = source.getElementLeft() + (source.getElementWidth() / 2 - vwPosition.getElementLeft());
				if(((width / 2) < center) && ((center + (width / 2)) < viewWidth())) {
					// OK we have enough space on the left and right?
					overlay.style.left = (center - (width/2)) + "px";
					if(arrowDiv != null) arrowDiv.style.left = (center - (padding / 2)) + "px";
					position += "-center";
				}
				else {
					// We can't center so go left or right
					// This also means that we can't use the current positioning because it assumed center
					// If we go left or right we need to move the item up or down so it aligns
					if(position == "top") {
						// Move the overlay down so the padding can connect
						overlay.style.top = ((vwTop - height) + (padding * 2)) + "px";						
					}
					else if(position == "bottom") {
						// Move the overlay up so the padding can connect
						overlay.style.top = (source.getElementBottom() - padding) + "px";						
					}
					
					// OK now that the top/bottom is set right figure out what to do
					if((viewWidth() - source.getElementRight()) > width) {
						overlay.style.left = (source.getElementRight() + padding) + "px";
						if(arrowDiv != null) arrowDiv.style.left = source.getElementRight() + "px";						
						position += "-right";
					}
					else {
						// Try to the Left if that doesn't work go back and do the right anyway
						// Try to fit to the Left
						if(source.getElementLeft() > width) {
							overlay.style.left = ((source.getElementLeft() - offsetLeft) - width - padding) + "px";
							if(arrowDiv != null) arrowDiv.style.left = ((source.getElementLeft() - offsetLeft) - padding) + "px";
							position += "-left";
						}
						else {
							// OK fine make sure the top left is on the screen
							overlay.style.left = (source.getElementRight() + padding) + "px";
							if(arrowDiv != null) arrowDiv.style.left = source.getElementRight() + "px";						
							position += "-right";						
						}
					}						
				}
			}
			else {
				// No the screen isn't big enough to display the overlay
				// Need to go bottom-left or bottom-right to keep this in view
				overlay.style.top = (source.getElementTop() - padding) + "px";
				if(arrowDiv != null) arrowDiv.style.top = source.getElementTop() + "px";
				position = "bottom";
				if((viewWidth() - source.getElementRight()) > width) {
					overlay.style.left = (source.getElementRight() + padding) + "px";
					if(arrowDiv != null) arrowDiv.style.left = source.getElementRight() + "px";						
					position += "-right";
				}
				else {
					// Try to the Left if that doesn't work go back and do the right anyway
					// Try to fit to the Left
					if((viewWidth() - source.getElementLeft()) > width) {
						overlay.style.left = ((source.getElementLeft() - offsetLeft) - width - padding) + "px";
						if(arrowDiv != null) arrowDiv.style.left = ((source.getElementLeft() - offsetLeft) - padding) + "px";
						position += "-left";
					}
					else {
						// OK fine make sure the top left is on the screen
						overlay.style.left = (source.getElementRight() + padding) + "px";
						if(arrowDiv != null) arrowDiv.style.left = source.getElementRight() + "px";						
						position += "-right";						
					}
				}						
			}
		}					
	}
	return(position);
}

function centerOverlay(overlay, height, width, padding) {
	var offsetLeft = YAHOO.util.Dom.getDocumentScrollLeft();
	var offsetTop = YAHOO.util.Dom.getDocumentScrollTop();
	
    var objectPosition = new positionInfo(overlay);    
    var vwPosition = new positionInfo('vW');
    	
	if((viewWidth() - width) > 0) 
	{
        var positionWFromScreen = (viewWidth() - width) / 2;
		overlay.style.left = positionWFromScreen - vwPosition.getElementLeft() + "px";
	}
	else {
		// Wow, make the left show up
		overlay.style.left = padding + "px";
	}							
	// Will it fit top to bottom?
	if((viewHeight() - height) > 0) {
	    
        var positionWFromScreen = (viewHeight() - height) / 2;
		overlay.style.top = positionWFromScreen + offsetTop - vwPosition.getElementTop() + "px";
	}
	else {
		// Wow, make the top show at least
		overlay.style.top = offsetTop + padding + "px";			
	}			
}

 function OverlayGetTopItem()
	 {  
	    var itemTop = 0;
        if(navigator.appName.indexOf("Microsoft") > -1)
        {
            itemTop = 30;
        } 
        else 
        {
            itemTop = 20;
        }
        return itemTop;
    }
     
     
     
      var cache = [];
  // Arguments are image paths relative to the current page.
      function preloadImages(arguments) {
          var args_len = arguments.length;
          for (var i = args_len; i--; ) {
              var cacheImage = document.createElement('img');
              cacheImage.src = arguments[i];
              cache.push(cacheImage);
          }
      }
      $vW(document).ready(function() { preloadImages(
      ["/common/img/overlay/overlay300Body.png",
      "/common/img/overlay/overlay300Bottom.png",
      "/common/img/overlay/overlay450Body.png",
      "/common/img/overlay/overlay450Bottom.png",
      "/common/img/overlay/overlay600Body.png",
      "/common/img/overlay/overlay600Bottom.png"]); });
