﻿//v2.1

// f(x) for passing pagename & site section to swf file for videos
// return comma delimited string --> "pagename,sitesection"
function GetOmnitureVarsForSWF() {
    try {
        return s.pageName + "," + s.channel;
    } catch (err) {
        return "None,None";
    }
}

/* This script builds an array from query strings. It looks for ID and adds it to the title var for Omniture. If ID doesn't exist, it uses p value. */
var wmt_getSubstring = window.location.search.substring(1);
var wmt_getQuerystrings = wmt_getSubstring.split("&");
var wmt_queryArray = new Array(wmt_getQuerystrings.length);
var wmt_documentTitle; var wmt_pageID;
for (i = 0; i < wmt_getQuerystrings.length; i++)
{
    var wmt_splitQueryString = wmt_getQuerystrings[i].split("=");
    wmt_queryArray[wmt_splitQueryString[0]] = wmt_splitQueryString[1];
}
if (wmt_queryArray["ID"]) { wmt_pageID = "(irol-" + wmt_queryArray["ID"] + ") -"; }
else if (wmt_queryArray["p"]) { wmt_pageID = "(" + wmt_queryArray["p"].toLowerCase() + ") -"; }
else { wmt_pageID = ""; }
//Strip 'Walmartstores.com:' in document.title and replace with pageID
wmt_documentTitle = document.title.replace("Walmartstores.com:", wmt_pageID);
// modify title to contain only valid standard ASCII characters or character entities; esape ' and "
wmt_documentTitle = wmt_documentTitle.replace(/\u2013/g, "-").replace(/\u2014/g, "--").replace(/\u2018|\u2019/g, "\'").replace(/\u201D|\u201C/g, "\"").replace(/\u2122/g, "&#153;").replace(/\u00AE/g, "&#174;").replace(/\u2026/g, "&#133;").replace(/'/g, "\'").replace(/"/g, '\"');
// trim to Omniture-recommended 100 chars
if (wmt_documentTitle.length > 100) wmt_documentTitle = wmt_documentTitle.substring(0, 100);

$(document).ready(function() {
    // find all links to PDFs and MP3s and modify class for Omniture    
    //$("a[href$=.pdf]").each(function() { $(this).addClass("pdf scTrack:download('pdf', '" + formatTitleForOmniture($(this).html()) + "')") });
    $("a[href$=.mp3]").each(function() { $(this).addClass("audio scTrack:download('mp3', '" + formatTitleForOmniture($(this).html()) + "')") });
    // if Featured Video Widget div is included on the page, attempt to load videos
    if (document.getElementById('widgetFeaturedVideo')) {
        $.getScript("http://rfi-walmart.com/wmstores/js/InvestorVideos.ashx?t=" + escape(document.title), function() {
            LoadInvestorVideos(); // note: appropriate Omniture variables for videos will be set in this function
        });
    }
    // following line must appear here after Omniture video variables, if any, have been set
    if (typeof(s) != "undefined")
    {
        var s_code = s.t(); if (s_code) document.write(s_code);
    }
});

function formatTitleForOmniture(title)
{
    // replace spaces with underscores and escape single and double quotes
    var formattedTitle = title.replace(/\s+/g, "_").replace(/'/g, "\'").replace(/"/g, '\"');
    return formattedTitle;
}

//****************** Widgets ******************

//* Featured Video
function LoadInvestorVideos()
{    
    if (g_featVids.length > 0) {
        var divHtml = '<h6>Featured Video</h6><div id="dFeaturedVideo" style="z-index:99;">' +
                          '<a id="iFeaturedVideo" href="https://www.adobe.com/go/getflashplayer" target="_blank">' +
                          '<img src="http://rfi-walmart.com/wmstores/images/noflash_small.png" alt="Get Flash Player" style="border-width:0px;" /></a></div>' +
                          '<div style="margin-top:6px; margin-bottom:6px;"><b><span id="spnVideoTitle"></span></b></div>' +
                          '<div style="margin-bottom:6px;"><span id="spnVideoDescription"></span></div>' +
                          '<a id="aNextFeatVid" href="javascript:void(0);" onclick="LoadDivFeaturedVideo(); return false;" class="moreLink" style="display:none;">&gt; Next Featured Video<br /></a>' +
                          '<a href="http://walmartstores.com/Video/" class="moreLink">&gt; See all videos</a>';
        $("#widgetFeaturedVideo").html(divHtml);
        var videoTitles = '';
        for (var index in g_featVids) {
            videoTitles += formatTitleForOmniture(g_featVids[index].Title) + ":";
        }
        if (g_featVids.length > 1) {
            $("#aNextFeatVid").css("display", "inline");
            //* randomize
            g_featVidIndex = Math.floor(Math.random() * g_featVids.length - 1);
            if (g_featVidIndex < 0) g_featVidIndex = 0;  // fix when g_featVidIndex = -1
        }
        s.prop6 = videoTitles;
        s.events = "event6";
        s.products = ";" + g_featVids[g_featVidIndex].Title.replace(/'/g, "\'") + ";;;;evar1=" + s.pageName + "|evar2=" + s.channel;
        $("#widgetFeaturedVideo").css("display", "block");
        LoadDivFeaturedVideo(); // this function call must be after s.products assignment (so g_featVidIndex will be correct)
    }
}

function ShowFeaturedVideo(featVid) {
    var flashvars = {
		    xmlpath: "http://walmartstores.com/Video/StreamXml.aspx%3fid=" + featVid.Id + "%26embed%3dtrue"
		};
        var params = {
            allowScriptAccess: "always",
            quality: "high",
            play: "true",
            loop: "false",
            scale: "showall",
            wmode: "opaque",
            menu: "false"
        };
		
		swfobject.embedSWF("http://walmartstores.com/video/flash/featuredVideo.swf", "dFeaturedVideo", "195", "175", "9.0.0", "http://walmartstores.com/video/flash/expressInstall.swf", flashvars, params);
        
        $("#spnVideoTitle").html(featVid.Title);
        $("#spnVideoDescription").html(featVid.Description);
}

function LoadDivFeaturedVideo() {    
    if (g_featVids.length == 0) return;

    ShowFeaturedVideo(g_featVids[g_featVidIndex]);
    g_featVidIndex++;
    if (g_featVidIndex >= g_featVids.length) g_featVidIndex = 0;
}

//* shareholders meeting webcast
function launchWebcast() {
    var docwidth = 910, docheight = 440;
    var top = (screen.height - docheight) / 2;
    var left = (screen.width - docwidth) / 2;
    var viewWin = window.open("http://walmartstores.com/sites/shareholders/2009", "Webcast", "height=" + docheight + ",width=" + docwidth + ",top=" + top + ", left=" + left + ",toolbar=0,directories=0,status=no,menubar=0,scrollbars=0,resizable=1");
    if (viewWin != null) viewWin.focus();
}