NO_USER_NOTIFY = 1;
eGCOnly = false;
eGC_PRODID = 'PROD12064';
// WDR: ALWAYS show the samples page.  User can change sample selection, if desired.
// Logic for filtering out extra samples is handled on backend.
SHOW_SAMPLES = /*Cookie.get('fsamlanding') ? false:*/ true;

document.observe('dom:loaded', function () {

//	new JsonRPCForm( 'offer-code', {
//		method: 'transaction.setoffercode',
//		msgComplete: 'Offer code saved!',
//		msgFailure: 'Offer code not saved.',
//		onSuccess: function(){
			//updateTotals
			// I am totally cheating here, but if the offer code succeeds,
			// reload the page since it may have involved adding a free sample
			// to the cart.
//			document.location.href = "/templates/transaction/viewbag.tmpl";
//		}	
//	} );	

    var quickshopLinks = $$(".similar-products .quickshop-link");
    quickshopLinks.each(function(lnk) {
        var prodID = (lnk.getAttribute("productid"));
        lnk.observe('click', function(evt) {
		    var onAddFn = function() {
		        location.href='/templates/transaction/viewbag.tmpl'; // force page back to basic viewbag
		    };
			productPage.launchQuickshop(prodID, {onAdd: onAddFn});
			evt.stop();
		});
    });
});


document.observe('cart:loaded', function () {

	// before we do anything, set up the chckout buttons
	$$('a.checkout-buttons').each( function (btn) {
		new CheckoutContinueButton(btn)
	});
	if (cart.contents.keys().length){
		cart_items = new CatProdPageData(
			[{ 
				"sku": cart.contents.keys(),
				"sku_fields": [
				"PRICE",
				"product"
				], "product_fields": [
				"PRODUCT_ID"
				]
			}], true, updateTotals
		);		
	} else {
		document.location.href="/";		
	}
	
	
});

// our button subclasses

var CheckoutContinueButton = Class.create( GenericButton, {
	initialize: function ($super, element, options) {
		this.element = $(element);
		this.options = Object.extend({
			click: function (evt) {
				if (!cart.contents.keys().length) {
					// no cart items... go home
					document.location.href = "/";
				}
				if (eGCOnly || !SHOW_SAMPLES) {
					document.location.href = "/templates/transaction/checkout_review.tmpl";
				} else {
					document.location.href = "/templates/transaction/checkout_samples.tmpl";
				}
				evt.stop();
			}			
		}, options || {});
		return $super(this.element, this.options);	
	}
});

var RemoveLink = Class.create({
	initialize: function (element) {
		this.element = $(element);
		this.element.observe (
			'click', 
			function (evt) {
				var el = evt.element();
				var elid = el.id;
				var skuid = elid.match(/SKU.+/);
				if (skuid.length) {
					sid = skuid[0];
					
					cart.remove( sid, { 
						success: function (t) {
							
							// To correct subordinate UCG skus
							if ( sid=='SKU33171' || sid=='SKU33201' ) {
								cart.initialize(); // will delete the other six items
							}
							
							$(elid).innerHTML = 'Removed';
							$('cart-item-row-'+sid).hide();
							$('cart-item-spacer-'+sid).hide();
							updateHighlights();
							updateTotals ();
						},
						failure: function (t) {
							$(elid).innerHTML = 'Failed, try again.';
						}
					});

					el.innerHTML = 'Removing';					
				}
				evt.stop();
			}
		);
				
		}
});

var SubButton = Class.create({
	initialize: function (element) {
		this.element = $(element);
		this.element.observe(
			'click',
			function (evt) {
				var el = evt.element();
				var elid = el.id;
				var skuid = elid.match(/SKU.+/);
				if (skuid.length) {
					sid = skuid[0];
					var curqty = cart.contents.get(sid);
					if ( curqty == 1 ) {
						// alert ('Min quantity is 1');
						evt.stop();
						return;
					}					
					id = cart.sub( sid, { 
						success: updateTotals
					});
				}
				evt.stop();
			}			
		);
		return this.element;
	}
	
});

var AddButton = Class.create({
	initialize: function (element) {
		this.element = $(element);
		this.element.observe(
			'click',
			function (evt) {
				var el = evt.element();
				var elid = el.id;
				var skuid = elid.match(/SKU.+/);
				if (skuid.length) {
					sid = skuid[0];
					var curqty = cart.contents.get(sid);
					if ( curqty == 6 ) {
						// alert ('Max quantity is 6');
						evt.stop();
						return;
					}					
					id = cart.add({
						sku_id: sid,
						increment: true,
						simple: true
					},
					{
						success: updateTotals
					});	
				}
				evt.stop();
			}
		);
		return this.element;
	}
	
});

function updateHighlights () {
	var classbase = 1, classpos = 0;
	var classname = $w('standard highlight');
	$$('tr.cart-item-rows').each(function (row) {
		if (row.visible()) {
			classpos += classbase;
			row.removeClassName('standard');
			row.removeClassName('highlight');		
			row.addClassName(classname[classpos]);
			classbase = classbase * -1;			
		}
	} );
}

// the functions we need to process the data
function updateTotals () {
	var TTL = 0;
	var TTL_NOEGC = 0;
	eGCOnly = true;
	cart.contents.keys().each( function( skuid ) { 
		if ($('cart-item-row-'+skuid) && $('cart-item-row-'+skuid).visible()) {
			var sku = cart_items.data.get('sku')[skuid];
			var prod = sku.products.first();
			var price = sku.PRICE;
			var qty = cart.contents.get(skuid);
			var prodid = prod.PRODUCT_ID;
						
			if ( eGCOnly && prodid != eGC_PRODID && qty > 0 ) eGCOnly = false; // check in egiftcard only
			var ttl = (price * qty).toCurrency('$');
			if ( prodid != eGC_PRODID) { // Don't include EGiftCard
				TTL_NOEGC += (price * qty);
			}			
			TTL += (price * qty);
			// Don't dynamically update the total column if the "noupdate" class is present.
			// (This is used by egifts and custom bottle, which can't be dynamically changed.)
			var elemTotal = $('cart-item-row-'+skuid).down('td.total');
			if ( elemTotal && !elemTotal.hasClassName('noupdate') ) {
				$('qty-'+skuid).innerHTML = qty;
				elemTotal.innerHTML = ttl;
			}
		}
	});
	updateHighlights();

	// Empty Cart
	if (TTL==0) {
		// What to do?
	}
	
	// Enable/disable checkout button - if no priced items, hide it.
	$$('a.checkout-buttons').each( function (btn) {
		if ( TTL ) btn.show();
		else btn.hide();
	});

	$('viewbag-checkout-total').innerHTML = TTL.toCurrency('$');
	
	// WDR: load msg from tmpl so we pull info from .net
	// (had been hard-coded here...)
	new Ajax.Updater('viewMsgBoxWrapper', '/templates/includes/transaction/viewbag_message.tmpl');
	
	$('top-nav-signin').observe('submit', function (evt) {
		// we'll reload the page if the user signs in.
		document.observe('user:loaded', function (evt) {
			if (user.isSignedIn) {
				location.reload(true);
			}
		});		
	});
	

	
}