// Name: BleauGoogleAnalyticsFunctions.js
/// <summary>The following code is used when calculating the age of content</summary>
// You will need to add these lines in the HEAD of code for it to work
/* 
 <script type="text/javascript">  // This object contains a Google Analytics UA for each Dynamicweb Area ID.  var _gaq = _gaq || [];  _gaq.push(['_setAccount', 'UA-8042213-1']); // Change to your own UA code </script> <script type="text/javascript" src="/Files/System/js/BleauGoogleAnalyticsFunctions.js"></script>*/


// Use this JavaScript to send the value to Google Analytics: _gaq.push(['_setCustomVar', 1, 'Content', getDays('<!--@DwPageUpdateDate-->')]);
function getDays(sDDMMYYYY) {
    var aDate = sDDMMYYYY.split('-')
    var ms = new Date() - new Date(aDate[2], aDate[1] - 1, aDate[0]);
    var iDay = 1000 * 60 * 60 * 24;
    return parseInt(ms / iDay)
}

// The following code is used if the site is using Dynamicweb 404-pages
var url = location.pathname;
if (url.indexOf("404") != -1) {
    _gaq.push(['_trackPageview', '/404.html?page=' + document.location.pathname + document.location.search + '&from=' + document.referrer]);
} else {
    _gaq.push(['_trackPageview']);
}

$(document).ready(function () {
    // Handle form abandonment rate
    $(':input').blur(function () {
        if($(this).val().length > 0){
            _gaq.push(['_trackEvent', 'Form', location.pathname + '/' + $(this).parents('form').attr('name'), $(this).attr('name')]);
        } 
    });

    // Handle file downloads
    $('a[href*="Files"]').click(function (data) {
        //_gaq.push(['_trackPageview', this.href]);
        _gaq.push(['_trackEvent', 'Download', this.href, location.pathname]);
    });

    // Handle login form submit
    $('#ExtUserFormPage').submit(function (data) {
        _gaq.push(['_trackEvent', 'Form', 'ExtUserFormPage', 'User_logged_in']);
    });

    // Handle mailto: clicks
    $('a[href*="mailto:"]').click(function (data) {
        _gaq.push(['_trackEvent', 'Email', location.pathname, this.href]);
    });

    // Handle external links
    $("a[href^='http']").not($('a[href*="' + document.domain + '"]')).click(function () {
        _gaq.push(['_trackEvent', 'udgående', "'" + this.href + "'", "'" + location.pathname + "'"]);
    });
});

// Insert GA-code
(function() {
  var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
  ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
  var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();

