
var tabs_divEgiftTabs = null;

var egift_charcounter = null;
var egift_buyform = null;

document.observe('dom:loaded', function () {
	
	// This became a little bit kludgey, especially when having
	// to recalc the heights of tabs.  But I can't find a cooler way
	// around that for now, and it's getting late.... and this works....

	var gcid = uri.queryKey['GCID'] || 0;
	var tabid = uri.queryKey['tabid'] || 'buy';
	
	var buyIsLoaded = function() {
				$('btnEGiftBuyTerms').observe( 'click', function(evt) {
					tabs_divEgiftTabs.setActiveTab("tab_id_about");
					evt.stop();
				});

				egift_charcounter = new CharCounter( 'idEgiftMessage', {
					maxLength: 215,
					elCounter: 'idEgiftMessageCounter',
					countDown: true
				});
	
				egift_buyform = new ModRPCForm ( $('buyEgiftForm'), {
						msgComplete: '',
						onSuccess: function() {
							
							var egiftForm = $('buyEgiftForm');
							
							var skuElement = egiftForm['SKU_ID_REQUIRED'];
							var skuText = skuElement.options[skuElement.selectedIndex].text;
							var skuID = skuElement.options[skuElement.selectedIndex].value;
							var gcID = $F(egiftForm['GIFTCARD_ID']);
							
							var buyinfo = {
								GIFTCARD_TO: $F(egiftForm['GIFTCARD_TO']),
								GIFTCARD_FROM: $F(egiftForm['GIFTCARD_FROM']),
								GIFTCARD_PRICE: skuText,
								EMAIL_ADDRESS: $F(egiftForm['EMAIL_ADDRESS_REQUIRED']),
								MESSAGE: $F(egiftForm['MESSAGE']),
								QTY: 1,
								TTL: skuText
							};
							
							$('tab_id_buy').down('.tab-product-info').style.height = '366px';

							egiftForm.reset();
							
							// If gcID exists, this is an update, not an add.
							if ( !gcID ) {
								cart.addSku ( skuID, 1 );
							}
							cart.fillinDropCart({
								template: templatefactory.get('dropcart-egift'),
								position: 'replace',
								object: buyinfo,
								callback: cart.showDropCart.bind(cart)
							});
						},
						onFailure: function() {
							$('tab_id_buy').down('.tab-product-info').style.height = '432px';
						},
						onValidateFailure: function() {
							$('tab_id_buy').down('.tab-product-info').style.height = '432px';
						}
					});

	};
	
	var checkIsLoaded = function() {

				$('checkEgiftButton').observe( 'click', function(evt) {
					evt.stop();
					
					var balDiv = $('remaining-balance');
					var errDiv = $('egift-error');
					balDiv.hide();
					errDiv.hide();
					
					var gcCheck = new EGiftCheck({
						gcNum: $('GIFTCARD_NUM').value,
						gcPin: $('GIFTCARD_PIN').value,
						
						callback: function() {
							if ( gcCheck.errorMsg ) {
								var html = 
									'<span class="red-text">' +
									gcCheck.errorMsg +
									'</span>'
									;
								errDiv.update(html);
								errDiv.show();
								balDiv.hide();
								$('tab_id_check').down('.tab-product-info').style.height = '275px';
							} else {
								var html = 
									'Remaining balance: <span class="red-text">'+
									gcCheck.balanceAsCurrency() +
									'</span>';
								balDiv.update(html);
								balDiv.show();
								errDiv.hide();
								$('tab_id_check').down('.tab-product-info').style.height = '275px';
							}
						}
						
					});
					
				});

	};

	
	tabs_divEgiftTabs = new TabbedContentWrapper ({
		wrapperContainerID: "divEgiftTabs",
    	tabContainerID: "tab-label-container"
	});
	
	tabs_divEgiftTabs.fixContentHeight("tab_id_about");
	tabs_divEgiftTabs.setActiveTab("tab_id_"+tabid);
	
	buyIsLoaded();
	checkIsLoaded();
	

});

document.observe("productPage.ProductTable:loaded", handleEgiftFooterCheckEvent);
document.observe("productPage.miniThumbTable:loaded", handleEgiftFooterCheckEvent);

function handleEgiftFooterCheckEvent(evt) {
	var obj = $('recommendation_accordion');
	if ( obj ) {
		obj.childElements().each(function(el){
			el.fixContentHeight();
		});
		obj.fixContentHeight();
	}
}
