﻿function recordOutboundLink(link, category, action, label, IsEvent) {
    _gat._getTrackerByName()._trackEvent(category, action, label);

    //we don't need to open link since jQuery just register an onlick event
    if (false) {
        // add code to check href.target is present
        if ((link.target != null) && (link.target.length > 0)) {
            // then pop a new window open and load link.href.
            window.open(link.href, link.target);
        } else {
            // if empty, load in existing window
            setTimeout('document.location = "' + link.href + '"', 100);
        }
    }

 }

function recordImpression(link, category, action, label) {
 _gat._getTrackerByName()._trackEvent(category, action, label);
}

var urlParams = {}; 
(function() {
    var e,
    a = /\+/g,  // Regex for replacing addition symbol with a space
    r = /([^&=]+)=?([^&]*)/g,
    d = function(s) { return decodeURIComponent(s.replace(a, " ")); },
    q = window.location.search.substring(1);
    while (e = r.exec(q)) urlParams[d(e[1])] = d(e[2]);
})();

jQuery(document).ready(
function() {
    jQuery.expr[':'].all_links = function(obj) {
        return !obj.href.match(/^mailto\:/);
    };
    jQuery("a:all_links").click(function() {

        var sessionId = $.cookie("SC_ANALYTICS_SESSION_COOKIE");
        var itemId = $('meta[name=scID]').attr("content");

        var sc_array;
        var sc_language;
        var sc_name;

        if (itemId != null) {
            sc_array = itemId.split("|");
            if ((sc_array != null) && (sc_array[0] != null)) {
                sc_itemId = sc_array[0];
            } else {
                sc_itemId = '';
            }
            if ((sc_array != null) && (sc_array[1] != null)) {
                sc_language = sc_array[1];
            } else {
                sc_language = "";
            }
            if ((sc_array != null) && (sc_array[2] != null)) {
                sc_name = sc_array[2];
            } else {
                sc_name = "";
            }
        }



        var containerSelector = $(this).closest("div[class='linkcontainer']")
        if (containerSelector.length) {
            var containerId = containerSelector.attr("name");
            if (containerId != null) {

                //alert(containerId);
                //alert(sessionId);
                /*
                jQuery.ajax({
                    type: "POST",
                    url: "/ClickSource.asmx/SaveClickSource",
                    data: '{ "sessionId":"' + sessionId + '", "sourceItemId":"' + containerId + '", "itemId":"' + itemId + '", "Url":"' + $(this).attr('href') + '" }',
                    contentType: "application/json; charset=utf-8",
                    dataType: "json"
                });
                */

                var link = $(this).attr('href');
                if ((link != null) && ((link.indexOf("http") >= 0) || (link.indexOf("daasclick.asp") >= 0))) {

                    if ((sc_itemId == '{556A1B5F-D80E-4EEF-B041-903145B3F44A}')  // matched awards
                            || (sc_itemId == '{8662AA0B-AB11-418C-A652-D883E2B70A5A}') // awards details   
                            || (sc_itemId == '{FBE23604-4A1D-43C1-8FCE-565EBBF0141F}')) // offers details
                    {
                        // let get awardID from the query string
                        var AwardID = urlParams["id"];

                        var target = $(this).attr('target');
                        var title = $(this).attr('title');
                        var name = $(this).attr('name');
                        var hrefTitle = '';
                        var category = 'Award Details';

                        if (sc_itemId == '{556A1B5F-D80E-4EEF-B041-903145B3F44A}') {
                            category = 'Matched ' + category;
                        }

                        if (sc_itemId == '{FBE23604-4A1D-43C1-8FCE-565EBBF0141F}') {
                            category = 'Offer Details';
                        }

                        if ((title != null) && (title.length > 0)) {
                            hrefTitle = title;
                        }
                        if ((name != null) && (name.length > 0) && (hrefTitle.length == 0)) {
                            hrefTitle = name;
                        }
                        if ((link != null) && (link.length > 0) && (hrefTitle.length == 0)) {
                            hrefTitle = link;
                        }

                       // alert(AwardID + '\nhref:' + link + '\ntarget:' + target + '\ntitle:' + title + '\nname:' + name);

                        var GA_Label = 'AwardID:' + AwardID + '(' + sc_language + '_' + hrefTitle + ')';

                        //alert(GA_Label);

                        recordOutboundLink(this, category, "Link Click", GA_Label, true)

                    }
                }
            }

        } else {
            // check if it it is external link
            var link = $(this).attr('href');
            if ((link != null) 
                    && (link.indexOf("http") >= 0) 
                    // we exclude absolute internal links
                    && (link.indexOf("http://www.studentawards.com") == -1)
                    && (link.indexOf("http://studentawards.com") == -1)
                    && (link.indexOf("http://www.boursetudes.com") == -1)
                    && (link.indexOf("http://boursetudes.com") == -1)
                    ) {
/*
                if ((sc_itemId == '{556A1B5F-D80E-4EEF-B041-903145B3F44A}')  // matched awards
            || (sc_itemId == '{556A1B5F-D80E-4EEF-B041-903145B3F44A}')) // awards details
                {
                    // let get awardID from the query string
                    var AwardID = urlParams["id"];
                    //alert(AwardID);
                } 
                else
                {
                */
                    var linkTitle = $(this).attr('title');
                    var linkName = $(this).attr('name');
                    var GA_Label =link;
                    if ( (linkTitle !=null) && (linkTitle.length > 0)  )
                    {
                        GA_Label = linkTitle;
                    } else
                    {
                        if ( (linkName !=null) && (linkName.length > 0)  )
                        {
                            GA_Label = linkName;
                        }
                    }
                    
                    GA_Label = sc_name+"("+GA_Label+")";
                    recordOutboundLink(this, "External Link", "Link Click", GA_Label, true);
               // }

                /*
                jQuery.ajax({
                    type: "POST",
                    url: "/ClickSource.asmx/SaveClickSource",
                    data: '{ "sessionId":"' + sessionId + '", "sourceItemId":"", "itemId":"' + itemId + '", "Url":"' + link + '" }',
                    contentType: "application/json; charset=utf-8",
                    dataType: "json"
                });
                */
            }
        }
    })
});

