// DCLK variables, required for every page
var axel = Math.random() + "";
var ord = axel * 1000000000000000000;

// Broswer detection - these must be consistent on every page.  make sure the old script file has the same variables.
var NS4 = (document.layers) ? 1 : 0;
var IE4 = ((document.all) && (!document.getElementById)) ? 1 : 0;
var IE5 = ((document.all) && (!document.fireEvent) && (!window.opera)) ? 1 : 0;
var DOM = (document.getElementById) ? 1 : 0;  // ns6+ and ie5+ and mozilla
var NS6 = ((!document.all) && (document.getElementById)) ? 1 : 0;  // ns6+ and mozilla, not ie6
var IE = (navigator.appName == "Microsoft Internet Explorer") ? 1 : 0;
var PC = (navigator.platform == "Win32") ? 1 : 0;
var MAC = ((navigator.appVersion.indexOf("PPC") >0) || (navigator.appVersion.indexOf("Mac") >0)) ? 1 : 0;

// fantasy football
var ff = new Object();

//Layer switch functions using the display property
function layerSwitchDisplay(theDivs, divId) {
// Layer switching functions
  for (var i = 0; i < theDivs.length; i++) {
    if (theDivs[i] == divId) {
      showLayerDisplay(theDivs[i]);
    }
    else {
      hideLayerDisplay(theDivs[i]);
    }
  }
}
function hideLayerDisplay(whichEl) {
  document.getElementById(whichEl).style.display = "none";
}
function showLayerDisplay(whichEl) {
  document.getElementById(whichEl).style.display = "";
}

function userState(type) {
  if (arguments.length == 0) {
    if (typeof(user) != 'object') return false;
    if (typeof(user.USER_ID) != 'string') return false;
    if (user.USER_ID == 0) return false;
    return true;
  } 
  else {
    if (userState()) {
      if (type == 'players') {
        if (typeof(userPlayers) != 'object') return false;
        if (typeof(userPlayers.length) != 'number') return false;
        if (userPlayers.length == 0) return false;
        return true;
      }
      else if (type == 'teams') {
        if (typeof(userTeams) != 'object') return false;
        if (typeof(userTeams.length) != 'number') return false;
        if (userTeams.length == 0) return false;
        return true;
      }  
    } 
    else {
      return false;
    }
  }  
}

function findX(el) {
  var x = 0;
  var obj = document.getElementById(el);
  while (obj.offsetParent) {
  x += obj.offsetLeft;
  obj = obj.offsetParent;
  }
  return x;
}
function findY(el) {
  var y = 0;
  var obj = document.getElementById(el);
  while (obj.offsetParent) {
  y += obj.offsetTop;
  obj = obj.offsetParent;
  }
  return y;
}

function Redirect(url) {
location.replace(url);
}

// used on story pages
var months = new Array ("Jan.", "Feb.", "Mar.", "Apr.", "May.", "Jun.", "Jul.", "Aug.", "Sep.", "Oct.", "Nov.", "Dec");
function formatDate(seconds) {
  var d = new Date(seconds * 1000);
  var y = d.getYear();
  var h = d.getHours();
  var m = d.getMinutes();
  var p;

  if (h >= 12) {
    p = "p.m.";
  } else {
    p = "a.m.";
  }
  if (h > 12) {
    h -=12;
  } else if (!h) {
    h = 12;
  }
  if (m < 10) {
    m = "0" + m;
  }
  if (y < 1000) {
    y += 1900;
  }
  return months[d.getMonth()] + " " + d.getDate() + ", " + y +
    " " + h + ":" + m + " " + p;
}

// Image swapping function
function imageSwap(imageName, imageSource) {
  // Don't do anything if images aren't supported in DOM
  if (document.images) {
    // Change the source of the image
    // Both of these are passed in the function arguments
    imageName.src = imageSource;
  }
}

function validate5DigitZIP(f) {
	var temp = '';
	var valid = "0123456789";
	var formZIP = f.zipcode.value;
	if (formZIP.length != 5) {
		alert("Please enter a 5 digit zip code.  Thank you.");
		return false;
	}
	for (var i=0; i < formZIP.length; i++) {
		temp = "" + formZIP.substring(i, i+1);
		if (valid.indexOf(temp) == "-1") {
			alert("Your zip code must be five numerals.  Please try again.");
			return false;
		}
	}
	return true;
}
				
//Generic Window Opener function
function openWin(winURL, winName, winWidth, winHeight) {
  var winTop = 25;
  var winLeft = 25;
  if (screen.availWidth <= 800) {
    var winLeft = screen.availWidth - winWidth;
  }
  if (arguments.length == 2) {  //open generic window
    var theWin = window.open(winURL, winName);
  }
  else { // open standard pop-up window with tool bars disabled  
    var theWin = window.open(winURL, winName, "width=" + winWidth + ",height=" + winHeight + ",top=" + winTop + ",left=" + winLeft + ",screenY=" + winTop + ",screenY=" + winLeft + ",toolbar=no,location=no,directories=no,status=no,menubar=no,resizable=1,scrollbars=no");
  }
}

//Flash Detection
if (typeof(requiredVersion)=="undefined") {
  var requiredVersion = 6;
}
var flash5Installed = false;
var flash6Installed = false;
var actualVersion = 0;
var useFlash = false;
if(IE && PC){
  document.writeln('<SCR' + 'IPT LANGUAGE=VBScript\>');
  document.writeln('on error resume next');
  document.writeln('flash6Installed  = (IsObject(CreateObject("ShockwaveFlash.ShockwaveFlash.6")))');
  document.writeln('If (flash6Installed) then');
  document.writeln('  flash5Installed  = True');
  document.writeln('Else');
  document.writeln('  flash5Installed  = (IsObject(CreateObject("ShockwaveFlash.ShockwaveFlash.5")))');
  document.writeln('End If');
  document.writeln('</SCR' + 'IPT\>');
}
function detectFlash() {
  if (navigator.plugins) {
    if (navigator.plugins["Shockwave Flash"]) {
      var flashDescription = navigator.plugins["Shockwave Flash"].description;
      //alert("Flash plugin description: " + flashDescription);
      var actualVersion = parseInt(flashDescription.charAt(flashDescription.indexOf(".") - 1));
    }
  }
  //alert("version detected: " + actualVersion);
  if (flash5Installed) {
    var actualVersion = 5;
  }
  if (flash6Installed) { 
    var actualVersion = 6;
  }
  if (actualVersion >= requiredVersion) {
    useFlash = true;
    var url = document.URL;
    if (url.indexOf("flash=0") != -1) useFlash = false; //debug code so user can add flash=0 to url string and see what the site looks like w/o flash
  }
}
detectFlash();

// Stuff for Plugin Detection
    // this is where we write out the VBScript for MSIE Windows
    var WM_startTagFix = '</';
    var msie_windows = 0;
    if ((navigator.userAgent.indexOf('MSIE') != -1) && (navigator.userAgent.indexOf('Win') != -1)){
        msie_windows = 1;
        document.writeln('<script language="VBscript">');
        document.writeln('\'This will scan for plugins for all versions of Internet Explorer that have a VBscript engine version 2 or greater.');
        document.writeln('\'This includes all versions of IE4 and beyond and some versions of IE 3.');
        document.writeln('Dim WM_detect_through_vb');
        document.writeln('WM_detect_through_vb = 0');
        document.writeln('If ScriptEngineMajorVersion >= 2 then');
        document.writeln('  WM_detect_through_vb = 1');
        document.writeln('End If');
        document.writeln('Function WM_activeXDetect(activeXname)');
        document.writeln('  on error resume next');
        document.writeln('  If ScriptEngineMajorVersion >= 2 then');
        document.writeln('     WM_activeXDetect = False');
        document.writeln('     WM_activeXDetect = IsObject(CreateObject(activeXname))');
        document.writeln('     If (err) then');
        document.writeln('        WM_activeXDetect = False');
        document.writeln('     End If');
        document.writeln('   Else');
        document.writeln('     WM_activeXDetect = False');
        document.writeln('   End If');
        document.writeln('End Function');
        document.writeln(WM_startTagFix+'script>');
    }

function WM_pluginDetect(plugindescription, pluginxtension, pluginmime, activeXname){
        //This script block will test all user agents that have a real plug-in array
        //(i.e. Netscape) and set the variables, otherwise it directs the routine
        // to WM_activeXDetect to detect the activeX control.
        // First define some variables
        var i,plugin_undetectable=0,detected=0, daPlugin=new Object();
        // Then we check to see if it's an MSIE browser that you can actually
        // check for the plugin in question.
        if (msie_windows && WM_detect_through_vb){
            plugin_undetectable = 0;
        } else {
            plugin_undetectable = 1;
        }
        // If it has a real plugins or mimetypes array, we look there for the plugin first
        if(navigator.plugins) {
            numPlugins = navigator.plugins.length;
            if (numPlugins > 1) {
                if (navigator.mimeTypes && navigator.mimeTypes[pluginmime] && navigator.mimeTypes[pluginmime].enabledPlugin && (navigator.mimeTypes[pluginmime].suffixes.indexOf(pluginxtension) != -1)) { // seems like we have it, let's just make sure and check the version (if specified)
                    if ((navigator.appName == 'Netscape') && (navigator.appVersion.indexOf('4.0') != -1)) { // stupid, stupid Netscape can't handle the references to navigator.plugins by number, sooo...
                        for(i in navigator.plugins) {
                            if ((navigator.plugins[i].description.indexOf(plugindescription) != -1) || (i.indexOf(plugindescription) != -1)) { // some versions of quicktime have no description. feh!
                                detected=1;
                                break;
                            }
                        }
                    } else {
                        for (i = 0; i < numPlugins; i++) {
                            daPlugin = navigator.plugins[i];
                            if ((daPlugin.description.indexOf(plugindescription) != -1) || (daPlugin.name.indexOf(plugindescription) != -1)) {
                                detected=1;
                                break;
                            }
                        }
                    }
                    // Mac weirdness
                    if (navigator.mimeTypes[pluginmime] == null) {
                        detected = 0;
                    }
                }
                return detected;
            } else if((msie_windows == 1) && !plugin_undetectable){
                return WM_activeXDetect(activeXname);
            } else {
                return 0;
            }
        } else {
            return 0;
        }
    }


// ---------------MSN Video functions---------------------------------------------------------------------
var msnV = null;
function msnvDw(pid,guid,mkt,pl) {
	if(mkt=='' || mkt==undefined) {mkt="us";}
	if(pl==undefined) {pl='';}
  var w = window.open("http://video.msn.com/v/"+mkt+"/dw.htm?p="+pid+"&g="+guid+"&m="+mkt+"&pl="+pl,"msnVDW","width=750,height=363,status=1");
}
function msnvJs(pid,guid,h,mkt,pl) {
	if(mkt=='' || mkt==undefined) {mkt="us";}
	if(pl==undefined) {pl='';}
	var req = msnvReq();
	if(req!=0) {
		var i = document.createElement("<iframe frameborder=0 scrolling=0 src='http://video.msn.com/v/"+mkt+"/req.aspx?r="+req+"&h="+location.href+"' width=750 height=363>");
		h.appendChild(i);
		return;
	}
	if(!window["msnVinh"]) {
		var holder = document.createElement("<div id=msnVinh class=msnVa>");
		if(h==undefined) {
			document.body.appendChild(holder);
		}
		else {
			h.appendChild(holder);
		}
		var script = document.createElement("<script language=javascript src=http://"+location.host+"/v/"+mkt+"/msnvinp.js onreadystatechange=msnvJsR(this,'"+guid+"','"+pl+"');>");
		holder.appendChild(script);
	}
	else {
		msnV.Play(guid);
	}
}
function msnvJsR(o,g,p) {
	if(o.readyState=="loaded") {
		msnV = new msnvINP(msnVinh,'msnV');
		msnV.Build();
		msnV.Play(g,p);
	}
}
function msnvReq() {
	var ok=0;
	var ua=navigator.userAgent.toLowerCase();
	if(ua.indexOf("mac")>-1) {ok = 32;}
	else if(navigator.appName=="Netscape") {ok = 8;}
	else {
		ok += (parseFloat(ua.slice(ua.indexOf("msie")+5))>5.9)?0:1;
		var o = new msnvTC("var w=new ActiveXObject('WMPlayer.OCX');return w.versionInfo;","return 0;");
		ok += (parseInt(o.tc())>=7)?0:2;
		o = new msnvTC("var w=new ActiveXObject('ShockwaveFlash.ShockwaveFlash.6');return 0;","return 4;");
		ok += o.tc();
	}
	return ok;
}
function msnvTC(t,c){
	this.tc=new Function("try{"+t+"}catch(e){"+c+"}");
}
function oMvsLink(gPV,sGuid,bDck,sMenu,sSubM,sURL,sPL,bRP,bStatic,sList){
	bDck=(bDck==1||bDck==true)?0:Math.min((bDck*1)+1,2);
	if (gPV.indexOf("/")<1){gPV+="/64";}
	if((gPV!="37/81")||(gPV!="38/82")||(gPV!="39/83")||(gPV!="40/98")||(gPV!="50/98")||(gPV!="51/98")) {
		var mkt=(gPV=="43/119")?"es-us":"";
		msnvDw(gPV,sGuid,mkt,escape(sList));
		return;
	}
	var sLink="http://g.msn.com/0VD0/"+gPV+"?t="+bDck;
	if (arguments.length>1){
		sLink+=(sList)?"&p="+escape(sList):"";
		sLink+=((sMenu)?("&m="+sMenu+"&mi="+sSubM):"");
		sPL=sGuid+((sPL)?","+sPL:"");
		sLink+=((!bRP)?"&i=":"&r=")+sPL;
	}
	if (bDck){
		var ovp=window.open(sLink,"ovp","width=788,height=598,status=1");
		if (sURL){location.href=sURL;}
	}
	else{
		location.href=sLink;
	}
}
// ---------------MSN Video functions---------------------------------------------------------------------

//NFL PRO BOWL
function openBallot(url,width,height) {
 popupWin = window.open(url, name, "menubar=0,toolbar=0,location=0,directories=0,status=0,scrollbars=0,resizable=0,width=" +width +",height=" +height +",left=50,top=50");
}

// MLB.com gameday04
// new window launch
// usage: <a href="[url]" onclick="popWin(this,'[window_name]','[width]','[height]' {,'_optional_options'}); return false;" ... >
var _pw_l,_pw_t,_pw_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;
  _pw_l=(screen.availWidth-w-12)/2;
  _pw_t=(screen.availHeight-h-48)/2;
  _pw_z=window.open(url,n,'width='+w+',height='+h+',left='+_pw_l+',top='+_pw_t+','+o);
}
function launchGameday(params) {
  var gamedayURL = 'http://mlb.mlb.com/mlb/gameday/gd2004.html?' + params;
  popWin(gamedayURL,'GamedayWin','770','600','location=no,menubar=no,scrollbars=no,status=no,toolbar=no,resizable=yes');
}

// retrieve arguments from url query string
function GetArgs() {
	var args = new Object();
	var query = location.search.substring(1);
	var pairs = query.split("&");
	for (var i = 0; i < pairs.length; i++) {
		var pos = pairs[i].indexOf('=');
		if (pos == -1) continue;
		var argname = pairs[i].substring(0,pos);
		var value = pairs[i].substring(pos+1);
		args[argname] = unescape(value);
	}
	return args;
}

// possible function if we center pages
function PositionElementsOnResize() {
	if (document.getElementById('ad300x250')) {
    var ad300x250 = document.getElementById('ad300x250');
    ad300x250.style.top = findY('ad300x250box');
    ad300x250.style.left = findX('ad300x250box');
	}
	if (document.getElementById('ad160x600')) {
    var ad160x600 = document.getElementById('ad160x600');
    ad160x600.style.top = findY('ad160x600box');
    ad160x600.style.left = findX('ad160x600box');
	}
}
window.onresize = PositionElementsOnResize;

// Called by the Point Roll AD Iframe to hide the AD.
function hidePointRoll() {
  document.all["pointRollAd"].style.display="none";
}

// function used to display fantasy information
// this function will take the teamId and return the team name either linked or not linked.
function ff_dt(teamId, type) {
	try {
		var teamName = teamsInfo[0][1];
		if (arguments.length == 1) {
			var link = '<a href="http://msnfantasy.foxsports.com/nfl/commish/team/home?teamId=' + teamId + '">' + teamName + '</a>';
			return link;
		}
		if (arguments.length == 2 && arguments[1] == 0) {
			return teamName;
		}
	}
  catch(e) {
	  //do nothing
	}
}
function ff_dw_dt(teamId, type) {
	try {
		var teamName = teamsInfo[0][1];
		if (arguments.length == 1) {
			var link = '<a href="http://msnfantasy.foxsports.com/nfl/commish/team/home?teamId=' + teamId + '">' + teamName + '</a>';
			document.write(link);
			return;
		}
		if (arguments.length == 2 && arguments[1] == 0) {
			document.write(teamName);
			return;
		}
	}
  catch(e) {
	  //do nothing
	}
}

function ff_dg(leagueId, type) {
	try {
		var leagueName = teamsInfo[0][5];
		if (arguments.length == 1) {
			var link = '<a href="http://msnfantasy.foxsports.com/nfl/commish/league/home?leagueId=' + leagueId + '">' + leagueName + '</a>';
			return link;
		}
		if (arguments.length == 2 && arguments[1] == 0) {
			return leagueName;
		}
	}
  catch(e) {
	  //do nothing
	}
}

function ff_dw_dg(leagueId, type) {
	try {
		var leagueName = teamsInfo[0][5];
		if (arguments.length == 1) {
			var link = '<a href="http://msnfantasy.foxsports.com/nfl/commish/league/home?leagueId=' + leagueId + '">' + leagueName + '</a>';
			document.write(link);
			return;
		}
		if (arguments.length == 2 && arguments[1] == 0) {
			document.write(leagueName);
			return;
		}
	}
  catch(e) {
	  //do nothing
	}
}



