/* Event Tracking methods and variables */
var pagelocation = document.location.href;

function TrackEvent(Category, Action, Label, Value) {
    $(document).ready(function () {        
        try
        {
            _gaq.push(['_setAccount', 'UA-10530236-1']);
            _gaq.push(['_setDomainName', 'www.torys.com']);
            _gaq.push(['_setAllowLinker', true]);
            _gaq.push(['_trackEvent', Category, Action, Label, Value]);
        }
        catch (err)
        {
            //alert('Event Error\nCategory:' + Category + '\nAction:' + Action + '\nLabel:' + Label + '\nValue:' + Value + '\n ' + err.description);
        }
    });
}

/* Begin Google Maps Click Tracking */
function TrackOffsiteClicks() {
    try
    {
        $("a").not("[href^='javascript:']").not("[href^='/']").not("[href*='.torys.com']").not("[href*='maps.google.com']").not("[href*='/g.co/']").not("[href*='tel:']").not("[href*='mailto:']").not("[href*='.vcf']").click(function () {
            //setTimeout was required so that this wouldn't be blocked by google popup blocker
            //alert($(this).attr("target"));
            if ($(this).attr('target') && $(this).attr("target").toLowerCase() == "_blank") {
                setTimeout('TrackEvent(\'Page Engagement\', \'Clicked Offsite Link\', \'' + $(this).attr("href") + '\', 1);', 0);
            }
            else {
                TrackEvent('Page Engagement', 'Clicked Offsite Link', $(this).attr("href"), 1);
            }
        });
    }
    catch (err)
    {
    }
}

/* Begin Google Maps Click Tracking */
function TrackvCardClicks() {
    try
    {
        $("[href*='.vcf']").click(function () {
            TrackEvent('Page Engagement', 'Clicked vCard', $(this).attr("href"), 1);
        });
    }
    catch (err)
    {
    }
}

/* Begin Google Maps Click Tracking */
function TrackEmailClicks() {
    try
    {
        $("[href*='mailto:']").click(function () {
            TrackEvent('Page Engagement', 'Clicked Mail To', $(this).attr("href"), 1);
        });
    }
    catch (err)
    {
    }
}

/* Begin Google Maps Click Tracking */
function TrackGoogleMaps() {
    try
    {
        $("[href*='maps.google.com']").click(function () {
            TrackEvent('Page Engagement', 'Clicked Google Maps', $(this).attr("href"), 1);
        });
        $("[href*='/g.co/']").click(function () {
            TrackEvent('Page Engagement', 'Clicked Google Maps', $(this).attr("href"), 1);
        });
    }
    catch (err)
    {
    }
}

/* Exec event tracking functions */
$(document).ready(function () {
    TrackGoogleMaps(); 
    TrackOffsiteClicks();
    TrackvCardClicks(); //tested
    TrackEmailClicks(); //tested
});
