/**
 * CrossSellProducts
 * @param {string} thisObjName = name of instantiation used for onclick
 * @param {string} thisCGID = name of content ID. Used for getting asset paths * @author Jermaine Jackson * @date 10/30/2009 */
var CrossSellProducts = Class.create();

CrossSellProducts.prototype = {

    initialize: function(thisObjName, cgID) {

        this.objName = thisObjName;
        this.cgID = cgID;
        this.marketingContainer = $('marketingSlot').down().down();
        this.maxVisibleProducts = 3;
        this.buyButton = 'buy.gif';
        this.productContainer = 'csProd';
        this.addedToBag = { 'divName': 'csAddedContainer', 'imgSrc': 'addedToBag.gif' };
        this.products = [
            {
                'prodName': 'Printed Stainless Steel Bottles',
                'prodPrice': '$7.50',
                'prodSKU': '6758381820000',
                'scID': '675838182',
                'prodCID': '35250',
                'prodColor': 'Peace Signs'
            },

            {
                'prodName': 'Performance Fleece Blankets',
                'prodPrice': '$10.00',
                'prodSKU': '6996650320000',
                'scID': '699665032',
                'prodCID': '46064',
                'prodColor': 'Cool Stripe'
            },

            {
                'prodName': "Women's Striped Chenille Socks",
                'prodPrice': '$5.00',
                'prodSKU': '6990590320000',
                'scID': '699059032',
                'prodCID': '14675',
                'prodColor': 'Endless Summer'
            },

            {
                'prodName': "Women's Graphic Canvas Totes",
                'prodPrice': '$10.00',
                'prodSKU': '6990760420000',
                'scID': '699076042',
                'prodCID': '12818',
                'prodColor': 'Tahiti Lagoon'
            },

            {
                'prodName': "Women's Printed Umbrellas",
                'prodPrice': '$10.00',
                'prodSKU': '6996690120000',
                'scID': '699669012',
                'prodCID': '45062',
                'prodColor': 'Green Stripe'
            }
        ];

        this.reStyleMarketingContainer();
        this.insertProducts();

    },

    reStyleMarketingContainer: function() {

        this.marketingContainer.style.width = '395px';
        this.marketingContainer.style.padding = '0px 0px 0px 16px';
        this.marketingContainer.style.height = '250px';
        this.marketingContainer.style.backgroundColor = '#F0EBEA';
        this.marketingContainer.style.textAlign = 'center';

    },

    insertProducts: function() {

        var self = this;
        var numProducts = 0;

        //insert 3 products
        for (var i = 0; i < this.maxVisibleProducts; i++) {

            var isSalableURL = '/browse/isProductSalable.do?pid=' + this.products[i].scID;

            //need to check if is salable
            new Ajax.Request(isSalableURL, {
                method: 'get',
                onSuccess: function(transport) {

                    var response = transport.responseText.strip();

                    if (response.indexOf('true') > 0) {

                        var thisProdID = self.productContainer + numProducts;

                        self.marketingContainer.style.backgroundImage = "url(" + wcdLib.getAssetPath(self.cgID, 'cool_stuff.gif') + ")";
                        self.marketingContainer.style.backgroundRepeat = "no-repeat";
                        self.marketingContainer.style.padding = '0px 0px 0px 14px';
                        self.marketingContainer.style.height = '308px';


                        //product container
                        var csProdContainer = document.createElement('div');
                        csProdContainer.style.cssText = 'color:#6f7f9b;font-size:12px;font-family:arial;';

                        csProdContainer.setAttribute('id', thisProdID);
                        csProdContainer.style.width = '120px';
                        csProdContainer.style.margin = '66px 9px 0px 0px';
                        if (clientBrowser.isIE6up) {
                            csProdContainer.style.styleFloat = 'left';
                        } else {
                            csProdContainer.style.cssFloat = 'left';
                        }

                        //image container
                        var csProdImgContainer = document.createElement('div');
                        csProdImgContainer.setAttribute('style', 'width: 120px; margin: 0px auto 0px auto;');
                        var csProdImg = document.createElement('img');
                        csProdImg.setAttribute('id', thisProdID + '_img');
                        csProdImg.setAttribute('src', self.getVIPath(self.products[numProducts].scID));
                        csProdImgContainer.appendChild(csProdImg);

                        //name container
                        var csProdNameContainer = document.createElement('div');
                        csProdNameContainer.style.cssText = 'width:120px;font-weight:bold;';
                        csProdNameContainer.appendChild(document.createTextNode(self.products[numProducts].prodName));

                        //price container
                        var csProdPriceContainer = document.createElement('div');
                        csProdPriceContainer.setAttribute('style', 'width:120px;');
                        csProdPriceContainer.appendChild(document.createTextNode(self.products[numProducts].prodPrice));

                        //buy button container
                        var csProdButtonContainer = document.createElement('div');
                        csProdButtonContainer.setAttribute('style', 'width: 120px;');

                        //buy button link

                        var csProdButtonLink = document.createElement('a');
                        //csProdButtonLink.setAttribute('href', '#buy');
                        //onclick workaround for IE
                        csProdButtonLink.setAttribute("href",
                            "javascript:" + self.objName + ".addToBag('" + self.products[numProducts].prodSKU + "', '" + self.products[numProducts].prodCID + "','" + thisProdID + "' );"
                        );

                        //buy button
                        var csProdButton = document.createElement('img');
                        csProdButton.setAttribute('src', wcdLib.getAssetPath(self.cgID, self.buyButton));
                        csProdButton.setAttribute('alt', 'Buy');
                        csProdButton.setAttribute('title', 'Buy');

                        csProdButtonLink.appendChild(csProdButton);
                        csProdButtonContainer.appendChild(csProdButtonLink);

                        //add to product container
                        csProdContainer.appendChild(csProdImgContainer);
                        csProdContainer.appendChild(csProdNameContainer);
                        csProdContainer.appendChild(csProdPriceContainer);
                        csProdContainer.appendChild(csProdButtonContainer);

                        //add to page (marketing contaier)
                        self.marketingContainer.appendChild(csProdContainer);

                        //counter for 'true' ajax responses
                        numProducts++;
                    }
                }
            });
        }

        //center products if showing less than 3
        if (numProducts == 2) {

        }
        else if (numProducts == 1) {

        }

        this.checkForAddedProduct();

    },

    /**
    * checkForAddedProduct - check cookie to see if a Cross Sell product was just added
    * addMsgParams (i.e. "0-234")
    * addMsgParams[0] = the product just added to bag (from left to right - 0,1,2)
    * addMsgParams[1] = scroll position at time the product was added.
    */
    checkForAddedProduct: function() {

        addMsgParams = gidLib.getCookieVar('mktUniversalPersist', 'CSAdd');
        gidLib.removeCookieVar('mktUniversalPersist', 'CSAdd');
        if (addMsgParams != '') {
            addMsgParams = addMsgParams.split('-');
            thisTarget = this.productContainer + addMsgParams[0];

            document.documentElement.scrollTop = addMsgParams[1];
            //new Effect.ScrollTo('universalBar', { offset: addMsgParams[1] });

            this.showAddedToBagMsg(thisTarget);
        }
    },

    showAddedToBagMsg: function(target) {

        var self = this;
        var csAddedToBagMsg = document.createElement('div');
        var addMsgDivID = this.addedToBag.divName
        csAddedToBagMsg.setAttribute('id', addMsgDivID);
        csAddedToBagMsg.style.width = '120px';
        csAddedToBagMsg.style.height = '22px';
        csAddedToBagMsg.style.diplay = 'none';
        csAddedToBagMsg.style.position = 'relative';

        var csAddedToBagImg = document.createElement('img');
        csAddedToBagImg.setAttribute('src', wcdLib.getAssetPath(this.cgID, this.addedToBag.imgSrc));

        csAddedToBagMsg.appendChild(csAddedToBagImg);

        //delay to wait for cross sell to load completely
        setTimeout(function() {
            $(target).appendChild(csAddedToBagMsg);

            var topPos = $(target).offsetHeight - $(target + '_img').offsetHeight;
            csAddedToBagMsg.style.top = '-' + topPos + 'px';

            //flash msg on and off
            new Effect.Appear(addMsgDivID, { duration: 2.0, afterFinish: function() {
                new Effect.Fade(addMsgDivID, { duration: 2.0 });
            }
            });
        },
            900
        );

    },

    addToBag: function(thisSKU, thisCID, prodContainer) {

        //var newPathname = location.pathname + location.search;
        var thisHost = location.protocol + '//' + location.host;
        var newPathName = '/buy/shopping_bag.do';
        newPathName = thisHost + newPathName;

        var scrollPos = document.documentElement.scrollTop + 70;
        var cookieStr = prodContainer.charAt(prodContainer.length - 1) + '-' + scrollPos;
        gidLib.setCookieVar('mktUniversalPersist', 'CSAdd', cookieStr);

        //set up omniture data
        for (i = 0; i < this.products.length; i++) {
            if (this.products[i].prodSKU == thisSKU) {
                prodIndex = i;
                break;
            }
        }

        //omni queryString - ?mlink=3090,1441447,STYLE-COLOR NUMBER&clink=1441447
        newPathName += '?mlink=3090,' + this.cgID + ',' + this.products[prodIndex].scID + '&clink=' + this.cgID;

        //myOmniInlineBag = "688928^,^3^,^Men&#39;s Wool-Blend Peacoat^,^1^,^79.50^,^Black Jack^,^XL"
        myOmniInlineBag = this.products[prodIndex].scID.substr(0, 6)
                            + "^,^3^,^"
                            + this.products[prodIndex].prodName
                            + "^,^1^,^"
                            + this.products[prodIndex].prodPrice
                            + "^,^"
                            + this.products[prodIndex].prodColorName
                            + "^,^One Size"
                            ;

        var bagItemCount = objInlineBag.model.data.itemCount;
        var data = {
            'omniInlineBagJS': myOmniInlineBag,
            'omniAddToBag': {
                'hasPromotionOrCrossSell': 'false',
                'inlineBagItemCount': bagItemCount,
                'inlineBagQuantity': bagItemCount
            }
        }

        // Omni related code to track add event
        if (data.omniInlineBagJS) {
            if (!(reportingService || {}).isActive) {
                omni.setAddToBag(data.omniAddToBag, data.omniInlineBagJS);
            }
            else {
                reportingService.controller.viewManagers.inlineBagAddViewManager.controller.getReportRequest(data.omniAddToBag, data.omniInlineBagJS);
            }
        }
        //end omniture data



        var requestURL = thisHost + '/buy/inlineShoppingBagAdd.do?skuid='
                         + thisSKU
                         + '&quantity'
                         + thisSKU
                         + '=1&sfl'
                         + thisSKU
                         + '=false&cid'
                         + thisSKU
                         + '='
                         + thisCID;

        new Ajax.Request(
            requestURL,
             {
                 method: 'get',
                 onComplete: function() {

                     quickLook.parseInlineBagAjaxResonse;

                     //refresh page
                     window.location = newPathName;
                 }
             }
        );

        //return false;
    },

    getVIPath: function(scID) {

        // /Asset_Archive/ONWeb/Assets/Product/675/675838/category/on675838-01viv01.jpg
        viPath = "/Asset_Archive/ONWeb/Assets/Product/"
                + scID.substr(0, 3)
                + "/"
                + scID.substr(0, 6)
                + "/category/"
                + brandConst.BRAND_ID.toLowerCase()
                + scID.substr(0, 6) + "-" + scID.substr(6, 2) + "viv01.jpg";

        return viPath;
    }

};
