/*-------------------------	*/
/*	Video lightbox			*/
/*	V1.4 by Chris Knight	*/
/*	23/06/08				*/
/*------------------------- */
/* Used to display Flash videos in the style of lightbox - fade in and out - uses scriptaculous, prototype and effects libraries*/

var copyText = new Array();

function setCopytext(copyText){
	copytext = copyText;
}

function getCopytext(){
	return copytext;
}


/*Startup - Define flash player properties, then create video links and attach event listeners to them (which will run the flash player)*/
function setUpPlayer(copyText, mediaServer, linkLocation){
	mediaServerLocation = mediaServer;
	setCopytext(copyText);
	
	setVideoLinks(linkLocation);
}

function textHide(linkLocation){
	var hideText = document.getElementById(linkLocation).getElementsByClassName("hideText_DyBox");
	
	var itemClassName = "";
	
	for(var i = 0; i < hideText.length; i++){
	
		itemClassName = "";
		itemClassName = hideText[i].className;
		
		hideText[i].className = itemClassName + " hide";
		
	}
}



function getBodyWidth(){
	var bodyWidth;
	if (document.body && typeof document.body.clientWidth!='undefined') {
		bodyWidth = document.body.clientWidth;
	}else{
		bodyWidth = "974px";	
	}
	return bodyWidth;
}

/*Creates a flash player object*/
function FlashPlayer(width, height, vidWidth, vidHeight, fullscreen, skipTime, allowscriptaccess, allowFullScreen, uiBarColour, uiBarBufferColor){
	this.width = width;
	this.height = height;
	this.vidWidth = vidWidth;
	this.vidHeight = vidHeight;
	
	this.fullscreen = fullscreen;
	this.skipTime = skipTime;
	this.allowscriptaccess = allowscriptaccess;
	this.allowFullScreen = allowFullScreen;
	this.uiBarColour = uiBarColour;
	this.uiBarBufferColor = uiBarBufferColor;
	this.pageWidth = getBodyWidth();
}

/*Creates a text holder object*/
function textPlayer(width, height){
	this.width = width;
	this.height = height;
	this.pageWidth = getBodyWidth();
}

function defineFlashPlayer(flashPlayerProperties){
	var so = new SWFObject(mediaServerLocation + "/swf/video/flvPlayer.swf", "flv", flashPlayerProperties.width, flashPlayerProperties.height, "8", "#000000");
	so.addVariable("viewHeight", flashPlayerProperties.vidHeight);
	so.addVariable("viewWidth", flashPlayerProperties.vidWidth);
	so.addVariable("fullscreen", flashPlayerProperties.fullscreen);
	so.addVariable("skipTime",flashPlayerProperties.skipTime);
	so.addVariable("uiBarTimeColor", flashPlayerProperties.uiBarColour);
	so.addVariable("uiBarBufferColor", flashPlayerProperties.uiBarBufferColor);
	so.addParam("menu", "false"); /*Disables the menu on the flash video when you right click it*/
	so.addParam("allowscriptaccess",flashPlayerProperties.allowscriptaccess);
	so.addParam("allowFullScreen",flashPlayerProperties.allowFullScreen);
	return so;
}

function defineSWFPlayer(flashPlayerProperties, videoName){
	var so = new SWFObject(videoName, "swf", flashPlayerProperties.width, flashPlayerProperties.height, "8", "#000000");
	so.addVariable("viewHeight", flashPlayerProperties.vidHeight);
	so.addVariable("viewWidth", flashPlayerProperties.vidWidth);
	so.addParam("menu", "false"); //Disables the menu on the flash video when you right click it
	return so;
}

/*Creates a video link for each occurrance of a product(css class) on the page*/
function setVideoLinks(linkLocation){
	var copyText = "";
	
	if(linkLocation.length > 0){
		for(var i = 0; i < linkLocation.length; i++){
		
			if($(linkLocation[i])){
			
				if($(linkLocation[i]).getElementsByClassName("addDyBoxLink")){
				
					var links = $(linkLocation[i]).getElementsByClassName("addDyBoxLink");
					var ignoreCertainIds = $("heroItem");
					for(var j = 0; j < links.length; j++){
					
					
						if(links[j] != ignoreCertainIds){
							copyText = getCopytext();
							if(hasFlashVersion(8) || copyText[j][3].toLowerCase() =="textbox") {
								textHide(linkLocation[i]);
								
								
								createVideoLink(links[j], copyText, j, linkLocation[i]);
								
							}
						}
					}
				}
			}
		}
	}else{
		if(document.getElementsByClassName("addDyBoxLink")){
			var links = document.getElementsByClassName("addDyBoxLink");
			var ignoreCertainIds = $("heroItem");
			for(var k = 0; k < links.length; k++){
				if(links[k] != ignoreCertainIds){
					copyText = getCopytext();
					if(hasFlashVersion(8) || copyText[k][3].toLowerCase() =="textbox") {
						textHide(linkLocation[i]);
						createVideoLink(links[k], copyText, k, "");
					}
				}
			}
		}
	}
}

/*Creates an image link for starting the flash video*/
/*(currentFlashPlayer = Object holding Flash player height, width, control settings etc. videoObject = Represents an instance of a div.product. contentDisplayProperties = Object instance holding content player / container details - height, width etc. Used in conjunction with currentFlashPlayer variable. copyText = copy text derived from the asp page that is calling technology.js. i = Used to help determine which)*/
function createVideoLink(videoObject, copyText, i, linkLocation){

    
    if (videoObject!= undefined ){
    
	    var videoAnchor, videoTxt, videoImg, videoObjectParent, videoSectionP, videoSectionP_Text, videoDiv, videoAnchor, linkName, loadFlashPlayer_Bind
	    var childPos = 0;
	    if(copyText[i][3].toLowerCase() == ""){
		    copyText[i][3] = "text";	
	    }
	    if(linkLocation == ""){
		    linkLocation = "Content";	
	    }
    	
	    if(copyText[i][3].toLowerCase() != "image"){
		    videoAnchor = document.createElement("a");
		    videoAnchor.setAttribute("href", "javascript:void(0)");
		    videoAnchor.className = "videoLink clear";
		    videoAnchor.id = "videoLink_" + linkLocation + "_" + i;
		    if(copyText[i][3].toLowerCase() == "text" || copyText[i][3].toLowerCase() == "textbox"){
			    videoAnchor.className = videoAnchor.className + " textLink";
			    videoAnchor.setAttribute("title", copyText[i][1]);
			    videoTxt = document.createTextNode(copyText[i][0]);
			    videoAnchor.appendChild(videoTxt);
		    }else if(copyText[i][3].toLowerCase() == "button" || copyText[i][3].toLowerCase() == "textimage"){
			    videoAnchor.setAttribute("title", copyText[i][1]);
			    videoImg = document.createElement("img");
			    videoImg.setAttribute("src", copyText[i][0]);
			    videoImg.setAttribute("alt", copyText[i][1]);
			    videoImg.setAttribute("id", "videoLinkImg" + i);
			    videoAnchor.appendChild(videoImg);
		    }
		    if(videoObject.parentNode){
			    videoObjectParent = videoObject.parentNode;
			    videoObjectParent.insertBefore(videoAnchor, videoObject.nextSibling);
		    }
    		
	    }else{	
		    var videoSectionDiv = document.createElement("div");
		    videoSectionDiv.className = "videoSection clear";
		    videoSectionDiv.setAttribute("id", "videoDiv_" + linkLocation + "_" + i);
		    videoSectionDiv.className = "videoSection clear";
		    if(copyText[i][0] != ""){
			    videoSectionP = document.createElement("p");
			    videoSectionP.className = "textColour1";
			    videoSectionP_Text = document.createTextNode(copyText[i][0])
		    }
		    videoDiv = document.createElement("div");
		    videoDiv.className = "flashcontent";
		    videoDiv.setAttribute("id", "video" + i);
		    videoAnchor = document.createElement("a");
		    videoAnchor.setAttribute("href", "javascript:void(0)");
		    videoAnchor.className = "videoLink";
		    videoAnchor.id = "videoLink_" + linkLocation + "_" + i;
		    videoImg = document.createElement("img");
		    videoImg.setAttribute("src", copyText[i][1]);
		    videoImg.setAttribute("alt", copyText[i][6]);
		    videoImg.setAttribute("id", "videoLinkImg" + i);
		    if(copyText[i][10] != "false"){
			    Event.observe(videoAnchor,"mouseover",function() { imageRollover(videoImg, copyText[i][1])});
			    Event.observe(videoAnchor,"mouseout",function() { imageRollout(videoImg, copyText[i][1])});
		    }
		    videoAnchor.appendChild(videoImg);
		    videoDiv.appendChild(videoAnchor);
		    if(copyText[i][0] != ""){
			    videoSectionP.appendChild(videoSectionP_Text);
			    videoSectionDiv.appendChild(videoSectionP);
		    }
		    videoSectionDiv.appendChild(videoDiv);
    		
		    if(videoObject.parentNode){
			    videoObjectParent = videoObject.parentNode;
			    videoObjectParent.insertBefore(videoSectionDiv, videoObject.nextSibling);
		    }
	    }	
	    linkName = videoAnchor.id;

	    loadFlashPlayer_Bind = loadFlashPlayer.bindAsEventListener(this, copyText, i);
	    Event.observe(linkName ,"click", loadFlashPlayer_Bind);
    }
}

function loadFlashPlayer(event, copyText, linkId){
	var linkName, loadFlashPlayer_Bind, linkNames;
	
	linkNames = document.getElementsByClassName("videoLink");
	
	for(var i = 0; i < linkNames.length; i++){
		linkName = linkNames[i].id;
		loadFlashPlayer_Bind = loadFlashPlayer.bindAsEventListener(this, copyText, i);
		Event.stopObserving(linkName ,"click", loadFlashPlayer_Bind);
	}
	
	loadFlashPlayerVideo(copyText[linkId][2], copyText[linkId][3], copyText[linkId][4], copyText[linkId][5], copyText[linkId][7], copyText[linkId][8], copyText[linkId][9], copyText[linkId][10], copyText[linkId][11], copyText[linkId][12]);		
}

function imageRollover(videoImg, videoImageArray){
	videoImageArray = videoImageArray.replace(".jpg","_Yellow.jpg");
	videoImg.src = videoImageArray;
}

function imageRollout(videoImg, videoImageArray){
	videoImageArray = videoImageArray.replace("_Yellow.jpg",".jpg");
	videoImg.src = videoImageArray;
}

function createDyBoxOverlay(){
	var bodyContainer = document.getElementsByTagName("body")[0];
	var dyBoxContainer = document.createElement("div");
	dyBoxContainer.setAttribute("id", "dyBox_overlay");	
	dyBoxContainer.setAttribute("class", "dyBox_AllowClose");	
	bodyContainer.appendChild(dyBoxContainer);	
}

function buildContainers(linkType, playerHeight, contentDisplayProperties, win){
	if(!$("dyBox_overlay")){
		createDyBoxOverlay();
		createBlackBackground(linkType, playerHeight);
	}
	if(!$("lightbox_Content")){
		createContentOverlay(contentDisplayProperties, win);
	}
	if(!$("lightbox_Flash")){
		createContentContainer(contentDisplayProperties, linkType);
	}	
}

function createContentOverlay(contentDisplayProperties, win){
	var containerContent, bodyContainer;
	
	containerContent = createPlayerContainer();
	containerContent.style.left = (-(getDyBoxOffset(contentDisplayProperties, win)) + ((win[0] - contentDisplayProperties.width) / 2)) + "px";

	bodyContainer = document.getElementsByTagName("body")[0];
	bodyContainer.appendChild(containerContent);	
}

/*Create the container to hold the Flash player / text to be displayed*/
function createPlayerContainer(){
	var containerContent
	
	containerContent = document.createElement("div");
	containerContent.setAttribute("id", "lightbox_Content");
	containerContent.style.overflow = "hidden";
	
	return containerContent;
}

function createContentContainer(contentDisplayProperties, linkType){
	var closeButton, closeButtonText, contentContainer;
	
	closeButton = document.createElement("a");
	closeButtonText = document.createTextNode("close - X");
	
	closeButton.setAttribute("href", "javascript:void(0)");
	closeButton.setAttribute("id", "closeFlash");
	
	/*Holds the Flash video / textbox*/
	contentContainer = document.createElement("div");
	contentContainer.setAttribute("id", "lightbox_Flash");
	contentContainer.setAttribute("class", "clear");
	contentContainer.style.width = contentDisplayProperties.width + "px";
	contentContainer.style.height = contentDisplayProperties.height + "px";
	if(linkType.toLowerCase() == "textbox"){
		contentContainer.style.overflow = "scroll";
		contentContainer.style.overflowX = "hidden";
	}
	if(linkType.toLowerCase() == "textimage"){
		contentContainer.style.overflow = "hidden";
		contentContainer.style.overflowX = "hidden";
		contentContainer.style.overflowY = "auto";
	}
	
	closeButton.appendChild(closeButtonText);
	$("lightbox_Content").appendChild(closeButton);
	$("lightbox_Content").appendChild(contentContainer);
	//Event.observe(contentContainer,"click",disableVideoPlayerSelect);
}

/*Loads the flash player with the relevant video / text*/
function loadFlashPlayerVideo(videoName, linkType, uiAvailable, bufferTime, playerWidth, playerHeight, copyTextCategory, allowRollover, productId, productType){
	var playerHeightUI, currentFlashPlayer, win, contentDisplayProperties;
	
	if (bufferTime = ""){

		bufferTime = 4;
	}
	
	if (uiEnabled = ""){
		uiEnabled = "true";
	}
	
	win = this.windowDimensions();
	/*This variable is used to specify the flash player video container height, when the control panel needs to be displayed. If no control panel is specified, then the player height will be set to match the video height*/
	playerHeightUI = playerHeight;
	
	/*Creates an instance of the relevant player (text or flash), depending on */
	if(linkType.toLowerCase() == "textbox" || linkType.toLowerCase() == "textimage"){
		contentDisplayProperties = new textPlayer(playerWidth, playerHeight);
	}else{
		if(uiAvailable.toLowerCase() =="true"){
			playerHeightUI = parseInt(playerHeight) + 26;
		}
		contentDisplayProperties = new FlashPlayer(playerWidth, playerHeightUI, playerWidth, playerHeight,"true",2,"sameDomain","true", "#666666", "#333333");
	}
	
	/*Creates the relevant containers if they are not present for some reason (predominantely used when dyBox is started up for the first time on the page)*/
	buildContainers(linkType, playerHeight, contentDisplayProperties, win);
	/*Hides the containers for now so that they can be faded-in and so that the javascript has time to get the text/ flash file*/
	$("lightbox_Content").style.display = "none";
	$("dyBox_overlay").style.display = "none";
	
	/*Get width of page and work out how much to offset div to the left, so that the entire page is faded rather than just the body section (window - flash size then divide by two to get the left over space either side of flash player. Then divide by two to know where the 0px boundary is within the browser.*/
	if(linkType.toLowerCase() == "textbox" || linkType.toLowerCase() == "textimage"){
		$("dyBox_overlay").style.left = -(getDyBoxOffset(contentDisplayProperties, win)) + "px";
		if(typeof(productType) != "undefined"){
			if(productType == "A"){
				$("lightbox_Flash").style.backgroundColor = "#FFFFFF";
			}else{
				$("lightbox_Flash").style.backgroundColor = "#000000";
			}
		}else{
			$("lightbox_Flash").style.backgroundColor = "#FFFFFF";
		}
		this.putCenter("lightbox_Content", contentDisplayProperties);
		Event.observe(this,"resize",function() {this.putCenter("lightbox_Content", contentDisplayProperties);})
	}else{
		$("dyBox_overlay").style.left = getDyBoxOffset(contentDisplayProperties, win) + "px";
		$("lightbox_Flash").style.backgroundColor = "#000000";
		if(videoName.lastIndexOf(".swf") != -1){
			currentFlashPlayer = defineSWFPlayer(contentDisplayProperties, videoName);
			//alert(videoName);
		}else{
			currentFlashPlayer = defineFlashPlayer(contentDisplayProperties);
			//alert(videoName);
			currentFlashPlayer.addVariable("file", videoName);
			currentFlashPlayer.addVariable("uiEnabled", uiAvailable);
			currentFlashPlayer.addVariable("buffer", bufferTime);
		}
		this.putCenter("lightbox_Content", contentDisplayProperties);
		Event.observe(this,"resize",function() {this.putCenter("lightbox_Content", contentDisplayProperties);})
	}
	
	$("closeFlash").className = "dyBox_AllowClose";
	$("dyBox_overlay").className = "dyBox_AllowClose clear";
	
	/*Allows dyBox to display within the entire browser screen rather than just being confined to the body tag size*/
	$("dyBox_overlay").style.width = win[0] + "px";

	/*Stops Mac users from losing the video player control panel by clicking on it*/
	if($("flv")){
		Event.observe($("flv"),"click", disableVideoPlayerSelect);
	}
	
	if($("swf")){
		Event.observe($("lightbox_Flash"),"click", disableVideoPlayerSelect);
	}
	
	/*Check for Firefox on Mac, if so then don't fade in and out of player - distorts flash controls*/	
	if((osType().toLowerCase() == "mac") && (identifyBrowser().toLowerCase() == "mozilla")){
		if(linkType.toLowerCase() == "textbox" || linkType.toLowerCase() == "textimage"){
			createTextBox(copyTextCategory, productId, productType);	
		}else{
			noFlashMessage()
			currentFlashPlayer.write("lightbox_Flash");
		}
		if(typeof sIFR == "function"){
			if(document.getElementsByClassName("sIFR-flash")){
				var flashTitles = document.getElementsByClassName("sIFR-flash");
				for(var i = 0; i < flashTitles.length; i++){
					var abc = flashTitles[i];
					flashTitles[i].style.visibility = "hidden";
				}
			}		
		}
		$("dyBox_overlay").style.display = "block";
		$("loadingGIF").style.display = "block";
		$("lightbox_Content").style.display = "block";
		$("closeFlash").style.display = "block";
		$("lightbox_Flash").style.display = "block";
		allowPlayerClose();
		$("loadingGIF").style.display = "none";
	}else{
		/*Fades in the dyBox_overlay div, which has a black colour background, and the flash video / text div.*/
		if(linkType.toLowerCase() == "textbox" || linkType.toLowerCase() == "textimage"){
			$("loadingGIF").style.display = "block";
			if($("lightbox_Flash")){
				new Effect.Appear($("dyBox_overlay"), {duration: 0.5, from: 0.0, to: 0.9, afterFinish:function(){if($("dyBox_overlay").style.display != "none"){createTextBox(copyTextCategory, productId, productType); $("lightbox_Flash").style.display = "block"; $("closeFlash").style.display = "block"; $("loadingGIF").style.display = "none"; new Effect.Appear($("lightbox_Content"), {duration: 0.5, from: 0.0, to: 1.0, afterFinish:function(){allowPlayerClose();}})}}});
				}
		}else{
			displayBackground(currentFlashPlayer);
		}
	}
}

/*Shows the black background by fading it onto the screen, once this call is completed, the main content function is then called. Also any flash replacement that has occured on the screen will be hidden while the video / text is hown. This is because it was showing clearly through the black background, rather than being faded like the rest of the content*/
function displayBackground(currentFlashPlayer){
	$("loadingGIF").style.display = "block";
	new Effect.Appear($("dyBox_overlay"), {duration: 0.5, from: 0.0, to: 0.7, afterFinish:function(){if($("dyBox_overlay").style.display != "none"){if(typeof sIFR == "function"){if(document.getElementsByClassName("sIFR-flash")){var flashTitles = document.getElementsByClassName("sIFR-flash");for(var i = 0; i < flashTitles.length; i++){var abc = flashTitles[i];flashTitles[i].style.visibility = "hidden";}}}; displayContent(currentFlashPlayer)}}});
}

/*Shows the video / textbox by fading it onto the screen*/
function displayContent(currentFlashPlayer){
	noFlashMessage();
	$("closeFlash").style.display = "block";
	$("lightbox_Flash").style.display = "block";
	new Effect.Appear($("lightbox_Content"), {duration: 0.5, from: 0.0, to: 1.0,afterFinish:function(){currentFlashPlayer.write("lightbox_Flash"); allowPlayerClose(); $("loadingGIF").style.display = "none";}});
}

/*This is for occassions where the user has flash installed, JS switched on but the video was not displaying because they had an earlier version of Flash. We used a function for checking the flash version but that wasn't always reliable as no message was displayed to the user.*/
function noFlashMessage(){
	var noFlashTag, noFlashText;
	
	noFlashTag = document.createElement("p");
	noFlashTag.id = "dyBox_noFlashText"; 
	noFlashText = document.createTextNode("Flash Player version 8 or above is required to view the video. Please download this from the relevant website.");
	noFlashTag.appendChild(noFlashText);
}

/*Gets the text from copytext. This is achieved by using AJAX to call the ASP file listed below. This gets the copytext and applies a relevant XSLT stylesheet to it, which is added to the lightbox_Flash container*/
function createTextBox(copyTextCategory, productId, productType){
	var data, ajaxResults;
	
	if(typeof(productId) != "undefined"){
		data = "copyTextCategory=" + copyTextCategory + "&productDetailsId=" + productId + "&productType=" + productType;
		if(productType.toLowerCase() == "a"){
			$("lightbox_Flash").className = "textBox clear dyBox_WhiteBackground";
		}else{		
			$("lightbox_Flash").className = "textBox clear dyBox_BlackBackground";
		}
	}else{
		data = "copyTextCategory=" + copyTextCategory;
		$("lightbox_Flash").className = "textBox clear";
	}
	ajaxResults = new Ajax.Updater("lightbox_Flash", "/local/library/dyBoxTextContent.asp", {parameters:data});

}

function allowPlayerClose(){
	if(($("dyBox_overlay").style.display = "block") && ($("lightbox_Content").style.display = "block")){
		Event.observe($("dyBox_overlay"),"click", closeLightboxContainer);
		Event.observe($("closeFlash"),"click", closeLightboxContainer);
	}
}

/*Stops Mac users from closing the video player by clicking on the player - It stopped users from being able to use the control panel*/
function disableVideoPlayerSelect(argEvent){
	Event.stop(argEvent);
}


function getDyBoxOffset(contentDisplayProperties, win){
	var offset;
	//+10px is to avoid a scrollbar appearing at the bottom of the page, when the video player loads up
	offset = (((win[0] - contentDisplayProperties.pageWidth) / 2) + 10);
	return offset;
}

function resetScrollBar(){
	if (self.scrollTo) {
		// all except Explorer
		self.scrollTo(0,0);
  	}else if (document.documentElement && document.documentElement.scrollTop) {
		// Explorer 6 Strict
		document.documentElement.scrollTop = 0;
  	}else if (document.body) {
		// all other Explorers
		document.body.scrollTop = 0;
  	}	
}

/*Creates the transparent background when a dyBox link is pressed*/
function createBlackBackground(linkType, playerHeight){
	var windowHeight, win;
	win = window.windowDimensions();
	if (document.body && document.body.clientHeight) {
		// Explorer 6 Strict Mode
		windowHeight = document.body.clientHeight;
		if (windowHeight < win[1]){
			windowHeight = win[1];
		}
	}
	overlay = $("dyBox_overlay");
	overlay.style.backgroundImage = "url(/images/dyBox/black-90.png)";
	overlay.style.opacity = "0.9";
	overlay.style.filter = "alpha(opacity=90)";
	overlay.style.backgroundRepeat = "repeat";
	if(document.body.offsetHeight){
		if(document.body.offsetHeight < (1.5 * playerHeight)){
			overlay.style.height = (1.5 * parseInt(playerHeight)) + "px";
		}else{
			overlay.style.height = document.body.offsetHeight + "px";
		}
	}else{
		overlay.style.height = "1400px";
	}
	overlay.style.width = "100%";
	
	var loadingGIF = document.createElement("img");
	loadingGIF.id = "loadingGIF";
	loadingGIF.src="/images/dyBox/Loading.gif";
	loadingGIF.style.height = "40px";
	loadingGIF.style.width = "40px";
	loadingGIF.style.display = "block";
	loadingGIF.style.marginLeft = "50%";
	overlay.appendChild(loadingGIF);
}

/*Places an item in the center of the screen*/
function putCenter(item, contentDisplayProperties) {
	var xy, win, scrollValue, playerPadding, topPos, leftPos, leftOffset;
	
	item = $(item);
	xy = item.getDimensions();
	win = this.windowDimensions();
	scrollValue = this.scrollOffset();
	leftOffset = getDyBoxOffset(contentDisplayProperties, win);
	
	/*Centers the content (flash/text). Compares the */
	if(item.id == "lightbox_Content"){
		if(win[0] < contentDisplayProperties.pageWidth){
			leftPos = ((win[0] - contentDisplayProperties.width) / 2);
		}else{
			leftPos = -(getDyBoxOffset(contentDisplayProperties, win)) + ((win[0] - contentDisplayProperties.width) / 2);
		}
	}else{
		leftPos = ((win[0] - contentDisplayProperties.width) / 2);		
	}
	/*Works out the mid position of the screen on the y-axis (distance from top of webpage to top left of browser window + the value of the difference between the browser window size and the flash player / textbox size (divided by 3))*/
	topPos = (scrollValue[1] + ((win[1] - contentDisplayProperties.height)/3));
	
	if(topPos < 0){
		topPos = 0;	
	}
	
	if(leftPos < 0){
		leftPos = 0;	
	}
	item.style.left = leftPos + "px";
	item.style.top = topPos + "px"; 
	$("loadingGIF").style.marginTop = (topPos + 150) + "px";
	item.style.position = "absolute";
	/*Resize the black background to take account of any changes in the browser window size*/
	updateDyBoxSize(contentDisplayProperties, win);
}

/*Resizes the black backround when the browser is resized. This makes sure that the background always covers the whole width of the screen, as the body tag width is less than the width of the browser*/
function updateDyBoxSize(contentDisplayProperties, win){
	if(win[0] < contentDisplayProperties.pageWidth){
		win[0] = contentDisplayProperties.pageWidth;
		$("dyBox_overlay").style.left = "0px";
	}
	else{
		$("dyBox_overlay").style.left = "-" + getDyBoxOffset(contentDisplayProperties, win) + "px";
	}
	$("dyBox_overlay").style.width = win[0] + "px";
}


function windowDimensions() {
	var x, y;
	if (self.innerHeight) {
		// all except Explorer
		x = self.innerWidth;
		y = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) {
		// Explorer 6 Strict Mode
		x = document.documentElement.clientWidth;
		y = document.documentElement.clientHeight;
	} else if (document.body) {
		// other Explorers
		x = document.body.clientWidth;
		y = document.body.clientHeight;
	}
	if (!x) x = 0;
	if (!y) y = 0;
	arrayWindowSize = new Array(x,y);
	return arrayWindowSize;
}

/*Finds out the distance of the top left hand corner of the browser window compared to the top of the webpage*/
function scrollOffset() {
  var x, y;
  if (self.pageYOffset) {
    // all except Explorer
    x = self.pageXOffset;
    y = self.pageYOffset;
  } else if (document.documentElement && document.documentElement.scrollTop) {
    // Explorer 6 Strict
    x = document.documentElement.scrollLeft;
    y = document.documentElement.scrollTop;
  } else if (document.body) {
    // all other Explorers
    x = document.body.scrollLeft;
    y = document.body.scrollTop;
  }
  if (!x) x = 0;
  if (!y) y = 0;
  arrayScrollOffset = new Array(x,y);
  return arrayScrollOffset;
}

function closeLightboxContainer(event){
	var linkNames = "";
	var linkName = "";
	var i;
	
	/*Switches on the Flash replacement text (if applicable) for the page*/
	if(typeof sIFR == "function"){
		if(document.getElementsByClassName("sIFR-flash")){
			var flashTitles = document.getElementsByClassName("sIFR-flash");
			for(var i = 0; i < flashTitles.length; i++){
				flashTitles[i].style.visibility = "visible";
			}
		}		
	}
	
	var element = Event.element(event);
	if(Element.hasClassName(element,"dyBox_AllowClose")){
		
		if((osType().toLowerCase() == "mac") && (identifyBrowser().toLowerCase() == "mozilla")){
			$("closeFlash").style.display = "none";
			$("lightbox_Flash").style.display = "none";
			unloadFlashPlayerVideo();
		}else{
			new Effect.Fade($("lightbox_Content"), {duration: 0.3, afterFinish:function(){unloadFlashPlayerVideo();}})
		}
		linkNames = document.getElementsByClassName("videoLink");
		/*Turns on the onclick function for the video links (images/text etc)*/
		//for(i = 0; i < linkNames.length; i++){
		//	linkName = linkNames[i].id;
			//Event.observe(linkName ,"click", loadFlashPlayer);
		//}
	}
}

//Unloads the flash video from the page and hides the background and container
function unloadFlashPlayerVideo(){
	var linkNames, i;
	$("lightbox_Content").style.display = "none";
	$("dyBox_overlay").style.display = "none";
	if($("dyBox_overlay")){
		Event.stopObserving($("dyBox_overlay"),"click",closeLightboxContainer);
	}
	if($("closeFlash")){
		Event.stopObserving($("closeFlash"),"click",closeLightboxContainer);
	}
	
	if($("lightbox_Content").hasChildNodes()){
		while($("lightbox_Content").firstChild){
			$("lightbox_Content").removeChild($("lightbox_Content").firstChild);       
		}
	}
}
