// Create Global Namespace for our properties and methods.
var LOST = window.LOST || {};

LOST.renderCart = function(update_only){
	if (!update_only) LOST.showCart();
	new Ajax.Updater('cart-payload', LOST.cartUrl, {
		evalScripts: true,
onException: function(requester, ex) {
	top.location.href=LOST.cartUrl;
},
		onComplete: function(response){
			// $$('ul.cart-items div.options a').each(function(el) {
			// 	Event.observe($(el), 'click', function(e){
			// 		Event.stop(e);
			// 		LOST.hideDealers();
			// 		LOST.updateCart();
			// 	});
			// });
			Event.observe($('continue-shopping-bttn'), 'click', function(e){
				Event.stop(e);
				LOST.hideCart();
			});
			// Event.observe($('continue-bttn'), 'click', function(e){
			// 	Event.stop(e);
			// 	LOST.hideCart();
			// });
		}
	});
};

LOST.toggleCart = function(){
	if ($('shopping-cart')) {
		if ($('shopping-cart-window').getStyle('display')=='none') {
			LOST.hideDealers();
			LOST.renderCart();
		}
		else {
			LOST.hideCart();
		}
	}
};

LOST.showCart = function(){
	LOST.hideDealers();

	$('shopping-cart').show();
	Effect.Appear('shopping-cart-window');
	Effect.BlindDown('shopping-cart-window');
};

LOST.hideCart = function(){
	if ($('shopping-cart')) {
		if ($('shopping-cart-window').getStyle('display')=='block') {
			Effect.Fade('shopping-cart-window');
			Effect.BlindUp('shopping-cart-window', {
				afterFinish: function() {
					$('shopping-cart').hide();
				}
			});
		}
	}
};

LOST.updateCart = function(){
	var cart = $('cart-form');
	new Ajax.Request(cart.action, {
		parameters: cart.serialize(),
		method: cart.method,
		onComplete: function(response){
			// console.log(response.status);
			LOST.hideDealers();
			LOST.renderCart(true);
		}
	});
};

Event.observe(document, 'dom:loaded', function(){
	[$('view-cart'), $('cart-count')].each(function(el) {
		Event.observe($(el), 'click', function(e){
			Event.stop(e);
			LOST.toggleCart();
		});
	});
});





LOST.renderDealers = function(update_only){
	if (!update_only) LOST.showDealers();
	new Ajax.Updater('dealers-payload', '/modules/dealers.php', {
		parameters: {'skip_scripts': '1'},
		evalScripts: true,
		onComplete: function(){
			Event.observe($('dealers-form'), 'submit', function(e){
				Event.stop(e);
				var options = $('dealers-form').serialize();
				options.skip_scripts = 1;
				new Ajax.Updater('dealers-payload', $('dealers-form').action, {
					method: 'get',
					parameters: options,
					evalScripts: true,
					onComplete: function(){
						LOST.attachDealersCancel();
					}
				});
			});
			LOST.attachDealersCancel();
		}
	});
};

LOST.attachDealersCancel = function(){
	$('dealers-act').insert({'after': '<input type="button" id="dealers-cancel-bttn" value="CANCEL" class="bttn" style="margin-left:10px;" />'});
	Event.observe($('dealers-cancel-bttn'), 'click', function(e){
		Event.stop(e);
		LOST.hideDealers();
	});
};

LOST.toggleDealers = function(){
	if ($('dealers-screen')) {
		if ($('shopping-cart-window-dealers').getStyle('display')=='none') {
			LOST.hideCart();
			LOST.renderDealers();
		}
		else {
			LOST.hideDealers();
		}
	}
};

LOST.showDealers = function(){
	LOST.hideCart();
	
	$('dealers-screen').show();
	Effect.Appear('shopping-cart-window-dealers');
	Effect.BlindDown('shopping-cart-window-dealers');
};

LOST.hideDealers = function(){
	if ($('dealers-screen')) {
		if ($('shopping-cart-window-dealers').getStyle('display')=='block') {
			Effect.Fade('shopping-cart-window-dealers');
			Effect.BlindUp('shopping-cart-window-dealers', {
				afterFinish: function() {
					$('dealers-screen').hide();
				}
			});
		}
	}
};

// Event.observe(document, 'dom:loaded', function(){
// 	[$('dealers')].each(function(el) {
// 		Event.observe($(el), 'click', function(e){
// 			Event.stop(e);
// 			LOST.toggleDealers();
// 		});
// 	});
// });
