﻿function basketClick(eventSrc, eventName) {
    // Register basket clicks
    _gaq.push(['_trackEvent', 'Cart click', eventName, 'From location: ' + location.pathname, parseInt($('#ShoppingBasketBarControl1_lblNumberOfItems').text().match(/\d+/))]);
}

$(document).ready(function () {
    // Handle form abandonment rate
    $(':input').blur(function () {
        if ($(this).val().length > 0) {
            _gaq.push(['_trackEvent', 'Form', 'input exit :' + location.pathname + $(this).parents('form').attr('name'), $(this).attr('name')]);
        }
    });

    // Login clicked
    $('#ctl00_TopToolBar1_login_btnLogin').click(function () {
        _gaq.push(['_trackEvent', 'Login', 'From location: ' + location.pathname, 'UserName: ' + $('#ctl00_TopToolBar1_login_tbUsername').attr('value')]);
        $(this).unbind('click');
    });

    // chat clicked
    $('#_lpChatBtn').click(function () {
        _gaq.push(['_trackEvent', 'Chat click', 'From location: ' + location.pathname]);
    });

    // Register videoplays
    $('.Medie').click(function () {
        _gaq.push(['_trackEvent', 'Medie', 'Varenummer: ' + $(this).attr("id"), 'Hvilken medie: ' + $(this).attr("href")]);
    });

    // Register productpictures
    /*
    $('.productDetailsPicture, .pp ').click(function () {
        _gaq.push(['_trackEvent', 'Product picture', 'From location: ' + location.pathname, 'Image: ' + $(this).attr("src")]);
    });
    */

    // Register banner shown
    $('div.banner img').each(function () {
        _gaq.push(['_trackEvent', 'Banner shown', 'Image: ' + $(this).attr("src"), 'Shown on location: ' + location.pathname + ' to destination: ' + $(this).parents('a').attr("href")]);
    });

    // Register banner clicks
    $('div.banner img').click(function () {
        _gaq.push(['_trackEvent', 'Banner clicked', 'Image: ' + $(this).attr("src"), 'Clicked on location: ' + location.pathname + ' to destination: ' + $(this).parents('a').attr("href")]);
        $(this).unbind('click');
    });

    // Register approve and go to dibs payment
    $('.approveAndPay').click(function () {
        _gaq.push(['_trackEvent', 'Payment', 'Approve, gå til betaling: ' + $(this).attr("class").replace('approveAndPay ', ''), parseInt($('.subTotal').text().replace(/[\.,]/, '').match(/\d+/))]);
        _gaq.push(['_link', 'https://payment.architrade.com/paymentweb/start.action']);
    });

    // Register generic clicks
    $('.gaEvent').click(function () {
        _gaq.push(['_trackEvent', $(this).text(), 'Clicked on location: ' + location.pathname]);
    });

    //loop though each anchor element
    $('a').each(function () {

        var href = $(this).attr('href');
        var filetypes = /\.(zip|exe|pdf|doc*|xls*|ppt*|mp3|mpg|avi|mov)$/i;

        if (href) {
            //check for links starting with http or https, making sure that links to our own domain are excluded
            if ((href.match(/^https?\:/i)) && (!href.match(document.domain))) {
                $(this).click(function () {
                    var extLink = href.replace(/^https?\:\/\//i, '');
                    _gaq.push(['_trackEvent', 'External link', 'Click', extLink]);
                });
            }
            //check for links starting with mailto:
            else if (href.match(/^mailto\:/i)) {
                $(this).click(function () {
                    var mailLink = href.replace(/^mailto\:/i, '');
                    _gaq.push(['_trackEvent', 'Email', 'Click', mailLink]);
                });
            }
            //check for links with file extension that match the filetypes regular expression:
            else if (href.match(filetypes)) {
                $(this).click(function () {
                    var extension = (/[.]/.exec(href)) ? /[^.]+$/.exec(href) : undefined;
                    _gaq.push(['_trackEvent', 'Download', 'Click - ' + extension, href]);
                });
            }
        }
    });
});
 
