
function showBitratePopup(popUpId, event, bitrates, selectedBandwidth, format, playerType, streamingLink) {
	
	loadBitratePopupForm(popUpId, bitrates, selectedBandwidth, format, playerType, streamingLink);
	openBitratePopup(popUpId, event);
}


function getNearestBitrate(bitrates, selectedBandwidth) {
	
	// assumes bitrates are lowest to highest
	for (var i=(bitrates.length - 1); i >= 0; i--) {
		if (selectedBandwidth >= bitrates[i]) {
			return bitrates[i];
		}
	}
	
	return null;
}


function loadBitratePopupForm(popUpId, bitrates, selectedBandwidth, format, playerType, streamingLink) {
	
	var selectedBitrate = getNearestBitrate(bitrates, selectedBandwidth); 
	
	var popUpForm = document.getElementById(popUpId + "Form");
	if (!popUpForm) return;
	var formContent = "";
	
	for (var i=(bitrates.length - 1); i >= 0; i--) {		
		formContent = formContent + '<div><input type="radio" name="bitrate" value="' + bitrates[i] + 
						'" onClick="bitrateSelection(' + bitrates[i] + ',\'' + format + '\',\'' + playerType + '\', \'' + streamingLink + '\')"';
		if (bitrates[i] == selectedBitrate) {
			formContent = formContent + ' checked="true"';
		} 
		formContent = formContent + '/><span>' + bitrates[i] + ' kbps</span></div>'; 
	}
	
	popUpForm.innerHTML = formContent; 
}


function openBitratePopup(popUpId, event){
    var eTarget = event.target ? event.target : event.srcElement;
	
   	brPopUp.show(popUpId, eTarget);
}


function closeBitratePopup(){
    brPopUp.hide();
}


/* ***************
* new popup menu *
*************** */
var brPopUp = {
	
	timer: null,
	activeMenuID: null,

	show: function(popUpId, eTarget) {
		this.reset();		
		this.eTarget = eTarget;
		this.setTargetPos = this.findTargetPos(this.eTarget);
		this.originalTargetParent = this.eTarget.parentNode.id;
		this.activeMenuID = popUpId;
		this.activeMenuSelector = '#' + this.activeMenuID;
		this.jQuerySelector = this.activeMenuSelector + ', #' + this.originalTargetParent;
		this.mnu = document.getElementById ? document.getElementById(popUpId) : null;
		
		if (!this.mnu) return;
		
		$(this.jQuerySelector).bind('mouseout', function() { brPopUp.hide() });
		$(this.jQuerySelector).bind('mouseover', function() { clearTimeout(brPopUp.timer) });
		
		this.position(this.mnu, this.setTargetPos.targetLeftEdge, this.setTargetPos.targetTopEdge);
		
		// Log CM element when user opens brPopUp
		cmCreatePageElementTag("ChangePopup", "UserBitrateDisplay");
	},
	
	
 	//returns targets x,y page coordinates
	findTargetPos: function (obj) {
		this.curleft = this.curtop = 0;
		
		if (obj.offsetParent) {
			do {
				this.curleft += obj.offsetLeft;
				this.curtop += obj.offsetTop;
			} 
			while (obj = obj.offsetParent);
		}
		
		return {targetLeftEdge:this.curleft, targetTopEdge:this.curtop};
	},
	

	hide: function() {
		if (brPopUp.timer) clearTimeout(brPopUp.timer);
		if (this.activeMenuID && document.getElementById) {
		  this.timer = setTimeout("document.getElementById('" + this.activeMenuID + "').style.display = 'none';activeMenu = '';", 3000);
		}
		this.clearPopup();
	},
  
  
	//hides the popup if page is clicked outside the 
	//delivery buttons, bitate buttons or popup
	clearPopup: function() {
		$('body').bind('click', function(event) {
			if (event.target.parentNode.id !== brPopUp.originalTargetParent) {
				while (event.target.id !== 'bitratePopUp'){
					event.target = event.target.parentNode;
					if(event.target.nodeName.toUpperCase() === 'HTML') {
						document.getElementById(brPopUp.activeMenuID).style.display = 'none';
						brPopUp.reset();
						break;				
					}
				}
				event.stopPropagation();
			}
		}); 
	},

	
	//clear timer, resets properties and clears event listeners
	reset: function() {
		clearTimeout(brPopUp.timer);
		brPopUp.activeMenu = '';
		brPopUp.originalTargetParent = '';
		$(brPopUp.jQuerySelector).unbind();		
		$('body').unbind('click')
	},


	position: function(mnu, xPos, yPos) {
		this.mnu.style.left = -9999 + "px";
		this.mnu.style.top = -9999 + "px";
		
		this.timer = setTimeout("document.getElementById('" + this.activeMenuID + "').style.display = 'block'", 40);
		
		this.mnu.style.left = this.adjustXPos(xPos) + 'px';
		this.mnu.style.top = this.adjustYPos(yPos) + 'px';
	},
 
 
  	//adjust popup position depending on relation of button to bottom of window
	adjustYPos: function(yPos) {
		yPos = yPos + this.eTarget.clientHeight;
		this.yOffset = window.pageYOffset ? window.pageYOffset : document.documentElement.scrollTop;;
		this.viewableHeight = window.innerHeight ? window.innerHeight : document.documentElement.offsetHeight;
		this.bottomOfPopUp = yPos + ($(this.activeMenuSelector).height());
		this.bottomOfPage = this.yOffset + this.viewableHeight;
		if($.browser.msie){
			var ieYOffset = 1;
		}
		else{
			var ieYOffset = 2;
		}	
		this.topOfPopUp =  yPos - $(this.activeMenuSelector).height() - $('#'+this.eTarget.id).height() - ieYOffset;
		
		if (this.bottomOfPopUp > this.bottomOfPage) {
			yPos = this.topOfPopUp;
		} 
		
		return yPos;
	},


	//adjust the popup x position 	
	adjustXPos: function(xPos) {
		this.buttonWidthAdj = this.whichButtonClicked();
		this.regexReal = new RegExp(/real/i);
		xPos = xPos - (this.buttonWidthAdj.xPosAdj);
		
		//if realNet play or bitrate buttons in scenes clicked, adjust x position
		if (this.regexReal.test(this.eTarget.id)) {
			$('#' + this.eTarget.id).parents().each( function() {
				if ($(this).attr('class') === 'extFunctSceneResult') {
					var whichButton = brPopUp.whichButtonClicked(xPos);
					
					if (brPopUp.regexPopUp.test(whichButton.thisButton)) {
						xPos = (brPopUp.setTargetPos.targetLeftEdge + $('#' + brPopUp.eTarget.id).width()) - 
							($('#' + brPopUp.activeMenuID).width() + 1);
					}
					else {
						xPos = (brPopUp.setTargetPos.targetLeftEdge + ($('#' + brPopUp.eTarget.id).width() + 
							$('#' + brPopUp.eTarget.id).next().width())) - ($('#' + brPopUp.activeMenuID).width() + 2);
					}
				}
			});	
		}
		
		return xPos;		
	},


	//returns x position of popup and which image in button set was clicked
	whichButtonClicked: function () {
		this.xPosAdjustment = 0;
		this.regexPopUp = new RegExp(/popup/i);
		this.button = 'play';
	
		if (this.regexPopUp.test(this.eTarget.id)) {
			this.xPosAdjustment = $('#' + this.eTarget.id).prev().width();
			this.button = 'popup';
		}
	
		return {xPosAdj: this.xPosAdjustment, thisButton: this.button};
	}
	  
};
/* *******************
* end new popup menu *
******************* */
