/*
 Copyright (c) 2008, Yahoo! Inc. All rights reserved.
 Code licensed under the BSD License:
 http://developer.yahoo.net/yui/license.txt
 version: 2.5.2
 */
YAHOO.namespace("util");
YAHOO.util.Cookie = {_createCookieString:function(B, D, C, A) {
    var F = YAHOO.lang;
    var E = encodeURIComponent(B) + "=" + (C ? encodeURIComponent(D) : D);
    if(F.isObject(A)) {
        if(A.expires instanceof Date) {
            E += "; expires=" + A.expires.toGMTString();
        }
        if(F.isString(A.path) && A.path != "") {
            E += "; path=" + A.path;
        }
        if(F.isString(A.domain) && A.domain != "") {
            E += "; domain=" + A.domain;
        }
        if(A.secure === true) {
            E += "; secure";
        }
    }
    return E;
},_createCookieHashString:function(B) {
    var D = YAHOO.lang;
    if(!D.isObject(B)) {
        throw new TypeError("Cookie._createCookieHashString(): Argument must be an object.");
    }
    var C = new Array();
    for(var A in B) {
        if(D.hasOwnProperty(B, A) && !D.isFunction(B[A]) && !D.isUndefined(B[A])) {
            C.push(encodeURIComponent(A) + "=" + encodeURIComponent(String(B[A])));
        }
    }
    return C.join("&");
},_parseCookieHash:function(E) {
    var D = E.split("&");
    var F = null;
    var C = new Object();
    for(var B = 0,A = D.length; B < A; B++) {
        F = D[B].split("=");
        C[decodeURIComponent(F[0])] = decodeURIComponent(F[1]);
    }
    return C;
},_parseCookieString:function(I, A) {
    var J = new Object();
    if(YAHOO.lang.isString(I) && I.length > 0) {
        var B = (A === false ? function(K) {return K;} : decodeURIComponent);
        if(/[^=]+=[^=;]?(?:; [^=]+=[^=]?)?/.test(I)) {
            var G = I.split(/;\s/g);
            var H = null;
            var C = null;
            var E = null;
            for(var D = 0,F = G.length; D < F; D++) {
                E = G[D].match(/([^=]+)=/i);
                if(E instanceof Array) {
                    H = decodeURIComponent(E[1]);
                    C = B(G[D].substring(H.length + 1));
                } else {
                    H = decodeURIComponent(G[D]);
                    C = H;
                }
                J[H] = C;
            }
        }
    }
    return J;
},get:function(A, B) {
    var D = YAHOO.lang;
    var C = this._parseCookieString(document.cookie);
    if(!D.isString(A) || A === "") {
        throw new TypeError("Cookie.get(): Cookie name must be a non-empty string.");
    }
    if(D.isUndefined(C[A])) {
        return null;
    }
    if(!D.isFunction(B)) {
        return C[A];
    } else {
        return B(C[A]);
    }
},getSub:function(A, C, B) {
    var E = YAHOO.lang;
    var D = this.getSubs(A);
    if(D !== null) {
        if(!E.isString(C) || C === "") {
            throw new TypeError("Cookie.getSub(): Subcookie name must be a non-empty string.");
        }
        if(E.isUndefined(D[C])) {
            return null;
        }
        if(!E.isFunction(B)) {
            return D[C];
        } else {
            return B(D[C]);
        }
    } else {
        return null;
    }
},getSubs:function(A) {
    if(!YAHOO.lang.isString(A) || A === "") {
        throw new TypeError("Cookie.getSubs(): Cookie name must be a non-empty string.");
    }
    var B = this._parseCookieString(document.cookie, false);
    if(YAHOO.lang.isString(B[A])) {
        return this._parseCookieHash(B[A]);
    }
    return null;
},remove:function(B, A) {
    if(!YAHOO.lang.isString(B) || B === "") {
        throw new TypeError("Cookie.remove(): Cookie name must be a non-empty string.");
    }
    A = A || {};
    A.expires = new Date(0);
    return this.set(B, "", A);
},set:function(B, C, A) {
    var E = YAHOO.lang;
    if(!E.isString(B)) {
        throw new TypeError("Cookie.set(): Cookie name must be a string.");
    }
    if(E.isUndefined(C)) {
        throw new TypeError("Cookie.set(): Value cannot be undefined.");
    }
    var D = this._createCookieString(B, C, true, A);
    document.cookie = D;
    return D;
},setSub:function(B, D, C, A) {
    var F = YAHOO.lang;
    if(!F.isString(B) || B === "") {
        throw new TypeError("Cookie.setSub(): Cookie name must be a non-empty string.");
    }
    if(!F.isString(D) || D === "") {
        throw new TypeError("Cookie.setSub(): Subcookie name must be a non-empty string.");
    }
    if(F.isUndefined(C)) {
        throw new TypeError("Cookie.setSub(): Subcookie value cannot be undefined.");
    }
    var E = this.getSubs(B);
    if(!F.isObject(E)) {
        E = new Object();
    }
    E[D] = C;
    return this.setSubs(B, E, A);
},setSubs:function(B, C, A) {
    var E = YAHOO.lang;
    if(!E.isString(B)) {
        throw new TypeError("Cookie.setSubs(): Cookie name must be a string.");
    }
    if(!E.isObject(C)) {
        throw new TypeError("Cookie.setSubs(): Cookie value must be an object.");
    }
    var D = this._createCookieString(B, this._createCookieHashString(C), false, A);
    document.cookie = D;
    return D;
}};
YAHOO.register("cookie", YAHOO.util.Cookie, {version:"2.5.2",build:"1076"});

/**
 * @author Adam McIntyre
 * A class used to manage a given cookie and its data.
 */

function CookieManager(cName, maxItems) {
    this.cName = cName;
    this.maxItems = maxItems || 10;

    this.ITEM_SEPARATOR = '~';
    this.VALUE_SEPARATOR = '|';
}

/***
 * Adds an array of values as | separated subcookies of this cookie.
 * @param {Array} vals Array of values we'd like to add. First item will be used as subcookie's name.
 * @param {Function} uniqueFunction Comparator function to use to check that this is indeed a unique value. Should take an array of values and current values as arguments.
 */
CookieManager.prototype.addCookieValue = function(vals, uniqueFunction) {
    var data = this.getCookie(this.cName) || { totalItems : 0, values : []};

    var alreadyCookied = false;
    var idx = 0;
    for(var i = 0; i < data.totalItems; i++) {
        var els = data.values[i].split(this.VALUE_SEPARATOR);
        if(uniqueFunction(els, vals)) {
            alreadyCookied = true;
            idx = i;
        }
    }

    if(! alreadyCookied) {
        // We're over! Shift off the first/oldest and push on the new guy.
        if(data.totalItems >= this.maxItems) {
            while(data.totalItems >= this.maxItems) {
                data.values.pop();
                data.totalItems--;
            }
        }
    }
    else {   // Item already cookied, so we have to splice this item out of the array
        data.values.splice(idx, 1);
    }

    data.values.unshift(vals.join('|'));
    YAHOO.util.Cookie.set(this.cName, data.values.join(this.ITEM_SEPARATOR), {
        expires : new Date('January 1, 2772'),
        path : "/"
    });
}

/***
 * Toggles a cookie value, to val if present, otherwise to its opposite Boolean value.
 * @param {Boolean} val (Optional) Value we'd like to set the cookie to
 */
CookieManager.prototype.toggleCookie = function(val) {
    var data = this.getCookie(this.name) || false;

    if(val) {
        data = val;
    }
    else {
        data = !data;
    }

    YAHOO.util.Cookie.set(this.cName, data, {
        expires : new Date('January 1, 2772'),
        path : "/"
    });
}

/***
 * Gets this particular Object's cookie and transforms the result into something we can use.
 */
CookieManager.prototype.getCookie = function() {
    var o = this;
    return YAHOO.util.Cookie.get(this.cName, function(stringValue) {
        allItems = stringValue.split(o.ITEM_SEPARATOR);
        var data = {
            totalItems : allItems.length,
            values : allItems
        };
        return data;
    });
}

/***
 * Gets a Boolean value stored in particular Object's cookie.
 * @return Boolean Cookie value
 */
CookieManager.prototype.getBooleanCookie = function() {
    return YAHOO.util.Cookie.get(this.cName, function(stringValue) {
        if(stringValue == 'true' || stringValue == true) {
            return true;
        }
        return false;
    });
}


/***
 * Gets all values stored in this Object's cookie.
 * @return Array Array of cookie's values
 */
CookieManager.prototype.getAllValues = function() {
    var data = this.getCookie(this.cName);
    return data ? data.values : [];
}

/***
 * Removes this cookie from the browser.
 */
CookieManager.prototype.clear = function() {
    YAHOO.util.Cookie.remove(this.cName, { path : '/' });
}

// Configuration constants, such as pre-set cookie names.
CookieManagerConstants = {
    pv : 'products-viewed',
    av : 'articles-viewed',
    pvs : 'products-viewed-state'
}

CookieManagerConstants.processProducts = function() {
    var allProducts = pv.getAllValues();

    if(allProducts.length > 0) {
        var bd = nikon('#viewed_products div.bd'); //YAHOO.util.Selector.query('div.bd','viewed_products',true);
        var isCarousel = allProducts.length > 1;

        var tmpHtml = "";

        if(isCarousel) {
            tmpHtml += '<div id="productsViewed-container" class="carousel-container"><div class="carousel-component"><div id="productsViewed-clip-region" class="carousel-clip-region">';
        }

        tmpHtml += '<ul id="productsViewed_list" class="products carousel-list">';
        for(var i = 0; i < allProducts.length; i++) {
            var els = allProducts[i].split('|');
            var linkStart = '<a onclick="OmnitureHelper.setPYVEvents(\'' + els[0] + '\',\'' + els[1] + '\')" href="' + els[3].replace('%2F',
                    '%252F') + '">';

            tmpHtml += '<li class="pv-item" id="productsViewed-item' + (i + 1) + '">' + linkStart + '<img class="product" src="' + els[2] + '" width="87" height="74" alt="' + els[1] + '"></a><label>'
                               + linkStart + els[1] + '</a></label></li>';
        }
        tmpHtml += '</ul></div></div></div>';

        bd.html(tmpHtml);

        // Carousel for greater than 1 product
        if(isCarousel) {
            // Add carousel nav
            bd.css('visibility', 'hidden');
            bd.removeClass('noD');

            bd.append('<div id="productsViewed_navigation" class="carousel-nav">' +
                      '<a id="productsViewed-prev" href="javascript:void(0)" class="nav_left">&nbsp;</a>' +
                      '<span class="indicators"><span id="productsViewed-curNum">1</span>/<span id="productsViewed-maxNum">' + allProducts.length + '</span></span>' +
                      '<a id="productsViewed-next" href="javascript:void(0)" class="nav_right"> </a><div class="rnd"> </div></div>');

            // Add "see all" link
            var ftEl = nikon('#viewed_products div.ft'); //YAHOO.util.Selector.query('div.ft','viewed_products',true);
            ftEl.html('<a href="javascript:void(0)" onclick="CookieManagerConstants.showAllProducts()">' + nikon.utils.locale.get("Global.SeeAll") + '</a>');

            var f = function() {
                bd.css('visibility', 'visible');
            }

            pv.carousel = new YAHOO.extension.Carousel('productsViewed-container',
            {
                numVisible : 1,
                animationSpeed : 0.25,
                scrollInc : 1,
                size: allProducts.length,
                wrap : true,
                orientation : "horizontal",
                loadInitHandler : f,
                prevElement : "productsViewed-prev",
                nextElement : "productsViewed-next",
                loadNextHandler : function() {}, // No-op functions here just to introduce the events
                loadPrevHandler : function() {}  // we subscribe to below.

            }
                    );

            pv.carousel._loadNextHandlerEvt.subscribe(function() {
                document.getElementById('productsViewed-curNum').innerHTML = pv.carousel.getFirstVisible();
            });

            pv.carousel._loadPrevHandlerEvt.subscribe(function() {
                document.getElementById('productsViewed-curNum').innerHTML = pv.carousel.getFirstVisible();
            });
        }
        else {
            bd.removeClass('noD');
        }
    }
    else {
        var bd = nikon('#viewed_products div.bd'); //YAHOO.util.Selector.query('div.bd','viewed_products',true);
        bd.removeClass('noD');
    }
    this.maybeShowProducts();
}

CookieManagerConstants.showAllProducts = function() {
    var bd = nikon('#viewed_products div.bd'); //YAHOO.util.Selector.query('div.bd','viewed_products',true);

    // Add appropriate class so list elements no longer float
    bd.addClass('open');

    // Hide the carousel navigation
    YAHOO.util.Dom.setStyle('productsViewed_navigation', 'display', 'none');

    // Animate body opening
    var anim = new YAHOO.util.Anim(bd[0], {
        height : { to : YAHOO.util.Dom.getFirstChild(bd[0]).offsetHeight }
    },
            0.25,
            YAHOO.util.Easing.easeIn);

    if(YAHOO.util.Dom.getStyle('footer', 'position') == 'absolute') {
        anim.onStart.subscribe(function() {
            // Hide the footer; we'll need to make sure it's positioned accordingly.
            YAHOO.util.Dom.setStyle('footer', 'display', 'none');
        });
    }

    anim.onComplete.subscribe(function() {
        if(YAHOO.util.Dom.getStyle('footer', 'position') == 'absolute') {
            // Calculate new footer position (30px below the tallest column) and unhide footer
            var reg;
            CookieManagerConstants.tc = 0;
            if(YAHOO.util.Dom.getRegion('tabbed_content')) {
                reg = YAHOO.util.Dom.getRegion('tabbed_content')
                CookieManagerConstants.tc = reg.bottom;
            }

            CookieManagerConstants.rc = 0;
            if(reg = YAHOO.util.Dom.getRegion('container_relatedContent')) {
                CookieManagerConstants.rc = reg.bottom;
            }
            if(reg = YAHOO.util.Dom.getRegion('glossary_widget')) {
                CookieManagerConstants.rc = Math.max(CookieManagerConstants.rc, reg.bottom);
            }

            CookieManagerConstants.lc = 0;
            if(reg = YAHOO.util.Dom.getRegion('leftNav')) {
                CookieManagerConstants.lc = reg.bottom;
            }
            if(reg = YAHOO.util.Dom.getRegion('shoppingTools')) {
                CookieManagerConstants.lc = Math.max(CookieManagerConstants.lc, reg.bottom);
            }
            if(reg = YAHOO.util.Dom.getRegion('imagePromo')) {
                CookieManagerConstants.lc = Math.max(CookieManagerConstants.lc, reg.bottom);
            }
            if(reg = YAHOO.util.Dom.getRegion('viewed_products')) {
                CookieManagerConstants.lc = Math.max(CookieManagerConstants.lc, reg.bottom);
            }

            var maxVal = Math.max(CookieManagerConstants.tc,
                    Math.max(CookieManagerConstants.lc, CookieManagerConstants.rc));
            YAHOO.util.Dom.setStyle('footer', 'top', maxVal + 30 + 'px');

            YAHOO.util.Dom.setStyle('footer', 'display', '');
        }

        // Change see all link to hide all, etc.
        var labelText;
        if(typeof nikonLabel == 'undefined') {
            labelText = "Close";
        }
        else {
            labelText = nikon.utils.locale.get("Global.Close");
        }
        var ftEl = nikon('#viewed_products div.ft'); //YAHOO.util.Selector.query('div.ft','viewed_products',true);
        ftEl.html('<a href="javascript:void(0)" onclick="CookieManagerConstants.hideAllProducts()">' + labelText + '</a>');

    });

    anim.animate();
    pvs.toggleCookie(true);
}

CookieManagerConstants.hideAllProducts = function() {
    var bd = nikon('#viewed_products div.bd'); //YAHOO.util.Selector.query('div.bd','viewed_products',true);
    bd.css('position', 'relative');

    // Animate body opening
    var anim = new YAHOO.util.Anim(bd[0], {
        height : { to : 139 }   // 139px is div.bd's height according to the style guide.
    },
            0.25,
            YAHOO.util.Easing.easeIn);

    if(YAHOO.util.Dom.getStyle('footer', 'position') == 'absolute') {
        anim.onStart.subscribe(function() {
            // Hide the footer; we'll need to make sure it's positioned accordingly.
            YAHOO.util.Dom.setStyle('footer', 'display', 'none');
        });
    }

    anim.onComplete.subscribe(function() {
        // Remove appropriate class so list elements float
        bd.removeClass('open');

        // Show the carousel navigation
        YAHOO.util.Dom.setStyle('productsViewed_navigation', 'display', '');

        if(YAHOO.util.Dom.getStyle('footer', 'position') == 'absolute') {
            // Calculate new footer position (30px below the tallest column) and unhide footer
            var reg;
            CookieManagerConstants.tc = 0;
            if(YAHOO.util.Dom.getRegion('tabbed_content')) {
                reg = YAHOO.util.Dom.getRegion('tabbed_content')
                CookieManagerConstants.tc = reg.bottom;
            }

            CookieManagerConstants.rc = 0;
            if(reg = YAHOO.util.Dom.getRegion('container_relatedContent')) {
                CookieManagerConstants.rc = reg.bottom;
            }
            if(reg = YAHOO.util.Dom.getRegion('glossary_widget')) {
                CookieManagerConstants.rc = Math.max(CookieManagerConstants.rc, reg.bottom);
            }

            CookieManagerConstants.lc = 0;
            if(reg = YAHOO.util.Dom.getRegion('leftNav')) {
                CookieManagerConstants.lc = reg.bottom;
            }
            if(reg = YAHOO.util.Dom.getRegion('shoppingTools')) {
                CookieManagerConstants.lc = Math.max(CookieManagerConstants.lc, reg.bottom);
            }
            if(reg = YAHOO.util.Dom.getRegion('imagePromo')) {
                CookieManagerConstants.lc = Math.max(CookieManagerConstants.lc, reg.bottom);
            }
            if(reg = YAHOO.util.Dom.getRegion('viewed_products')) {
                CookieManagerConstants.lc = Math.max(CookieManagerConstants.lc, reg.bottom);
            }

            var maxVal = Math.max(CookieManagerConstants.tc,
                    Math.max(CookieManagerConstants.lc, CookieManagerConstants.rc));
            YAHOO.util.Dom.setStyle('footer', 'top', maxVal + 30 + 'px');

            // Calculate new footer position and unhide footer
            YAHOO.util.Dom.setStyle('footer', 'display', '');
        }

        // Change see all link to hide all, etc.
        var labelText;
        if(typeof nikonLabel == 'undefined') {
            labelText = "See All";
        }
        else {
            labelText = nikon.utils.locale.get("Global.SeeAll");
        }
        var ftEl = nikon('#viewed_products div.ft'); //YAHOO.util.Selector.query('div.ft','viewed_products',true);
        ftEl.html('<a href="javascript:void(0)" onclick="CookieManagerConstants.showAllProducts()">' + labelText + '</a>');

    });

    anim.animate();
    pvs.toggleCookie(false)
}

CookieManagerConstants.productsComparator = function(els, vals) {
    if(els[0] == vals[0]) {
        return true;
    }
    return false;
}

CookieManagerConstants.maybeShowProducts = function() {
    if(pvs.getBooleanCookie()) {
        if(YAHOO.env.ua.ie > 0 && YAHOO.env.ua.ie < 7) {
            var o = this;
            YAHOO.util.Event.addListener(window, 'load', function() {
                setTimeout(function() {
                    o.showAllProducts();
                }, 200);
            });
        }
        else {
            YAHOO.util.Event.onDOMReady(this.showAllProducts);
        }
    }
}

CookieManagerConstants.processArticles = function() {
    if(YAHOO.env.ua.ie > 0 && YAHOO.env.ua.ie < 7) {
        var o = this;
        YAHOO.util.Event.addListener(window, 'load', function() {
            setTimeout(function() {
                o._processArticles();
            }, 200);
        });
    }
    else {
        YAHOO.util.Event.onDOMReady(this._processArticles);
    }
}

CookieManagerConstants._processArticles = function() {
    var allAritcles = av.getAllValues();

    if(allAritcles.length > 0) {
        var bd = YAHOO.util.Selector.query('div.bd', 'viewed_articles', true);

        var tmpHtml = '<ol class="viewed">';
        for(var i = 0; i < allAritcles.length; i++) {
            var els = allAritcles[i].split('|');

            tmpHtml += '<li><a onclick="OmnitureHelper.setAYVEvents(\'' + els[0] + '\',\'' + els[1] + '\',\'' + els[3] + '\',\'' + els[2] + '\')" href="' + els[4] + '">' + els[1].replace(/(&amp;)/ig,
                    '&') + '</a></li>';
        }
        tmpHtml += '</ol>';

        bd.innerHTML = tmpHtml;
        YAHOO.util.Dom.removeClass(bd, 'noD');
    }
    else {
        var bd = YAHOO.util.Selector.query('div.bd', 'viewed_articles', true);
        YAHOO.util.Dom.removeClass(bd, 'noD');
    }
}

CookieManagerConstants.articlesComparator = function(els, vals) {
    if(els[0] == vals[0]) {
        return true;
    }
    return false;
}

