﻿// analytics object, where all external functions 
// are registered and later are executed when the click event happend
var analytics = {
    funcs: [],
    settings: [],
    register: function(fn) {
        this.funcs.push(fn);
    },
    init: function(obj) {
        $.extend(this.settings, obj);
    },
    tracking: function(obj) {
        var d = {
            url: document.URL,
            link: '',
            tag: ''
        };

        $.extend(d, obj);
        $.ajax({
            type: 'POST',
            url: this.settings.url,
            data: d,
            dataType: 'json',
            cache: false,
            async: true,
            complete:function(ret){}
        });
    },
    click: function(obj, value) {
        $.each(this.funcs, function() {
            this(obj, value);
        });
    }
}
$(function() {
    $("[sa]:not('select')").live("click", function() {
        sa_click(this);
    });

    $("select[sa]").live("change", function() {
        sa_click(this);
    });

    function sa_click(t) {
        var el = $(t);
        if (el.is('select')) {
            var selValue = el.val();
            var sa = el.attr("sa").replace("_sacode_", selValue == '' ? 'All' : selValue);
            analytics.click(el, sa);
        
        } else {
            analytics.click(el);
        }
    }
});
