﻿function InitializeHierarchyVariables() {
    /* Hierarchy Variables */
    s.hier1 = '';
}

function OmnitureSetPromotionThankYouPageView() {
    var promoCode = getQuerystring('p');
    s.channel = 'offer';
    s.pageName = 'thank you ' + promoCode;
}

function OmnitureSetPromotionExpiredPageView() {
    var promoCode = getQuerystring('promo');
    s.channel = 'offer';
    s.pageName = 'expired ' + promoCode;
}

function OmnitureSetPromotionConfirmationPageView() {
    var promoCode = getQuerystring('promo');
    s.channel = 'offer';
    s.pageName = 'confirmation ' + promoCode;
}

/*This function can also be found in productFilter.js*/
function getQuerystring(key) {
    var defaultVal = "";
    key = key.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
    var regex = new RegExp("[\\?&]" + key + "=([^&#]*)");
    var qs = regex.exec(window.location.href);
    if (qs == null) {
        return defaultVal;
    } else {
        return qs[1];
    }
}

function OmnitureProductTabClick(tab, product) {
    var linkName = 'product_' + tab + '_' + product;
    s.eVar3 = product;
    s.tl(this, 'o', linkName);
}

function OmnitureProductDirectionsClick(step, product) {
    var linkName = 'product_directions_steps_' + product + ',' + step;
    s.eVar3 = product;
    s.tl(this, 'o', linkName);
}

function OmnitureProductFilterClick(styles, surfaces, issues) {
    initialize();

    s.channel = 'home';
    s.eVar3 = styles + ',' + surfaces + ',' + issues;
    s.prop2 = styles + ',' + surfaces + ',' + issues;
    s.tl(this, 'o', 'solution_finder_' + styles + ',' + surfaces + ',' + issues);
}

function OmnitureProductBrowseClick(surfaces) {
    if (surfaces != 'All') {
        initialize();

        s.eVar3 = 'filter';
        s.tl(this, 'o', 'browse_products_' + surfaces);
    }
}

function OmnitureTipSurfaceClick(filter) {
    if (filter != 'Select') {
        s.tl(this, 'o', 'tips_filter_' + filter);
    }
}

function OmnitureTipSubmitSurfaceClick(filter) {
    s.tl(this, 'o', 'submit_tips_surface_' + filter);
}

function OmnitureSearchResultClick(keyword) {
    s.channel = 'home';
    s.tl(this, 'o', 'search_results _' + keyword);
}

function OmnitureEmailOpenForm(type, name) {
    name = name.replace(/[^0-9a-zA-Z  &!,;:.@]/g, "");
    s.channel = type;
    s.pageName = 'email_' + type + ' ' + name;
    s.events = 'event11';
    s.eVar6 = 'email ' + type + ' ' + name;
    s.prop6 = 'email ' + type + ' ' + name;
    s.tl(this, 'o', 'email_' + name + '_' + name);
}

function OmnitureEmailSend(type, name) {
    name = name.replace(/[^0-9a-zA-Z  &!,;:.@]/g, "");
    s.channel = type;   
    s.events = 'event12';
    s.eVar6 = 'email ' + type + ' send ' + name;
    s.prop6 = 'email ' + type + ' send ' + name;
    s.tl(this, 'o', 'email ' + type + ' send ' + name);
}

function OmnitureSocialNetworkingSend(service, type, name) {
    name = name.replace(/[^0-9a-zA-Z  &!,;:.@]/g, "");
    s.channel = type;
    s.prop2 = 'share_' + service + '_' + name;
    s.tl(this, 'o', 'share_' + type + '_' + service + '_' + name);
}

function OmnitureWatchVideo(action, name) {
    if (action == 'play') {
        s.events = 'event1';
        s.eVar1 = name + '_' + action;
    }
    else if (action == 'stop') {
        s.events = 'event2';
        s.eVar1 = name + '_' + action;
    }
    else if (action == 'refresh') {
        s.events = 'event15';
        s.eVar12 = name + '_replay';
    }

    s.tl(this, 'o', name + '_' + action);
}

function OmniturePrintCoupon(promo) {
    s.events = 'event6';
    s.eVar4 = 'print coupon ' + promo;
    s.tl(this, 'e', 'coupon print ' + promo);
}

function OmnitureSubmitCouponRequest(promo) {
    s.events = 'event12';
    s.eVar6 = 'submit coupon ' + promo;
    s.prop6 = 'submit coupon ' + promo;
    s.tl(this, 'o', 'offer_submit ' + promo);
}

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.trackLink(trackVars, { "linkName": linkName, "linkType": linkType }, objLink);
}

//added below script for the task 9676 - add Omniture tags to the amazon and drugstore links on this page Promotions Landing Page
//Add clear_tracking function to all links. Defect
$(document).ready(function (e) {
    $('.ToiletCleanerInAmazon').click(function (e) {
        //Set LinkName property, track link will be called by default
        s.pev2 = 'buy_automatic_toilet_bowl_cleaner_amazon';
    });
    $('.ToiletCleanerInDrugstore').click(function (e) {
        //Set LinkName property, track link will be called by default
        s.pev2 = 'buy_automatic_toilet_bowl_cleaner_drugstore';
    });

    $("a").click(function () {
        clear_tracking();
        return true;
    });

    var PageURL = window.location.pathname;
    PageURL = PageURL.toLowerCase();
    if (PageURL.indexOf("/whats-new.aspx") > 0) {
        $('.related_products a[href*="/Products/Pages/bathroom-cleaner.aspx"]').click(function () {            
            s.channel = "whats new";
            s.tl(this, 'o', 'BATH_CLEAN_COLOR_POWER_TECH');
        });
    }   

    if (PageURL.indexOf("/default.aspx") > 0) {
        $('.item_list li a[href*="/Products/Pages/Bathroom-Cleaner.aspx"]').attr("Name", "home");
        $('.item_list li a[href*="/Products/Pages/Bathroom-Cleaner.aspx"]').click(function () {
            var trackVals = "{ 'channel' : 'home' ,'linkName' : 'BLUE_TO_WHITE_HP_MARQUEE' , 'linkType' : 'o' }";
            $(this).attr('rel', trackVals);
            Track($(this));
        });
    }

});

