// Copyright Channel Intelligence, Inc. 2002-2008

var ci_vid;
ci_vid = 13467115;
var ci_host = 'a01c7f55-4a7e-49d3-9f50-4833ad70b700';
var localeCode = "en-US";

function CI_ReadQueryValue(name) {
    var ci_queryStringParts = new RegExp('[\\?&]' + name + '=?([^&#]*)').exec(window.document.location);

    if (ci_queryStringParts == null)
        return null;
    else if (ci_queryStringParts.length < 2)
        return '';
    else
        return ci_queryStringParts[1];

}

function CI_CreateCookie(name, value, daysTillExpire) {

    var ci_expires = '';

    if (daysTillExpire) {
        var ci_expireDate = new Date();
        ci_expireDate.setTime(ci_expireDate.getTime() + (daysTillExpire * 24 * 60 * 60 * 1000));
        ci_expires = '; expires=' + ci_expireDate.toGMTString();
    }

    document.cookie = name + '=' + value + ci_expires + '; path=/';
}

//added to inject mx smart code
function callMX() {
    if (ci_sku == null) {
        ci_sku = readSkuFromUrl();
    }

    if (ci_sku.length > 0 && ci_vid.toString().length > 0) {
        var oScript = document.createElement("SCRIPT");
        oScript.type = "text/javascript";
        //<script type="text/javascript" src="http://edge.services.sellpath.com/Syndication/ProductTour/SKU/a01c7f55-4a7e-49d3-9f50-4833ad70b700/en-US/RetailerSku/ci_anchor"></script>
        oScript.src = "http://edge.services.sellpath.com/Syndication/ProductTour/SKU/" + ci_host + "/" + localeCode + "/" + ci_sku + "/ci_anchor";
        var thisBlock = getTrueTagScriptNode();
        if (thisBlock != null && thisBlock.parentNode != null) {
            try {
                var insertedElement = thisBlock.parentNode.insertBefore(oScript, thisBlock);
            }
            catch (err) { }
        }
    }
}

function readSkuFromUrl() {
    //example url: http://www.electronics-expo.com/make-a-store/item/SKU GOES HERE/Nikon/D3000/1.html 
    var url = location.href;
    url = url.replace("http://", "");
    var segments = url.split('/');
    var sku = segments.length > 3 ? segments[3] : '';
    sku = sku.replace(/%20/gi, "");
    sku = sku.replace(/ /gi, "");
    return sku;
}

function getTrueTagScriptNode() {
    var thisScript = null;
    var scripts = document.getElementsByTagName("script");
    try {
        for (var i = 0; i < scripts.length; i++) {
            if (scripts[i].src.length > 0 && scripts[i].src.toLowerCase().match("cts.channelintelligence") != null) {
                thisScript = scripts[i];
                break;
            }
        }
    } 
    catch (err) { }
    return thisScript;
}


var ci_cpncode;
var ci_src;
var ci_sku;
var ci_cid;
var ci_pic;

try {

    ci_cpncode = CI_ReadQueryValue('cpncode');
    ci_src = CI_ReadQueryValue('ci_src');
    ci_sku = CI_ReadQueryValue('ci_sku');
    ci_cid = CI_ReadQueryValue('ci_cid');
    ci_eeprodid = CI_ReadQueryValue('id');

    if (ci_src != null && ci_eeprodid != null && ci_sku == null) {
        ci_sku = ci_eeprodid
    }

    if (ci_cpncode != null) {
        CI_CreateCookie('ci_cpncode', ci_cpncode, 30);
        CI_CreateCookie('ci_tid', "", -1);
    }
    else if ((ci_src != null && ci_sku != null) || ci_cid != null) {
        var ci_today = new Date();
        var ci_tid = Math.floor(Math.random() * 9999999999) + ci_today.getFullYear().toString() + ci_today.getMonth().toString() + ci_today.getDay().toString() + ci_today.getHours().toString() + ci_today.getMinutes().toString() + ci_today.getSeconds().toString() + ci_today.getMilliseconds().toString()
        if (ci_cid != null) { ci_tid = ci_vid + "-" + ci_cid + "-" + ci_tid }
        if (ci_sku != null) { ci_tid = ci_vid + "-" + ci_sku + "-" + ci_tid }
        var ci_url = "http://origin.channelintelligence.com/log.asp?vid=" + ci_vid + "&tid=" + ci_tid

        ci_pic = document.createElement('img');

        CI_CreateCookie('ci_cpncode', "", -1);
        CI_CreateCookie('ci_tid', ci_tid, 30);

        if (ci_src != null) { ci_url += "&src=" + ci_src; }
        if (ci_sku != null) { ci_url += "&sku=" + ci_sku; }
        if (ci_cid != null) { ci_url += "&cid=" + ci_cid; }

        ci_url += "&ref=" + document.referrer

        ci_pic.width = 1;
        ci_pic.height = 1;
        ci_pic.src = ci_url;
    }
    
    //call mx
    if (document.addEventListener)
    {  document.addEventListener("DOMContentLoaded", callMX, false);}
    else if (document.all && !window.opera)//Crude test for IE
    { 
      document.write('<script type="text/javascript" id="contentloadtag" defer="defer" src="javascript:void(0)"><\/script>');
      
      var contentloadtag = document.getElementById("contentloadtag");
      contentloadtag.onreadystatechange = function() {
              if (this.readyState == "complete")
              { callMX(); }
          }
     }
}
catch (err) {
}