
function GetChildItems(popup, funcType)
{
    doc = document.getElementById(popup);
    if(funcType == 'go')
        return FindCICodes_GoFunc(doc.innerHTML);
    else if(funcType == 'lnk')
        return FindCICodes_LnkFunc(doc.innerHTML);
    else if(funcType == 'a')
        return FindCICodes(doc.innerHTML);
}

//This function is used for the tabbed dynamic content.
//'str' is the anchor URL that is passed.  It parses the querystring for the ci=xxxx code.
//This function searches 'str' to find all occurances of ci=XXXXX  where XXXXX is alphanumeric.  This function uses regular expressions
//to parse 'str'.  This returns a string in the format of ci=xxxxx,ci=xxxxx,.....,ci=xxxxxx.  It cycles through the array of ci codes and replaces ci= with 
//an empty string, leaving an array of plain cicodes - xxxxx,xxxxx,xxxxx,xxxxx,....,xxxxx
//RETURNS: an array of cicodes.
function FindCICodes(str)
{
    //Strip out any script tags you may find in str.
    var scripttxt = /<script(.|\n)+?<\/script>/gi;
    str = str.replace(scripttxt, '');

    var re = /ci=[0-9]+/gi; //look for all occurances of ci=XXXXXXX.  XXXXXX can be any combo of 0-9
    var cistr = /ci=/gi; // look for all occurances of ci= 
    
    inStr = str.match(re);
    if(inStr != null)
    {
        for(x=0;x<inStr.length;x++)
        {
            inStr[x] = inStr[x].replace(cistr,'');
        }
    }
    return inStr==null?"":inStr;
}

//this function is used for the popup dynamic content
//str here is the innerHTML of the corresponding div element to the tab.  This function searches the div HTML content for the function call 
//'go(linkto, cicode)'
//ASSUMPTION: the cicode is the second parameter passed to the go function.
//Returns: an array of cicodes presented in the go functions.
function FindCICodes_GoFunc(str)
{
    var gofunc = /go\(['"][a-zA-Z0-9/=&%?_.#]+['"],[0-9]+\)/gi; //look for all occurances the function call 'go(linkto, cicode)'
    inStr = str.match(gofunc);//returns an array of matching strings
    if(inStr != null)
    {   
        for(x=0;x<inStr.length;x++)
        {
            Arr = inStr[x].split(',')[1];
            inStr[x] = Arr.substr(Arr.indexOf('\'')+1, Arr.lastIndexOf('\'')-1);
        }
    }
    return inStr==null?"":inStr;
}
//this function is used for the popup dynamic content
//str here is the innerHTML of the corresponding div element to the tab.  This function searches the div HTML content for the function call 
//'lnk(url)'
//Returns: an array of cicodes presented in the go functions.
function FindCICodes_LnkFunc(str)
{
    var lnkfunc = /lnk\(['"][a-zA-Z0-9/?&%:=_#.]+["']\)/gi; //look for all occurances the function call 'lnk(url)'
    zz = str.match(lnkfunc);//returns an array of matching strings
    if(zz != null)
    {
        for(yx=0;yx<zz.length;yx++)
        {   
            zz[yx] = FindCICodes(zz[yx]);
        }
    }
    return zz==null?"":zz;
}


//Called to loop through all of the document elements to determine which of those are visible to the user at the time of pageload.
function GetVisibleCICodes(e)
{
    var gvcic_str = "";
    
    //Check anchor tags for lnk(), go(), popPage(), popPage2() and href
    var doc = document.getElementsByTagName('a');
    for(AnchorLen=0;AnchorLen<doc.length;AnchorLen++)
    {
        if(TestForVisibility(doc[AnchorLen]))
        {   
            cicode = "";
            if(doc[AnchorLen].href.indexOf('go(') > 0)
                cicode = FindCICodes_GoFunc(doc[AnchorLen].href);
            else if(doc[AnchorLen].href.indexOf('lnk(') > 0)
                cicode = FindCICodes_LnkFunc(doc[AnchorLen].href);
            else
                cicode = FindCICodes(doc[AnchorLen].href);
                
            if(cicode != "")
                gvcic_str += cicode + ",";
        }
    }

    //check area tags for lnk(), go() and href
    var doc = document.getElementsByTagName('area');
    for(AnchorLen=0;AnchorLen<doc.length;AnchorLen++)
    {
        if(TestForVisibility(doc[AnchorLen]))
        {
            cicode = "";
            if(doc[AnchorLen].href.indexOf('go(') > 0)
                cicode = FindCICodes_GoFunc(doc[AnchorLen].href);
            else if(doc[AnchorLen].href.indexOf('lnk(') > 0)
                cicode = FindCICodes_LnkFunc(doc[AnchorLen].href);
            else
                cicode = FindCICodes(doc[AnchorLen].href);
                
            if(cicode != "")
                gvcic_str += cicode + ",";
        }
    } 
        
    //analayze tbody tag with id="headerTable".  This is the header menu of the site.
    maindiv = document.getElementById("headerTableBody");  
    if(maindiv != null)
    {
        //the table holding the header navigation menus must have an id tag of "headerTable" in order for this to work properly
        cicode = "";
        if(TestForVisibility(maindiv))
        {
            cicode += FindCICodes_GoFunc(maindiv.innerHTML);
            cicode += FindCICodes_LnkFunc(maindiv.innerHTML);

            if(cicode != "")
                gvcic_str += cicode + ",";

        }
    }

    //analayze tbody tag with id="homepageNavigation".  This is the navigation menu of the site.
    navMenu= document.getElementById("homepageNavigation");  
    if(navMenu != null)
    {
        //the table holding the header navigation menus must have an id tag of "headerTable" in order for this to work properly
        cicode = "";
        if(TestForVisibility(navMenu))
        {
            cicode += FindCICodes_GoFunc(navMenu.innerHTML);
            cicode += FindCICodes_LnkFunc(navMenu.innerHTML);
                        
            if(cicode != "")
                gvcic_str += cicode + ",";

        }
    }
       
    if(gvcic_str.lastIndexOf(',') == gvcic_str.length-1)
        gvcic_str = gvcic_str.substr(0, gvcic_str.length-1);

    TrackFastball(null, e, null, gvcic_str);

}

function TestForVisibility(item)
{
    myItem = item;

    while(document !== myItem && myItem.style != null)
    {
        if(myItem.style.display == 'none')
            return false;
        myItem = myItem.parentNode;
    }
    return true;            
}



//Called on the mouseover event for the elemenet that needs to be tracked.
//Example call: FastballEvent_MouseOver(event,'1234', this, 'tdsite');
//e - the event - should always be 'event'
//cicode - the cicode of the element
//item - use the 'this' the element that is registering the event. Should always be 'this'
//popup - the name of the div that is popped up.  In this case it is also the first parameter passed into the hpovr function.  
//popup can be an empty string, '', and in this case it assumes it is a tabbed dialog, not a div popup.
//this function will not bubble for children element events.
//funcType - The type of string to look for while doing search
//  -a - search <a> tag
//  -lnk - search lnk(...) function calls
//  -go - search go(...,...) function calls
function FastballEvent_MouseOver(e, cicode, item, popup, funcType)
{
    //e.target = element you are going into
    //e.relatedTarget = element you are coming FROM
    //items = the item with the events assigned to them
    
    if(!e) e = window.event;
    var t1;
    var t2;
    
    var impressions = '';
    
    if(e.target)
    {
        t1 = e.target;
        t2 = e.relatedTarget;
    }
    else
    {
        t1 = e.toElement;
        t2 = e.fromElement;
    }
        
 
    if(IsChildOf(t1, item))
        return false;

    if(!IsChildOf(t2, item))
    {
        if(popup != '')
            impressions = GetChildItems(popup, funcType);
        TrackFastball(cicode, e, item, impressions);
    }
    else
        return false;
 
}  

//Called on the mouseout event for the elemenet that needs to be tracked.
//Example call: FastballEvent_MouseOut(event,'1234', this);
//e - the event - should always be 'event'
//cicode - the cicode of the element
//item - use the 'this' the element that is registering the event. Should always be 'this'
//this function will not bubble for children element events.
function FastballEvent_MouseOut(e, cicode, item)
{
    //e.target = element you are leaving
    //e.relatedTarget = element you are stepping into
    //item is the item with the events assigned to them

    if(!e) e = window.event;
    var t1;
    var t2;
    
    if(e.target)
    {
        t2 = e.target;
        t1 = e.relatedTarget;
    }
    else
    {
        t2 = e.toElement;
        t1 = e.fromElement;
    }
    
    if(IsChildOf(t1, item))
        return false;

    if(!IsChildOf(t2, item))
        TrackFastball(cicode, e, item, '');
    else
        return false;
}

//Called on the mouseclick event for the elemenet that needs to be tracked.
//Example call: FastballEvent_MouseClick(event, '1234', this, 'tab0')
//e - the event - should always be 'event'
//cicode - the cicode of the element
//item - use the 'this' the element that is registering the event. Should always be 'this'
//popup - should be the id of the containing tab <div> that has the ci codes to search in it.
//this function will not bubble for children element events.
//funcType - The type of string to look for while doing search
//  -a - search <a> tag
//  -lnk - search lnk(...) function calls
//  -go - search go(...,...) function calls
// - popPage - search popPage(...,...,...,...) function calls
// - popPage2 - search popPage2(...,...,...,...,...,...) function calls
function FastballEvent_MouseClick(e, cicode, item, popup, funcType)
{
    var impressions = '';
    if(popup != '')
        impressions = GetChildItems(popup, funcType);
    TrackFastball(cicode, e, item, impressions);
}


//determines if the 'child' passed in is a child of the 'parent'.  In other words, the child occurs within 
//the containing element wrapper of the parent.
function IsChildOf(child, parent)
{
    //checks to see if 'child' is a child of 'parent'
    if(child == parent) return false;
    var rval = false;
    var x = child;

    while(x != parent && x.nodeType != 9)
    {
        x= x.parentNode;
    }
    
    if(x == parent)
        rval = true;
        
    return rval;
}

//parses out the domain (minus the sub-domain) for use in the AJAX call.
function parseDomain(domainstr)
{
	return domainstr.substr(domainstr.indexOf('.')+1);
}


//This does the actual tracking of the Fastball elements gathered in these function calls.  
//This function also gets mouse x,y coordinates of the mouse movements into the impression.
//This gathers up the available information and calls jx.load (AJAX) to post back to the server.  
function TrackFastball(cicode, e, targetElement, ciimp)
{
    var qStr = "";
    var AbsX;
    var AbsY;
    var RelX;
    var RelY;
    var EventType;
  
    if (!e) e = window.event; // works on IE, but not NS (we rely on NS passing us the event)
    
    if (e)
    { 
        try
        {
            if(e.type != 'load')
            {
                if (e.pageX || e.pageY)
                { // FF,Moz,Opera7
                  AbsX = e.pageX;
                  AbsY = e.pageY;
                  RelX = e.clientX;
                  RelY = e.clientY;
                }
                else if (e.clientX || e.clientY)
                { //IE6,FF,Moz,Opera7
                  AbsX = e.clientX + document.body.scrollLeft;
                  AbsY = e.clientY + document.body.scrollTop;
                  RelX = e.clientX;
                  RelY = e.clientY;
                }
            }  
            EventType = e.type;
        } catch(err) {}
    }
    
    var pageName = location.pathname;
    var ciImpressions = "";
    
    EventType = EventType == undefined ? "" : EventType;
    cicode = cicode == undefined ? "" : cicode;
    ciimp = ciimp == undefined ? "" : ciimp;   
    RelX = RelX == undefined ? "0" : RelX;
    RelY = RelY == undefined ? "0" : RelY;
    AbsX = AbsX == undefined ? "0" : AbsX;
    AbsY = AbsY == undefined ? "0" : AbsY; 
        
    qStr = "page_name=" + pageName;
    qStr = qStr + "&ci=" + cicode;
    qStr = qStr + "&eventtype=" + EventType;
    qStr = qStr + "&relativex=" + RelX;
    qStr = qStr + "&relativey=" + RelY;
    qStr = qStr + "&absolutex=" + AbsX;
    qStr = qStr + "&absolutey=" + AbsY;
    qStr = qStr + "&ciimpressions=" + ciimp;
    qStr = qStr + "&r=" + Math.random();
    qStr = qStr + "&comview=0";
    
    var dname="";
    
    if (fb_domainName != null)
    {
        dname = fb_domainName;
    }
    else
    {
        //add command to the ajax queue for processing
        //CHANGE THIS URI TO INDICATE WHERE THE FASTBALL INFORMATION IS BEING POSTED TO
        var hname = window.location.hostname;
        
        //when using the .tv domain, post traffic info to .com.
        hname = hname.replace(/-tv/i, "-com");
        hname = hname.replace(/\.tv/i, ".com");
        
        //when using the .me domain, post traffic info to .com
        hname = hname.replace(/-me/i, "-com");
        hname = hname.replace(/\.me/i, ".com");
        
        
        if(hname.indexOf(".") < hname.lastIndexOf("."))
        {
            dname = hname.substr(hname.indexOf('.')+1);
        }
        else
        {
            dname = hname;
        }
    }
    
    queue.push("https://img." + dname + "/pageevents.aspx?" + qStr);


}

function SetDomainName(name)
{
    fb_domainName = name;
}


function doVoid() { return; }

//==============================================================================================
 var queue = new Array();
 var queuebusy = false;
 
 var fb_domainName;
 
 
 function CheckQueue()
 {
    if(!queuebusy && queue.length > 0)
    {
        queuebusy = true;
        var obj = queue.shift();
        var i = new Image(1,1);
        i.src = obj;
        i.onload = function() {doVoid();}

        queuebusy = false;
    }
 }
  
 var queueWatch = setInterval(CheckQueue, 20);
 //==============================================================================================