var Spring = {
	shopping_options_toggle : function() {
		var show_button = $$('.shopping-controls .show-button')[0];
		var hide_button = $$('.shopping-controls .hide-button')[0];
		var option_list = $$('.shopping-options .option_lists')[0];
		option_list.toggle();
		hide_button.toggle();
		show_button.toggle();
	},
	
	show_more : function(clicked) {
		var info = $$('.information')[0];
		var more = $$('.more_information')[0];
		if (!more || !info) {
			return;
		}
		more.toggle();
		info.toggle();
		if (more.visible()) {
			clicked.innerHTML = "&#171; read less";
		} else {
			clicked.innerHTML = "read more &#187;";
		}
	},
	
	show_gift_options : function(clicked) {
        var options = $('gift_wrap_options');
        var text = $('gift_wrap_text');
        options.toggle();
        if (options.style.display != 'none') {
            text.focus();
        }
	},
	
	calc_product_options : function(clicked) {
	    var options         = $$('.dropDownBox_Height2');
        var priceValue      = $('priceValue');
        var selectedOptions = [];
        var optionsString   = '';
        var productId       = $('hiddenProductId').value;
        var productLink     = $('hiddenProductLink').value;
        var selectQty       = $('selectQty');
        var optionsCount     = 0;
        
        for (var i in options) {
            if (!options[i].value == '') {
                optionsString += '&options[]=' + options[i].value;
                optionsCount++;
            }
        };
        
        new Ajax.Request('/cart', {
            method: 'post',
            parameters: "&action=calcProductPrice&productId=" + productId + "&productLinkId=" + productLink + optionsString,            
            onSuccess: function(transport) {
                if (optionsCount == options.size()) {
                    response = transport.responseText.toQueryParams();
                    // Handle the price first
                    selectQty.disabled = false;
                    $('priceRange').hide();
                    $('priceValue').show();
                    priceValue.innerHTML = response.price;
                    // Handle the quantity
                    selectQty.innerHTML = null;
                    if (response.qty <= 0) {
                        selectQty.hide();
                        $('bottomButtons').hide();
                        $('qtyError').show();
                    } else {
                        $('qtyError').hide();
                        $('bottomButtons').show();
                        selectQty.show();
                    }
                    for (var i=1; i <= response.qty; i++) {
                        var anOption = document.createElement("option");
                        anOption.innerHTML = i;
                        anOption.value = i;
                        selectQty.appendChild(anOption);
                        if (i == 10) { break; }
                    };   
                } else {
                    $('priceValue').hide();
                    $('priceRange').show();
                    selectQty.disable();
                    $('bottomButtons').hide();
                }
            }
        });
	},
	
    instruction_fields : {
        fields : null,
		focusedField : null,
        _mouseup : null,
        _keyup : null,
        
        create : function() {
            fields = $$('.instruction-field');
            this._mouseup = this.mouseup.bindAsEventListener(this);
            this._keyup = this.keyup.bindAsEventListener(this);
            Event.observe(document, 'mouseup', this._mouseup);
            for (var i=0, c=fields.length; i<c; i++) {
                var field = fields[i];
                field.instruction = field.value;
                Event.observe(field, 'mouseup', this._mouseup);
				Event.observe(field, 'keyup', this._keyup);
            }
        },
        
        mouseup : function(event) {
			var elem = Event.findElement(event, 'input');
			if (!elem || elem.nodeName.toLowerCase() == 'document') {
				if (this.focusedField && !this.focusedField.value.length) {
					this.focusedField.value = this.focusedField.instruction;	
				}
				return;
			}
			if (elem.value == elem.instruction) {
				this.focusedField = elem;
				elem.value = '';
			}
        },
        
        keyup : function(event) {
			return;
			var elem = Event.findElement(event, 'input');
			if (elem.value.length == 0) {
				elem.value = elem.instruction;
				elem.select();
			}
        }
    },
	
	cart_dropdown : {
		cart : null,
		tweening : null,
		tweener : null,
		shown : false,
		offset : null,
		
		target_y : null,
		progress_y : null,
		start_y : null,
		
		initialize : function() {
			this.cart = $$('.cart-dropdown')[0];
            // this.offset = this.cart.getHeight();
            // this.cart.setStyle({
            //  top: - this.offset + 'px'              
            // });
            // this.cart.show();
		},
		
		// toggle : function() {
		//            if (this.tweening) {
		//                return; 
		//            }
		//            if (this.shown) {
		//                this.hide();
		//            } else {
		//                this.show();
		//            }
		//        },
		//        
		//        show : function() {
		//            if (this.shown == false) {
		//                this.target_y = this.cart.getHeight();
		//                this.start_y = parseInt(this.cart.getStyle('top'));
		//                this.progress_y = 0;
		//                this.shown = true;
		//                this.tweening = true;
		//                this.tweener = new PeriodicalExecuter(this.tween.bind(this), .03);
		//            }
		//        },
		//        
		//        hide : function() {
		//            if (this.shown == true) {
		//                this.target_y = this.cart.getHeight();
		//                this.start_y = parseInt(this.cart.getStyle('top'));
		//                this.progress_y = 0;
		//                this.shown = false;
		//                this.tweening = true;
		//                this.tweener = new PeriodicalExecuter(this.tween.bind(this), .03);
		//            }
		//        },
		
		tween : function() {
			if (this.shown) {
				this.progress_y += Math.ceil((this.offset - this.progress_y)/12);
			} else {
				this.progress_y -= Math.ceil((this.offset + this.progress_y)/12);
			}
			if (Math.abs(this.progress_y) == this.target_y) {
				this.tweening = false;
				this.tweener.stop();
			}
			this.cart.setStyle({
				top: this.start_y + this.progress_y + 'px'
			});
		},
		
		update_list : function() {

            new Ajax.Request('/tr_cart/tr_plugins/cartdropdown.php', {
                method: 'get',
                onSuccess: function(transport) {
                    $('cart-dropdown-product-list').innerHTML = transport.responseText;
                }
            });
		    
		},
		
		update_subtotal : function() {

            new Ajax.Request('/tr_cart/tr_plugins/subtotal.php', {
                method: 'get',
                onSuccess: function(transport) {
                    $('subtotal-price').innerHTML = transport.responseText;
                }
            });
		    
		},
		
		add_to_cart : function() {
            $('adding').show();
            new Ajax.Request('/cart', {
                method: 'post',
                parameters: $('add').serialize(),
                onSuccess: function(transport) {
                    self.scrollTo(0,0);
                    Spring.cart_dropdown.update_list();
                    Spring.cart_dropdown.update_subtotal();
                    if (!Spring.cart_dropdown.cart.visible()) {
                        Effect.toggle('cart-dropdown','slide', {duration: .8});
                    }

                    $('cartQuantity').innerHTML = transport.responseText;
                    $('adding').hide();
                    $('out-of-stock').hide();
                    
                },
                onFailure: function(transport) {
                    if (transport.responseText == 'out of stock') {
                        $('out-of-stock').show();
                    }
                    $('adding').hide();
                }
         });
		}
	}
}