//	*********************************************************
//	Program: 	Lightbox
//	Author:		Nick Power 2009 - nickpower.com (DO NOT REMOVE)
//	Summary		Displays Lightboxes and center positions.
//	*********************************************************

//	Functions Include:
//	-----------------
//
//	pageDimensions() - 2009
//	scrollDetect() - 2009
//	updatePos() - 2009
//	initLightbox() - 2009
//	hideLightbox() - 2009
//	
//	Stuff used with lightbox functions but can be used with other stuff
//	opacity - 5th Jan 2009 - fades the opacity of a HTML object in the DOM
//	fadeID - 5th Jan 2009 - Fades a HTML Element by its ID 
//	initAjax - 2008 - Initiates the Ajax object
// 	loadpage - 2008 - loads a page into a HTML element using AJAX and the innerHTML method.
// 




//	Function pageDimensions
//	=======================
//
//	Original Author:	Nick Power, 2009 nickpower.com
//
//	Summary: 			Returns dimensions of the viewport, page and scrolling. This function is used by the 
//						Lightbox features throughout the university college website.
//
//	Parameters (In):	itemWidth - The width (in pixels) of the element which is to be positioned absolutely 
//						centred.

document.domain = 'coxheath.net';

function updatePos(){
	
	//We'll need to declare some variables to store the dimensions.
	var viewPortWidth;
	var viewPortHeight;
	
	var scrollX;
	var scrollY;
	
	var windowWidth;
	var windowHeight;
	
	var boxWidth = 950;
	var boxHeight = 600;
	
	//var newDiv = document.createElement('DIV');
	//newDiv.innerHTML = 'boxWidth=' + boxWidth + ';boxHeight=' + boxHeight + ' ;';
	//document.getElementById('lightbox').appendChild(newDiv);
	
	//Depending on the browser, populate our variables with the dimensions.
	
	//Scrolling Dimensions
	if (window.pageYOffset||window.pageXOffset)
	{
		scrollX = window.pageXOffset;
		scrollY = window.pageYOffset;
	}
    else 
	{
		scrollX = (document.body.parentElement) ? document.body.parentElement.scrollLeft : 0;
		scrollY = (document.body.parentElement) ? document.body.parentElement.scrollTop : 0;
	}
		
	//alert(scrollX);
	// View Port Dimensions
	if (self.innerHeight) {	// all except Explorer
		viewPortWidth = self.innerWidth;
		viewPortHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		viewPortWidth = document.documentElement.clientWidth;
		viewPortHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		viewPortWidth = document.body.clientWidth;
		viewPortHeight = document.body.clientHeight;
	}	
		
	// Window Dimensions
	windowWidth = document.body.clientWidth;
	windowHeight = document.body.clientHeight;
	
	scrollHeight = window.pageYOffset || document.body.scrollTop || document.documentElement.scrollTop;
	
	
	
	// Finallly-Work out the Box positioning
	
	//If there is a div with ID setLightBoxDimensions then use the top and left parameters of that div for out positioning. 
	
	//		> Grab Top, Left style property values
	
	//else
	
	// We wish to directly centre, so ...
	
	// 		> find middle point of the page (x and y)
	// 		> Now devide box width, height into 2
	// 		> Subtract this from our middle point
	

	boxX = Math.floor(viewPortWidth * 0.5) - (boxWidth / 2) + scrollX;
	boxY =  Math.floor(viewPortHeight * 0.5) - (boxHeight / 2) + scrollY;
	
	//Make sure values are at least zero
	if (boxX < 0){boxX=0;}
	if (boxY < 0){boxY=0;}

	//if the user resises the window smaller than the lightbox:
	
	//	Width - Do not move with scrolling or it will be infinite,  Keep to 0
	//	height - Move to the scroller position so that it is always visible.
	
	if (viewPortWidth < boxWidth){boxX = 0;}
	if (viewPortHeight < boxHeight){boxY = scrollY;}
	
	//Now position the Box and setup update the overlay
	if (document.getElementById('lightbox'))
	{
		objOverlay = document.getElementById('overlay');
		objLightbox = document.getElementById('lightbox');
	}
	
	objOverlay.style.width = '100%';
	objOverlay.style.height = viewPortHeight + 'px';
	objOverlay.style.top =  scrollHeight + 'px';

	if (boxY>=0 && boxX>=0){
	objLightbox.style.top = boxY + 'px';
	objLightbox.style.left =  boxX + 'px';
	}
	
	//objLightbox.innerHTML = boxX + 'px';

}


//	Function scrollDetect
//	=======================
//
//	Original Author:	Nick Power, 2008
//
//	Summary: 			Keeps the pageDimensions function operating at a certain interval.  This is so that 
//						position is 			
//						updated straight away if the user resizes the browser window, maximises, etc

function scrollDetect(){
	//Make an initial call to populate the variables.
	updatePos();
	
	//Set an interval to repeat the operation of our function
	lightboxPos = setInterval("updatePos()", 250);
	
}


//	Function initAJAX
//	=======================
//
//	Original Author:	Nick Power, 2008
//
//	Summary: 			Creates a Http Request ready for any AJAX, Makes a request and returns it into a element.

//	Parameters:			target - ID of the target element. innerHTML of this element will be set.

var xmlHttp;
function initAJAX(target){
//Setup Ajax Object
//Npower 
try
  {
  // Firefox, Opera 8.0+, Safari
  xmlHttp=new XMLHttpRequest();
  }
catch (e)
  {
  // Internet Explorer
  try
    {
    xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
    }
  catch (e)
    {
    try
      {
      xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
      }
    catch (e)
      {
      alert("Your browser does not support AJAX!");
      }
    }
  }
// Provide the user with a Loading message...
document.getElementById(target).innerHTML='<p><small>Loading...</small></p>';
xmlHttp.onreadystatechange=function()
    {
    if(xmlHttp.readyState==4)//Ok
      {
      document.getElementById(target).innerHTML=xmlHttp.responseText;
      }
    }

}
function loadPage(url,frmData){
// Initiate / Create LightBox
// Show Add Category Wizard in Lightbox
// Npower 2008
showLightbox();

pageURL = url;

target = 'lightbox';

initAJAX(target);
xmlHttp.open("POST",pageURL,true);
xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xmlHttp.setRequestHeader("Content-length", frmData.length);
xmlHttp.setRequestHeader("Connection", "close");
xmlHttp.send(frmData);
}

function loadPageGET(url){
// Initiate / Create LightBox
// Show Add Category Wizard in Lightbox
// Npower 2008
showLightbox();

pageURL = url;

target = 'lightbox';

initAJAX(target);
xmlHttp.open("GET",pageURL,true);
xmlHttp.send(null);
}

function processForm(script,frm,target){
//Set Defaults
//Npower 2008
var theForm = document.forms[frm]
var frmData = ""
//Detect if the form has File Data
   for(i=0; i<theForm.elements.length; i++){
   		if (theForm.elements[i].type=='checkbox'){
			if (theForm.elements[i].checked){
			frmData += theForm.elements[i].name + '=' + theForm.elements[i].value;
	if (i<theForm.elements.length){frmData+='&';}
			}
		}else{
			frmData += theForm.elements[i].name + '=' + theForm.elements[i].value;
	if (i<theForm.elements.length){frmData+='&';}
		}
}
if (target.length > 0){
initAJAX(target);
}
xmlHttp.open("POST",script,true);
xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xmlHttp.setRequestHeader("Content-length", frmData.length);
xmlHttp.setRequestHeader("Connection", "close");
xmlHttp.send(frmData);
}
//Function Showlightbox - Nick Power 2008
function showLightbox()
{
	
		// Lightbox and Overlay Elements
		objOverlay = document.getElementById('overlay');
		objLightbox = document.getElementById('lightbox');
	
		//objLightbox.style.height = "auto";
		//objLightbox.style.width = "auto";
		
		// 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";
        }

		//Fade the lightbox into display
		
		//set opacity to zero
		//changeOpac(0,'overlay');
		
			objLightbox.style.display = 'block';
		

			objOverlay.style.display = 'block';
		
		//Start the function that detects dinmensions and positions the lightbox
		scrollDetect();
		
		//Stop scroll movement on the body (this is reverted in hidelightbox function)
		
		// View Port Dimensions
	if (self.innerHeight) {	// all except Explorer
		viewPortWidth = self.innerWidth;
		viewPortHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		viewPortWidth = document.documentElement.clientWidth;
		viewPortHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		viewPortWidth = document.body.clientWidth;
		viewPortHeight = document.body.clientHeight;
	}	
		
		//if (document.body.style.height = viewPortHeight + 'px'){alert('Body height ok')};
		//if (document.body.style.width = viewPortWidth - 17 + 'px'){alert('Body width ok')};
		//if (document.body.style.overflow = 'hidden'){alert('Body overflow ok')};
		
		//document.body.style.height = viewPortHeight + 'px';
		//document.body.style.width = viewPortWidth - 17 + 'px';
		//document.body.style.overflow = 'hidden';
		
		return false;
	}
//
// hideLightbox()
//
function hideLightbox()
{
	// get objects
	objOverlay = document.getElementById('overlay');
	objLightbox = document.getElementById('lightbox');

	//fade it out of display and then set display:block when fading had completed
			
			objLightbox.style.display = 'none';
			objLightbox.innerHTML = '';

			objOverlay.style.display = 'none';
			objLightbox.innerHTML = '';
			

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

	//restore the visible and srollable state for body
	document.body.style.height = 'auto';
	document.body.style.overflow = 'visible';
	

}



function initLightbox()
{
	
	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") == "lightbox")){
			anchor.onclick = function () {showLightbox(this); return false;}
		}
	}

		
	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','overlay');
	objOverlay.onclick = function () {hideLightbox(); 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);
	
	// create lightbox div, same note about styles as above
	//var objLightbox = document.createElement("div");
	if (document.getElementById("lightbox")){
		var objLightbox = document.getElementById("lightbox");
	}else{
		var objLightbox = document.createElement("div");
		objLightbox.setAttribute('id','lightbox');
	}
	objLightbox.setAttribute('id','lightbox');
	objLightbox.style.display = 'none';
	objLightbox.style.position = 'absolute';
	objLightbox.style.zIndex = '100';	
	objBody.insertBefore(objLightbox, objOverlay.nextSibling);
	
	// create link
	var objLink = document.createElement("a");
	objLink.setAttribute('href','#');
	objLink.setAttribute('title','Click to close');
	objLink.onclick = function () {hideLightbox(); return false;}
	objLightbox.appendChild(objLink);

}

//change the opacity for different browsers 
function changeOpac(opacity, id) { 
    var object = document.getElementById(id).style; 
	
    object.opacity = (opacity / 100); 
    object.MozOpacity = (opacity / 100); 
    object.KhtmlOpacity = (opacity / 100); 
    object.filter = "alpha(opacity=" + opacity + ")"; 
} 

//Other random stuff

function chkFeedbackFrm(){
	return true;	
}


