var cargoHold = {

	initialize:function() {
	if ($('shoppingBagSubTotalValue')) {
		this.subtotalThreshold = 50.00;
		this.currentSubTotal = 0.00;
		this.UniversalCookieState = gidLib.getCookieVar('mktUniversalPersist','FS1022');
		this.lastSavedSubTotal = gidLib.getCookieVar('mktUniversalSession','FS1022sub');
		this.isThresholdMet = false;
		this.checkoutModified = false;
		this.CountSisterBrandsInBag = 0;
		this.sisterBrandCount = gidLib.getCookieVar('mktUniversalSession','sisterBrandCount');
		this.hasButtonBeenMade = false; // fix a bug where the big ship is replicated more than once.  Needs to be up here because of scope.

		// if the user entered a valid free standard ship promo, we default them back to standard here and set a cookie
		this.hasOtherFreeShipPromo = (gidLib.getQuerystringParam("fspc") == "st" || gidLib.getCookieVar('mktUniversalSession','FS1022Promo') == 'true');
		if (this.hasOtherFreeShipPromo == true) {
			gidLib.setCookieVar('mktUniversalSession','FS1022Option','BFSAB_OTHER');
			gidLib.setCookieVar('mktUniversalSession','FS1022Promo','true');
		}

		this.hasUserSelectedOption = (gidLib.getQuerystringParam("fspc") == "fs" || gidLib.getCookieVar('mktUniversalSession','FS1022Option') == 'BFSAB_BASIC');
		if (this.hasUserSelectedOption == true) {
			gidLib.setCookieVar('mktUniversalSession','FS1022Option','BFSAB_BASIC');
			gidLib.setCookieVar('mktUniversalSession','FS1022Promo','false');
		}
		
		this.brandScanner();
		this.subTotalScanner();
		this.radioBlaster();
		this.shippingTracker();
		this.optionMonger();
	}
	}, // END OF INITIALIZE
	
	subTotalScanner:function() {
		this.subTotalAsString = $('shoppingBagSubTotalValue').innerHTML;
		this.subTotalAsStringNumbersOnly = this.subTotalAsString.substring(1,this.subTotalAsString.length);

		// code added to deal with commas.
		if (this.subTotalAsStringNumbersOnly.indexOf(",") != -1) {
			this.subTotalAsStringNumbersOnly = this.subTotalAsStringNumbersOnly.replace(',','');
		}
		
		this.currentSubTotal = parseFloat(this.subTotalAsStringNumbersOnly);
		gidLib.setCookieVar('mktUniversalSession','FS1022sub',this.currentSubTotal);
		
		if (this.currentSubTotal >= this.subtotalThreshold) {
			this.isThresholdMet = true;
		}
	},
	
	
	brandScanner:function() {
		this.badgeCollection = $('mainContent').getElementsBySelector('ol[class="lineItemDisplay"]');
		this.brandBadges = this.badgeCollection[0].getElementsBySelector('dd[class="brandBadge"]');

		for (z=0; z<this.brandBadges.length; z++) {
			this.brandScannerResult = this.brandBadges[z].getElementsBySelector('img')[0].getAttribute('alt');
				if (this.brandScannerResult =='piperlime.com') {
				  // do nothing just move along
				} else if (this.brandScannerResult =='gap.com') { // adding converse scanning code, 1st see if it's a gap product
					var listOfVendorNames = $('mainContent').getElementsBySelector('dd[class="productName"]');
					isItGapConverse = listOfVendorNames[0].getElementsBySelector('a');
					for(i = 0; i < listOfVendorNames.length; i++){ // now iterate through to see if any are converse
					var contents = isItGapConverse[0].innerHTML;
						if (contents.indexOf("Converse") != -1) {
							// do nothing just move along
						} else { // it's gap and not converse
							this.CountSisterBrandsInBag +=1;
						}
					}		
				} else {
					// not PLOL or Gap/Converse, so increment the sister count
					this.CountSisterBrandsInBag +=1;
				}
		}
		
		gidLib.setCookieVar('mktUniversalSession','sisterBrandCount',this.CountSisterBrandsInBag);
			
	},


	radioBlaster:function() {
		new Insertion.Before('selectedShippingMethod1','<div style="margin-bottom: 10px; width: 210px;" id="selectedShippingMethod1"><input style="margin: 0px; height: 12px; vertical-align: bottom;" name="selectedShippingMethod" value="1" type="radio"> <span style="vertical-align: top;"> Free 10-12 Bus Days (<a id="basicShippingLink" onclick="return gidLib.layeredPopup.openLayeredPopup({str:\'/browse/info.do?cid=35296\',id:\'Free Ship on $50\',width:405,height:380});" style="font-size:9px;cursor:pointer;">on orders over $50</a>)</span></div>');
	
		// CREATE AN ARRAY OF RADIO BUTTONS THAT INCLUDE THE NEW ONE
		this.radioButtonsArray = $('shoppingBagSummaryContainer').getElementsBySelector('div[class="shippingSelect"]');
		this.radioSpans = $('shoppingBagSummaryContainer').getElementsBySelector('span');
		this.radioButtons = this.radioButtonsArray[0].getElementsBySelector('input');
	},

	shippingTracker:function() { // this function greys out the FS button if the threshold is not met
		if (!this.isThresholdMet) {
			this.radioButtons[0].onclick = function() {return false;};
			this.radioButtons[0].setStyle({opacity:'.5',filter:'alpha(opacity=50)'});
			this.radioSpans[0].setStyle({color: '#b9b0b0'});// opacity:'.5',filter:'alpha(opacity=50)'
			this.radioButtons[0].disabled = true;
			this.radioButtons[0].checked = false;
			//$('basicShippingLink').onclick = function() {return false;};
			//$('basicShippingLink').setStyle({cursor:'default'});
		}
	
	
		if (this.isThresholdMet) {
			//// alert(this.isThresholdMet);
			this.radioButtons[0].onclick = function() {
		
			// KEY OFF OF THIS TO AUTOMATICALLY HAVE THIS OPTION CHECKED IN CHECKOUT
			gidLib.setCookieVar('mktUniversalSession','FS1022Option','BFSAB_BASIC');
			gidLib.setCookieVar('omniSession','FS1022Option','BFSAB_BASIC');
			submitShippingMethodForm();
			};
		}
	},
	
	optionMonger:function() {

		// if the user qualified for free standard shipping in checkout, we want to set to standard ship
		if (this.hasOtherFreeShipPromo == true) {
			this.radioButtons[1].checked = true;

		} else if (this.hasUserSelectedOption == true) {

			this.radioButtons[0].checked = true;
			
			// IF A USER SELECTS FREE, THEN FALLS UNDER THE THRESHOLD, MAKE SURE PPC SWITCHES TO STD.
			if ( (this.hasUserSelectedOption == true) && (!this.isThresholdMet) ) {
				this.radioButtons[1].checked = true;
				this.radioButtons[0].disabled = true;
				gidLib.setCookieVar('mktUniversalSession','FS1022Option','BFSAB_OTHER');
			}
				
			for (var i=1;i<=3;i++) {
				this.radioButtons[i].onclick = function () {
					gidLib.setCookieVar('mktUniversalSession','FS1022Option','BFSAB_OTHER');
					submitShippingMethodForm();
				}
			}

			if ((gidLib.getCookieVar('mktUniversalSession','sisterBrandCount') != "0") && this.hasOtherFreeShipPromo == false) { 
				this.radioSpans[1].innerHTML = 'Standard 5-9 Bus Days - $7.00';
			} else if ( (gidLib.getCookieVar('mktUniversalSession','sisterBrandCount') == "0") || (gidLib.getQuerystringParam("fspc") == "fs") ) { 
				this.radioSpans[1].innerHTML = 'Standard 5-9 Bus Days - FREE';
			}
		}
	}

}; // END OF cargoHold




