// Creation and initialization of a RIAx object makes one tracking call 
// which captures referring domain, source query string parameter,
// and search provider/keywords if applicable.
//
RIAx = 
{
    // Initialization Method
    Go : function( project )
    {

        // reference to document referrer to save bytes below
        var dr = document.referrer;

        var src = this.GetQuerystringValue( "source" );
        var search = this.GetSearchResult( dr );
        var keywords = this.GetSearchKeywords( dr );
        var domain = this.GetDomain( dr );
        var base = "http://switch.atdmt.com/action/" + project + "/v3/";
        var data = "location.home/action.view/source." + src + "/search." + search + "/keywords." + keywords + "/domain." + domain + "/?" + Math.random();
        var url = base + data;
        var image = new Image();

        // bind to event handler
        image.onload = function()
        {
            // cleanup memory
            if (!(delete image))
            {
                image = null;
            } 
        }
        image.onerror = function()
        {
            // cleanup memory
            if (!(delete image))
            {
                image = null;
            }
        }
        image.onabort = function()
        {
            // cleanup memory
            if (!(delete image))
            {
                image = null;
            }
        }
        
        // send tracking by setting src property of image
        image.src = url;
    },

    
    // capture search keywords from search queries
    GetSearchKeywords : function( i )
    {        
        // loop through all m_searchProviders
        for ( var search in this.m_searchProviders )
        {
            // check for a match
            if ( i.indexOf( this.m_searchProviders[ search ].form ) == 0 )
            {                
                // extract keywords
                return this.GetQuerystringValue( this.m_searchProviders[ search ].key, i ).replace( /[+]/g, "," );
            }
        }
        
        // no major search m_searchProviders found, return empty string
        return "";
    },
    
    // capture search engine name from search queries
    GetSearchResult : function( i )
    {        
        // loop through all m_searchProviders
        for ( var search in this.m_searchProviders )
        {
            // check for a match
            if ( i.indexOf( this.m_searchProviders[ search ].form ) == 0 )
            {                
                // return search provider name
                return search;
            }
        }
        
        // no major search m_searchProviders found, return empty string
        return "";
    },
    
    // extract a domain value from a document referrer string
    GetDomain : function( i )
    {   
        var regex  = "(?:http|https)://(.*?)/";
             
        if ( i && i.length > 0 )
        {
            var matches = i.match( regex );
            
            if ( matches && matches.length > 1 )
            {
                return encodeURIComponent( matches[ 1 ] );
            }
        }
        
        return "";
    },
    
    // gets a querystring value
    GetQuerystringValue : function( s, u )
    {
        s += "=";
        var h  = u ? u : window.location.href,
            ss = h.indexOf( s ),
            a  = "&";
        q = h.indexOf( '?' );
        if ( q > 0 && ss > q )
        {
            sh = h.substring( ss + s.length );
            return sh.indexOf( a ) > 0 ? 
                       sh.substring( 0, sh.indexOf( a ) ) : 
                       sh;
        }
        return "";
    },
	
	m_searchProviders :
    {
        Live   : { form : "http://search.live.com/results.aspx?", key : "q"  },
        Google : { form : "http://www.google.com/search?",        key : "q"  },
        Yahoo  : { form : "http://search.yahoo.com/search?",      key : "p"  },
        Ask    : { form : "http://www.ask.com/web?",              key : "q"  },
        Baidu  : { form : "http://www.baidu.com/s?",              key : "wd" }    
    },

    // cleanup images
    OnImageLoaded : function( key )
    {
        // cleanup memory
        if (!(delete this.m_imgCollection[key]))
        {
            this.m_imgCollection[key] = null;
        } 
    }, 

    // cleans up a string and removes 'bad' characters
    CleanString : function( i )
    {
        return i.replace( /\/|\?|&|\.|#/g, "_" );
    },
  
    // cleans up a string and removes 'bad' characters except dot
    // this is for addtional parameter to remain its dot
    CleanStringExceptDot : function( i )
    {
        return i.replace( /\/|\?|&|#/g, "_" );
    }

}