function SiteCatalyst () {
    this.channel;
    this.hierarchy;
    this.pageName;
    this.server;
    this.subClass;
    this.topLevelSiteSection;
    this.page = new PageIdentification();
    this.linkTrackVars = [];
    this.linkTrackEvents = [];
    this.properties = {};
    this.linkPositions = {
        "topnav" : "midNav-menu",
        "topnav_text" : "topNav",
        "bottomnav_callout" : "hours",
        "bottomnav_promos" : "footer-promos",
        "footer_text" : "botNav-bucket",
        "promo_rightnav" : "promo",
        "main" : "main"
    };

    this.addLinkPositions = function _addLinkPositions() {
        var parents = {};
        for (var i in this.linkPositions) {
            var element = document.getElementById(this.linkPositions[i]);
            if (!element) {
                element = getElementsByClassName(this.linkPositions[i])[0];
            }
            parents[i] = element;
        }
        var links = document.getElementsByTagName("a");
        for (var i = 0; i < links.length; i++) {
            var link = links[i];
            var sc = this;
            (function() {
                for (var i  in  parents) {
                    if (sc._hasParent(link, parents[i])) {
                        // console.log(link);
                        // console.log(i);
                        // console.log("____________________________________");
                        link.setAttribute("name", "?LPOS=" + i);
                        return;
                    }
                }
            })();
        
        }
    };

    this._hasParent = function __hasParent(element, parent) {
        while (element && (element != document.body)) {
            if (element == parent) {
                return true;
            }
            element = element.parentNode;
        }

        return false;
    };

    this.track = function _track() {
        this.properties.prop1    = this.getTopLevelSiteSection();
        this.properties.pageName = this.getPageName();
        this.properties.server   = this.getServer();
        this.properties.channel  = this.getChannel();
        this.trackLead();
        this.trackEmail();
        this.trackPrint();

        try {
            if (s !== undefined) {
                s.linkTrackVars += "," + this._getPropertyList(this.linkTrackVars);
                s.linkTrackEvents += "," + this._getPropertyList(this.linkTrackEvents);
                this.setProperties(s, this.properties);

                s.pageType = "";
                /* Conversion Variables */
                s.campaign = "";
                s.state = "";
                s.zip = "";
                s.events = "";
                s.products = "";
                s.purchaseID = "";
                s.eVar1 = "";
                /* Hierarchy Variables */
                s.hier1 = "";
                if (window.console) {
                    window.console.log(this.properties);
                    window.console.log(this.linkTrackVars);
                    window.console.log(this.linkTrackEvents);
                }
                /************* DO NOT ALTER ANYTHING BELOW THIS LINE ! **************/
                var s_code=s.t();if(s_code)document.write(s_code);//-->
            }
        } catch (e) {
            if (window.console) {
                console.log(e);
            }
        }
    };
    
    this._getPropertyList = function __getPropertyList(properties) {
        if (properties.length) {
            if (this.linkTrackVars instanceof Array) {
                return this.linkTrackVars.join(",");
            } else {
                return this.linkTrackVars;
            }
        }

        return "";
    };
    
    this.setProperties = function _setProperties(obj, properties)
    {
        if (properties ===  undefined) {
            properties = this.properties;
        }
        for (var idx in properties) {
            s[idx] = properties[idx];
        }
    };

    this.trackLink = function _trackLink(element, vars, propValues, linkName, linkType) {
        try {
            if (vars instanceof Array) {
                s.linkTrackVars = vars.join(",");
            } else {
                s.linkTrackVars = vars;
            }
            for (var idx in propValues) {
                var prop = propValues[idx];
                s[idx] = prop;
            }
            if (window.console) {
                window.console.log(vars);
                window.console.log(propValues);
            }
            s.tl(element, linkType, linkName);
        } catch(e) {
            if (window.console) {
                window.console.log(e);
            }
        }
        
    };

    this.flashLink = function _flashLink(linkName, linkPosition) {
        this.trackLink(true, 'o', linkName);
    };

    this.trackLead = function _trackLead() {
        if (this.page.hasForm() && document.getElementById("form_cmdSubmit")) {
            this.linkTrackVars.push("prop28");
            this.linkTrackVars.push("evar28");
            this.linkTrackEvents.push("event25");
            this.properties.prop28 = this.getFormTitle();
            this.formSubmission();
        }
    };

    this.formSubmission = function _formSubmission() {
        var vars = ["evar28"];
        var propValues = {
            evar28 : this.getFormTitle(),
            events : "event26"
        };
        
        var submit         = document.getElementById("form_cmdSubmit");
        if (submit) {
            var onclickHandler = submit.onclick;
            var sc = this;
            if (typeof onclickHandler == "function") {
                submit.onclick = function() {
                    if (onclickHandler()) {
                        sc.trackLink(this, vars, propValues, "submit", 'o');
                    } else {
                        return false;
                    }
                }
            } else {
                submit.onclick = function() {
                    sc.trackLink(this, vars, propValues, "submit", 'o');
                }
            }
        }
    };

    this.trackPrint = function _trackPrint() {
        var print = document.getElementById("print");
        if (print) {
            var vars  = [];
            var propValues = {
                events : "event27"
            };
            var sc = this;
            print.onclick = function() {
                sc.trackLink(this, vars, propValues, 'print', 'o');
            }
        }
    };

    this.trackEmail = function _trackEmail() {
        var email = document.getElementById("email");
        if (email) {
            var vars  = [];
            var propValues = {
                events : "event28"
            };
            var sc = this;
            email.onclick = function() {
                sc.trackLink(this, vars, propValues, 'email', 'o');
            }
        }
    };

    this.getFormTitle = function _getFormTitle() {
        if (document.getElementsByTagName("h1").length > 0) {
            var formTitle = document.getElementsByTagName("h1")[0].innerHTML;
            return this.page.getParkName() + ":" + formTitle;
        }
    };

    this.pickAPark = function _pickAPark(element) {
        var linkName = element.innerHTML;
        var linkType = "o";
        var vars = "prop2,evar2";
        var propValues = {
            prop2 : linkName
        };
        this.trackLink(element, vars, propValues, linkName, linkType);
    };

    this.merge = function _merge(first, second) {
        var i = first.length, j = 0;

        if (typeof second.length === "number") {
            for ( var l = second.length; j < l; j++ ) {
                first[ i++ ] = second[ j ];
            }

        } else {
            while (second[j] !== undefined) {
                first[i++] = second[j++];
            }
        }

        first.length = i;

        return first;
    };

    this.getServer = function _getServer() {
        if (!this.server) {
            this.server = document.location.host;
        }

        return this.server;
    };

    this.getHierarchy = function _getHierarchy() {
        if (!this.hierarchy) {
            var path = document.location.pathname;
            var regex = /^\/[\w|\-|\/]+\/WORKAREA/i;
            path = path.replace(regex, "");

            var hierarchy = path.split("/");
            if (hierarchy.length > 0 && hierarchy[0] == "") {
                hierarchy = hierarchy.splice(1, hierarchy.length);
            }
            this.hierarchy = hierarchy;
        }

        return this.hierarchy;
    };

    this.getPageName = function _getPageName() {
        if (!this.pageName) {
            var pageName = this.getHierarchy().join(":").toLowerCase();
            pageName = pageName.replace(/.aspx|.html/i, "");
            this.pageName = pageName;
        }

        return this.pageName;
    };

    this.getChannel = function _getChannel() {
        if (!this.channel) {
            var channel = this.getHierarchy().slice(0, -1);
            if (this.getSubClass() != "" && this.getSubClass() != channel.slice(-1).shift()) {
                channel.push(this.getSubClass());
            }

            channel = channel.join(":").toLowerCase();
            if (this.getPageName().match(/index/i)) {
                channel += ":home";
            }

            this.channel = channel;
        }

        return this.channel;
    };

    this.getTopLevelSiteSection = function _getTopLevelSiteSection() {
        if (!this.topLevelSiteSection) {
            var hierarchy = this.getHierarchy();
            if (hierarchy.length > 2) {
                this.topLevelSiteSection = hierarchy.slice(0,2).join(":");
            } else if (hierarchy.length == 2){
                this.topLevelSiteSection = this.getChannel();
            }
        }

        return this.topLevelSiteSection;
    };

    this.getSubClass = function _getSubClass() {
        if (!this.subClass) {
            if (document.evaluate) {
                var xpath = "//div[@class='crumbs']/a[last()]/text()";
                var subClass = document.evaluate(xpath, document, null, XPathResult.STRING_TYPE, null).stringValue;
                this.subClass = subClass.replace("\s+", "").toLowerCase();
            } else {
                var crumbs = getElementsByClassName("crumbs");
                if (crumbs.length > 0) {
                    crumbs = crumbs[0];
                } else {
                    return "";
                }
                var anchors = crumbs.getElementsByTagName("a");
                if (anchors.length > 0) {
                    var anchor = anchors[anchors.length - 1];
                    this.subClass = anchor.innerHTML;
                } else {
                    this.subClass = "";
                }
            }
        }

        return this.subClass;
    };
}
