var WCDLibrary = Class.create();

WCDLibrary.prototype = {

    initialize: function() {


    },

    /**     * getAssetPath - gets the path to an asset dynamically, based on the content group number and brand     * This is a method of class WCDLib     * @param {string} contentID = 1234567     * @param {string} asset OPTIONAL i.e. someImage.jpg     * @return (string) assetPath i.e. /Asset_Archive/ONWeb/content/0001/234/567/assets/someImage.jpg     * @author Jermaine Jackson     * @date 08/27/2009     */
    getAssetPath: function(contentID, asset) {

        asset = asset && asset != '' ? asset : '';

        var contentPath = '000' + contentID.substr(0, 1) + '/' + contentID.substr(1, 3) + '/' + contentID.substr(4, 3);
        var assetPath = '/Asset_Archive/' + brandConst.BRAND_ID + 'Web/content/' + contentPath + '/assets/' + asset;

        return assetPath;
    },


    /**    * getMlink - changes browser url to a new url with Omniture tracking appended. *Omniture tracking should be sorted by mlink    * @return (string) mlink    * @author Jermaine Jackson    * @date 08/27/2009    */
    getMlink: function() {
        var mlink;
        if (reportingService) {
            mlink = reportingService.controller.viewManagers.commonViewManager.model.commonCurrentBusinessId;
        }
        else {
            mlink = false;
        }
        return mlink;
    },


    /**     * goToTrackingURL - changes browser url to a new url with Omniture tracking appended     * This is a method of class WCDLib     * @param {string} baseURL = /browse/category.do?cid=12345     * @param {string} contentID = (content Group # used for tracking and clink) i.e. 1234567     * @param {string} label OPTIONAL = additional label used in mlink for tracking     * @return - returns nothing (changes browser location)     * @author Jermaine Jackson     * @date 00/00/2009     */
    /*
    goToTrackingURL: function(baseURL, contentID, label) {

        thisMlink = this.getMlink();
        label = label && label != '' ? ',' + label : '';
        //var trackingURL = location.protocol + '//' + location.host + baseURL + '&mlink=' + thisMlink + ',' + contentID + label + '&clink=' + contentID;
        //location.href = 'http://oldnavy.gap.com/browse/category.do?cid=00000&mlink=5421,1234567&clink=1234567';
    },
	*/

    /**
     * isProuctSalable() 
     * makes ajax request to check if product is salable
     * @return returns true or false;
     * ??? Returns 'fail' onFailure
	 * @author Jermaine Jackson
     */
	 /*
    isProductSalable: function(thisPID) {
        new Ajax.Request('/browse/isProductSalable.do?pid=' + thisPID, {
            method: 'get',
            onSuccess: function(transport) {
                var response = transport.responseText.strip();
                if (response.indexOf('true') >= 0) {
                    return true;
                }
                else {
                    return false;
                }
            },
            onFailure: function() {
                retVal = 'fail';
                return retVal;
            }
        });
    },
	*/
	
    /** 
     * getVIAssetPath()
	 * @Param (STRING) styleID - product's style id
     * @retrn returns the path to the vi image for the given styleID
	 * @author Jermaine Jackson
     */
    getVIAssetPath: function(styleID) {
        // /Asset_Archive/ONWeb/Assets/Product/730/730382/category/on730382-03viv01.jpg

        //TODO : use Brand code insted of ONWeb

        viPath = '/Asset_Archive/ONWeb/Assets/Product/' + styleID.substr(0, 3) + '/'
            + styleID.substr(0, 6) + '/category/on' + styleID.substr(0, 6) + '-' + styleID.substr(6, 2) + 'viv01.jpg';

        return viPath;
    },

    /**
     * getProductData
     * @Param (STRING) thisPID - product id (scid/style-color number or style number (6 digits)
     * @Param (STRING) thisField - field of the product that is being requested. Currently either "price" or "name"
     * @Param (BOOLEAN) includeCurrencySymbol - include the symbol or not
     * @return - updates thisElementID with retrieved price     * @author Jeff Held
     * @modified Jermaine Jackson
     */
    getProductPrice: function(thisPID, thisElementID) {

        var theCurrencySymbol = "$";

        new Ajax.Request('/browse/productData.do?pid=' + thisPID, {
            method: 'get',
            onSuccess: function(transport) {

                var rawData = transport.responseText.strip();
                rawData = rawData.replace("&#39;", "'");
                rawData = rawData.replace("&#160;", "'");
                initialdataArray = rawData.split(";");
                firstLineArray = initialdataArray[0].split(",");

                var thePrice = firstLineArray[13];
                thePrice = thePrice.replace(/'/gi, "");
                //thePrice = thePrice.replace("$", "");
                thePrice = thePrice.replace("<span class=\"priceDisplay\">", "");
                if (thePrice.indexOf("priceDisplaySale") != -1) {
                    thePrice = thePrice.replace("<span class=\"priceDisplayStrike\">", "");
                    thePrice = thePrice.replace("<span class=\"brandBreak\">", "");
                    thePrice = thePrice.replace("<span class=\"priceDisplaySale\">", "");
                    thePrice = thePrice.replace("<\/span>", "");
                    thePrice = thePrice.replace("<\/span>", "");
                    thePrice = thePrice.replace("<\/span>", "");
                    var thePriceArr = thePrice.split("$");
                    thePrice = thePriceArr[1];
                }
                thePrice = thePrice.replace("<\/span>", "");

                //get the next value if the price happens to be true/false
                if (thePrice == "false" || thePrice == "true") {

                    thePrice = firstLineArray[14];
                    thePrice = thePrice.replace(/'/gi, "");
                    //thePrice = thePrice.replace("$", "");
                    thePrice = thePrice.replace("<span class=\"priceDisplay\">", "");
                    if (thePrice.indexOf("priceDisplaySale") != -1) {
                        thePrice = thePrice.replace("<span class=\"priceDisplayStrike\">", "");
                        thePrice = thePrice.replace("<span class=\"brandBreak\">", "");
                        thePrice = thePrice.replace("<span class=\"priceDisplaySale\">", "");
                        thePrice = thePrice.replace("<\/span>", "");
                        thePrice = thePrice.replace("<\/span>", "");
                        thePrice = thePrice.replace("<\/span>", "");
                        var thePriceArr = thePrice.split("$");
                        thePrice = thePriceArr[1];
                    }
                    thePrice = thePrice.replace("<\/span>", "");

                }

                thePrice = thePrice.indexOf("$") < 0 ? "$" + thePrice : thePrice;
				
				//remove trailing hyphen
				if(thePrice.charAt(thePrice.length - 1) == "-"){
					thePrice = thePrice.substr(0,thePrice.length - 1);
				}

                $(thisElementID).update(thePrice);

            },
            onFailure: function() {

                $(thisElementID).update("");

            }
        });
    },

	
	/**
     * getProductName
     * @Param (STRING) thisPID - product id (scid/style-color number or style number (6 digits)
     * @Param (STRING) thisElementID - Element that will be updated with the products price
     * @return - updates thisElementID with retrieved name
     * @author Jermaine Jackson
     */
    getProductName: function(thisPID, thisElementID) {

        new Ajax.Request('/browse/productData.do?pid=' + thisPID, {
            method: 'get',
            onSuccess: function(transport) {

                var rawData = transport.responseText.strip();
                rawData = rawData.replace("&#39;", "'");
                rawData = rawData.replace("&#160;", "'");
                initialDataArray = rawData.split(";");
                firstLineArray = initialDataArray[0].split(",");

                var theItemName = firstLineArray[25];
                theItemName = theItemName.substring(1, theItemName.length - 1);

                //if name < 5 chars, get next value
                if (theItemName.length < 5) {
                    theItemName = firstLineArray[26];
                    theItemName = theItemName.substring(1, theItemName.length - 1);
                }

                $(thisElementID).update(theItemName);

            },
            onFailure: function() {

                $(thisElementID).update("");

            }
        });

    },

    /**
     * trackLink - tracks link and gives a name to the link for Omniture.
     * @Param (Element) thisElement - the element that fires the link/click (i.e. this)
     * @Param (STRING) linkName - name that should be assigned to the link
	 * @Author Jermaine Jackson
     */
    trackLink: function(thisElement, linkName) {

        /* EXAMPLE
        <a href="index.html" onClick="
        s.linkTrackVars='eVar6';
        s.eVar6='homepage banner link1';
        s.tl(this,'o','homepage banner link1');
        ">Click me</a>
        */

        s.linkTrackVars = 'eVar6';
        s.eVar6 = linkName;
        s.tl(thisElement, 'o', linkName);

    },

	/**
	 * getCleanString - removes all whitespace, tabs and newline characters from the string
	 * @PARAM (STRING) thisString - the string that gets cleaned
	 * @PARAM (boolean) OPTIONAL makeLowerCase - converts entire string to lowercase if set to true
	 * @return returns string with no whitespace
	 */
    getCleanString: function(thisString, makeLowerCase) {
       thisString = thisString.replace(/^\s+|\t+|\n+| |\s+$/g, '') ;
	   thisString = makeLowerCase ? thisString.toLowerCase() : thisString;
       return thisString;
    },
	
	/**
	 * scrollToHeader - scrolls page to requested header (<h2 class=header4 />)
	 * @PARAM (STRING) thisHeader - the string that inside the header tag that will be scrolled to
	 */
	scrollToHeader: function(thisHeader){
	
		thisHeader = wcdLib.getCleanString(thisHeader,true);
		
		$$("h2[class=header4]").each(function(thisNode){
			
			str = wcdLib.getCleanString(thisNode.innerHTML, true);
			str = str.replace(/&amp;/g, '&');

			if(str == thisHeader){
				new Effect.ScrollTo(thisNode);
				return true;
			}
			
		});
		
	},
	
	/**
     * changeImage()
     * Changes the image of an img element
     * thisImage = (STRING) image element as 'someID'
     * thisImagePath = (STRING) the new path of the new image
     * [optional] thisImageMap = (STRING) the name of the image map to use
     */
    changeImage: function(thisImage, thisImagePath, thisImageMap) {

		var thisImage = $(thisImage);
        thisImage.src = thisImagePath;

        if (thisImageMap != '') {
            thisImageMap = thisImageMap.charAt(0) == '#' ? thisImageMap : '#' + thisImageMap;
            thisImage.useMap = thisImageMap;
        }

    }

};

var wcdLib = new WCDLibrary();
