﻿//----------------
//Google Analytics
//----------------
// add middleclick event to jQuery
jQuery.event.special.middleclick = {
    setup: function (data, namespaces) {
        var elem = this, $elem = jQuery(elem);
        $elem.bind('mousedown mouseup mouseout', jQuery.event.special.middleclick.handler);
    },

    teardown: function (namespaces) {
        var elem = this, $elem = jQuery(elem);
        $elem.unbind('mousedown mouseup mouseout', jQuery.event.special.middleclick.handler);
    },

    handler: function (event) {
        var elem = this, $elem = jQuery(elem);
        if (event.type == 'mousedown' && event.which == 2)
            $elem.data('mouseTrigger', 1);
        else
            if (event.type == 'mouseout')
                $elem.data('mouseTrigger', 0);
            else
                if (event.type == 'mouseup') {
                    if (event.which == 2 && $elem.data('mouseTrigger')) {
                        event.type = "middleclick";
                        jQuery.event.handle.apply(this, arguments)
                    }
                    $elem.data('mouseTrigger', 0);
                }
    }
};

//$('selector').bind('click middleclick', function (event)
//   {
//    var now = +new Date;
//    if ($(this).data('lastClick') && now - $(this).data('lastClick') < 500)
//      return; // filter double click for IE
//    $(this).data('lastClick', now);
//   });
function getGATracker() {
    var args = arguments;
    return function () {
        _gaq.push.apply(_gaq, args);
    };
}
function makeDefferedLinkRequest(event, link, isTargetBlank) {
    event.preventDefault();
    event.stopPropagation();
    if (isTargetBlank) {
        if (!makeDefferedLinkRequest.form) {
            makeDefferedLinkRequest.form = document.createElement("form");
            makeDefferedLinkRequest.form.method = "POST";
            makeDefferedLinkRequest.form.target = "_blank";
            document.body.appendChild(makeDefferedLinkRequest.form);
        }
        makeDefferedLinkRequest.form.action = link;
        makeDefferedLinkRequest.form.submit();
    }
    else
        setTimeout("document.location='" + link + "'", 100);
}

function subscribeGoogleAnalytics() {
    if (!window._gaq)
        return;
    // track all links with http:// prefix but not for current domain
    $("a[href^='http://']").not("*[href*='http://" + window.location.hostname + "']").click(
        function (event) {
            getGATracker(["_trackPageview", "/virtual/external/" + $(this).attr("href").replace("http://", "")])();
            makeDefferedLinkRequest(event, $(this).attr("href"), $(this).attr("target") == "_blank");
        });

    // track Buy online link
    $("a.buy").click(
        function() {
            getGATracker(['_trackEvent', 'Kop pa Apoteket', $(this).attr('rel'),
                    document.location.pathname + document.location.search, 1])();
        });

    // track home teasers clicks
    $('.subExposureArea .box a.textWrapper').parent().click(
        function() {
            // '_trackEvent', 'image-home page', title, URL
            getGATracker(['_trackEvent', 'image-home page', $(this).find('h2').text(), $(this).find('.textWrapper').attr("href").replace("http://", "")])();
        });

    // track home dynamic banners clicks
    $('#first-carousel a').click(
        function(event) {
            // '_trackEvent', 'banner- home page', 'title', 'URL'
            getGATracker(['_trackEvent', 'banner- home page', $(this).parents('.jcarousel-item').find('h2').text(), $(this).attr("href").replace("http://", "")])();
            makeDefferedLinkRequest(event, $(this).attr("href"));
        });

    // track document downloads
    $('a[href*=".pdf"],a[href*=".zip"],a[href*=".doc"],a[href*=".docx"],a[href*=".txt"]').click(
        function() {
            var filetype = ( /\.\w{3,4}/i ).exec($(this).attr('href'))[0];
            var docpath = $(this).attr('href').replace("http://", "");
            getGATracker(['_trackPageview', '/virtual/download/' + filetype + '/' + docpath])();
            makeDefferedLinkRequest(event, $(this).attr("href"));
        });

    // track links in the menu for a product article
    $(".anchorLinks a").click(
        function() {
            var linktitle = $(this).text();
            getGATracker(['_trackPageview', document.location.pathname + '?link=' + linktitle])();
        });
    
    // track email links
    $("a[href^='mailto:']").click(
        function() {
            var receiver = $(this).attr('href').replace("mailto:", "");
            getGATracker(['_trackPageview', '/virtual/contact/e-mail/' + receiver, document.location.pathname + document.location.search])();
        });
}
// fire tracking
$(document).ready(subscribeGoogleAnalytics);

