/*=========================== Start - Flash player functions ==================================================*/
var flashPlayer = Class.create();
flashPlayer.prototype = {
	initialize : function(dyBoxObject){
		this.width = dyBoxObject.dyboxObject.options.containerDimensions.width;
		this.height = dyBoxObject.dyboxObject.options.containerDimensions.height;
		this.vidWidth = dyBoxObject.dyboxObject.options.containerDimensions.width;
		this.uiEnabled = dyBoxObject.dyboxObject.options.videoPlayerConfig.uiEnabled;
		//Display video controls - If so then add 26 pixels to player height
		if(this.uiEnabled == true){
			this.playerHeight = parseInt(dyBoxObject.dyboxObject.options.containerDimensions.height) + 26;
		}else{
			this.playerHeight = dyBoxObject.dyboxObject.options.containerDimensions.height;
		}
		this.vidHeight = dyBoxObject.dyboxObject.options.containerDimensions.height;
		this.fullscreen = dyBoxObject.dyboxObject.options.videoPlayerConfig.allowFullScreen;
		this.skipTime = dyBoxObject.dyboxObject.options.videoPlayerConfig.skipTime;
		this.allowscriptaccess = dyBoxObject.dyboxObject.options.videoPlayerConfig.allowscriptaccess;
		this.allowFullScreen = dyBoxObject.dyboxObject.options.videoPlayerConfig.allowFullScreen;
		this.uiBarColour = dyBoxObject.dyboxObject.options.videoPlayerConfig.uiBarColour;
		this.uiBarBufferColor = dyBoxObject.dyboxObject.options.videoPlayerConfig.uiBarBufferColor;
		
		this.bufferTime = dyBoxObject.dyboxObject.options.videoPlayerConfig.bufferTime;
		this.pageWidth = getBodyWidth();
		this.videoLocation = dyBoxObject.dyboxObject.options.videoDetails.videoLocation;
	},
	
	defineFlashPlayer : function(){
		var so = new SWFObject(mediaServerLocation + "/swf/video/flvPlayer.swf", "flv", this.vidWidth, this.playerHeight, "8", "#000000");
		so.addVariable("viewHeight", this.vidHeight);
		so.addVariable("viewWidth", this.vidWidth);
		so.addVariable("fullscreen", this.fullscreen);
		so.addVariable("skipTime",this.skipTime);
		so.addVariable("uiBarTimeColor", this.uiBarColour);
		so.addVariable("uiBarBufferColor", this.uiBarBufferColor);
		so.addVariable("uiEnabled", this.uiEnabled);
		so.addVariable("buffer", this.bufferTime);
		so.addParam("menu", "false"); //Disables the menu on the flash video when you right click it
		so.addParam("allowscriptaccess",this.allowscriptaccess);
		so.addParam("allowFullScreen",this.allowFullScreen);
		so.addVariable("file", this.videoLocation);
		return so;	
	},
	
	defineYouTubePlayer : function(){
		var objectContainer, paramContainer;
		
		objectContainer = document.createElement("object");
		objectContainer.setAttribute("type", "objectContainer");
		objectContainer.setAttribute("data", this.videoLocation);
		objectContainer.setAttribute("width", this.width);
		objectContainer.setAttribute("height", this.height);
		
		paramContainer = document.createElement("param");
		paramContainer.setAttribute("name", "objectContainer");
		paramContainer.setAttribute("value", this.videoLocation);
		
		objectContainer.appendChild(paramContainer);
		return objectContainer;
	},
	
	defineSWFPlayer : function(){
		var so = new SWFObject(this.videoLocation, "swf", this.vidWidth, this.playerHeight, "8", "#000000");
		so.addVariable("viewHeight", this.vidHeight);
		so.addVariable("viewWidth", this.vidWidth);
		so.addParam("menu", "false"); //Disables the menu on the flash video when you right click it
		return so;	
	},
	
	disableClick : function(){
		
	}
};
/*=========================== End - Flash player functions ========================================*/

/*Loads the flash player with the relevant video / text*/
function loadFlashPlayerVideo(dyBoxObject){
	var currentFlashPlayer, win, flashPlayerObject;
	
	dyBoxObject.disableClick();
	
	win = window.windowDimensions();		
	/*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(dyBoxObject);
	
	if(dyBoxObject.dyboxObject.options.videoDetails.videoType.toLowerCase() == "swf"){
		flashPlayerObject = new flashPlayer(dyBoxObject);
		currentFlashPlayer = flashPlayerObject.defineSWFPlayer();
	}else if(dyBoxObject.dyboxObject.options.videoDetails.videoType.toLowerCase() == "youtube"){
		flashPlayerObject = new flashPlayer(dyBoxObject);
		currentFlashPlayer = flashPlayerObject.defineYouTubePlayer();
	}else{
		flashPlayerObject = new flashPlayer(dyBoxObject);
		currentFlashPlayer = flashPlayerObject.defineFlashPlayer();
	}
	if(videoJukeBox.length > 0) {
	    createJukeBoxCentre();
	    this.putCenter("lightbox_Content", dyBoxObject);
	}
	displayContent(dyBoxObject, currentFlashPlayer);
}

/*Video Jukebox code - allows you to add video links to the bottom of the flash player - the user can then change the video that they are using, without leaving the lightbox*/

var viewJukeBox = false; //Is the jukebox currently open or closed

function addToVideoJukeBox(dyBoxVideoObjectArray, clearJukeBoxArray) {
    var numberOfVideos, i;
	if(clearJukeBoxArray == undefined) {
        clearJukeBoxArray = false;
    }
    if(clearJukeBoxArray == true) {
        clearJukeBox();
    }
    numberOfVideos = videoJukeBox.length;
    for(i = 0;i < dyBoxVideoObjectArray.length; i++) {
        videoJukeBox[numberOfVideos + i] = dyBoxVideoObjectArray[i];
    }
}

function clearJukeBox() {
    videoJukeBox.length = 0;
}

var jukeBoxItem = Class.create();
jukeBoxItem.prototype = {
    imageSRC: "",
    videoLinkText: "",
    videoLocation: "",
    videoType: "",
    jukeBoxID: "",
    linkBind: "",
    dyboxObject: "",
    currentFlashPlayer: "",

    initialize: function(dyBoxObject, itemNo) {
        this.imageSRC = dyBoxObject.options.imageLink.imageSRC;
        this.videoLinkText = dyBoxObject.options.imageLink.linkText;
        this.videoLocation = dyBoxObject.options.videoDetails.videoLocation;
        this.videoType = dyBoxObject.options.videoDetails.videoType;
        this.jukeBoxID = itemNo;
        this.linkBind = "";
		this.linkBindRollover = "";
        this.dyboxObject = dyBoxObject;
        this.currentFlashPlayer = new flashPlayer(this);
    },

    createJukeBoxItem: function() {
        var jukeBoxItem, jukeBoxVideoContainer, jukeBoxItem_Image, jukeBoxItem_PTag, jukeBoxItem_Text, linkBind;

        jukeBoxVideoContainer = document.createElement("div");
        jukeBoxVideoContainer.setAttribute("class", "lightbox_JukeBoxItem");

        jukeBoxItem = document.createElement("a");
        jukeBoxItem.setAttribute("href", "javascript:void(0);");
        jukeBoxItem.setAttribute("id", "lightbox_JukeboxItem_" + this.jukeBoxID);

        jukeBoxItem_Image = document.createElement("img");
        jukeBoxItem_Image.setAttribute("src", this.imageSRC);
        jukeBoxItem.appendChild(jukeBoxItem_Image);

        jukeBoxItem_PTag = document.createElement("p");
        jukeBoxItem_Text = document.createTextNode(this.videoLinkText);
        jukeBoxItem_PTag.appendChild(jukeBoxItem_Text);
        jukeBoxItem.appendChild(jukeBoxItem_PTag);

        jukeBoxVideoContainer.appendChild(jukeBoxItem);
        
        return jukeBoxVideoContainer;
    },

    jukeBoxItemClicked: function() {
        var currentFlashPlayer, flashPlayerObject;
		$("lightbox_Flash").style.width = this.dyboxObject.options.containerDimensions.width + "px";
		if(this.dyboxObject.options.videoPlayerConfig.uiEnabled == true){
			$("lightbox_Flash").style.height = this.dyboxObject.options.containerDimensions.height + 26 + "px";
		}else{
			$("lightbox_Flash").style.height = this.dyboxObject.options.containerDimensions.height + "px";
		}
		if(viewJukeBox == false){
			$("lightbox_Jukebox").style.width = setJukeBoxWidth("startingPos");
		}else{
			$("lightbox_Jukebox").style.width = setJukeBoxWidth("full");
		}
		if(this.videoType.toLowerCase() == "youtube"){
			unloadContent("lightbox_Container");
			currentFlashPlayer = this.currentFlashPlayer.defineYouTubePlayer();
			$("lightbox_Container").appendChild(currentFlashPlayer);
		}else{
			if(this.videoType.toLowerCase() == "swf") {
				currentFlashPlayer = this.currentFlashPlayer.defineSWFPlayer();
			}else{
				currentFlashPlayer = this.currentFlashPlayer.defineFlashPlayer();
			}
			currentFlashPlayer.write("lightbox_Container");
		}
    },
	
	jukeBoxItemRollover: function() {
        $("lightbox_JukeboxItem_" + this.jukeBoxID).setAttribute("src", this.imageSRC.replace(".jpg","_Alt.jpg"));
    }
	
};

function jukeBoxToggle(){
	var lightboxContainerWidth, jukeBoxOriginalWidth;
	
	lightboxContainerWidth = getLightBoxContainerWidth();
	
	if(viewJukeBox == false){
		new Effect.Morph('lightbox_Jukebox', {style:"width:" + lightboxContainerWidth + "px", queue:{position:'end', scope:"jukeBoxToggle"}});
		Effect.SlideDown('lightbox_JukeboxContent', {duration:1.0, scaleContent:false, scaleTo:100, queue:{position:'end', scope:"jukeBoxToggle"}});
		viewJukeBox = true;
		$("lightbox_JukeboxToggle").innerHTML = jukeBoxText_Close;
	}else{
		
		Effect.SlideUp('lightbox_JukeboxContent', {duration:1.0, scaleContent:false, scaleFrom:100, queue:{position:'end', scope:"jukeBoxToggle"}});
		new Effect.Morph('lightbox_Jukebox', {style:"width:" + setJukeBoxWidth("startingPos"), queue:{position:'end', scope:"jukeBoxToggle"}});
		viewJukeBox = false;
		$("lightbox_JukeboxToggle").innerHTML = jukeBoxText_Open;
	}
}

function setJukeBoxWidth(jukeBoxWindowStatus){
	var width;
	lightboxContainerWidth = getLightBoxContainerWidth();
	
	if(jukeBoxWindowStatus == "full"){
		width = lightboxContainerWidth + "px";	
	}else{
		if(jukeBoxItemWidth > lightboxContainerWidth){
			width = lightboxContainerWidth + "px";
		}else {
			width = jukeBoxItemWidth + "px";
		}
	}
	return width;
}

function getLightBoxContainerWidth(){
	var lightboxContainerWidth = parseInt(($("lightbox_Flash").style.width).replace("px",""));
	return lightboxContainerWidth ;
}

function createJukeBoxCentre() {
    var jukeBox, jukeBoxContainer, jukeBoxContainerLink, jukeBoxContainerLinkText, i, jukeBoxItemObject, jukeBoxVideoContainer, jukeBoxInfoTextContainer, jukeBoxInfoText, jukeBoxContainerWidthCounter, jukeBoxAdditionalDiv;

	jukeBoxContainerWidthCounter = 0;
	
    jukeBox = document.createElement("div");
    jukeBox.setAttribute("id", "lightbox_Jukebox");
	jukeBox.setAttribute("class", "clear");
	
	if(viewJukeBox == false){
		jukeBox.style.width = setJukeBoxWidth("startingPos");
	}else{
		jukeBox.style.width = setJukeBoxWidth("full");
	}
	
	jukeBoxContainer = document.createElement("div");
    jukeBoxContainer.setAttribute("id", "lightbox_JukeboxContent");
	jukeBoxContainer.setAttribute("class", "clear");
	
	if(viewJukeBox == false){
		jukeBoxContainer.style.display = "none";
	}
	jukeBoxContainer.setAttribute("class", "clear");
	
	//Need additional div for Effect.SlideUp/Down to work correctly - According to scriptaculous API
	jukeBoxAdditionalDiv = document.createElement("div");
	jukeBoxAdditionalDiv.setAttribute("id", "lightbox_JukeboxContentContainer");
	jukeBoxAdditionalDiv.setAttribute("class", "clear");
	jukeBoxContainer.appendChild(jukeBoxAdditionalDiv);
	jukeBox.appendChild(jukeBoxContainer);
	
    $("lightbox_Content").appendChild(jukeBox);
  
    for(i = 0;i < videoJukeBox.length;i++) {
		jukeBoxContainerWidthCounter+= parseInt(jukeBoxItemWidth);
		
		if(jukeBoxContainerWidthCounter < getLightBoxContainerWidth()){
			jukeBoxItemObject = new jukeBoxItem(videoJukeBox[i], i);
			jukeBoxVideoContainer = jukeBoxItemObject.createJukeBoxItem();
			jukeBoxAdditionalDiv.appendChild(jukeBoxVideoContainer);
			jukeBoxItemObject.linkBind = jukeBoxItemObject.jukeBoxItemClicked.bindAsEventListener(jukeBoxItemObject);
			Event.observe("lightbox_JukeboxItem_" + i, 'click', jukeBoxItemObject.linkBind);
		}
    }
	
	jukeBoxContainerLink = document.createElement("a");
	jukeBoxContainerLink.setAttribute("id", "lightbox_JukeboxToggle");
	if(viewJukeBox == false){
		jukeBoxContainerLinkText = document.createTextNode(jukeBoxText_Open); 
	}else{
		jukeBoxContainerLinkText = document.createTextNode(jukeBoxText_Close);
	}
	jukeBoxContainerLink.appendChild(jukeBoxContainerLinkText);
	jukeBox.appendChild(jukeBoxContainerLink);
	
	Event.observe("lightbox_JukeboxToggle", 'click', jukeBoxToggle);
}