/*-------------------------------------------------------+
 | Utilizes event tracking in Google Analytics to track  |
 | clicks on outbound, document, email, and image links. |
 |                                                       |
 | Requires jQuery                                       |
 +-------------------------------------------------------*/

(function ($) {
    $(document).ready(function () {
        $('a').on('click', function (e) {
            var $a = $(this);
            var href = $a.attr("href");

            var ga = window.ga || function () { };

            if (href !== undefined) {
                if ((href.match(/^http/i)) && (!href.match(document.domain))) {
                    var category = "Outgoing";
                    var event = "Click";
                    var label = href;
                    ga('send', 'event', category, event, label, { 'hitCallback': function () { } });
                }

                if (href.match(/\.(avi|doc|docx|exe|mov|mp3|pdf|ppt|pptx|rar|txt|vsd|vxd|wma|wmv|xls|xlsx|zip)$/i)) {
                    var category = "Downloads";
                    var event = "Click";
                    var label = href;
                    ga('send', 'event', category, event, label, { 'hitCallback': function () { } });
                }

                if (href.match(/^mailto:/i)) {
                    var category = "Emails";
                    var event = "Click";
                    var label = href;
                    ga('send', 'event', category, event, label, { 'hitCallback': function () { } });
                }
            }
        });
    });
})(jQuery);;
var GaEventTracker = (function ($) {

    var lastCall;

    var ga = window.ga || function () { };
    var console = window.console || function () { };

    return {
        Track: function (category, action, label) {
            var ga = window.ga || function () { };            
            var now = new Date().getTime();
            var duplicate = false;

            if (lastCall === undefined) {
                lastCall = {
                    action: action,
                    category: category,
                    label: label,
                    time: now
                }
            }
            else {
                if (lastCall.category === category && lastCall.action === action && lastCall.label === label && now - lastCall.time < 1000) {
                    duplicate = true;
                }
            }

            if (!duplicate) {
                //console.log(category, '|', action, '|', label);
                ga('send', 'event', category, action, label, { 'hitCallback': function () { } });

                lastCall = {
                    action: action,
                    category: category,
                    label: label,
                    time: now
                }
            }
        }
    }

})(jQuery);;
var jjgpsGaEventTracker = (function ($, et) {

    var vars = {
        text: {
            jb: 'JB',
            jd: 'JD',
            jjs: 'JJS',
            ref: 'REF',
            si: 'SI',
            soi: 'SOI'
        }
    }

    return {
        category: {
            jb: {
                delinquencyAgeBoundaries: vars.text.jb + ' > Delinquency age boundaries',
                transferDiscretion: vars.text.jb + ' > Transfer discretion',
                policyBoundaries: vars.text.jb + ' > Compare transfer provisions',
                transferProvisions: vars.text.jb + ' > Transfer provisions',
                transferTrends: vars.text.jb + ' > Transfer trends',
                progressiveData: vars.text.jb + ' > Progressive data',
                state: {
                    transferPolicies: 'State profile > ' + vars.text.jb + ' > Transfer policies',
                    transferTrends: 'State profile > ' + vars.text.jb + ' > Transfer trends'
                }
            },
            jc: {
            	courtroomShackling: vars.text.jc + ' > Courtroom shackling',
            	competencyToStandTrial: vars.text.jc + ' > Competency to stand trial',
            	state: {
            		
            	}
            },
            jd: {
                defenseStructure: vars.text.jd + ' > Defense structure',
                waiverOfCounsel: vars.text.jd + ' > Waiver of counsel',
                timingOfCounsel: vars.text.jd + ' > Timing of counsel',
                indigencyRequirements: vars.text.jd + ' > Indigency requirements',
                collateralConsequences: vars.text.jd + ' > Collateral consequences',
                nationalOutcomes: vars.text.jd + ' > National outcomes',
                trendOfLegalCounsel: vars.text.jd + ' > Trend of legal counsel',
                reportedData: vars.text.jd + ' > Reported data',
                progressiveData: vars.text.jd + ' > Progressive data'
            },
            jjs: {
                basicServices: vars.text.jjs + ' > Basic services',
                correctionsAgency: vars.text.jjs + ' > Corrections agency',
                evidenceBasedPractices: vars.text.jjs + ' > Evidence-based practices',
                progressiveRecidivismData: vars.text.jjs + ' > Progressive recidivism data',
                recidivismReporting: vars.text.jjs + ' > Recidivism reporting',
                releaseDecision: vars.text.jjs + ' > Release decision',
                riskAssessment: vars.text.jjs + ' > Risk assessment',
                solitaryConfinement: vars.text.jjs + ' > Solitary confinement',
                intakeDiscretion: vars.text.jjs + ' > Intake discretion',
                purposeClauses: vars.text.jjs + ' > Purpose clauses',
                sexOffenseRegistry: vars.text.jjs + ' > Sex offense registry',
            },
            si: {
                agencyIntegration: vars.text.si + ' > Agency integration',
                coordination: vars.text.si + ' > Coordination',
                reportedData: vars.text.si + ' > Reported data',
                progressiveData: vars.text.si + ' > Progressive data',
                state: {
                    reportedData: 'State profile > ' + vars.text.si + ' > Reported data'
                }
            },
            soi: {
                labeling: vars.text.soi + ' > Labeling',
                ageBoundaries: vars.text.soi + ' > Age boundaries',
                nationalOutcomes: vars.text.soi + ' > National outcomes',
                reportedData: vars.text.soi + ' > Reported data',
                progressiveData: vars.text.soi + ' > Progressive data',
                state: {
                    structure: 'State profile > ' + vars.text.soi + ' > Structure',
                    ageBoundaries: 'State profile > ' + vars.text.soi + ' > Age boundaries',
                    nationalOutcomes: 'State profile > ' + vars.text.soi + ' > National outcomes',
                    reportedData: 'State profile > ' + vars.text.soi + ' > Reported data'
                }
            },
            ref: {
            	populations: vars.text.ref + ' > Populations',
            	tribalDelinquency: vars.text.ref + ' > Tribal delinquency jurisdiction',
                monitoringMethods: vars.text.ref + ' > Monitoring methods',
                reportedData: vars.text.ref + ' > Reported data',
                dmcCoordinators: vars.text.ref + ' > DMC coordinators',
                progressiveData: vars.text.ref + ' > Progressive data',
                state: {
                	populations: 'State profile > ' + vars.text.ref + ' > Populations',
                	tribalDelinquency: 'State profile > ' + vars.text.ref + ' > Tribal delinquency jurisdiction',
                    monitoringData: 'State profile > ' + vars.text.ref + ' > Monitoring data',
                    reportedData: 'State profile > ' + vars.text.ref + ' > Reported data',
                    dmcAssessment: 'State profile > ' + vars.text.ref + ' > DMC assessment',
                    dmcCoordinators: 'State profile > ' + vars.text.ref + ' > DMC coordinators'
                }
            },
            generic: {
                jumpToSection: 'Jump to section',
                stateSwitcher: 'State switcher'
            }
        },

        action: {
            click: 'Click',
            customize: 'Customize',
            download: 'Download',
            hideTable: 'Hide table',
            load: 'Load',
            pause: 'Pause',
            play: 'Play',
            showPolicy: 'Show policy',
            showTable: 'Show table',
            sortTable: 'Sort table'
        },

        track: function (category, action, label) {
            et.Track(category, action, label);
        }
    }

})(jQuery, GaEventTracker);

var tracker = jjgpsGaEventTracker
var track = jjgpsGaEventTracker.track;;
