// new window launch
// usage: <a href="[url]" onclick="popWin(this,'[window_name]','[width]','[height]' {,'_optional_options'}); return false;" ... >
var l,t,z;
function popWin(url,n,w,h,o) {
	if (w>screen.availWidth-12) w=screen.availWidth-12;
	if (h>screen.availHeight-48) h=screen.availHeight-48;
	l=(screen.availWidth-w-12)/2;
	t=(screen.availHeight-h-48)/2;
	z=window.open(url,n,'width='+w+',height='+h+',left='+l+',top='+t+','+o);
}

// sniff
var isWin = window.navigator.platform.toLowerCase().indexOf('win') != -1 ? 1:0;
var isMac = window.navigator.platform.toLowerCase().indexOf('mac') != -1 ? 1:0;
var isDOM = document.getElementById ? 1:0;
var isIE = document.all ? 1:0;
var isIE4 = isIE && !isDOM ? 1:0;
var isIE5 = isIE && isDOM ? 1:0;
var isNS = navigator.appName=='Netscape';
var isNS4 = isNS && !isDOM ? 1:0;
var isNS6 = isNS && isDOM ? 1:0;
var isOp = window.opera ? 1:0;
var isDyn = isDOM||isIE||isNS4;

//gameday
function launchGameday(params) {
	var gamedayURL = 'http://mlb.mlb.com/NASApp/mlb/mlb/gameday/mlb_gameday.jsp?gameID=' + params;
	popWin(gamedayURL,'GamedayWin','770','582','location=no,menubar=no,scrollbars=no,status=no,toolbar=no,resizable=yes');
}
// append stc to url
function appendSTC(page) { 
	var ran_unrounded = Math.random() * 1000000000;
	var ran_number = Math.round(ran_unrounded);
	if ( page.indexOf("?") == -1 ) window.location.href = page + "?stc=" + ran_number;
	else window.location.href = page + "&stc=" + ran_number;
}

//mediaplayer
var dq = "";
var agt= navigator.userAgent.toLowerCase();
var is_nav = ((agt.indexOf('spoofer') > -1) || (agt.indexOf('webtv') > -1) || (agt.indexOf('hotjava') > -1));

if (!is_nav) {
	if ((agt.indexOf("win95")!=-1) || (agt.indexOf("windows 95")!=-1)) { dq += "&os=win95"; }
	else if (agt.indexOf("mac")!=-1) { dq += "&os=mac";}
	else { dq += "&os=winX";}
} else { dq += "&os=other";}
dq += ((isIE) ? "&browser=IE" : "&browser=nonIE");

if (isNS4) { //bypass ns 4.x 
	SetCookie("playMedia", "netscape", 300, "/", ".mlb.com");
} else {
	testreal = GetCookie("playMedia");
	if ((testreal == "") || (testreal == null)) {
		window.open("/components/global/realone_detect.html", "realdetect", "width=80, height=40, left=800, top=600");
	}
}

//cookie functions(partial from cookie.js)
function SetCookie (name, value, daysexpire, path, domain, secure) {
	if (daysexpire){
        expire = new Date();
        expire.setTime(expire.getTime() + daysexpire*24*60*60*1000);
		//fix mac date bug by calling this fn.
		//FixCookieDate(expire);
    }
	document.cookie = name + "=" + escape(value) +
        ((daysexpire) ? "; expires=" + expire.toGMTString() : "") +
        ((path) ? "; path=" + path : "") +
        ((domain) ? "; domain=" + domain : "") +
        ((secure) ? "; secure" : "");
}
function GetCookieVal (offset) {// "Internal" function
  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 DeleteCookie (name,path,domain) {
  if (GetCookie(name)) {
    document.cookie = name + "=" +
      ((path) ? "; path=" + path : "") +
      ((domain) ? "; domain=" + domain : "") +
      "; expires=Thu, 01-Jan-70 00:00:01 GMT";
  }
  //alert("cookie " + name + " deleted.");
}

// get and trap partnerId
str = document.location.search;
if (str.indexOf("partnerId=") > -1) {
	idx1 = str.indexOf("partnerId=")+10;
	idx2 = ((str.indexOf("&",str.indexOf("partnerId")) > -1) ? str.indexOf("&",str.indexOf("partnerId")) : str.length);
	SetCookie("partnerId",(str.slice(idx1,idx2)),null,"/",".mlb.com");
}

//mURL = media URL; cURL = context URL; cw & ch = context width & height; s = subscription true/false; cparam = context URL parameters (if any)
function playMedia(mURL, cURL, cw, ch, s, cparam, gid) {
	var testreal = GetCookie("playMedia");
	if (testreal == "R1" || testreal == "R1_usr_def" || testreal == "Rx" || testreal == "nR") {
		if ((s) && (s != "false")) {
			if (s == "mlb_ga" && GetCookie("r1Superpass") == "true") {
				/* UNENCODED RBN SMIL */
				mURL = mURL.slice(mURL.lastIndexOf("/")+1, mURL.indexOf(".rm"));
				urlstr = "http://start.real.com/rd?pid="+s+"_sp&url=play_2003.smil"+
					((mURL.indexOf("live") == -1) ? "&team="+mURL.slice(7,10)+"&clipDate="+mURL.slice(0,6) : "&team="+mURL.slice(0,3))+
					((mURL.indexOf("_spa") > -1) ? "&lang=spa" : "")+
					((mURL.indexOf("_fre") > -1) ? "&lang=fre" : "")+
					((mURL.indexOf("_jap") > -1) ? "&lang=jap" : "")+
					((mURL.indexOf("_dbl1") > -1) ? "&dbl=1" : "")+
					((mURL.indexOf("_dbl2") > -1) ? "&dbl=2" : "");
				/* NO SMIL */
				//urlstr = "http://start.real.com/rd?pid="+s+"_rbn&url="+mURL+"&rpcontexturl="+cURL+"&rpcontextwidth="+(parseInt(cw) + 320)+"&rpcontextheight="+ch+"&rpcontextparams="+escape("pid="+s+"&"+cparam);
				//alert(urlstr);
				void(document.location.href = urlstr);
				return;
			}
			//else { urlstr = "/NASApp/mlb/media?";}	/* FOR USE IN TESTING ONLY */
			else { urlstr = "http://www.mlb.com/NASApp/mlb/media?";} 	/* FOR USE ON WWW ONLY */
			if (testreal == "R1" || testreal == "R1_usr_def") {
				urlstr += "pid="+s+"&cparam="+escape(cparam);
				//urlstr += ((gid) ? "&gid="+escape(gid) : "");
				urlstr += ((cURL) ? "&rpcontexturl="+cURL : "");
				if (s == "mlb_ga") { //add playback img dimension for audio
					urlstr += ((cw) ? "&rpcontextwidth="+ (parseInt(cw) + 320) : "");
				} else {
					urlstr += ((cw) ? "&rpcontextwidth="+cw : "");
				}
				urlstr += ((ch) ? "&rpcontextheight="+ch : "");
				urlstr += "&url=" + escape(mURL);
			} else { urlstr += "pid="+s+"&url=" + escape(mURL);}
			void(window.open(urlstr));
		}
		else {
			urlstr = "http://play.rbn.com/?url="+mURL+"&proto=rtsp";
			urlstr += ((cw) ? "&rpcontextwidth="+cw : "&rpcontextwidth=400");
			urlstr += ((ch) ? "&rpcontextheight="+ch : "&rpcontextheight=241");
			urlstr += ((cURL) ? "&rpcontexturl="+cURL : "&rpcontexturl=http://mlb.mlb.com/NASApp/mlb/mlb/video/mlb_mediaPlayer_tv_mlb.jsp");
			urlstr += ((cparam) ? "&rpcontextparams="+escape(cparam) : "");
			void(document.location.href = urlstr);
		}
	}
	else { document.location.href = "/NASApp/mlb/mlb/help/pluginerror.jsp?emsg="+testreal;}
}


