(function() {
	
	//ecommerce functions for Spring APL November 24, 2010

	if ($('cart')) {
		ajaxcart();
	}

	if ($('product-listing')) {
		ajaxaddtocart();
	}

	function ajaxaddtocart(){
	
		if(Prototype.Browser.IE) {
		  $('product-listing').observe("focusin", function(event) {
		    var form = event.findElement("form");
		    if(form && !form.submit_bubbles_on_ie) {
		      form.submit_bubbles_on_ie = true;
		      form.observe("submit", addtocart);
		    }
		  });
		} else {
		  $('product-listing').observe("submit", addtocart);
		}
	
		$('product-listing').observe('click',clickcart);
		$$('div.order').each(Element.hide);
		notificiationspanel();
	}

	function addtocart(event) {
		var form = event.findElement('form.addtocart');
		if (!form) return;
		form.request({
				parameters: { ajax:'1' },
				onSuccess: function(t){
					//Display a confirmation notice
					var confirm = new Element('span',{'class': 'success'});
					form.select("p:last-child")[0].insert({bottom: confirm});				
					confirm.fade({duration: 3.0,from: 6.0, to: 0});
					notificiationspanel();
				}
			});
		Event.stop(event);
	}

	function clickcart(event) {
		var togglebutton = event.findElement('.toggle');
		if (!togglebutton) return;
		Effect.toggle(togglebutton.id+'-h','blind');
		Event.stop(event);
	}

	function notificiationspanel() {
		if ($('notifications')) {
			var target = $('notifications');
			refreshnotificiationspanel('/feed/source/template/format/system.form.cart.small.xslt/current/cart/display/summary/',target);
		} else {
			//create the noticiations panel and hide it
			var notifications = new Element('div',{id: 'notifications'});
			notifications.hide();
			$('canvas').insert(notifications);
		}
	}

	function refreshnotificiationspanel(source,target) {
		new Ajax.Request(source, {
			method:'get',
			onSuccess: function(response) {
				target.replace(response.responseText);
				// reveal the cart if not shown already
				if (target.visible()==false) {
					Effect.SlideDown(target, {duration: 0.3});
				}
			}
		});
	}


	//cart specific functions

	function ajaxcart(){
		$('cart').observe('click',changecart);
		$('cart').observe('keyup',changequantity);
		//lets hide the quantity update link
		$$('.quantity a').each(function(element){
			element.setStyle({visibility: 'hidden'});
		});
	}

	function changecart(event) {
		var remove = event.findElement('.remove a');
		if (!remove) return;
		writeremove(remove);
		Event.stop(event);
	}

	function changequantity(event) {
		var quantity = event.findElement('.quantity');
		if (!quantity) return;
		writequantity(quantity);
		Event.stop(event);
	}

	function writequantity(quantity) {
		var qty = quantity.down('input').value;
		var url = quantity.down('a').rel;
		// lets change the qty
		new Ajax.Request(url+qty, {
			method:'get',
			onCreate: function() {
				//lets fade the row so they know something is happening
				var updating = new Element('span',{'class': 'updating'}).update("Updating");
				quantity.down('p').insert(updating);
				//set the class for the row
				var row = quantity.up('tr');
				row.addClassName("changing");
				//lets fade the contents of the cells
				// row.childElements().each(function(cell){
				// 	cell.down().fade({duration: 1.0,from: 1.0, to: 0.5});
				// })				
				$$('.changing p').each(function(fader){
					fader.fade({duration: 1.0,from: 1.0, to: 0.5});
				})
			},
			onSuccess: function() {
				//refresh the cart display
				var node = $('springform');
				refreshcartdisplay('/feed/source/template/format/system.form.cart.xslt/current/cart/',node);
			}
		});
	}

	function writeremove(remove) {
		//lets delete the row
		new Ajax.Request(remove.href, {
			method:'get',
			onCreate: function() {
				//lets fade the row so they know something is happening
				remove.up('tr').addClassName("removing");
				$$('.removing p').each(function(fader){
					fader.fade({duration: 1.0,from: 1.0, to: 0.2});
				})
			},
			onSuccess: function() {
				//refresh the cart display
				var node = $('springform');
				refreshcartdisplay('/feed/source/template/format/system.form.cart.xslt/current/cart/',node);
			}
		});
	}

	function refreshcartdisplay(source,target) {
		new Ajax.Request(source, {
			method:'get',
			onSuccess: function(response) {
				target.update(response.responseText);
				//make sure legacy form buttons still work
				$('formsubmit').enable();
				//reinitilize the ajax functions
				ajaxcart();
			}
		});
	}

})();
