/*
JavaScript Document for NewquaySurfCentre.com
Coded by Chris Stanyon
*/

window.addEvent('domready', function() {
	DropDowns();
});


function DropDowns() {
	$each($$('select.select_dropdown'), function(MenuNav){
		$(MenuNav.id).addEvent('change', function(e){
			window.location.href=("/" + MenuNav.id + "/" + MenuNav.value);
		});
	});
}


function SurfReport() {
		$('SurfReport').addEvent('submit', function(e) {
		//Prevents the default submit event from loading a new page.
		e.stop();
		//Empty the log and show the spinning indicator.
		var log = $('SurfReportResponse').empty().addClass('ajax-loading');
		//Set the options of the form's Request handler. 
		//("this" refers to the $('myForm') element).
		this.set('send', {onComplete: function(response) { 
			log.removeClass('ajax-loading');
			log.set('html', response);
		}});
		//Send the form.
		this.send();
		});
}


function AddToBasket() {

	var AjaxFlag = new Element('input', {
		'type': 'hidden',
		'name': 'ajax',
		'value': '1'
	});

	AjaxFlag.inject($('productForm'));

	$('productForm').addEvent('submit', function(e) {

		//e = new Event(e).stop(); //use this to prevent default click action like on a link if needed
		e.stop();

		var log = $('ajaxResponse').empty();
		log.set('html', "<img src='/gfx/ajax-loader.gif' alt='Please wait...' />");
				
		this.set('send', {onComplete: function(response) { 
			var obj = JSON.decode(response);
			log.set('html', obj.message);
			$('BasketSummary').set('html', obj.basketString);
		}});

		this.send();
	});

}

function RemoveFromBasket() {
	
	var basket = $('basket');

	$$('a.deleteButton').each(function(el) {

		el.addEvent('click',function(e) {
			e.stop();
			var parent = el.getParent('div');

			var request = new Request({
				url: el.get('href'),
				link: 'chain',
				method: 'post',
				data: { ajax: 1	},

				onRequest: function() {
					new Fx.Tween(parent,{
						duration:300
					}).start('background-color', '#fb6c6c');
				},
				onSuccess: function(response) {
					new Fx.Slide(parent,{
						duration:300,
						onComplete: function() {
							parent.dispose();
							
							var obj = JSON.decode(response);

								$('BasketSummary').set('html', obj.SidebarBasket);
								
								if(obj.Content == "TRUE")
								{
									//we now need to set the subtotal, delivery and total
									$('BasketSubtotal').set('html', obj.SubTotal);
									$('BasketDelivery').set('html', obj.DeliveryValue);
									$('BasketTotal').set('html', obj.OrderTotal);
								}
								else
								{
									basket.set('html', obj.Message)
								};
						}
					}).slideOut();
				}
			}).send();
		});
	});
	
	
	
	
}