//	ADD TEXT BOX FUNCTIONALITY
//
// showTextbox()
// Loads an external page into an iframe
//

var TB_width = 600;
var TB_height = 500;


function showTextbox(objLink)
{

	// prep objects
	var objOverlay = document.getElementById('text_overlay');
	var objTextbox = document.getElementById('textbox');
	var objCaption = document.getElementById('textboxCaption');
	var objFrame = document.getElementById('textboxframe');
// 	var objLoadingImage = document.getElementById('loadingImage');
	var objTextboxDetails = document.getElementById('textboxDetails');

	
	var arrayPageSize = getPageSize();
	var arrayPageScroll = getPageScroll();

	// center loadingImage if it exists
// 	if (objLoadingImage) {
// 		objLoadingImage.style.top = (arrayPageScroll[1] + ((arrayPageSize[3] - 35 - objLoadingImage.height) / 2) + 'px');
// 		objLoadingImage.style.left = (((arrayPageSize[0] - 20 - objLoadingImage.width) / 2) + 'px');
// 		objLoadingImage.style.display = 'block';
// 	}

	// set height of Overlay to take up whole page and show
	objOverlay.style.height = (arrayPageSize[1] + 'px');
	objOverlay.style.display = 'block';



	// preload image
// 	imgPreload = new Image();
// 
// 	imgPreload.onload=function(){
// 		objImage.src = objLink.href;
// 
		// center lightbox and make sure that the top and left values are not negative
		// and the image placed outside the viewport
		var textboxTop = arrayPageScroll[1] + ((arrayPageSize[3] - 35 - TB_height) / 2);
		var textboxLeft = ((arrayPageSize[0] - 20 - TB_width) / 2);
		
		objTextbox.style.top = (textboxTop < 0) ? "0px" : textboxTop + "px";
		objTextbox.style.left = (textboxLeft < 0) ? "0px" : textboxLeft + "px";


		objTextboxDetails.style.width = TB_width + 'px';
	
// 		if(objLink.getAttribute('title')){
// 			objCaption.style.display = 'block';
// 			//objCaption.style.width = imgPreload.width + 'px';
// 			objCaption.innerHTML = objLink.getAttribute('title');
// 		} else {
// 			objCaption.style.display = 'none';
// 		}
// 		
// 		// A small pause between the image loading and displaying is required with IE,
// 		// this prevents the previous image displaying for a short burst causing flicker.
// 		if (navigator.appVersion.indexOf("MSIE")!=-1){
// 			pause(250);
// 		} 
// 
// 		if (objLoadingImage) {	objLoadingImage.style.display = 'none'; }
// 
// 		// Hide select boxes as they will 'peek' through the image in IE
// 		selects = document.getElementsByTagName("select");
//         for (i = 0; i != selects.length; i++) {
//                 selects[i].style.visibility = "hidden";
//         }

	
		objTextbox.style.display = 'block';
// objLoadingImage.style.display = 'none';
// 	alert(objLink.href+' - '+objFrame.id);
	ajaxFunction(objLink.href, objFrame.id);


		// After image is loaded, update the overlay height as the new image might have
		// increased the overall page height.
		arrayPageSize = getPageSize();
		objOverlay.style.height = (arrayPageSize[1] + 'px');
		
		// Check for 'x' keypress
		listenKey();

		return false;
// 	}

// 	imgPreload.src = objLink.href;
	
}





//
// hideTextbox()
//
function hideTextbox()
{
	// get objects
	objOverlay = document.getElementById('text_overlay');
	objTextbox = document.getElementById('textbox');

	// hide lightbox and overlay
	objOverlay.style.display = 'none';
	objTextbox.style.display = 'none';

// 	var mainText = document.getElementById('objFrame');
// 	mainText.innerHTML = '';

	// make select boxes visible
	selects = document.getElementsByTagName("select");
    for (i = 0; i != selects.length; i++) {
		selects[i].style.visibility = "visible";
	}

	// disable keypress listener
	document.onkeypress = '';
}




//
// initTextbox()
// hacked up from initLightbox()
//
function initTextbox()
{
	
	if (!document.getElementsByTagName){ return; }
	var anchors = document.getElementsByTagName("a");

	// loop through all anchor tags
	for (var i=0; i<anchors.length; i++){
		var anchor = anchors[i];

		if (anchor.getAttribute("href") && (anchor.getAttribute("rel") == "textbox")){
			anchor.onclick = function () {showTextbox(this); return false;}
		}
	}

	// the rest of this code inserts html at the top of the page that looks like this:
	//
	// <div id="overlay">
	//		<a href="#" onclick="hideLightbox(); return false;"><img id="loadingImage" /></a>
	//	</div>
	// <div id="textbox">
	//		<a href="#" onclick="hideTextbox(); return false;" title="Click anywhere to close image">
	//			<iframe id="textboxframe" />
	//		</a>
	//		<div id="textboxDetails">
	//			<div id="textboxCaption"></div>
	//			<div id="keyboardMsg"></div>
	//		</div>
	// </div>
	
	var objBody = document.getElementsByTagName("body").item(0);
	
	// create overlay div and hardcode some functional styles (aesthetic styles are in CSS file)
	var objOverlay = document.createElement("div");
	objOverlay.setAttribute('id','text_overlay');
	objOverlay.onclick = function () {hideTextbox(); return false;}
	objOverlay.style.display = 'none';
	objOverlay.style.position = 'absolute';
	objOverlay.style.top = '0';
	objOverlay.style.left = '0';
	objOverlay.style.zIndex = '90';
 	objOverlay.style.width = '100%';
	objBody.insertBefore(objOverlay, objBody.firstChild);
	
	var arrayPageSize = getPageSize();
	var arrayPageScroll = getPageScroll();

	// preload and create loader image
// 	var imgPreloader = new Image();
// 	
// 	// if loader image found, create link to hide lightbox and create loadingimage
// 	imgPreloader.onload=function(){
// 
// 		var objLoadingImageLink = document.createElement("a");
// 		objLoadingImageLink.setAttribute('href','#');
// 		objLoadingImageLink.onclick = function () {hideLightbox(); return false;}
// 		objOverlay.appendChild(objLoadingImageLink);
// 		
// 		var objLoadingImage = document.createElement("img");
// 		objLoadingImage.src = loadingImage;
// 		objLoadingImage.setAttribute('id','loadingImage');
// 		objLoadingImage.style.position = 'absolute';
// 		objLoadingImage.style.zIndex = '150';
// 		objLoadingImageLink.appendChild(objLoadingImage);
// 
// 		imgPreloader.onload=function(){};	//	clear onLoad, as IE will flip out w/animated gifs
// 
// 		return false;
// 	}

// 	imgPreloader.src = loadingImage;

	// create lightbox div, same note about styles as above
	var objTextbox = document.createElement("div");
	objTextbox.setAttribute('id','textbox');
	objTextbox.style.display = 'none';
	objTextbox.style.position = 'absolute';
	objTextbox.style.zIndex = '100';
	objTextbox.style.width = TB_width;
	objTextbox.style.height = TB_height;	
	objBody.insertBefore(objTextbox, objOverlay.nextSibling);
	
	// create link
// 	var objLink = document.createElement("a");
// 	objLink.setAttribute('href','#');
// 	objLink.setAttribute('title','Click to close');
// 	objLink.onclick = function () {hideTextbox(); return false;}
// 	objTextbox.appendChild(objLink);

	// preload and create close button image
	var imgPreloadCloseButton = new Image();

	// if close button image found, 
	imgPreloadCloseButton.onload=function(){

		var objCloseButton = document.createElement("img");
		objCloseButton.src = closeButton;
		objCloseButton.setAttribute('id','closeButton');
		objCloseButton.style.position = 'absolute';
		objCloseButton.style.zIndex = '200';
		objCloseButton.onclick = function () {hideTextbox(); return false;}
		objCloseButton.onmouseover = function () {objCloseButton.style.cursor = "pointer"; objCloseButton.style.cursor = "hand";}
		objCloseButton.onmouseout = function () {objCloseButton.style.cursor = "pointer";}
		objTextbox.appendChild(objCloseButton);

		return false;
	}

	imgPreloadCloseButton.src = closeButton;

	// create image
// 	var objImage = document.createElement("img");
// 	objImage.setAttribute('id','lightboxImage');
// 	objLink.appendChild(objImage);
	var objFrame = document.createElement("div");
	objFrame.setAttribute('id','textboxframe');
	objTextbox.appendChild(objFrame);

	
	// create details div, a container for the caption and keyboard message
	var objTextboxDetails = document.createElement("div");
	objTextboxDetails.setAttribute('id','textboxDetails');
	objTextbox.appendChild(objTextboxDetails);

	// create caption
	var objCaption = document.createElement("div");
	objCaption.setAttribute('id','textboxCaption');
// 	objCaption.style.display = 'none';
	objCaption.innerHTML = 'HEllo!';
	objTextboxDetails.appendChild(objCaption);

	// create keyboard message
	var objKeyboardMsg = document.createElement("div");
	objKeyboardMsg.setAttribute('id','keyboardMsg');
	objKeyboardMsg.innerHTML = 'Click to close';
	objTextboxDetails.appendChild(objKeyboardMsg);


}



function ajaxFunction(pageResource, pageTarget){

  var ajaxRequest;  // The variable that makes Ajax possible

  try{
    ajaxRequest = new XMLHttpRequest();
  } catch (e){
    try{
      ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
    } catch (e) {
      try{
        ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
      } catch (e){
        alert("Your browser broke!");
        return false;
        }
     }
  }

//   loadingMessage(voteName, "loadingMessage", "Submitting rating");

// Create a function that will receive data sent from the server
  ajaxRequest.onreadystatechange = function(){

  if(ajaxRequest.readyState == 4) 
   {
    // UPDATE PAGE
	var targetDiv = document.getElementById(pageTarget);
// 	alert(ajaxRequest.responseText);
	targetDiv.innerHTML = '</a>'+ajaxRequest.responseText;

	return true;
   }
  }

  var getThis =  pageResource;
  ajaxRequest.open("GET", getThis, true);
  ajaxRequest.send(null); 
}



addLoadEvent(initTextbox);	// run initTextbox onLoad