﻿
var Fairmont = Fairmont || {};

var _gaq = _gaq || [];

// Tracking Number UA-8735706-7


Fairmont.GoogleAnalytics = function (options) {
    var settings = $.extend({
        enabled: true,

        // Google account ID e.g. UA-0000000-00
        accountId: null,

        // clickable elements to attach events to
        elements: ['a', 'area'],

        // collection of link types, each of which may be 
        // identified and click-handled using custom logic
        linkTypes: {
            outbound: {
                cssClass: 'outbound',
                identify: function (obj) {
                    return !obj.href.match(/^mailto\:/) && (obj.hostname !== location.hostname);
                },
                onClick: function () {
                    _gaq.push(['_trackEvent', 'OutboundLink', 'Click', $(this).attr('href')]);
                }
            },
            download: {
                cssClass: 'download',
                identify: function (obj) {
                    var // download filetypes may be extended
                                                     ext = ['pdf', 'doc'],
                                                     pattern = (function () {
                                                         var p = '';
                                                         $.each(ext, function (index, value) {
                                                             p += '\\.' + value + '|';
                                                         });
                                                         p = p.substring(0, p.length - 1);
                                                         return '(' + p + ')$';
                                                     } ()),
                                                     m = obj.href.match(new RegExp(pattern, 'i'));

                    return (m && m.length >= 1);
                },
                onClick: function () {
                    var url = $(this).attr("href"),
                                                     ext = url.split('.').pop().toUpperCase();
                    _gaq.push(['_trackEvent', 'Downloads', ext, url]);
                }
            }
        }
    }, options || {}),
               identifyElements = function () {
                   $.each(settings.linkTypes, function (key, obj) {
                       $.expr[':'][key] = obj.identify;
                       markElements(obj.cssClass);
                   });
               },
               markElements = function (className) {
                   $.each(settings.elements, function (index, value) {
                       $(value + ':' + className).addClass(className);
                   });
               },
               attachHandlers = function () {
                   $.each(settings.linkTypes, function (key, obj) {
                       $.each(settings.elements, function (index, value) {
                           $(value + ':' + key).click(obj.onClick);
                       });
                   });
               };

    identifyElements();

    if (settings.enabled === true) {
        if (!settings.accountId) {
            throw 'You must provide an account ID';
            return false;
        }
        attachHandlers();

        _gaq.push(['_setAccount', settings.accountId]);
        _gaq.push(['_trackPageview']);
        (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);
        } ());
    }
};

Fairmont.isProduction = (function () {
    return (/christmasinnovember\.com$/).test(window.location.hostname);
} ());


$(function () {
    if (Fairmont.isProduction) {
        Fairmont.GoogleAnalytics({ enabled: true, accountId: 'UA-8735706-7' });
    }
});
   
