﻿// ********************************************************
// OLYMPUS library for handling of COREMETRIC tracking tags
//
// Date         Who   Changes                            
// --------------------------------------------------------
// 2009/09/30   SDA   Created
// 2009/10/08   SDA   Changed categorization
//
// ********************************************************

_tagObj = {

    // Constructor
    create: function(p) {
        // Mandatory parameters
        this.setPageID(p._cm_page_id);
        this.setCountryCode(p._cm_countrycode);
        this.setPageTitle(p._cm_page_title);
        this.setCategoryID(p._cm_dynamic_category.length == 0 || this.trim(p._cm_dynamic_category) == "######" ? this.getCategoryIDFromHierarchy(p._cm_categories) : this.getCategoryIDFromDynamicCategory(p._cm_dynamic_category));
        // Optional parameters
        this.setClientID(typeof p._cm_cid == "undefined" ? "" : p._cm_cid);
        this.setProductive(p._cm_productive);
        this.setDomain(typeof p._cm_domain == "undefined" ? "" : p._cm_domain);
        this.setTrackDomain(typeof p._cm_trackdomain == "undefined" ? "" : p._cm_trackdomain);
        this.setSearchTerm(typeof p._cm_searchTerm == "undefined" ? "" : p._cm_searchTerm);
        this.setSearchResults(typeof p._cm_searchResults == "undefined" ? "" : p._cm_searchResults);
        this.setAction(typeof p._cm_action == "undefined" ? "" : p._cm_action);
        return this;
    },

    // Setters, Getters
    setClientID: function(clientID) { this.clientID = clientID; },
    getClientID: function() { return this.clientID; },
	
    setProductive: function(productive) { this.productive = productive; },
    getProductive: function() { return this.productive; },

    setDomain: function(domain) { this.domain = domain; },
    getDomain: function() { return this.domain; },
	
    setTrackDomain: function(trackdomain) { this.trackdomain = trackdomain; },
    getTrackDomain: function() { return this.trackdomain; },

    setPageID: function(pageID) { this.pageID = pageID; },
    getPageID: function() { return this.pageID; },

    setPageTitle: function(pageTitle) { this.pageTitle = pageTitle; },
    getPageTitle: function() { return this.pageTitle; },

    setCountryCode: function(countryCode) { this.countryCode = countryCode.toString().toUpperCase(); },
    getCountryCode: function() { return this.countryCode; },

    setCategoryID: function(categoryID) { this.categoryID = categoryID.toString().toUpperCase(); },
    getCategoryID: function() { return this.categoryID; },

    setSearchTerm: function(searchTerm) { this.searchTerm = searchTerm; },
    getSearchTerm: function() { return this.searchTerm; },

    setSearchResults: function(searchResults) { this.searchResults = searchResults; },
    getSearchResults: function() { return this.searchResults; },

    setAction: function(action) { this.action = action; },
    getAction: function() { return this.action; },

    // Send Coremetrics Tags
    send: function() {
	
	return true;
        // Involve runtime data manipulation if defined
        if (typeof _tagProcess == "object") { _tagProcess.run(this) };

        // Product sub pages
        if (jQuery('td.link5').attr("id") == "productsub") {
            // Remove last entry of content hierarchy if page is product sub page
            var strCat = this.getCategoryID().substring(0, this.getCategoryID().lastIndexOf("::"));
            strCat = strCat.substring(0, strCat.lastIndexOf("::")) + "::";
            this.setCategoryID(strCat);
            // Add product name to page title
            this.pageTitle = this.getCategoryID().match(/[^:]*::$/).toString().replace("::", "") + " | " + jQuery('td.link5').text();
        };

        if (this.getClientID().length > 0) {
            // Set the client ID            
            if (typeof cmSetClientID == "function") {
			
                cmSetClientID(this.getClientID(), "false", this.getTrackDomain(), this.getDomain());
            };
            // Send data to production environment
            if ((typeof cmSetProduction == "function") && (this.getProductive() == "true")) {
                cmSetProduction();
				//console.info("cmSetProduction: "+this.getProductive()+"\n");
            } else {
				//console.info("cmSetProduction: "+this.getProductive()+"\n");
			};

            // PageView Tag            
            if (typeof cmCreatePageviewTag == "function") {
                cmCreatePageviewTag(
                        this.getCMPageID(this.pageTitle, this.pageID),
                        ((this.getCategoryID())),
                        (this.getSearchTerm().length > 0 ? this.getSearchTerm() : null),
                        (this.getSearchResults().length > 0 ? this.getSearchResults() : null)
                    );
            };
            // ProductView Tag
			/*
            if (this.getAction().search(/PRODUCT_VIEW/i) > -1) {
                if (typeof cmCreateProductviewTag == "function") {
                    cmCreateProductviewTag(
                        this.getCMPageID(this.pageTitle, this.pageID),
                        this.getPageTitle(),
                        this.getCategoryID()
                    );
                };
            };
			*/
            // Other Tags
        }
        this.debug();
    },

    searchForm: function(objForm) {
        if (objForm.elements["cmid"] != null) {
            objForm.elements["cmid"].value = this.getClientID();
        }
    },

    // Generates a unique Coremetrics Page ID
    getCMPageID: function(pageTitle, pageID) {
        return pageTitle + (pageID.toString().length > 0 ? " [ID: " + pageID + "]" : "");
    },

    // Generates Category ID from hierarchy / breadcrumb stored in _cm_categories
    getCategoryIDFromHierarchy: function(strHierarchy) {
        // Format string to ID format
        strHierarchy = strHierarchy.replace(/<.+?>/g, '');
        strHierarchy = strHierarchy.replace(/\s>\s/g, "::");

        // DISCARDED 09/10/08
        // Remove last entry in string
        // strHierarchy = strHierarchy.substring(0, strHierarchy.lastIndexOf("::"));
        // DISCARDED 09/10/08

        // Replace special characters here
        strHierarchy = strHierarchy.replace(/[`~!@#\$%\^&\*\(\)=\+\[\]\\;,\<\>\.\?]/ig, "_").replace(/µ/ig, "mju") + "::";

        return strHierarchy.replace(/\s+::/ig, "::").replace(/::\s+/ig, "::");
    },

    getCategoryIDFromDynamicCategory: function(dynCategory) {
        return dynCategory;//.substring(3, dynCategory.length - 3).replace(/######/ig, "::") + "::";
    },

    trim: function(p) {
        return p.replace(/^\s+/, '').replace(/\s+$/, '');
    },

    // Output function for debugging (works with Firebug and IE Web Development Helper					
    debug: function() {
        var out = "";
        out = out + "Page ID: " + this.getCMPageID(this.getPageTitle(), this.getPageID()) + "\n";
        out = out + "Category ID: " + this.getCategoryID() + "\n";
        out = out + "\n\nFunctions fired:";
		out = out + "\ncmSetClientID: (" + this.getClientID() + ", " + "false" + ", " + this.getTrackDomain() + ", " + this.getDomain() + ")";
		out = out + "\nPageViewTag: (" + this.getCMPageID(this.pageTitle, this.pageID) + ", " + ((this.getCategoryID())) +", " + (this.getSearchTerm().length > 0 ? this.getSearchTerm() : null) + ", " + (this.getSearchResults().length > 0 ? this.getSearchResults() : null) + ")";
        if (this.getSearchTerm().length > 0) out = out + "Search term: " + this.getSearchTerm() + "\n";
        if (this.getSearchResults().length > 0) out = out + "Search results: " + this.getSearchResults() + "\n";
        if (this.getAction().search(/PRODUCT_VIEW/i) > -1) out = out + "PRODUCT VIEW\n";
        if (typeof console == "object") console.info(out);
        if (typeof Debug == "object") Debug.writeln(out + "\n");
    }

};                // cmUtil

