
/*

amaze.autotag.v1.1

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/
PG - Amended to work as jquery plugin & to have the option to use async tracking code

*/

(function( $ ){
    $.fn.AmazeAutoTag = function(options) {

        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\:/,
                isAsync: true
            },
            
            strCurrentPath: "",
            strCurrentFolder: "",
            
            host: "",    
            pageTrackerFound: false,
            
            root: null,

            init: function( optionOverrides, jqRoot ) {
                
                //alert( "amaze.autotag.v1" );
                if ( optionOverrides )
                    $.extend( this.options, optionOverrides );
                
                if ( typeof( _gaq ) == 'undefined' && this.options.isAsync ) return;
                if ( typeof( pageTracker ) == 'undefined' && !this.options.isAsync ) return;
                
                pageTrackerFound = true;
                
                
                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.root = jqRoot;
                
                this.findLinksAndAttachEvents();
            },    
            
            findLinksAndAttachEvents: function() {
            
                this.root.find( "a[href]" ).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;
                
                    amazeAutoTag.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;
                
                    amazeAutoTag.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;
                    
                    amazeAutoTag.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;
                        
                   amazeAutoTag.trackPageView( outboundLink );
                    
                } );
                
            },
            
            attachRSSEvent: function( link ) {
            
                $( link ).click( function( e ) {
                    var tmp = $( link ).attr( "href" ).substr( $( link ).attr( "href" ).lastIndexOf( "/" ) + 1, $( link ).attr( "href" ).length );
                    var rss = ( amazeAutoTag.options.blnRSSIncludePath )
                        ? amazeAutoTag.strCurrentFolder + "rss/" + tmp :
                        "rss/" + tmp;
                    amazeAutoTag.trackPageView( rss );
                } );
            
            },
            
            fireNoResultsEvent: function( searchTerm ) {
                    amazeAutoTag.trackPageView( "/zerosearchresults/" + searchTerm );
            },
            
            trackPageView: function(path) {
                if ( pageTrackerFound && amazeAutoTag.options.isAsync )
                    _gaq.push(['_trackPageview', path]);
                
                if ( pageTrackerFound && !amazeAutoTag.options.isAsync )
                    pageTracker._trackPageview( download );
            }
            
        };
        
        amazeAutoTag.init(options,this);
        return this;
    }
})( jQuery );


//$( 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
    } );
    
    */
    
//} );
