﻿// Requires the Prototype.js file.
function overlay() {  
    // on creation add the onLoad method to the window
    // load event    
    this._onLoad();
}

// Properties
overlay.prototype._outerContainerElement = null;
overlay.prototype._innerContainerElement = null;
overlay.prototype._closeButton = null;

//
// _onLoad()
// Sets the control up ready for use
overlay.prototype._onLoad = function() {

    // Sets up the required client side controls    
    var _pageDimensions = getPageSize();          
    var _closeHandler = overlay.prototype.Hide.bind(overlay);
    
    
    _outerContainerElement = null;
    _innerContainerElement = null;
    _closeButton = null;

    // Create background div
    _outerContainerElement = document.createElement("div");
    _outerContainerElement.setAttribute("id", "overlaySheet");  
    _outerContainerElement.style.width = _pageDimensions[0] + "px";
    _outerContainerElement.style.height = _pageDimensions[1] + "px";   
    
    
    var _iframeElement  = document.createElement("iframe");
    _iframeElement.setAttribute("id", "overlaySheetFrame");  
                  
        
    // Create inner div
    _innerContainerElement = document.createElement("div");
    _innerContainerElement.setAttribute("id", "innerContents");   
    _innerContainerElement.style.width = "709px";
    
    var _left = ((_pageDimensions[0] - 709) /2);       
    _innerContainerElement.style.left = _left + "px";   
    
    //Create a close button
    _closeButton = document.createElement("div");
    _closeButton.innerHTML = "<img src='" + location.protocol + "//www.andrewsonline.co.uk/images/close.gif' alt='Close' />";
    _closeButton.setAttribute("id", "close");
    _closeButton.style.left = _left + "px";
     // append objects to page    
    document.body.appendChild(_outerContainerElement);   
    _outerContainerElement.appendChild(_iframeElement);
    document.body.appendChild(_innerContainerElement);                                  
    document.body.appendChild(_closeButton);   
    
    Event.observe( _closeButton, "click", _closeHandler, false);        
}
//
// Show()
// Shows the pop up dialog box
// Content = the HTML Contents to show on
// the pop up dialog box
//
overlay.prototype.Show = function(url, parameterName, parameterValue) {           
    var _url = encodeURIComponent(url);   
               
    new Ajax.Request(_url,
    {
        method: "get",
        parameters: parameterName + "=" + parameterValue,        
        onSuccess: this.onSuccess, 
        onFailure: this.onFailure,
        onException: this.onException                
    });    
}

//
overlay.prototype.onSuccess = function(response) {    
    var _pageScroll = getPageScroll();     
    var _top  = (_pageScroll[1]) + 50 + "px";
    _innerContainerElement.style.top = _top;
    _closeButton.style.top = _top;
    _outerContainerElement.style.visibility = "visible"; 
    _closeButton.style.visibility = "visible"; 
	_innerContainerElement.style.visibility = "visible";	
     
    if (response.status == 200) {
        // Successful retrieval
        _innerContainerElement.innerHTML = response.responseText;
    }
    else {
        _innerContainerElement.innerHTML = "<h1> There was a problem with this webpage<h1>"    ;
        _innerContainerElement.innerHTML += "<p class=first>There was a problem retrieving this information. An email has been sent to our software developers who will work on correcting the issue. </p>";
    }    
}

overlay.prototype.onFailure = function(response) { 	     
    _innerContainerElement.innerHTML = "<h1> There was a problem with this webpage<h1>"    ;
    _innerContainerElement.innerHTML += "<p class=first>There was a problem retrieving this information. An email has been sent to our software developers who will work on correcting the issue. </p>";   
}

overlay.prototype.onException = function(response) { 	  
   _innerContainerElement.innerHTML = "<h1> There was a problem with this webpage<h1>"    ;
   _innerContainerElement.innerHTML += "<p class=first>There was a problem retrieving this information. An email has been sent to our software developers who will work on correcting the issue. </p>";   
}






//
// Hide()
// Hides the pop up dialog box
//
overlay.prototype.Hide = function() {

   _closeButton.style.visibility = "hidden"; 
   _outerContainerElement.style.visibility = "hidden";         
   _innerContainerElement.style.visibility = "hidden";         
   // call the onLoad method to reset the controls
   overlay.prototype._onLoad.call();
}

//
// getPageScroll()
// Returns array with x,y page scroll values.
// Core code from - quirksmode.com
//
function getPageScroll(){

	var xScroll, yScroll;

	if (self.pageYOffset) {
		yScroll = self.pageYOffset;
		xScroll = self.pageXOffset;
	} else if (document.documentElement && document.documentElement.scrollTop){	 // Explorer 6 Strict
		yScroll = document.documentElement.scrollTop;
		xScroll = document.documentElement.scrollLeft;
	} else if (document.body) {// all other Explorers
		yScroll = document.body.scrollTop;
		xScroll = document.body.scrollLeft;	
	}

	arrayPageScroll = new Array(xScroll,yScroll) 
	return arrayPageScroll;
}

//
// getPageSize()
// Returns array with page width, height and window width, height
// Core code from - quirksmode.com
// Edit for Firefox by pHaez
//
function getPageSize(){
	
	var xScroll, yScroll;
	
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = window.innerWidth + window.scrollMaxX;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	
	var windowWidth, windowHeight;


	if (self.innerHeight) {	// all except Explorer
		if(document.documentElement.clientWidth){
			windowWidth = document.documentElement.clientWidth; 
		} else {
			windowWidth = self.innerWidth;
		}
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}	
	
	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else { 
		pageHeight = yScroll;
	}

	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){	
		pageWidth = xScroll;		
	} else {
		pageWidth = windowWidth;
	}

	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight) 
	return arrayPageSize;
}

// Initialize the control on the window.load
function initOverlay() { myOverlay = new overlay(); }
Event.observe(window, 'load', initOverlay, false);