/*-------------------------	*/
/*	Video lightbox			*/
/*	V2.0.2 by Chris Knight	*/
/*	29/05/09				*/
/*------------------------- */
/* Used to display content in the style of lightbox - fade in and out - uses scriptaculous, prototype and effects libraries*/

//Define global variables
var sitePageWidth = 974; //(current wide site width)
var closeButtonList = new Array();
var closeButtonListPos = 0;
var setAppCloseOptions = false;
var flashVersion = 6;

function addToCloseButtonList(closeItem){
	closeButtonList[closeButtonListPos] = closeItem;
	closeButtonListPos = closeButtonListPos + 1;
}
 

function getDyboxClassLinks(divLocation){
	return $(divLocation).select(".addDyBoxLink");	
}

function setUpDyBoxLinks(dyBoxObjectArray){
    if ($$(".addDyBoxLink").length > 0) {
		var flashNeedDefined = false;
		textHide(dyBoxObjectArray);
		for(var i =0; i < dyBoxObjectArray.length; i++){
			if(dyBoxObjectArray[i].options.compulsory.mediaType.toLowerCase() == 'video'){
				flashNeedDefined = hasFlashVersion(flashVersion);
			}else{
				flashNeedDefined = true;
			}
			
			if(flashNeedDefined == true){
				dyBoxObjectArray[i].createLink();
			}
		}
	}
}

function textHide(dyBoxObjectArray){
	var i;

	for(i = 0; i < dyBoxObjectArray.length; i++){
		var linkID = dyBoxObjectArray[i].options.compulsory.originalLinkID;
		if($(linkID).className.indexOf("hideText_DyBox") != -1){
			$(linkID).className = $(linkID).className + " hide hidden";
		}
	}
}

function osType(){
	var os = navigator.platform;

	os = os.toLowerCase();
	if(os.indexOf("mac") != -1){
		os = "mac";
	}else if(os.indexOf("linux") != -1){
		os = "linux";	
	}else{
		os = "win";	
	}
	return os;
}

function identifyBrowser() {
	var agent = navigator.userAgent.toLowerCase();
	if (typeof navigator.vendor != "undefined" && navigator.vendor == "KDE" && typeof window.sidebar != "undefined")  {
		return "kde";
	}else if (typeof window.opera != "undefined"){
		var version = parseFloat(agent.replace(/.*opera[\/ ]([^ $]+).*/, "$1"));
	    if (version >= 7){
			return "opera7";
		}else if (version >= 5){
			return "opera5";
		}
	    return false;
	}else if (typeof document.all != "undefined"){
		if (typeof document.getElementById != "undefined"){
			var browser = agent.replace(/.*ms(ie[\/ ][^ $]+).*/, "$1").replace(/ /, "");
			if (typeof document.uniqueID != "undefined"){
				if (browser.indexOf("5.5") != -1){
					return browser.replace(/(.*5\.5).*/, "$1");
				}else{
					return browser.replace(/(.*)\..*/, "$1");
				}
      		}else{
				return "ie5mac";
			}
		}
		return false;
	}else if (typeof document.getElementById != "undefined")  {
		if( window.devicePixelRatio && window.getMatchedCSSRules && !window.Opera){
			return "safari3";
		}else if (navigator.vendor.indexOf("Apple Computer, Inc.") != -1){
			if (typeof window.XMLHttpRequest != "undefined"){
				return "safari1.2";
			}
			return "safari1";
		}else if (agent.indexOf("gecko") != -1){
			return "mozilla";
		}
	}
	return false;
}

function getBodyWidth(){
	var bodyWidth;
	if (document.body && typeof document.body.clientWidth!='undefined') {
		bodyWidth = document.body.clientWidth;
	}else{
		bodyWidth = sitePageWidth + "px";	
	}
	return bodyWidth;
}
	
/*Creates a text holder object*/
function textPlayer(width, height){
	this.width = width;
	this.height = height;
	this.pageWidth = getBodyWidth();
}

function buildContainers(dyBoxObject){
	if(!$("dyBox_overlay")){
		createDyBoxOverlay();
		createBackground(dyBoxObject);
	}else{
		defineBackgroundDimensions();
	}
	
	if(!$("lightbox_Content")){
		createContentOverlay(dyBoxObject);
	}else{
		this.putCenter("lightbox_Content", dyBoxObject);	
	}
	
	if(!$("lightbox_Flash")){
		createContentContainer(dyBoxObject);
		setTimeout(function(){createAppCloseOptions(dyBoxObject)}, 2000)
	}	
	
	Event.observe(this,"resize",function(){this.putCenter("lightbox_Content", dyBoxObject);})
	Event.observe(window,"resize",function(){this.putCenter("lightbox_Content", dyBoxObject);})
	Event.observe(window,"scroll",function(){defineBackgroundDimensions();})
}

function createDyBoxOverlay(){
	var bodyContainer = document.getElementsByTagName("body")[0];
	
	var dyBoxContainer = document.createElement("div");
	dyBoxContainer.setAttribute("id", "dyBox_overlay");	
	dyBoxContainer.setAttribute("class", "dyBox_AllowClose clear");
	/*Allows dyBox to display within the entire browser screen rather than just being confined to the body tag size*/
	dyBoxContainer.style.display = "none";
	bodyContainer.appendChild(dyBoxContainer);	
}

function defineBackgroundDimensions(){
	var overlay;
	var win = window.windowDimensions();
	
	overlay = $("dyBox_overlay");
	
	if(window.innerHeight) {
		if(window.scrollMaxY){
			overlay.style.height = window.innerHeight + window.scrollMaxY + "px";
		}else if( typeof( window.pageYOffset ) == 'number' ) {
			if(window.pageYOffset > 0){
				overlay.style.height = window.innerHeight + window.pageYOffset + "px";
			}else{
				overlay.style.height = window.innerHeight + "px";
			}
		}else{
			overlay.style.height = window.innerHeight + document.body.scrollTop + "px";
		}	
		
		if(document.body.offsetWidth){
			if(siteType == "wide"){
				overlay.style.width = "100%";
			}else if(document.body.offsetWidth > win[0]){
				overlay.style.width = document.body.offsetWidth + "px";
			}else{
				overlay.style.width = win[0] + "px";
			}
		}
	}else if(document.body.scrollHeight){
	    if (document.body.scrollHeight >= win[1]) {
			overlay.style.height = document.body.scrollHeight + "px";
		}else{
			overlay.style.height = win[1] + "px";
		}
		
		if(document.body.scrollWidth > win[0]){
			overlay.style.width = document.body.scrollWidth + "px";
		}else{
			overlay.style.width = win[0] + "px";
		}
		
	}else if(document.body.offsetHeight){
		if (document.body.offsetHeight >= win[1]) {
			overlay.style.height = document.body.offsetHeight + "px";
		}else{
			overlay.style.height = win[1] + "px";
		}
		
		if(document.body.offsetWidth > win[0]){
			overlay.style.width = document.body.offsetWidth + "px";
		}else{
			overlay.style.width = win[0] + "px";
		}
	}else{
		overlay.style.height = win[1] + "px";
		overlay.style.width = win[0] + "px";
	}
}

/*Creates the transparent background when a dyBox link is pressed*/
function createBackground(dyBoxObject){
	var win = window.windowDimensions();	
	overlay = $("dyBox_overlay");
	defineBackgroundDimensions();
	overlay.style.opacity = "0.9";
	overlay.style.filter = "alpha(opacity=90)";
    
	var loadingGIF = document.createElement("img");
	loadingGIF.id = "loadingGIF";
	loadingGIF.src="/images/dyBox/Loading.gif";
	overlay.appendChild(loadingGIF);
}

/*Create the container to hold the Flash player / text to be displayed*/
function createContentOverlay(dyBoxObject){
	var containerContent, bodyContainer;
	
	containerContent = document.createElement("div");
	containerContent.setAttribute("id", "lightbox_Content");
	if(dyBoxObject.dyboxObject.options.compulsory.mediaType.toLowerCase() == "text") {
	    containerContent.style.overflow = "hidden";
	}
	containerContent.style.display = "none";

	bodyContainer = document.getElementsByTagName("body")[0];
	bodyContainer.appendChild(containerContent);
	this.putCenter("lightbox_Content", dyBoxObject);
}

function createContentContainer(dyBoxObject){
	var container, contentContainer, closeButton, closeButtonTextNode;
	
	/*Holds the Flash video / textbox*/
	contentContainer = document.createElement("div");
	contentContainer.setAttribute("id", "lightbox_Flash");
	contentContainer.setAttribute("class", "clear");
	contentContainer.style.backgroundColor = dyBoxObject.dyboxObject.options.containerStyle.backgroundColour;
	setContainerPropertiesDependingOnContentType(dyBoxObject, contentContainer);
	
	closeButton = document.createElement("a");
	closeButton.setAttribute("id", "closeFlash");
	closeButtonTextNode = document.createTextNode(closeButtonText + " - X");
	closeButton.appendChild(closeButtonTextNode);
	$("lightbox_Content").appendChild(closeButton);
	$("lightbox_Content").appendChild(contentContainer);
	$("lightbox_Flash").style.clear = "both";
    
	container = document.createElement("div");
	container.setAttribute("id", "lightbox_Container");
	container.setAttribute("class", "clear");
	$("lightbox_Flash").appendChild(container);
}

/*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 shown. This is because it was showing clearly through the black background, rather than being faded like the rest of the content*/
function displayContent(dyBoxObject, contentContainer){
	hideFlashReplacement();
	$("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"){showDyBoxContent(dyBoxObject, contentContainer); $("loadingGIF").style.display = "none";}}});
}

/*Shows the video / textbox by fading it onto the screen*/
function showDyBoxContent(dyBoxObject, contentContainer){
	/*Do not remove this - css is here to fix IE6 bug where it doesn't float to the right correctly (either goes to far to the right or not enough - Seems to not be able to work out the width of the container if put into css file)*/

	$("lightbox_Flash").style.display = "block";
	getRelevantContent(dyBoxObject, contentContainer);
	putCenter("lightbox_Content", dyBoxObject);
	new Effect.Appear($("lightbox_Content"), { duration: 0.5, from: 0.0, to: 1.0, afterFinish: function() {$("lightbox_Container").style.display = "block"; setTimeout(function(){getContentSize(dyBoxObject)}, 400);}});
}

function createAppCloseOptions(dyBoxObject){
	var backgroundObject = new dyBoxButton({properties:{ id: "dyBox_overlay", buttonType: "close", dyBox: dyBoxObject}});
	backgroundObject.buttonLinkClicked();
	
	var closeButtonObject = new dyBoxButton({properties:{text: closeButtonText + " - X", href: "javascript:void(0)", id: "closeFlash", itemClass: "dyBox_AllowClose", buttonType: "close", dyBox: dyBoxObject}});
	closeButtonObject.buttonLinkClicked();
	//$("closeFlash").style.display = "block";
	
	if(setAppCloseOptions == false){
		addToCloseButtonList(backgroundObject);
		addToCloseButtonList(closeButtonObject);
		setAppCloseOptions = true;
	}
	//backgroundObject.buttonBind = backgroundObject.buttonClick.bindAsEventListener(backgroundObject);
	//Event.observe(backgroundObject.options.properties.id, 'click', backgroundObject.buttonBind);
	
	//var closeButtonObject = new dyBoxButton({properties:{text: closeButtonText + " - X", href: "javascript:void(0)", id: "closeFlash", itemClass: "dyBox_AllowClose", buttonType: "close", dyBox: dyBoxObject}});
	//closeButtonObject.buttonBind = closeButtonObject.buttonClick.bindAsEventListener(closeButtonObject);
	//Event.observe($("closeFlash"), 'click', closeButtonObject.buttonBind);
}

function getContentSize(dyBoxObject){
	if($("lightbox_Container")){
		if($("lightbox_Container").offsetHeight > 0){
			if(dyBoxObject.dyboxObject.options.containerDimensions.height > $("lightbox_Container").offsetHeight){
				var percentDiff = Math.round((parseInt($("lightbox_Container").offsetHeight) / parseInt(dyBoxObject.dyboxObject.options.containerDimensions.height)) * 100) + 1;
				new Effect.Scale('lightbox_Flash', percentDiff, {scaleX: false, scaleContent: false, scaleMode:{originalHeight:dyBoxObject.dyboxObject.options.containerDimensions.height}});
				//$('lightbox_Flash').style.display = "block";
			}
		}
	}
}
/*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 noFlashContainer, noFlashTextHeaderTag, noFlashTextHeaderValue, noFlashText1Tag, noFlashText1Value, noFlashText2Tag, noFlashText2Value;
	
	noFlashContainer = document.createElement("div");
	noFlashContainer.setAttribute("id","dyBox_noFlashText");
	
	noFlashTextHeaderTag = document.createElement("h1");
	noFlashTextHeaderValue = document.createTextNode(noFlashTextHeader);
	noFlashTextHeaderTag.appendChild(noFlashTextHeaderValue);
	
	noFlashText1Tag = document.createElement("p");
	noFlashText1Value = document.createTextNode(noFlashText1);
	noFlashText1Tag.appendChild(noFlashText1Value);
	
	noFlashContainer.appendChild(noFlashTextHeaderTag);
	noFlashContainer.appendChild(noFlashText1Tag);
	
	if(noFlashText2.length > 0){
		noFlashText2Tag = document.createElement("p");
		noFlashText2Value = document.createTextNode(noFlashText2);
		noFlashText2Tag.appendChild(noFlashText2Value);
		noFlashContainer.appendChild(noFlashText2Tag);
	}
	$("lightbox_Container").appendChild(noFlashContainer);
}

/*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);
}

//Places an item in the center of the screen
function putCenter(item, dyBoxObject) {
	var xy, win, scrollValue, playerPadding, topPos, leftPos, leftOffset, pageWidth, pageHeight;
	
	win = window.windowDimensions();
	item = $(item);
	
	/*Alternative position needed for IE6 due to not accepting position:fixed and recognised hacks not working (overflow:auto and body height:100%)*/
	if(identifyBrowser() == "ie6"){
		item.style.left = (document.body.clientWidth - dyBoxObject.dyboxObject.options.containerDimensions.width)/2 + "px";	
		item.style.top = ((win[1] / 2) + document.documentElement.scrollTop) - ((dyBoxObject.dyboxObject.options.containerDimensions.height + 65) / 2) + "px";	
		$("loadingGIF").style.top = (win[1] / 2)  + document.documentElement.scrollTop + "px";
	}else{
		item.style.left = (win[0] / 2) - ((dyBoxObject.dyboxObject.options.containerDimensions.width + 50) / 2) + "px";	
		item.style.top = (win[1] / 2) - ((dyBoxObject.dyboxObject.options.containerDimensions.height + 65) / 2) + "px";	
		if(identifyBrowser() == "ie7"){
			$("loadingGIF").style.top = (win[1] / 2) + document.documentElement.scrollTop + "px";
		}else{
			$("loadingGIF").style.top = (win[1] / 2) + "px";
		}
	}
	$("loadingGIF").style.left = (win[0] / 2) + "px";
	item.style.position = "fixed";	
	$("loadingGIF").style.position = "fixed";
	//Resize the black background to take account of any changes in the browser window size
	updateBackgroundSize();
}

/*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 updateBackgroundSize(){
	var win = window.windowDimensions();
	
	if(siteType == "wide"){
		$("dyBox_overlay").style.width = "100%";
	}else if(win[0] < sitePageWidth){
		$("dyBox_overlay").style.left = "0px";
		$("dyBox_overlay").style.width = sitePageWidth + "px";
	}else{
		//Get the difference between the current background size and the width of the main site content (wide site width), divide by two (difference either side of the background vs site content, or vice versa which ever is smaller), + 10 to avoid a scrollbar appearing.
		$("dyBox_overlay").style.left = ((sitePageWidth - win[0]) / 2) - 10 + "px";
		$("dyBox_overlay").style.width = win[0] + "px";
	}
	
}

function windowDimensions() {
	var x, y;
	 if (typeof window.innerWidth != 'undefined')
	 {
		  x = window.innerWidth,
		  y = window.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(dyBoxObject){
	var linkNames = "";
	var linkName = "";
	var i;
	$("lightbox_Container").style.display = "none";
	$("loadingGIF").style.display = "block";
	var lightBoxContainer = $("lightbox_Container");
	new Effect.Fade($("lightbox_Content"), {duration: 0.4, afterFinish:function(){unloadContent($("lightbox_Content")); dyBoxObject.enableClick(); new Effect.Fade($("dyBox_overlay"), {duration: 1.0, afterFinish:function(){swithOnFlash(); $("loadingGIF").style.display = "none";}})}});
}

//Unloads the flash video from the page and hides the background and container
function unloadContent(idName){
	var linkNames, i;
	
	if($(idName).hasChildNodes()){
		while($(idName).firstChild){
			$(idName).removeChild($(idName).firstChild);       
		}
	}
}