/*

	@Class: ProductDetailInterface 
	
	@Brief: The Product Detail Interface is used to change out any type of 
	        dynamic material in the Detail section. This includes images 
		   and any select bars. 
			
	@Notes: This class requires MooTools!
	
*/

var ProductDetailInterface = new Class({
	options: {
		AddCartButton:    Class.empty,  // Add To Cart Button
		AddCartForm:      Class.empty,  // Cart Form
		AddWishButton:    Class.empty,  // Add To Wishlist
		AddWishForm:      Class.empty,
		ThumbClass:       Class.empty,
		CardContainer:    Class.empty,
		SizeContainer:    Class.empty,
		ImageContainer:   Class.empty,
		ZoomContainer:    Class.empty,
		BoxContainer:     Class.empty
	}, 
	
	initialize: function(options) {
		// Start-Up Code	
		this.setOptions(options);
				
		// Find Instances
		this.AddCart   = $(this.options.AddCartButton);
		this.CartForm  = $(this.options.AddCartForm);
		this.AddWish   = $(this.options.AddWishButton);
		this.WishForm  = $(this.options.AddWishForm);
		this.Thumbs    = $$(this.options.ThumbClass);
		this.IsActive  = null;
		this.GiftPrice = $(this.options.CardContainer); 
		
		if (this.RingSize) {
			this.RingSize  = $(this.options.SizeContainer);
		}
		this.DetailImage = $(this.options.ImageContainer);
		this.Zoom        = $(this.options.ZoomContainer);
		this.Drag        = $(this.options.BoxContainer);
		
		// Setup Zoom
		if( !window.ie6 )
			this.ImageZoom = new ZoomImage();
		
		if( this.AddCart )
			this.AddCart.addEvent( 'click', this.AddCartItem.bindWithEvent(this) );	
		if( this.AddWish )
			this.AddWish.addEvent( 'click', this.AddWishItem.bindWithEvent(this) );
		if( this.GiftPrice ) 
		{
			this.GiftPrice.addEvent( 'change', this.ChangeCardPrice.bindWithEvent(this) );
			this.GiftPrice.selectedIndex = 0;
		}
		if( this.RingSize )
		{
			this.RingSize.addEvent( 'change', this.ChangeRingSize.bindWithEvent(this) );
			this.RingSize.selectedIndex = 0;
			$('WishForm').ringSize.value = this.RingSize.options[this.RingSize.selectedIndex].value;
		}
				
		if( this.Thumbs.length != 0 )
		{
			for( var i = 0; i < this.Thumbs.length; i++ )
			{
				if( this.Thumbs[i].className.split(' ')[0] == 'active' || this.Thumbs[i].className.split(' ')[1] == 'active' )
					this.IsActive = this.Thumbs[i];
					
				this.Thumbs[i].addEvent( 'click', this.ThumbnailPress.bindWithEvent(this, [ this.Thumbs[i] ]) );	
			}
		}
	},
	
	LoadContent: function(sAttributes, oContainer, postFunction) {
		if( postFunction != null )
			new Ajax( this.options.Wrapper + sAttributes, { method: 'get', update: oContainer, onComplete: postFunction}).request();
		else
			new Ajax( this.options.Wrapper + sAttributes, { method: 'get', update: oContainer}).request();
	},
	
	AddCartItem: function( event ) {
		if( this.GiftPrice )
		{
			if( !(/(^-?\d\d*\.\d*$)|(^-?\d\d*$)|(^-?\.\d\d*$)/.test( this.CartForm.Quantity.value )) )
				alert( 'Quantity\'s must be numeric values.' );
			else if( this.CartForm.Quantity.value == 0 )
				alert( 'You must enter a quantity greater than 0 to add this item to your bag.' );
			else if( this.GiftPrice.selectedIndex == 0 )
				alert( 'Please choose a gift card amount.' );
			else
				this.CartForm.submit();
			
			event.stop();
		}
		else
		{
			if( !(/(^-?\d\d*\.\d*$)|(^-?\d\d*$)|(^-?\.\d\d*$)/.test( this.CartForm.Quantity.value )) )
				alert( 'Quantity\'s must be numeric values.' );
			else if( this.CartForm.Quantity.value == 0 )
				alert( 'You must enter a quantity greater than 0 to add this item to your bag.' );
			else
				this.CartForm.submit();
			
			event.stop();
		}
	},
	
	AddWishItem: function( event ) {
		if( this.GiftPrice )
		{
			if( this.GiftPrice.selectedIndex == 0 )
				alert( 'Please choose a gift card amount.' )
			else
				this.WishForm.submit();
			
			event.stop();	
		}
		else
		{
			this.WishForm.submit();
			event.stop();
		}
	},
	
	ThumbnailPress: function( event, Thumb ) {
		
		if( this.IsActive != Thumb )
		{
			this.IsActive.className = 'thumb';
			Thumb.className = 'active thumb';
			
			var CurID           = this.IsActive.id.split('_')[1];
			var SelID           = Thumb.id.split('_')[1];
			
			$('large_image').className = 'zoomHide';
			$('zoomImg').className     = 'zoomHide';
			$('dragImg').className     = 'zoomHide';
			
			$('large_image').id = 'large_image_' + CurID;
			$('zoomImg').id     = 'zoomImg_' + CurID;
			$('dragImg').id     = 'dragImg_' + CurID;
			
			$('large_image_' + SelID).className = '';
			$('zoomImg_' + SelID).className     = '';
			$('dragImg_' + SelID).className     = '';
			
			$('large_image_' + SelID).id = 'large_image';
			$('zoomImg_' + SelID).id     = 'zoomImg';
			$('dragImg_' + SelID).id     = 'dragImg';
											
			this.IsActive = Thumb;
		}
		
		event.stop();
	},
	
	ChangeCardPrice: function( event ) {
		$('WishlistPrice').value = this.GiftPrice.options[this.GiftPrice.selectedIndex].value;
	},
	
	ChangeRingSize: function( event ) {
		if (this.RingSize) {
			$('WishForm').ringSize.value = this.RingSize.options[this.RingSize.selectedIndex].value;
		}
	},
	
	StripSlash: function( sMessage ) {
		return sMessage.replace( /\//g, "slash_" );	
	},
	
	Echo: function( sMessage ) {
		alert( sMessage ); 
	}

});

ProductDetailInterface.implement(new Options);

window.addEvent('load', function() {
	var DetailInterface = new ProductDetailInterface({
		AddCartButton:  'AddToBag',
          AddCartForm:    'CartForm',
		AddWishButton:  'AddToList',
		AddWishForm:    'WishForm',
		ThumbClass:     '.thumb',
		CardContainer:  'GiftPrice',
		SizeContainer:  'ringSize',
		ImageContainer: 'large_image_bg',
		ZoomContainer:  'zoomLayer',
		BoxContainer:   'dragBox'
	}); 							 
 });
