
/*

amaze.autotag.v1

NOTES: Re-work of initial autotag.js supplied by Adam.G
Constructed using javascript object literal notation, http://www.dustindiaz.com/json-for-the-masses/

TODO: Very much a beta, needs a bit more thought about structure and the options override.
Should we be able to turn certain events on/off...?
Look at adding in RSS event tracking but we need a standard way of pinpointing RSS links.
You could add a class="rss" to each link or make sure you output a <link /> in the head which would then reference the link on the page.
For example: <link rel="alternate" type="application/rss+xml" title="RSS" href="url/to/rss/file" />

*/


var amazeAutoTag = {

    options: {
        blnDocumentIncludePath: true,
        blnDisableDocuments: false,
        blnImageIncludePath: true,
        blnDisableImages: false,
        blnEmailIncludePath: true,
        blnDisableEmails: false,
        blnOutboundLinkIncludePath: true,        
        blnDisableOutboundLinks: false,
        blnRSSIncludePath: true,
        blnDisableRSS: false,
        regexDocuments: /^.+\.((doc)|(docx)|(pdf))$/g, 
        regexImages: /^.+\.((gif)|(jpg)|(png))$/, 
        regexMailto: /^mailto\:/
    },
    
    strCurrentPath: "",
    strCurrentFolder: "",
    
    host: "",    
    pageTrackerFound: false,

    init: function( optionOverrides ) {
        
        //alert( "amaze.autotag.v1" );
        
        if ( typeof( pageTracker ) == 'undefined' ) return;
        
        pageTrackerFound = true;
        
        if ( optionOverrides )
            $.extend( this.options, optionOverrides );
        
        this.host = window.location.host.toLowerCase();
        
        this.strCurrentPath = document.location.pathname.replace( "default.aspx", "" );
        this.strCurrentFolder = this.strCurrentPath;
        this.strCurrentFolder = ( this.strCurrentFolder.search( /\.(aspx)$/ ) > 0 ) ? this.strCurrentFolder + "/" : this.strCurrentFolder;
        
        this.findLinksAndAttachEvents();
    },    
    
    findLinksAndAttachEvents: function() {
    
        $( "a" ).each( function( index ) {
        
            var match;
        
            if ( !amazeAutoTag.options.blnDisableDocuments )
            {        
                match = $( this ).attr( "href" ).match( amazeAutoTag.options.regexDocuments );
                
                if ( match )
                {
                    amazeAutoTag.attachDownloadEvent( this );                
                    return;
                }
            }
            
            if ( !amazeAutoTag.options.blnDisableImages )
            {
                match = $( this ).attr( "href" ).match( amazeAutoTag.options.regexImages );
                
                if ( match )
                {                
                    amazeAutoTag.attachImageEvent( this );
                    return;
                }
            }
            
            if ( !amazeAutoTag.options.blnDisableEmails )
            {            
                match = $( this ).attr( "href" ).match( amazeAutoTag.options.regexMailto );
                
                if ( match )
                {                
                    amazeAutoTag.attachEmailEvent( this );
                    return;
                }
            }
            
            if ( !amazeAutoTag.options.blnDisableOutboundLinks )
            {
                if ( $( this ).is( "a[href^='http']:not([href^='http://" + amazeAutoTag.host + "']):not([href^='http://www." + amazeAutoTag.host + "']):not([href^='https://" + amazeAutoTag.host + "']):not([href^='https://www." + amazeAutoTag.host + "'])" ) ) 
                {
                    amazeAutoTag.attachOutboundLinkEvent( this );
                    return;
                }
            }
            
            if ( !amazeAutoTag.options.blnDisableRSS )
            {
                if ( $( this ).is( "a[class*='rss-link']" ) )
                {
                    amazeAutoTag.attachRSSEvent( this );
                    return;
                }
            }
            
        } );
    
    },
    
    attachDownloadEvent: function( link ) {
    
        $( link ).click( function() {
    
            var tmp = $( link ).attr( "href" ).substr( $( link ).attr( "href" ).lastIndexOf( "/" ) + 1, $( link ).attr( "href" ).length );
            
            var download = ( amazeAutoTag.options.blnDocumentIncludePath )
                ? amazeAutoTag.strCurrentFolder + "download/" + tmp :
                "download/" + tmp;
        
            pageTracker._trackPageview( download );
        
        } );
            
    },
        
    attachImageEvent: function( link ) {
    
        $( link ).click( function() {
    
            var tmp = $( link ).attr( "href" ).substr( $( link ).attr( "href" ).lastIndexOf( "/" ) + 1, $( link ).attr( "href" ).length );
            
            var image = ( amazeAutoTag.options.blnImageIncludePath )
                ? amazeAutoTag.strCurrentFolder + "download/" + tmp :
                "download/" + tmp;
        
            pageTracker._trackPageview( image );
        
        } );
            
    },
    
    attachEmailEvent: function( link ) {
    
        $( link ).click( function() {
        
            var tmp = $( link ).attr( "href" ).substr( $( link ).attr( "href" ).lastIndexOf( "mailto:" ), $( link ).attr( "href" ).length ).replace( "mailto:", "" );
            
            var email = ( amazeAutoTag.options.blnEmailIncludePath )
                    ? amazeAutoTag.strCurrentFolder + "email/" + tmp :
                    "email/" + tmp;
            
            pageTracker._trackPageview( email );
        
        } );
    
    },
    
    attachOutboundLinkEvent: function( link ) {
    
        $( link ).click( function() {
        
            var tmp = $( link ).attr( "href" ).replace( /(http:\/\/)/, "" );
                
            var outboundLink = ( amazeAutoTag.options.blnOutboundLinkIncludePath ) 
                ? amazeAutoTag.strCurrentFolder + "outboundlink/" + tmp :
                "outboundlink/" + tmp;
                
           pageTracker._trackPageview( outboundLink );
            
        } );
        
    },
    
    attachRSSEvent: function( link ) {
    
        $( link ).click( function( e ) {
        
            //e.preventDefault();
            
            //alert( $( link ).attr( "href" ) );
        
            var tmp = $( link ).attr( "href" ).substr( $( link ).attr( "href" ).lastIndexOf( "/" ) + 1, $( link ).attr( "href" ).length );
            
            //alert( tmp );
            
            var rss = ( amazeAutoTag.options.blnRSSIncludePath )
                ? amazeAutoTag.strCurrentFolder + "rss/" + tmp :
                "rss/" + tmp;
                
            //alert( rss );
        
            pageTracker._trackPageview( rss );
        
        } );
    
    },
    
    fireNoResultsEvent: function( searchTerm ) {
    
        if ( pageTrackerFound )
            pageTracker._trackPageview( "/zerosearchresults/" + searchTerm );
    
    }
    
}


$( document ).ready( function() {
    
    amazeAutoTag.init();
    
    /* 
    
    You can pass in override options at this point, monitor powerpoint docs for example...    
    
    amazeAutoTag.init( {
        blnDocumentIncludePath: false,
        regexDocuments: /^.+\.((doc)|(docx)|(pdf)|(ppt)|(pptx))$/g,
        blnDisableEmails: true,
        blnDisableOutboundLinks: true,
        blnDisableImages: true
    } );
    
    */
    
} );
