//=============================================================================================================================
/* This file contains tracking code placed in third party pages
   Usage is done at the html/jsp page. MUST exist in this order
   Usage: 1- Customize this variable, for example:
             <script type="text/javascript">
               var fsxPage = {
                  name: 'Liga+Fantastica',
                  path: '/beisbol/juegos/ligafantastica',
                  site: 'fse',
                  contentType: '',
                  source:'',
                  author:'No+Author'
               }
             </script>
          2- Import these four js files in the <head> tag (MUST be imported AFTER the variable in 1 is declared): 
             <script type="text/javascript"   src="http://msn.foxsports.com/fe/fsi/js/tracking/fsxTrackerThirdParty.js"></script>
             <script language="javascript1.1" src="http://msn.foxsports.com/fe/js/ntpagetag.js"></script>
             <script language="javascript1.1" src="http://msn.foxsports.com/fe/js/migration.js"></script>
             <script language="javascript1.1" src="http://msn.foxsports.com/fe/js/hbx.js"></script>
          3- Place this js call right before the closing body tag (e.g: </body>):
             <script language="Javascript">fsxTracker.init();</script>
             note: init() will include any other functions we want for any purpose, thus, avoiding asking partners to add an extra line like <script language="Javascript">anotherFunction();</script>
             
*/
//=============================================================================================================================
//GA implementation
var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));

//include here all custom-call functions
var fsxTracker = {
  init:function(){
    fsxInitGoogle();
  }
};

//-------------------------------------------
// fsxInitGoogle()
//-------------------------------------------
var fsxGoogleTracker;
function fsxInitGoogle() {
  var _googleAccount = null;
  var _pagePath      = _hbxStrip(fsxPage.path);
  if(fsxPage.site=='fsc' || fsxPage.site=='foxsoccer'){_googleAccount='UA-4869874-2';}else{_googleAccount='UA-4869874-1';}
  fsxGoogleTracker=_gat._getTracker(_googleAccount);
  fsxGoogleTracker._trackPageview(_pagePath);
}
//end GA implementation

function checkCookie(cookieName, length) {
  var allcookies = document.cookie;
  var cookiePos = allcookies.indexOf(cookieName + "=");
  if(cookiePos != -1) {
    var start = cookiePos + length;
    var end = allcookies.indexOf(";", start);
    if(end == -1)
      end = allcookies.length;
    var value = allcookies.substring(start, end);
    value = unescape(value);
    return value;
  }
  return "";
  }
  var uid = "";
  var euid = checkCookie("pUID", 5);
  var fuid = checkCookie("pFUID", 6);
  var fanid = checkCookie("pFANID", 6);
  var lmd = checkCookie("pLMD", 5);
  var flmd = checkCookie("pFLMD", 6);
  var rme = checkCookie("pRME", 5); //Hitbox Reg Addition
  var curDate = new Date();

  if (flmd == null || flmd == "") {
   flmd = curDate.getMilliseconds();
   document.cookie = "pFLMD=" + curDate + "; path=/; domain=.foxsports.com;";
  }

  if (lmd != null && lmd != "") {
    uid = euid;
  } else{
    uid = checkCookie("pRME", 5);
    if (uid != null && uid != "") {
      curDate = curDate.getMilliseconds();
      document.cookie = "pLMD=" + curDate + "; path=/; domain=.foxsports.com;";
    }
  }
  //document.write('<scr' + 'ipt language="JavaScript1.2" src="/fsi/user.js?uid=' + escape(uid) + '&lmd=' + escape(lmd) + '" type="text/javascript"></scr' + 'ipt>');
  //var fantasyUserJsPath = 'http://msn.foxsports.com/fantasy/football/commissioner/fantasyUser.js?epuid=' + escape(euid) + '&flmd=' + escape(flmd);
  //document.write('<scr' + 'ipt language="JavaScript1.2" src="' + fantasyUserJsPath + '" type="text/javascript"></scr' + 'ipt>');
  
  function _hbxStrip(a){
    a = a.replace(/\s+$/, ''); a = a.split(",").join("");
    a = a.split("&").join(""); a = a.split("'").join("");
    a = a.split("#").join(""); a = a.split("$").join("");
    a = a.split("%").join(""); a = a.split("^").join("");
    a = a.split("*").join(""); a = a.split(":").join("");
    a = a.split("!").join(""); a = a.split("<").join("");
    a = a.split(">").join(""); a = a.split("~").join("");
    a = a.split(";").join(""); a = a.split("\\").join("");
    a = a.split(" ").join("+");
    return a;
  }
  var _hbEC=0,_hbE=new Array;function _hbEvent(a,b){b=_hbE[_hbEC++]=new Object();b._N=a;b._C=0;return b;}
  var hbx=_hbEvent("pv");hbx.vpc="HBX0100u";hbx.gn="h.foxsports.com";
  hbx.pndef="title";
  hbx.ctdef="full";
  hbx.fv="";
  hbx.lt="auto"; //LINK TRACKING
  hbx.dlf="n";
  hbx.dft="n";
  hbx.elf="n";
  hbx.seg="";
  hbx.fnl="";
  hbx.cmp="";
  hbx.cmpn="CMP"; //
  hbx.dcmp="";
  hbx.dcmpn="DCMP"; //
  hbx.dcmpe="";
  hbx.dcmpre="";
  hbx.hra="";
  hbx.hqsr="";
  hbx.hqsp="ATT"; //
  hbx.hlt="";
  hbx.hla="";
  hbx.gp="";
  hbx.gpn="GP"; //
  hbx.hcn="";
  hbx.hcv="";
  hbx.cp="null";
  hbx.cpd="";

  // Hitbox registration additions

  function findAge(yr, mon, day, unit, decimal, round) {
    //Sample usage
    //findAge (year, month, day, unit, decimals, rounding)
    //Unit can be "years", "months", or "days"
    //Decimals specifies demical places to round to (ie: 2)
    //Rounding can be "roundup" or "rounddown"
    //findAge(1997, 11, 24, "years", 0, "rounddown")

    var one_day = 1000*60*60*24;
    var one_month = 1000*60*60*24*30;
    var one_year = 1000*60*60*24*30*12;
    today = new Date();
    var pastdate = new Date(yr, mon-1, day);
    var countunit = unit;
    var decimals = decimal;
    var rounding = round;
    finalunit = (countunit=="days")? one_day : (countunit=="months")? one_month : one_year;
    decimals = (decimals<=0)? 1 : decimals*10;
    if(unit!="years") {
      if (rounding=="rounddown")
        return(Math.floor((today.getTime()-pastdate.getTime())/(finalunit)*decimals)/decimals+" "+countunit);
      else
        return(Math.ceil((today.getTime()-pastdate.getTime())/(finalunit)*decimals)/decimals+" "+countunit);
    }
    else {
      yearspast = today.getFullYear()-yr-1;
      tail = (today.getMonth()>mon-1 || today.getMonth()==mon-1 && today.getDate()>=day)? 1 : 0;
      pastdate.setFullYear(today.getFullYear());
      pastdate2 = new Date(today.getFullYear()-1, mon-1, day);
      //tail = (tail==1)? tail+Math.floor((today.getTime()-pastdate.getTime())/(finalunit)*decimals)/decimals : Math.floor((today.getTime()-pastdate2.getTime())/(finalunit)*decimals)/decimals;
      return(yearspast+tail);
    }
  }

  function returnAge() {
    var age = -1;
    if(user.MONTH_CODE == "0" || user.MONTH_CODE == "" || user.DATE_CODE == "0" || user.DATE_CODE == "" || user.BIRTH_YEAR == "0" || user.BIRTH_YEAR == "")
    {
    }
    else
    {
      var bdayMonth = user.MONTH_CODE;
      var bdayDate = user.DATE_CODE;
      var bdayYear = user.BIRTH_YEAR;
      age = findAge(bdayYear, bdayMonth, bdayDate, "years", 0, "rounddown");
    }
    if (age > 12 && age < 18) {(age = 1);}
    if (age > 17 && age < 25) {(age = 2);}
    if (age > 24 && age < 35) {(age = 3);}
    if (age > 34 && age < 45) {(age = 4);}
    if (age > 44 && age < 55) {(age = 5);}
    if (age > 54 && age < 65) {(age = 6);}
    if (age > 64) {(age = 7);}
    return age;
  }

  /*if (typeof(user.USER_ID) == 'undefined') {user.USER_ID = '';}*/
  var user_status="";
  //var user_age=returnAge();
/*
  if (rme.length > 0){
    if (user.USER_ID == '0'){
      (user_status = "No-Log");
    }else{
      (user_status = "Yes|" + returnAge());
    }
  }else{
    (user_status = "Not-Reg");
  }*/

  //CUSTOM VARIABLES
  /*if (typeof(user.USER_ID) == 'undefined') {
    user.USER_ID = '';
  }*/
  //hbx.ci=user.USER_ID;
  hbx.ci="";

  hbx.hc1=_hbxStrip(fsxPage.contentType) + "|" + _hbxStrip(fsxPage.source);
  hbx.hc2=_hbxStrip(fsxPage.author)  + "|" + _hbxStrip(fsxPage.name);
  hbx.hc3="";
  hbx.hc4="";
  hbx.hrf="";
  hbx.pec="";

  hbx.acct                = setHbxAccountNumber(fsxPage.site);
  var _acct               = setHbxAccountNumber(fsxPage.site); //legacy for back compat
  var _acctMedia          = setHbxAccountMedia(fsxPage.site);  // legacy for back compat
  var hitboxAccountNumber = setHbxAccountNumber(fsxPage.site);
  

function setHbxAccountNumber(whichSite){
  var account = null;
  if(whichSite=='fse'){account = 'DM551123MOSS';}
  else if(whichSite=='fsla'){account = 'DM551123BKRC';}
  else if(whichSite=='fslasc'){account = 'DM520329JBZZ';}
  else if(whichSite=='fsc' || whichSite=='foxsoccer'){account = 'DM520329A1SZ95EN3';}
  else{account = '';}
  return account;
}

function setHbxAccountMedia(whichSite){
  var accountMedia = null;
  if(whichSite=='fse'){accountMedia = 'DM551123MOSS';}
  else if(whichSite=='fsla'){accountMedia = 'DM551123BKRC';}
  else if(whichSite=='fslasc'){accountMedia = 'DM520329JBZZ';}
  else if(whichSite=='fsc' || whichSite=='foxsoccer'){accountMedia = 'DM520329OJCS95EN3';}
  else{accountMedia = '';}
  return accountMedia;
}

  hbx.pn                 =_hbxStrip(fsxPage.name);
  hbx.mlc                =_hbxStrip(fsxPage.path);
  var _pn                =_hbxStrip(fsxPage.name);
  var _mlc               =_hbxStrip(fsxPage.path);
  var hitboxPageName     = fsxPage.name;
  var hitboxTrackingPath = fsxPage.path;
  
  
function _debugFsxTracker(){
 alert("trackingPath: "+fsxPage.path+"\n"+
       "pageName: "+fsxPage.name+"\n"+
       "site: "+fsxPage.site+"\n"+
       "googleAccount: "+"test"+"\n"+
       "author: "+fsxPage.author+"\n"+
       "source: "+fsxPage.source+"\n"+
       "hitboxAccount: "+hbx.acct+"\n"+
       "hitboxAcctMedia: "+_acctMedia);
}
