﻿$(document).ready(function () {
    $('.trackLink').click(function (link) {
        Track(this);
        return true;
    });

  
    $('.trackLinkByPageMethod').click(function (link) {
        TrackByPageMethod(this);
        return true;
    });
});


function Track(objLink) {
    var linkType = $(objLink).attr("target") && $(objLink).attr("target") == '_blank' ? 'e' : 'o';
    var trackVars = $(objLink).attr("rel") ? jQuery.parseJSON($(objLink).attr("rel").replace(/'/g, "\"")) : {};
    var linkName = trackVars.linkName ? trackVars.linkName : ($(objLink).attr("name") ? $(objLink).attr("name") : $(objLink).attr("text"));
    linkName = linkName.replace(/&#39;/g, '\'');
    linkType = trackVars.linkType ? trackVars.linkType : linkType;
    scj.omniture.trackLink(trackVars, { "linkName": linkName, "linkType": linkType }, objLink);
}

function TrackByPageMethod(objLink) {
    //var linkType = $(objLink).attr("target") && $(objLink).attr("target") == '_blank' ? 'e' : 'o';
    var trackVars = $(objLink).attr("rel") ? jQuery.parseJSON($(objLink).attr("rel").replace(/'/g, "\"")) : {};
    //var linkName = trackVars.linkName ? trackVars.linkName : ($(objLink).attr("name") ? $(objLink).attr("name") : $(objLink).attr("text"));
    scj.omniture.track(trackVars);
}


function InitializeHierarchyVariables() {
    /* Hierarchy Variables */
    s.hier1 = '';
}

function TrackOmnitureFromSIFR(selector) {

    //alert('SIFR Omniture Log Start');
    //alert(selector)
    //var anchor = $(selector).find("a.trackLink");
    Track($(selector).find("a.trackLink"));
    //alert('Omniture is tagged. Check Fiddler Now');
    return true;
}


/*
* R/GA written function from Scrubbing bubbles. The global.js functions from Glade use mootools
*/


function update_tracking(obj) {

    var obj = obj ? obj : {};

    for (key in obj) {
        s[key] = obj[key];
    }

    var s_code = s.t(); if (s_code) document.write(s_code);
    clear_tracking();
}

function clear_tracking() {
    s.events = '';
    s.products = '';
    s.channel = '';
    s.pageName = '';

    for (i = 1; i < 30; i++) {
        s['prop' + i.toString()] = '';
    }

    for (i = 1; i < 30; i++) {
        s['eVar' + i.toString()] = '';
    }

}

var scj = {};

scj.omniture = {
    symbols: ['&#174;', '&#8482;', '&copy;', '&trade;', '&reg;', '%AE', '%3F', '%u2122', '&#153;'],
    clear: function () {
        s = s_gi(s_account);
        s.events = '';
        s.products = '';
        s.pageName = '';
        s.linkTrackVars = '';
        s.linkTrackEvents = '';

        for (i = 1; i < 51; i++) {
            s['prop' + i.toString()] = '';
            s['eVar' + i.toString()] = '';
        }

        return true;
    },
    format: function (name) {
        return name.underscore().replaceSymbols(scj.omniture.symbols).toLowerCase();
    },
    track: function (tagObj) {
        s = s_gi(s_account);

        var tagObj = tagObj ? tagObj : {};
        for (key in tagObj) { s[key] = tagObj[key]; }
        s_code = s.t(); if (s_code) document.write(s_code);
        scj.omniture.clear();
    },
    trackLink: function (tagObj, trackLinkObj) {
        //tagObj sample - {events: "event5", channel: "Product", eVar6: "value", prop6: "value"}
        //trackLink will not record page view so if you include a pageName key in the tagObj that will cause an error
        //trackLinkObj sample s.tl(this,'o','Link Name');
        // var trackLinkObj = {elementName: "View Testimonial Button", 
        //                     linkType: "[o|d|e]", 
        //					   linkName: "View Testimonial OnClick"}
        s = s_gi(s_account);
        scj.omniture.clear();
        var tagObj = tagObj ? tagObj : {};
        var trackLinkObj = trackLinkObj ? trackLinkObj : {};
        for (key in tagObj) {
            if ( key != 'linkType') {// key != 'pageName' &&
                s[key] = tagObj[key];
                s.linkTrackVars += key + ',';
            }
            else if (key == 'linkType') {
                if (tagObj[key] == 'custom') {
                    trackLinkObj.linkType = 'o';
                }
                else if (tagObj[key] == 'exit') {
                    trackLinkObj.linkType = 'e';
                }
            }
            else if (tagObj.events) {
                s.linkTrackEvents = tagObj.events;
            } else { ; }
        }
        s.tl(trackLinkObj.elementName ? trackLinkObj.elementName : {}, trackLinkObj.linkType, trackLinkObj.linkName);
    }
};

