/*
 * Generic overlay class
 * opens a popup
 */

var overlayList = new Array();
var popupWidth = "600";

/**
  * @author Bernard Brink. Edited By: David du Toit
  * @parms String descriptionString Content (html+images+etc) of the overlay
  * @parms int width The width for the popup
  * @parms int id The unique id for the popup (if there is more than one on a page)
  * @parms boolean showCancel Is true by default. if false is passed as parameter, no cancel button is added to the overlay (added by Henk Scheepers)
  * @param String className The name of an overlay style class to use instead of the default grey
  * @param boolean showContentBox Determines to show content in white content box or not. Default is true
  */
function populateOverlay(descriptionString, width, id, showCancel, className, showContentBox, addToContentId) {	
	
	   var mainBody= null;
	   var overlayContent= "";
       popupWidth = width;
       overlayList[id]= id;
       showContentBox = (undefined == showContentBox) ? true : showContentBox;
	   showCancel = (undefined == showCancel) ? true : showCancel;	   
	   className = (undefined == className) ? "genericOverlay-bg genericOverlay-bg-fullScreen" : className;
	   addToContentId = (undefined == addToContentId) ? false : addToContentId;
	   
	   overlayContent= buildOverlayContent(descriptionString, showContentBox, showCancel);

       popupString = '<table style="width:100%; height:100%;"><tr><td valign="middle" style="vertical-align:middle">';
       popupString += overlayContent;       
       popupString += '</td></tr></table>';


       // Check if the overlay should be added to the BODY or a specific tag
       if( !addToContentId ) {
         mainBody = document.getElementsByTagName('body')[0];
       }
       else {
    	 //get the body tag of the html page
         mainBody = document.getElementById(addToContentId);         
       }       

       //populate the popup div with the popup
       var popupDiv = document.createElement('div');
       popupDiv.innerHTML = popupString;
       popupDiv.id= id;
       popupDiv.className= className;
       //popupDiv.style.display= "block";

       //add the popup div to the body
       mainBody.appendChild(popupDiv);

       showOverlay();
}//end


/**
 * @abstract Build overlay content html. Either display content box woth close button or not.
 * @author David du Toit
 * @return String The HTML content to display in the overlay 
 */
function buildOverlayContent(descriptionString, showContentBox, showCancel) {
	
  var overlayContent= "";
  if( showContentBox )
  {
    overlayContent += '<div class="genericOverlayDiv" style="width:'+popupWidth+'px">';
    if(showCancel){
      overlayContent += '<input type="button" class="genericOverlayCancel" value="" onclick="hideOverlay()"/>';
    }
    overlayContent += descriptionString.join("<div style='margin-top:10px'></div>");
    overlayContent +=  '</div>';
  }
  else {
    overlayContent += descriptionString.join("<div style='margin-top:10px'></div>");
  }
 
  return overlayContent;
}//end


/**
 * @abstract Show the overlay section 
 * @author Bernard Brink. Edited By: David du Toit
 */
function showOverlay() {
	
    for(var overlayID in overlayList)
    {   	
      $("#"+overlayList[overlayID]+"").show();
    }
}//end


/**
 * @abstract Hide the overlay section
 * @author Bernard Brink. Edited By: David du Toit
 */
function hideOverlay(specificOverlayId) {
	if( specificOverlayId && '' != specificOverlayId ) {
		$("#"+specificOverlayId+"").hide();
	}
	else {
		for(var overlayID in overlayList)
		{ 
		  var overlayId= overlayList[overlayID];
		  if( overlayId )
		  {
			  $("#"+overlayId+"").hide();
		  }            
		}
	}
}//end

/**
 * 
 * @author Henk Scheepers
 */
function fadeInOverlay() {
	$("#"+feedbackID+"").fadeIn("fast");
}

/**
 * @abstract 
 * @author Henk Scheepers
 */
function fadeOutOverlay() {
        if(feedbackID) {
            $("#"+feedbackID+"").fadeOut("fast");
        }
}



