/**
 * George Bonnes 20090715
 * george@olm1.com
 */


// custom google analytics implementation to track page views from
// niagara-usa.com that go to niagara-usa.biz
// data will show in analytics reports as /booking_engine/[ext link]
$(document).ready(function() { 
    // first perform our standard page tracking
    var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www."); 
    $.getScript(gaJsHost + "google-analytics.com/ga.js", function() { 
        try { 
            var pageTracker = _gat._getTracker("UA-8794852-1"); 
            pageTracker._trackPageview(); 
        } catch(err) {}   

        // now loop through each anchor tag and if the href points to niagara-usa.biz 
        // then bind the following on click or on keypress (e.g. enter/return). 
        // track click on the link in analytics as a link to "/booking_engine/[ext link]"
        // notice the use of filter() and not each() for performance
        $('a').filter(function() { 
            var theHref = this; 
            //alert('theHref.hostname: ' + theHref.hostname); return false;
            if (theHref.hostname && theHref.hostname == 'www.niagara-usa.biz') { 
                $(theHref).bind('click keypress', function(event) { 
                    var code=event.charCode || event.keyCode; 
                    if (!code || (code && code == 13)) { 
                        if (pageTracker) { 
                            var fixedLink = this.href; 
                            fixedLink = fixedLink.replace(/https?:\/\/(.*)/,"$1"); 
                            fixedLink = '/booking_engine/' + fixedLink;
                            pageTracker._trackPageview(fixedLink); 
                        } 
                    }; 
                }); 
            }; 
        }); 

        // now bind to each of the widget links to track those clicks via analytics as well
        // widget clicks will show up as "/booking_engine_widget/" entries in analytics
        $('a.booking_engine_control').filter(function() {
            var theHref = this;
            $(theHref).bind('click keypress', function(event) {
                var code = event.charCode || event.keyCode;
                if (!code || (code && code == 13)) {
                    if (pageTracker) {
                        pageTracker._trackPageview('/booking_engine_widget/');
                    }
                }
            });
        });
    });           
}); 
