﻿var yedda_track_prefix = 'Track_';
var yedda_track_pattern = new RegExp(yedda_track_prefix + "[^\\s]+", "g");

var virtual_pageview_tracker = null;

function yedda_oltrack(jqElement) {                       
    var hasService = false;
    if (typeof(Yedda) != 'undefined' && typeof(Yedda.Api) != 'undefined' && typeof(Yedda.Api.Internal) != 'undefined' && typeof(Yedda.Api.Internal.Tracking) != 'undefined' && typeof(Yedda.Api.Internal.Tracking.OutgoingLinksTracker) != 'undefined') {
        hasService = true;
    }        
        
    if (hasService) {    
        
        var yedda_track_outgoinglinks = Yedda.Api.Internal.Tracking.OutgoingLinksTracker.Track;
        
        var host = window.location.host;
        var selector = null;
        if (jqElement == null) {
            selector = $('a');
        } else {
            selector = jqElement;
        }
        selector.each(function () {
            var obj = $(this);

            var linkClass = obj.attr("class");
            var linkHostname = obj[0].hostname;

            if (linkClass != null && typeof (linkClass) != 'undefined') {
                var matches = linkClass.match(yedda_track_pattern);

                if (matches != null || linkHostname != host) {

                    obj.bind('mousedown', function (event) {
                        var classesCombined = "";

                        if (matches != null) {
                            classesCombined = matches.join("|").replace(yedda_track_prefix, "");
                        }

                        try {
                            href = obj.attr("href");
                            if (href == null || href == "") {
                                href = window.location.href;
                            }
                            yedda_track_outgoinglinks(window.location.href, href, classesCombined);
                            if (classesCombined != "") {
                                if (typeof (_gaq) != 'undefined') {
                                    var label = 'Click';
                                    if (classesCombined.indexOf('#') > 0) {
                                        var mySplitResult = classesCombined.split("#");
                                        classesCombined = mySplitResult[0];
                                        label = mySplitResult[1];
                                    }
                                    _gaq.push(['_trackEvent', classesCombined, label])
                                }

                                if (virtual_pageview_tracker == null) {
                                    if (typeof (_gat) != 'undefined') {
                                        var tracker = _gat._getTrackerByName("vpt");
                                        if (tracker != null) {
                                            virtual_pageview_tracker = tracker;
                                        }
                                    }
                                }

                                if (virtual_pageview_tracker) {
                                    if (typeof (AA) != 'undefined' && typeof (AA.pageName) != 'undefined') {
                                        var pageName = AA.pageName;
                                        if (pageName && pageName != '') {
                                            var vUrl = "/" + pageName + "/" + classesCombined;
                                            virtual_pageview_tracker._trackPageview(vUrl);
                                        }
                                    }
                                }
                            }
                        } catch (e) { }
                    });
                }
            }
        });    
    }
}
