/* ---------------------------------------- *
* abandon hope, all ye who enter here ----- *
* author: gregory nicholas            ----- *
* uri: www.phasedreduction.com        ----- *
* ----------------------------------------- */
Cufon.replace ( '.replace-c', {
	fontFamily : 'HelveticaNeue87Condensed'
});

VAEL = {
	Forms : null,
	init : function () {
		Font.Set ( '.replace-c' );
		ImageLoaders.init ();
		Modals.init ();
		MenuA.init ();
		VAEL.init_forms ( 'form' );
		$ ( 'logo' ).observe ( 'mouseover', function () {
			$ ( 'logo-over' ).appear ( { duration : .3 } );
			VAEL.isOver = true;
		});
		$ ( 'logo' ).observe ( 'mouseout', function () {
			VAEL.isOver = false;
		});
		$ ( 'logo-over' ).observe ( 'mouseover', function () {
			VAEL.isOver = true;
		});
		$ ( 'logo-over' ).observe ( 'mouseout', function () {
			VAEL.isOver = false;
			setTimeout ( '$ ( \'logo-over\' ).fade ( { duration : .2 } );', 50 );
		});
		//Cufon.now ();
	},
	init_forms : function ( selector ) {
		if ( VAEL.Forms ) {
			VAEL.Forms.each ( function ( form ) {
				form = null;
			});
			VAEL.Forms.clear ();
		}
		VAEL.Forms = new Array ();
		$$ ( selector ).each ( function ( form_el ) {
			VAEL.Forms [ form_el.id ] = new GnForm ( form_el );
		});
	},
	gtrack : function ( category, action, opt_label, opt_value ) {
		if ( action ) {
			try {
				if ( ! pageTracker._trackEvent ( category, action, opt_label, 1 ) ) {
					if ( VAEL.debug ) {
						alert ( 'Page tracking event failed!\n' + category + '\n' + action + '\n' + opt_label + '\n' + opt_value );
					}
				}
			} catch ( e ) { 'Error tracking event : ' + alert ( e ); }
		} else {
			try {
			var page_uri = window.location.href.split ( '.com' ) [ 1 ].split ( '.aspx' ) [ 0 ];
			page_uri += '/' + category + '.aspx';
			pageTracker._trackPageview ( page_uri );
			} catch ( e ) { 'Error tracking pageview : ' + alert ( e ); }
		}
	}
};

MenuA = {
	init : function () {
		$$ ( '#menu-a a' ).each ( function ( item ) {
			item.onmouseover = function () {
				new Effect.Morph ( this, {
					style : { height : '104px' },
					duration : .15
				});
			};
			item.onmouseout = function () {
				var newHeight = '111px';
				if ( this.up ().hasClassName ( 'selected' ) ) {
					newHeight = '100px';
				}
				new Effect.Morph ( this, {
					style : { height : newHeight },
					duration : .2
				});
			};
		});
	}
};

Logo = {
	click : function () {
		VAEL.gtrack ( 'Interface', 'Logo Click', '' );
	}
}

Event.observe ( window, 'load', VAEL.init, false );

Product = {
	currentview : null,
	currentzoom : null,
	currentvar : 0,
	page : function ( pitem ) {
		var item = $ ( pitem );
		var position = parseInt ( item.readAttribute ( 'position' ) );
		var varid = item.readAttribute ( 'variation-id' );
		$ ( varid + '-variation-slider' ).setStyle ( { left : '0px' } );
		if ( $ ( varid + '-page-prev' ) ) {
			Product.currentvar = 0;
			$ ( varid + '-page-prev' ).setOpacity ( .35 );
			$ ( varid + '-page-next' ).setOpacity ( 1 );
		}
		if ( Product.currentview ) {
			if ( item == Product.currentview ) {
				return;
			}
			positionb = parseInt ( Product.currentview.readAttribute ( 'position' ) );
			position = position - positionb;
			if ( position == 0 ) {
				position = -1;
			}
		}
		Product.slide ( 'product-image-slider', ( 450 * - ( position ) ) );
		try {
			Product.setSizeSelector ( varid );
		} catch ( e ) {
			if ( VAEL.debug ) {
				alert ( 'Error setting size selector : ' + e );
			}	
		}
		ShoppingCart.variation_id = item.readAttribute ( 'variation-id' );
		ShoppingCart.product_color = item.readAttribute ( 'product-title' );
		ShoppingCart.product_thumb = item.readAttribute ( 'product-thumb' );
		$ ( 'product-color' ).update ( ShoppingCart.product_color );
		$ ( 'product-color' ).setOpacity ( 0 );
		Font.Set ( '.product-variation' );
		$ ( 'product-color' ).appear ( { duration : .4 } );
		$ ( 'product-color' ).blindDown ( { duration : .4 } );
		$$ ( '.product-view' ).each ( function ( variation ) {
			variation.removeClassName ( 'selected' );
		});
		Product.currentview = item;
		item.addClassName ( 'selected' );
		VAEL.gtrack ( 'Product Details', 'View Variation', ShoppingCart.product_title + ' - ' + ShoppingCart.product_color );
	},
	pageVariation : function ( varid, direction ) {
		$ ( varid + '-page-prev' ).setOpacity ( 1 );
		$ ( varid + '-page-next' ).setOpacity ( 1 );
		var item_total = $$ ( '#' + varid + '-variation-slider .image-slider-item' ).length - 1;
		var newx;
		if ( Product.currentvar >= 0 && direction == -1 ) { $ ( varid + '-page-prev' ).setOpacity ( .35 ); return; }
		if ( ( Product.currentvar <= - ( 450 * item_total ) ) && direction == 1 ) { $ ( varid + '-page-next' ).setOpacity ( .35 ); return; }
		if ( direction == 1 ) {
			newx = -450;
			Product.currentvar -= 450;
		}
		else {
			newx = 450;
			Product.currentvar += 450;
		}
		if ( Product.currentvar >= 0 ) { $ ( varid + '-page-prev' ).setOpacity ( .35 ); }
		if ( ( ( Product.currentvar ) <= - ( 450 * item_total ) ) ) { $ ( varid + '-page-next' ).setOpacity ( .35 ); }
		Product.slide ( varid + '-variation-slider', newx );
	},
	slide : function ( slider, newx ) {
		new Effect.Move ( slider, {
			x : newx,
			duration : .55,
			mode : 'relative',
			transition : Effect.Transitions.sinoidal
		});
	},
	toggledescription : function ( item ) {
		$ ( 'handler-description' ).removeClassName ( 'selected' );
		$ ( 'handler-technical' ).removeClassName ( 'selected' );
		item.setOpacity ( 0 );
		item.addClassName ( 'selected' );
		$ ( 'product-description' ).hide ();
		$ ( 'product-technical' ).hide ();
		Font.Set ( '.product-description-toggler a' );
		item.appear ( { duration : .35 } );
		$ ( item.readAttribute ( 'tab' ) ).appear ( { duration : .5 } );
		VAEL.gtrack ( 'Product Details', 'Toggle Description', ShoppingCart.product_title + ' - ' + ShoppingCart.product_color );
	},
	setSizeSelector : function ( varid ) {
		return;
		$$ ( '#product-size-selector .variation-sizes' ).each ( function ( variation ) {
			variation.hide ();
		});
		if ( $ ( 'variation-sizes-' + varid ) ) {
			if ( $ ( 'variation-sizes-' + varid ).hasClassName ( 'outofstock' ) ) {
				$ ( 'size-selector' ).hide ();
			}
			else {
				$ ( 'size-selector' ).show ();
				$ ( 'variation-sizes-' + varid ).show ();
			}
		}
		else {
			$ ( 'size-selector' ).show ();
		}
	},
	onImageZoom : function ( modalid ) {
		$ ( 'image-zoom-item' ).hide ();
		$ ( 'image-zoom-item' ).grow ( {
			duration : .75,
			afterFinish : function () {
				Modals.modals [ modalid ].position ();
			}
		});
		var title = ShoppingCart.product_title;
		if ( ShoppingCart.product_color ) {
			title += ' - ' + ShoppingCart.product_color;
		}
		VAEL.gtrack ( 'Product Details', 'Image Zoom', title );
	},
	partner : function ( product, partner ) {
		VAEL.gtrack ( 'Purchase From Partner', product, partner );
	}
};

SizeSelector = {
	hasChosen : false,
	open : function () {
		$ ( 'product-size-selector' ).appear ( { duration : .35 } );
		VAEL.gtrack ( 'Product Details', 'Open Sizes', ShoppingCart.product_title + ' - ' + ShoppingCart.product_color );
	},
	close : function () {
		$ ( 'product-size-selector' ).fade ( { duration : .35 } );
	},
	choose : function ( item ) {
		$$ ( '#product-size-selector .option' ).each ( function ( option ) {
			option.removeClassName ( 'selected' );
		});
		SizeSelector.hasChosen = true;
		$ ( item ).addClassName ( 'selected' );
		ShoppingCart.product_size = $ ( item ).readAttribute ( 'value' );
		ShoppingCart.product_id = $ ( 'product-id' ).innerHTML.stripTags();
		ShoppingCart.product_sku = $ ( 'product-sku' ).innerHTML.stripTags();
		ShoppingCart.addProduct ();
	},
	value : function () {
		$$ ( '#product-size-selector .option' ).each ( function ( option ) {
			if ( option.hasClassName ( 'selected' ) ) {
				return option.readAttribute ( 'value' );
			}
		});
	},
	toggle : function () {
		if ( $ ( 'product-size-selector' ).style.display == 'none' ) {
			SizeSelector.open ();
		} else {
			SizeSelector.close ();
		}
	}
};

ShoppingCart = {
	modal : null,
	forms : null,
	currentblock : null,
	blocks : null,
	block_width : 0,
	xoffset : 0,
	position : 0,
	product_color : null,
	product_size : null,
	product_sku : null,
	product_id : null,
	product_thumb : null,
	init : function () {
		new Effect.ScrollTo ( 'cart-toolbar', { duration : .2, afterFinish : function () { this.open (); }.bind ( this ) } );
	},
	open : function () {
		this.modal = new Modal ( 'modal-shopping-cart', '/shopping-bag.aspx', 701, 520, this.on_init, this.on_show, this.on_destroy );
		this.modal.init ();
		VAEL.gtrack ( 'Shopping Bag', 'Open Shopping Bag', '' );
	},
	close : function () {
		this.modal.destroy ();
		this.modal = null;
		var links = $$ ( '#cart-head .bulleted' );
		var current = null;
		links.each ( function ( item ) {
			if ( item.readAttribute ( 'position' ) == ShoppingCart.position ) {
				current = item;
			}
		});
		if ( current ) {
			VAEL.gtrack ( 'Shopping Bag', 'Close Shopping Bag', current.innerHTML.stripTags () );
		}
	},
	on_init : function () {
		document.body.setStyle ( { overflow : 'hidden' } );
		$ ( 'cart-toolbar' ).removeClassName ( 'closed' );
		$ ( 'cart-toolbar' ).addClassName ( 'open' );
	},
	on_show : function () {
		$ ( 'modal-indicator' ).hide ();
		var links = $$ ( '#cart-head .bulleted' );
		links.each ( function ( item ) {
			item.onclick = function () {
				ShoppingCart.page_block ( this );
			};
		});
		ShoppingCart.currentblock = links [ 0 ];
		var vpDims = document.viewport.getDimensions ();
		ShoppingCart.block_width = vpDims.width - 100;
		ShoppingCart.xoffset = parseInt ( vpDims.width );
		ShoppingCart.blocks = $$ ( '#cart-slider .cart-block' );
		ShoppingCart.blocks.each ( function ( block, i ) {
			block.setStyle ({
				left : ( ( ShoppingCart.xoffset * i ) + 20 ) + 'px',
				width : ShoppingCart.block_width + 'px',
				visibility : 'visible'
			});
		});
		$ ( 'cart-head' ).setStyle ({
			visibility : 'visible'
		});
		ShoppingCart.init_forms ();
	},
	on_destroy : function () {
		$ ( 'cart-toolbar' ).removeClassName ( 'open' );
		$ ( 'cart-toolbar' ).addClassName ( 'closed' );
		document.body.setStyle ( { overflow : 'auto' } );
	},
	page : function ( direction ) {
		if ( direction == 'next' ) {
			ShoppingCart.position ++;
		} else if ( direction == 'back' ) {
			ShoppingCart.position --;
		} else {
			ShoppingCart.position = direction;
		}
		var links = $$ ( '#cart-head .bulleted' );
		var link;
		links.each ( function ( item ) {
			if ( item.readAttribute ( 'position' ) == ShoppingCart.position ) {
				link = item;
			}
		});
		ShoppingCart.page_block ( link );
	},
	page_block : function ( link ) {
		ShoppingCart.position = parseInt ( link.readAttribute ( 'position' ) );
		if ( ShoppingCart.currentblock ) {
			if ( link == ShoppingCart.currentblock ) {
				return;
			}
			positionb = parseInt ( ShoppingCart.currentblock.readAttribute ( 'position' ) );
			ShoppingCart.position = ShoppingCart.position - positionb;
			if ( ShoppingCart.position == 0 ) {
				ShoppingCart.position = -1;
			}
		}
		new Effect.Move ( 'cart-slider', {
			x : ( ShoppingCart.xoffset * - ( ShoppingCart.position ) ),
			duration : .55,
			mode : 'relative',
			transition : Effect.Transitions.sinoidal
		});
		ShoppingCart.currentblock.removeClassName ( 'selected' );
		ShoppingCart.currentblock = link;
		link.addClassName ( 'selected' );
		//Font.Set ( '#cart-head .bulleted' );
		VAEL.gtrack ( 'Shopping Bag', 'Progress Click', link.innerHTML.stripTags () );
	},
	ToggleBillingFields : function () {
		if ( $ ( 'shipping-to-billing' ).checked ) {
			$ ( 'form-fields-billing' ).fade ( { duration : .35 } );
			return;
		}
		$ ( 'form-fields-billing' ).appear ( { duration : .35 } );
	},
	init_forms : function () {
		if ( ShoppingCart.forms ) {
			ShoppingCart.forms.each ( function ( form ) {
				form = null;
			});
			ShoppingCart.forms.clear ();
		}
		ShoppingCart.forms = new Array ();
		$$ ( '#modal-window form' ).each ( function ( form_el ) {
			ShoppingCart.forms [ form_el.id ] = new GnForm ( form_el );
		});
	},
	addProduct : function () {
		if ( $ ( 'modal-indicator' ) ) {
			$ ( 'modal-indicator' ).show ();
		}
		var request = new Ajax.Request ( '/shopping-bag.aspx?action=add-product', {
			synchronous : true,
			parameters : {
				'productid' : ShoppingCart.product_id,
				'variationid' : ShoppingCart.variation_id,
				'sku' : ShoppingCart.product_sku,
				'color' : ShoppingCart.product_color,
				'size' : ShoppingCart.product_size,
				'thumbnail' : ShoppingCart.product_thumb,
				'quantity' : '1'
			},
			onSuccess : ShoppingCart.onAddProduct,
			onFailure : function ( e ) { alert ( e + ' : ' + e.statusText ); $ ( 'modal-indicator' ).hide (); }
		});
		VAEL.gtrack ( 'Shopping Bag', 'Add Product', ShoppingCart.product_title + ' - ' + ShoppingCart.product_color );
	},
	onAddProduct : function ( e ) {
		var jSON = eval ( e.responseText );
		if ( jSON.Status == 'Success' ) {
			ShoppingCart.init ();
		}
	},
	onRemoveProduct : function ( jSON, pos ) {
		var title = $ ( 'producttitle-' + pos ).innerHTML.stripTags ();
		var item = $ ( 'cart-product-' + pos );
		$ ( item ).fade ( { duration : .5 } );
		$ ( item ).slideUp ( { duration : .5 } );
		$ ( 'bag-totalitems' ).update ( jSON.ShoppingBag.TotalItems );
		$ ( 'bag-subtotal' ).update ( jSON.ShoppingBag.SubTotal );
		VAEL.gtrack ( 'Shopping Bag', 'Remove Product', title );
	},
	updateQuantity : function ( position ) {
		var field = $ ( 'quantity-' + position );
		if ( field.value == 0 ) {
			ShoppingCart.forms [ 'form-remove-product-' + position ].submit ();
			return;
		}
		if ( ! parseInt ( field.value ) || field.value == field.readAttribute ( 'origvalue' ) ) {
			field.value = field.readAttribute ( 'origvalue' );
			return;
		}
		field.writeAttribute ( 'origvalue', field.value );
		ShoppingCart.forms [ 'form-update-quantity-' + position ].submit ();
		VAEL.gtrack ( 'Shopping Bag', 'Change Quantity', '' );
	},
	onUpdateQuantity : function ( jSON, position ) {
		if ( jSON.Status == 'Success' ) {
			$ ( 'bag-totalitems' ).update ( jSON.ShoppingBag.TotalItems );
			$ ( 'bag-subtotal' ).update ( jSON.ShoppingBag.SubTotal );
			$ ( 'bag-review-subtotal' ).update ( jSON.ShoppingBag.SubTotal );
			$ ( 'bag-review-shippingtotal' ).update ( jSON.ShoppingBag.ShippingTotal );
			$ ( 'bag-review-taxtotal' ).update ( jSON.ShoppingBag.TaxTotal );
			$ ( 'bag-review-total' ).update ( jSON.ShoppingBag.Total );
			$ ( 'quanitity-' + position ).highlight ();
			$ ( 'quanitity-' + position ).value = jSON.Product.Quantity;
		} else {
			//alert ( jSON.Message );
		}
	},
	onAccountSignup : function ( jSON ) {
		if ( jSON.Status == 'Success' ) {
			ShoppingCart.page ( 2 );
			$ ( 'wrapper-account-modify' ).show ();
			$ ( 'wrapper-account-signup' ).hide ();
			$ ( 'wrapper-account-login' ).hide ();
			//$ ( 'head-link-logout' ).show ();
			$ ( 'modify-firstname' ).value = jSON.User.FirstName;
			$ ( 'modify-lastname' ).value = jSON.User.LastName;
			$ ( 'modify-email' ).value = jSON.User.Email;
			VAEL.gtrack ( 'Shopping Bag', 'Create Account', 'Success' );
			VAEL.gtrack ( ShoppingCart.product_color.toLowerCase () + '/shoppingbag/account/signup' );
		} else {
			$ ( 'form-account-signup-error' ).update ( jSON.Message );
			$ ( 'form-account-signup-error' ).show ();
			VAEL.gtrack ( 'Shopping Bag', 'Create Account', 'Failure' );
		}
	},
	onAccountModify : function ( jSON ) {
		if ( jSON.Status == 'Success' ) {
			$ ( 'modify-password' ).value = '';
			$ ( 'modify-password-b' ).value = '';
			VAEL.gtrack ( 'Shopping Bag', 'Modify Account', 'Success' );
		} else {
			VAEL.gtrack ( 'Shopping Bag', 'Modify Account', 'Failure' );
		}
		$ ( 'form-account-modify-error' ).update ( jSON.Message );
		$ ( 'form-account-modify-error' ).show ();
	},
	onAccountLogin : function ( jSON ) {
		if ( jSON.Status == 'Success' ) {
			ShoppingCart.page ( 2 );
			$ ( 'wrapper-account-modify' ).show ();
			$ ( 'wrapper-account-signup' ).hide ();
			$ ( 'wrapper-account-login' ).hide ();
			//$ ( 'head-link-logout' ).show ();
			$ ( 'modify-firstname' ).value = jSON.User.FirstName;
			$ ( 'modify-lastname' ).value = jSON.User.LastName;
			$ ( 'modify-email' ).value = jSON.User.Email;
			for ( var i = 0, total = $ ( 'shipping-method' ).options.length; i < total; i ++ ) {
				if ( $ ( 'shipping-method' ).options [ i ].value == jSON.User.Shipping.Method ) {
					$ ( 'shipping-method' ).options [ i ].selected = true;
				}
			}
			$ ( 'shipping-address' ).value = jSON.User.Shipping.Address;
			$ ( 'shipping-city' ).value = jSON.User.Shipping.City;
			for ( var i = 0, total = $ ( 'shipping-state' ).options.length; i < total; i ++ ) {
				if ( $ ( 'shipping-state' ).options [ i ].value == jSON.User.Shipping.State ) {
					$ ( 'shipping-state' ).options [ i ].selected = true;
				}
			}
			$ ( 'shipping-zip' ).value = jSON.User.Shipping.Zip;
			$ ( 'shipping-phone' ).value = jSON.User.Shipping.Phone;
			$ ( 'shipping-phone-ext' ).value = jSON.User.Shipping.PhoneExt;
			$ ( 'shipping-fax' ).value = jSON.User.Shipping.Fax;
			if ( jSON.User.Billing.ShippingToBilling == 'True' || jSON.User.Billing.ShippingToBilling == 'true' ) {
				$ ( 'shipping-to-billing' ).checked = true;
				$ ( 'form-fields-billing' ).hide ();
			} else {
				$ ( 'shipping-to-billing' ).checked = false;
				$ ( 'form-fields-billing' ).show ();
			}
			$ ( 'billing-address' ).value = jSON.User.Billing.Address;
			$ ( 'billing-city' ).value = jSON.User.Billing.City;
			for ( var i = 0, total = $ ( 'billing-state' ).options.length; i < total; i ++ ) {
				if ( $ ( 'billing-state' ).options [ i ].value == jSON.User.Billing.State ) {
					$ ( 'billing-state' ).options [ i ].selected = true;
				}
			}
			$ ( 'billing-zip' ).value = jSON.User.Billing.Zip;
			$ ( 'billing-phone' ).value = jSON.User.Billing.Phone;
			$ ( 'billing-phone-ext' ).value = jSON.User.Billing.PhoneExt;
			$ ( 'billing-fax' ).value = jSON.User.Billing.Fax;
			VAEL.gtrack ( 'Shopping Bag', 'Account Login', 'Success' );
		} else {
			$ ( 'form-account-login-error' ).update ( jSON.Message );
			$ ( 'form-account-login-error' ).show ();
			VAEL.gtrack ( 'Shopping Bag', 'Account Login', 'Failure' );
		}
	},
	onAccountLogout : function ( jSON ) {
		if ( jSON.Status == 'Success' ) {
			$ ( 'wrapper-account-modify' ).hide ();
			$ ( 'wrapper-account-signup' ).show ();
			$ ( 'wrapper-account-login' ).show ();
			VAEL.gtrack ( 'Shopping Bag', 'Account Logout', 'Success' );
		}
	},
	onShippingBilling : function ( jSON ) {
		if ( jSON.Status == 'Success' ) {
			ShoppingCart.page ( 3 );
			$ ( 'bag-review-subtotal' ).update ( jSON.ShoppingBag.SubTotal );
			$ ( 'bag-review-shippingtotal' ).update ( jSON.ShoppingBag.ShippingTotal );
			$ ( 'bag-review-taxtotal' ).update ( jSON.ShoppingBag.TaxTotal );
			$ ( 'bag-review-total' ).update ( jSON.ShoppingBag.Total );
			VAEL.gtrack ( 'Shopping Bag', 'Edit Shipping & Billing', 'Success' );
		} else {
			$ ( 'form-shipping-billing-error' ).update ( jSON.Message );
			$ ( 'form-shipping-billing-error' ).show ();
			VAEL.gtrack ( 'Shopping Bag', 'Edit Shipping & Billing', 'Failure' );
		}
	},
	onApplyPromoCode : function ( jSON ) {
		if ( jSON.Status == 'Success' ) {
			var promoel = new Element ( 'div', { 'class' : 'promo-code', 'id' : 'promo-code-' + jSON.PromoCode.Id } );
			var promobody = '<form callback_success="$ ( \'promo-code-' + jSON.PromoCode.Id + '\' ).remove ();" ajax-uri="/shopping-bag.aspx?action=remove-promo-code" notifier="form-promo-code-notifier-' + jSON.PromoCode.Id + '" label="Remove Promo Code" name="form-promo-code-' + jSON.PromoCode.Id + '" id="form-promo-code-' + jSON.PromoCode.Id + '" method="post" enctype="application/x-www-form-urlencoded">';
			promobody += '<input type="hidden" class="form-field" value="' + jSON.PromoCode.PromoId + '" name="promo-code" id="promo-code-remove-' + jSON.PromoCode.Id + '" />';
			promobody += '<img id="form-promo-code-notifier-' + jSON.PromoCode.Id + '" src="/faction/app_themes/VAEL/images/indicator-b.gif" width="16" height="16" style="display:none; vertical-align:middle;" />';
			promobody += jSON.PromoCode.Title + ' : ' + jSON.PromoCode.Discount;
			//promobody += '<a form="form-promo-code-' + jSON.PromoCode.Id + '" class="form-handler" href="javascript:function voi(){};voi()">x Remove</a>';
			promobody += '</form>';
			promoel.update ( promobody );
			$ ( 'promo-code-list' ).insert ( promoel );
			$ ( 'bag-review-subtotal' ).update ( jSON.ShoppingBag.SubTotal );
			$ ( 'bag-review-shippingtotal' ).update ( jSON.ShoppingBag.ShippingTotal );
			$ ( 'bag-review-taxtotal' ).update ( jSON.ShoppingBag.TaxTotal );
			$ ( 'bag-review-total' ).update ( jSON.ShoppingBag.Total );
			$ ( 'promo-code' ).value = '';
			$ ( 'wrapper-promo-code' ).shrink ();
			VAEL.gtrack ( 'Shopping Bag', 'Add Promo Code', jSON.PromoCode.PromoId );
		} else {
			$ ( 'form-promo-code-error' ).show ();
			$ ( 'form-promo-code-error' ).update ( jSON.Message );
			VAEL.gtrack ( 'Shopping Bag', 'Add Promo Code', 'Failure' );
		}
	},
	onPaymentInit : function () {
	},
	url : '',
	onProcessPayment : function ( jSON ) {
		if ( jSON.Status == 'Success' ) {
			$ ( 'form-payment-handler' ).hide ();
			try {
				pageTracker._addTrans ( jSON.Transaction.Id, 'VAEL Project', jSON.ShoppingBag.Total, jSON.ShoppingBag.TaxTotal, jSON.ShoppingBag.ShippingTotal );
				var products = jSON.ShoppingBag.Products;
				var total = products.length;
				for ( var l = 0; l < total; l ++ ) {
					pageTracker._addItem (
						jSON.Transaction.Id,
						products [ l ].SKU,
						products [ l ].Title,
						products [ l ].Color + ' - ' + products [ l ].Size,
						products [ l ].Amt,
						products [ l ].Quantity
					);
				}
				pageTracker._trackTrans ();
			} catch ( e ) { alert ( 'Error with Google Analytics : ' + e ); }
			ShoppingCart.url = jSON.Transaction.TransactionUrl;
			ShoppingCart.thankYou ();
			try {
				pageTracker._setCustomVar (
				  2,
				  'Faction User Profiles',
				  'Purchaser',
				  2
			   ); 
			} catch ( e ) {}
			VAEL.gtrack ( 'Shopping Bag', 'Process Payment', 'Success' );
		} else {
			$ ( 'form-payment-error' ).update ( jSON.Message );
			$ ( 'form-payment-error' ).show ();
			$ ( 'form-payment-handler' ).show ();
			VAEL.gtrack ( 'Shopping Bag', 'Process Payment', 'Failure' );
			try {
				pageTracker._setCustomVar (
				  3,
				  'Faction User Profiles',
				  'Payment Failure',
				  2
			   ); 
		   } catch ( e ) {}
		}
	},
	thankYou : function () {
		var thankyou = 'Wurd. Your payment has been successfully processed.<br /><br />';
		thankyou += 'Be forewarned, you have set a new course for yourself..';
		thankyou += 'You will start to notice subtle transition from a lowly, infantile, washedup up, peasant to one part gentleman, three parts dashing, dastardly rogue.<br /><br />'
		thankyou += 'Enough chatter, '
		thankyou += '<a class="bulleted" href="' + ShoppingCart.url + '">View order details</a>';
		$ ( 'thank-you' ).update ( thankyou );
		$ ( 'thank-you' ).appear ();
		$ ( 'form-payment' ).hide ();
		$ ( 'form-payment-error' ).hide ();
		$ ( 'wrapper-payment-review' ).hide ();
	}
};

FeedLoader = {
	init : function ( rendering_path, display_limit ) {
		var feed_request = new Ajax.Request ( '/tech-feed.aspx?rendering-path=' + rendering_path, {
			synchronous : true,
			parameters : {
				'feedurl' : 'http://feeds.technorati.com/search/vael+project',
				'limit' : display_limit
			},
			onSuccess : function ( e ) {
				$ ( 'tech-feed' ).update ( e.responseText );
				Cufon.replace ( '#tech-feed .hdr-wrap-b .hdr', {
					hover : true
					,fontFamily : 'HelveticaNeue25UltraLight'
					,textShadow : '1px 1px rgba(0, 0, 0, .75)'
				});
				Font.Set ( '.replace-c' );
				Pager.init ();
			}
		});
	}
};

Pager = {
	item_total : 0,
	item_width : 420,
	scroller_x : 0,
	init : function () {
		Pager.item_total = $$ ( '#scroll-content .press-article' ).length;
		$ ( 'tech-page-prev' ).observe ( 'click', Pager.page );
		$ ( 'tech-page-next' ).observe ( 'click', Pager.page );
		$ ( 'tech-page-prev' ).setOpacity ( .15 );
	},
	page : function ( e ) {
		$ ( 'tech-page-prev' ).setOpacity ( 1 );
		$ ( 'tech-page-next' ).setOpacity ( 1 );
		if ( Pager.scroller_x >= 0 && this.hasClassName ( 'previous' ) ) {
			$ ( 'tech-page-prev' ).setOpacity ( .15 );
			return;
		}
		if ( ( Pager.scroller_x <= - ( ( Pager.item_width / 2 ) * ( Pager.item_total - 4 ) ) ) && this.hasClassName ( 'next' ) ) {
			$ ( 'tech-page-next' ).setOpacity ( .15 );
			return;
		}
		var xpos = Pager.item_width;
		if ( this.hasClassName ( 'previous' ) )
		{
			xpos = Pager.item_width;
		}
		else
		{
			xpos = - ( xpos );
		}
		Pager.scroller_x += xpos;
		if ( Pager.scroller_x >= 0 )
		{
			$ ( 'tech-page-prev' ).setOpacity ( .15 );
		}
		if ( ( Pager.scroller_x <= - ( Pager.item_width * ( Pager.item_total - 3 ) ) ) )
		{
			$ ( 'tech-page-next' ).setOpacity ( .15 );
		}
		new Effect.Move ( 'scroll-content',
		{
			x : xpos,
			duration : .5
		});
	}
}

Comment = {
	onPost : function ( jSON ) {
		if ( jSON.Status == 'Success' ) {
			$ ( 'comment-firstname' ).value = '';
			$ ( 'comment-lastname' ).value = '';
			$ ( 'comment-email' ).value = '';
			$ ( 'comment-userurl' ).value = '';
			$ ( 'comment-body' ).value = '';
			var commentposition = $$ ( '#comment-items .comment-item' ).length + 1;
			var comment = new Element ( 'div', { 'class' : 'comment-item', 'id' : 'comment-item-' + commentposition, 'style' : 'display:none;' } );
			var commentbody = '<div class="comment-head">';
			commentbody += 'Post on : ' + jSON.Comment.DateCreated;
			if ( jSON.User.PostAnonymously != 'True' ) {
				commentbody += '<br />Posted by : ' + jSON.User.Name;
				if ( jSON.User.URL != '' ) {
					commentbody += ' // ' + jSON.User.URL;
				}
			}
			commentbody += '<div class="comment-position">';
			commentbody += commentposition;
			commentbody += '</div>';
			commentbody += '</div>';
			commentbody += '<div class="comment-body">';
			commentbody += jSON.Comment.Body;
			commentbody += '</div>';
			comment.update ( commentbody );
			$ ( 'article-comments' ).insert ( comment );
			$ ( 'article-comments' ).show ();
			comment.appear ( { duration : .3 } );
			comment.blindDown ( { duration : .3, afterFinish : function () { new Effect.ScrollTo ( 'comment-item-' + commentposition, { duration : .4 } ); } } );
			VAEL.gtrack ( 'addcomment' );
		} else {
			$ ( 'form-post-comment-error' ).update ( jSON.Message );
			$ ( 'form-post-comment-error' ).show ();
		}
	}
};

User = {
	submitted : false,
	showform : function ( form ) {
		$ ( 'user-profile' ).morph ( { marginTop : '33px' } );
		$ ( 'head-user-password' ).hide ();
		$ ( 'head-user-login' ).hide ();
		$ ( 'head-user-signup' ).hide ();
		$ ( 'head-user-' + form ).blindDown ( { duration : .5 } );
		VAEL.init_forms ( '#user-profile form' );
	},
	onUserForm : function ( jSON ) {
		if ( User.submitted ) {
			return;
		}
		User.submitted = true;
		if ( jSON.Status == 'Success' ) {
			$ ( 'head-profile-username' ).update ( 'Hi ' + jSON.User.FirstName + ' ' + jSON.User.LastName );
			$ ( 'head-user-tagline' ).update ( 'Status : ' + jSON.User.Status );
			$ ( 'user-profile-thumb-img' ).writeAttribute ( 'src', jSON.User.Avatar );
			$ ( 'user-profile' ).morph ( { marginTop : '70px' } );
			$ ( 'head-user-login' ).blindUp ( { duration : .5 } );
			$ ( 'head-anonymous-menu' ).slideUp ( { duration : .5 } );
			$ ( 'head-user-menu' ).blindDown ( { duration : .5, afterFinish : function () { User.submitted = false; } } );
			$ ( 'head-profile-username' ).hide ();
			$ ( 'head-profile-username' ).appear ();
		} else {
			$ ( 'form-head-login-error' ).update ( jSON.Message );
			$ ( 'form-head-login-error' ).show ();
		}
	},
	onLogout : function ( jSON ) {
		if ( jSON.Status == 'Success' ) {
			$ ( 'user-profile' ).hide ();
			$ ( 'head-user-menu' ).hide ();
			$ ( 'head-anonymous-menu' ).show ();
			$ ( 'head-profile-username' ).update ( 'Hi Anonymous...' );
			//$ ( 'head-profile-username' ).update ( 'You got peeps, I got mad email skillz.')
			$ ( 'head-user-tagline' ).update ( 'These so <a href="http://www.youtube.com/watch?v=k1rlThKe1qo" target="_blank">aren\'t the droids you\'re looking for</a>..' );
			$ ( 'user-profile-thumb-img' ).writeAttribute ( 'src', '/faction/app_uploads/b4308cfe-03e6-4ee3-b5ab-3bbec615e515/anonymous.png' );
			$ ( 'user-profile' ).appear ();
		}
	}
}; 
