//Determine browser
var ie = (navigator.appName.indexOf('Microsoft') != -1) ? true : false;
var ns = (navigator.appName.indexOf('Netscape') != -1) ? true : false;
var ns4 = (ns && document.layers) ? true : false;
var ns6 = (ns && document.getElementById) ? true : false;

// Array and function necessary for ads to show up in netscape
var ads = new Array();
	function ad(site, page, position, size){
		this.site = site;
		this.page = page;
		this.pos = position;
		this.sz = size;
}

//Variables needed for email a photo
var currentEPhoto = 0;
var imageLoc = new Array();
var imageCap = new Array();
var imageCre = new Array();

//Random Number generator
function Random(N) 
	{ return Math.floor( N*Math.random() );	} 
	
//Blank onLoad,init functions
function onLoad() {}
function init(bool) {}
var popupPath = '/popups/';

//Determine if user has registered in the past
function isRegistered() {
	var login = getCookie( "login" );
	if (( login != null ) && ( login != "null" )){
		//User is registered
		return true;
	}
	else {
		//User not registered
		return false;
	}
}

//Redirect to registration
function switchLocation(regLocation) {
	if ((typeof(regLocation) != "undefined") && (regLocation != null))
		window.location = '/' + regLocation + '/' + regLocation + 'registration.html';
	else
		window.location = '/rm/login.jsp?dest=/rm/rmSuccess.html?loc=login';
}

//define secure reg parameters in path
function getMorePathInfo(isPremium, regLocation) {
	var path = '';
	
	if ((typeof(regLocation) != "undefined") && (regLocation != null))
		path += "&regLocation=" + regLocation;
	if(!isPremium)
		path += "&reload=true";
	
	return path;
}


//Premium content gatekeeper (videoWin)
function pcGateKeeper(pcType, linkType, src, clipType, topImage, adSponsor, brandImage, brandImageLink, regLocation, relatedFrag, relatedTitle, curBGColor) {
	
	if(!isRegistered()) {
		//Not reg. Redirect to login
		if(pcType.indexOf("vid") != -1) {
			//Video content, set cookies
			var vidProperties = '<src>' + unescape(src) + '</src>';
			vidProperties += '<cType>' + unescape(clipType) + '</cType>';
			vidProperties += '<tImg>' + unescape(topImage) + '</tImg>';
			vidProperties += '<adSp>' + unescape(adSponsor) + '</adSp>';
			vidProperties += '<brImg>' + unescape(brandImage) + '</brImg>';
			vidProperties += '<brLink>' + unescape(brandImageLink) + '</brLink>';
			
			if ((typeof(relatedFrag) != "undefined") && (relatedFrag != null))
				vidProperties += '<rFrag>' + unescape(relatedFrag) + '</rFrag>';
				
			if ((typeof(relatedTitle) != "undefined") && (relatedTitle != null))
				vidProperties += '<rTitle>' + unescape(relatedTitle) + '</rTitle>';
			
			if ((typeof(curBGColor) != "undefined") && (curBGColor != null))
				vidProperties += '<curBGColor>' + unescape(curBGColor) + '</curBGColor>';
			
			
			var premiumCookie = '<pcType>' + pcType + '</pcType>'
			premiumCookie += '<linkType>' + linkType + '</linkType>';
			premiumCookie += '<loc>' + window.location + '</loc>';
			premiumCookie +=  vidProperties;
			SetCookie("NBA_PC", premiumCookie, null, "/");
		}

		//switchLocation(regLocation);
		/* return false;*/
	}
	return true;
}


//this function is specifically for videoWinSecureWNBA
function pcGateKeeperWNBA(pcType, linkType, src, clipType, topImage, adSponsor, brandImage, brandImageLink, regLocation, relatedFrag, relatedTitle, curBGColor) {
	
		if(pcType.indexOf("vid") != -1) {
			//Video content, set cookies
			var vidProperties = '<src>' + unescape(src) + '</src>';
			vidProperties += '<cType>' + unescape(clipType) + '</cType>';
			vidProperties += '<tImg>' + unescape(topImage) + '</tImg>';
			vidProperties += '<adSp>' + unescape(adSponsor) + '</adSp>';
			vidProperties += '<brImg>' + unescape(brandImage) + '</brImg>';
			vidProperties += '<brLink>' + unescape(brandImageLink) + '</brLink>';
			
			if ((typeof(relatedFrag) != "undefined") && (relatedFrag != null))
				vidProperties += '<rFrag>' + unescape(relatedFrag) + '</rFrag>';
				
			if ((typeof(relatedTitle) != "undefined") && (relatedTitle != null))
				vidProperties += '<rTitle>' + unescape(relatedTitle) + '</rTitle>';
			
			if ((typeof(curBGColor) != "undefined") && (curBGColor != null))
				vidProperties += '<curBGColor>' + unescape(curBGColor) + '</curBGColor>';
			
			
			var premiumCookie = '<pcType>' + pcType + '</pcType>'
			premiumCookie += '<linkType>' + linkType + '</linkType>';
			premiumCookie += '<loc>' + window.location + '</loc>';
			premiumCookie +=  vidProperties;
			SetCookie("NBA_PC", premiumCookie, null, "/");

			//set the hostname as a cookie (i.e. "linuxweb1w", "www.wnba.com")
			//secureVideoWNBA.js needs to know where to redirect after login/register
			var host = document.location.hostname;
			SetCookie("WSV_HOST", host, null, "/");
		}

	return true;
}



function videoWinRel(src, clipType, topImage, adSponsor, brandImage, brandImageLink, relatedFrag, isPremium, regLocation, relatedTitle, curBGColor)
{
	if(isPremium)
		pcGateKeeper('vid', 'videoWinRel', src, clipType, topImage, adSponsor, brandImage, brandImageLink, regLocation, relatedFrag, relatedTitle, curBGColor)
		path = popupPath + "vidPopup_related.html";
	
	
	path += "?clipType=" + clipType + "&src=" + src + "&topImage=" + topImage + "&adSponsor=" + adSponsor + "&relatedFrag=" + relatedFrag + "&relatedTitle=" + relatedTitle + "&curBGColor=" + curBGColor;
	path += getMorePathInfo(isPremium, regLocation);
	
	attrs = 'toolbar=0,menubar=0,scrollbars=0,scrolling=no,resizable=no,width=522';
	brand(462,462,brandImage,brandImageLink);
	gallery=window.open(path,'player',attrs);
	gallery.focus();
}


//need a different function to handle WNBA Secure Video (Related) because of the login cookie on NBA domain
function videoWinRelSecureWNBA(src, clipType, topImage, adSponsor, brandImage, brandImageLink, relatedFrag, isPremium, regLocation, relatedTitle, curBGColor)
{
	if(isPremium)
		pcGateKeeperWNBA('vid', 'videoWinRel', src, clipType, topImage, adSponsor, brandImage, brandImageLink, regLocation, relatedFrag, relatedTitle, curBGColor)
		path = "http://www.nba.com/popups/vidPopup_related_SecureWNBA.html";
	
	
	path += "?clipType=" + clipType + "&src=" + src + "&topImage=" + topImage + "&adSponsor=" + adSponsor + "&relatedFrag=" + relatedFrag + "&relatedTitle=" + relatedTitle + "&curBGColor=" + curBGColor;
	path += getMorePathInfo(isPremium, regLocation);
	
	attrs = 'toolbar=0,menubar=0,scrollbars=0,scrolling=no,resizable=no,width=522';
	brand(472,472,brandImage,brandImageLink);
	gallery=window.open(path,'player',attrs);
	gallery.focus();
}


// Popup video player
function videoWin(src, clipType, topImage, adSponsor, brandImage, brandImageLink, isPremium, regLocation)
{

	if(isPremium)
		pcGateKeeper('vid', 'videoWin', src, clipType, topImage, adSponsor, brandImage, brandImageLink, regLocation)

	path = popupPath + "vidPopup.html";

	path += "?clipType=" + clipType + "&src=" + src + "&topImage=" + topImage + "&adSponsor=" + adSponsor;
	path += getMorePathInfo(isPremium, regLocation);
		
	attrs = 'toolbar=0,menubar=0,scrollbars=0,scrolling=no,resizable=no,width=355';
	brand(462,462,brandImage,brandImageLink);
	gallery=window.open(path,newWinName(),attrs);
	gallery.focus();
}



//need a different function to handle WNBA Secure Video because of the login cookie on NBA domain
function videoWinSecureWNBA(src, clipType, topImage, adSponsor, brandImage, brandImageLink, isPremium, regLocation)
{

	if(isPremium)
		pcGateKeeperWNBA('vid', 'videoWin', src, clipType, topImage, adSponsor, brandImage, brandImageLink, regLocation)

	path = "http://www.nba.com/popups/vidPopupSecureWNBA.html";

	path += "?clipType=" + clipType + "&src=" + src + "&topImage=" + topImage + "&adSponsor=" + adSponsor;
	path += getMorePathInfo(isPremium, regLocation);
		
	attrs = 'toolbar=0,menubar=0,scrollbars=0,scrolling=no,resizable=no,width=355';
	brand(462,462,brandImage,brandImageLink);
	gallery=window.open(path,newWinName(),attrs);
	gallery.focus();
}



//Real media window
function rmWin(src, title, brandImage, brandImageLink, isPremium, regLocation) 
{
	if(isPremium)
		pcGateKeeper('vid', 'rmWin', src, '', '', '', brandImage, brandImageLink, regLocation)

	selectTemplate('RM');
	path += "&src=" + src;
	path += getMorePathInfo(isPremium, regLocation);
	
	attrs = 'toolbar=0,menubar=0,scrollbars=0,scrolling=no,resizable=no,width=355';
	brand(462,462,brandImage,brandImageLink);
	gallery=window.open(path,newWinName(),attrs);
	gallery.focus();
}

//Avi window
function aviWin(src,title,brandImage,brandImageLink, isPremium, regLocation) {
	
	if(isPremium)
		pcGateKeeper('vid', 'aviWin', src, '', '', '', brandImage, brandImageLink, regLocation)

	selectTemplate('AVI');
	path += "&src=" + src + "&title=" +title;
	path += getMorePathInfo(isPremium, regLocation);
	
	attrs = 'toolbar=0,menubar=0,scrollbars=0,scrolling=no,resizable="no",width=355';
	brand(462,462,brandImage,brandImageLink);
	gallery=window.open(path,newWinName(),attrs);
	gallery.focus();
}

//Quicktime movie window
function movWin(src,title,brandImage,brandImageLink, isPremium, regLocation) {
	
	if(isPremium)
		pcGateKeeper('vid', 'movWin', src, '', '', '', brandImage, brandImageLink, regLocation)
	
	selectTemplate('MOV');
	path += "&src=" + src + "&title=" +title;
	path += getMorePathInfo(isPremium, regLocation);
	
	attrs = 'toolbar=0,menubar=0,scrollbars=0,scrolling=no,resizable="no",width=355';
	brand(462,462,brandImage,brandImageLink);
	gallery=window.open(path,newWinName(),attrs);
	gallery.focus();
}

//Popup audio window
function audioWin(src,clipType,topImage,headshot,headshotLink,adSponsor,brandImage,brandImageLink)
{
	path = popupPath + "audPopup.html";
	path += "?clipType=" + clipType + "&src=" + src + "&topImage=" + topImage + "&headshot=" + headshot + "&headshotLink=" + headshotLink + "&adSponsor=" + adSponsor;
	attrs = 'toolbar=0,menubar=0,scrollbars=0,scrolling=no,resizable=no,width=355';
	brand(268,268,brandImage,brandImageLink);
	gallery=window.open(path,newWinName(),attrs);
	gallery.focus();
}

//Real audio window
function raWin(src,title,brandImage,brandImageLink, isPremium, regLocation) {
	selectTemplate('RA');
	path += "&src=" + src + "&title=" +title;
	attrs = 'toolbar=0,menubar=0,scrollbars=0,scrolling=no,resizable="no",width=355';
	brand(427,347,brandImage,brandImageLink);
	gallery=window.open(path,newWinName(),attrs);
	gallery.focus();
}

//Generic Open-Window function
function open_window_ex(url, title, height, width, options) {
    var path = url;
    if( url.search(/^http:/) == -1 ) {
        path = "/popups/" + url;
    } else {
        var nbaDomain = new RegExp("/^http:\/\/www.nba.com\/([.*]*)/");
        var result = url.match(nbaDomain);
        if( result != null ) {
            path = "/popups/" + result[1];
        } else {
            path = url;
        }
    }
    var opts = '';
	if(options == null || options == "null")
		opts = 'toolbar=0, location=0, directories=0, status=0, menubar=0, scrollbars=yes, resizable=yes';
    else
		opts = options;
		
	opts += ',width=' + width;
    opts += ',height=' + height;
    mywin=window.open(path,title,opts);
}

//Reason: Real player on ie will explode if re-opened in same window
function newWinName() {
	return('vidWin' +(new Date).valueOf());
}

function brand(bSize,noBSize,brandImage,brandImageLink) {
   if (brandImage != '') {
       path += "&brandImage="+brandImage;
       
	   if (brandImageLink != '')
	   		path += "&brandImageLink="+brandImageLink;
       
	   attrs += ",height="+bSize;
   }
   else {
       attrs += ",height="+noBSize;
   }
}

function selectTemplate(clipType) {
	if((clipType == 'RA') || (clipType=='RM'))
		path = popupPath + 'vidPopup_rn.html'
	else
		path = popupPath + 'vidPopup_ms.html'
	path+= '?clipType=' + clipType;
}
		
//Cookie Functions
function getCookieVal (offset) {
  var endstr = document.cookie.indexOf (";", offset);
  if (endstr == -1)
    endstr = document.cookie.length;
  return unescape(document.cookie.substring(offset, endstr));
}

function getCookie (name) {
  var arg = name + "=";
  var alen = arg.length;
  var clen = document.cookie.length;
  var i = 0;
  while (i < clen) {
    var j = i + alen;
    if (document.cookie.substring(i, j) == arg)
      return getCookieVal (j);
    i = document.cookie.indexOf(" ", i) + 1;
    if (i == 0) break; 
  }
  return null;
}

function SetCookie (name,value,expires,path,domain,secure) {
  document.cookie = name + "=" + escape (value) +
    ((expires) ? "; expires=" + expires.toGMTString() : "") +
    ((path) ? "; path=" + path : "") +
    ((domain) ? "; domain=" + domain : "") +
    ((secure) ? "; secure" : "");
}

function openBroadBand(src,type){
tokenPath = "/broadband/Connection.jsp?dest=" + src
	if(type=='audio'){
		window.open(tokenPath,"BroadbandWin", "width=510,height=377,location=no,toolbar=no,menubar=no,scrollbars=no,resizable=no");
	}else{
		window.open(tokenPath,"BroadbandWin", "width=652,height=379,location=no,toolbar=no,menubar=no,scrollbars=no,resizable=no");
	}
}
//global drop down nav
function ddTeamLinks(ddID,ddHeight,ddWidth){
	document.getElementById(ddID).style.height = ddHeight;
	document.getElementById(ddID).style.width = ddWidth;
}
function ddTeamClose(ddID){
	parent.document.getElementById(ddID).style.height = ddHeight;
	parent.document.getElementById(ddID).style.width = 50;
}
function ddTeamCloseWide(ddID){
	parent.document.getElementById(ddID).style.height = ddHeight;
	parent.document.getElementById(ddID).style.width = 70;
}
function getURL(teamURL){location.href=teamURL;}