// JQuery JavaScript Document
//google.setOnLoadCallback(function() {  
	$(function(){
		$('.addToBasketClientside').attr('href', '/rpc/addToBasket.aspx');
		$('.addToBasketPurchaseHistory').attr('href', '/rpc/addToBasket.aspx');
		$('.threefortwo a').attr('href', '/rpc/addToBasket.aspx');
		$('.threefortwohome a').attr('href', '/rpc/addToBasket.aspx');
		$('.omega3balmoffer a').attr('href', '/rpc/addToBasket.aspx');
		
		// ---- UI Events ----														
		$(".addToBasketClientside").click(
			function(objEvent) {
				
				// ---- Ajax Event listeners ----
				//$("#loading").ajaxStart(function(){$("#loading").html('<img src="/img/ajax-loader.gif" />')});
				//$("#loading").ajaxStop(function(){$("#loading").html('')});				
				//$("#loading").ajaxComplete(function(){$("#loading").html('')});					
				//$("#debug").ajaxSuccess("Event listener - Ajax succeeded.  <br />");									
				//$("#debug").ajaxError(function(event, XMLHttpRequest, settings){this.text( "Event listener - Ajax error. " + XMLHttpRequest.status + " " + settings.url + "<br />");});//http://docs.jquery.com/Ajax/ajaxError#callback

				objEvent.preventDefault();		
				var productID = $('#productID').val() || $(this).parent().attr("id");
				
				$.ajax(
					{
						url: $(this).attr('href'),
						type: 'post',
						datatype: "html",
						data:{'ProductID': productID},
						success: function(sReturnHtml) //sReturnHtml represent the return html from the rpc
						{
							$('#ModalBasket .inner').html(sReturnHtml);
						}
					}
				); //End ajax

				$('#ModalBasket').modal({onOpen: modalOpen, onClose: modalClose, width: 800, position: ["160px"]});
				closeModalWithOverlay();
			}
		); //End Click
		
		$(".threefortwo a").click(
			function(objEvent) {
				objEvent.preventDefault();				

				$.ajax(
					{
						url: $(this).attr('href'),
						type: 'post',
						datatype: "html",
						data:{'ProductID': iProdID, 'qty': 3},												
						success: function(sReturnHtml) //sReturnHtml represent the return html from the rpc
						{									
							$('#ModalBasket .inner').html(sReturnHtml);
						}	
					}				
				); //End ajax	
			
				$('#ModalBasket').modal({onOpen: modalOpen, onClose: modalClose, width: 500, position: ["160px"]});
				closeModalWithOverlay();
			}
		); //End Click
		
		$(".threefortwohome a").click(
			function(objEvent) {
				objEvent.preventDefault();
				var productID = $(this).parent().attr("prodId");

				$.ajax(
					{
						url: $(this).attr('href'),
						type: 'post',
						datatype: "html",
						data:{'ProductID': productID, 'qty': 3},												
						success: function(sReturnHtml) //sReturnHtml represent the return html from the rpc
						{									
							$('#ModalBasket .inner').html(sReturnHtml);
						}	
					}				
				); //End ajax	
			
				$('#ModalBasket').modal({onOpen: modalOpen, onClose: modalClose, width: 500, position: ["160px"]});
				closeModalWithOverlay();
			}
		); //End Click
		
		$(".omega3balmoffer a").click(
			function(objEvent) {
				objEvent.preventDefault();
				
				// adds 3 omega 3 products and 1 balm product to basket

				$.ajax(
					{
						url: $(this).attr('href'),
						type: 'post',
						datatype: "html",
						data:{'ProductID': iProdID, 'qty': 3},												
						success: function(sReturnHtml) //sReturnHtml represent the return html from the rpc
						{									
							//$('#ModalBasket .inner').html(sReturnHtml);
						}	
					}				
				); //End ajax
				
				$.ajax(
					{
						url: $(this).attr('href'),
						type: 'post',
						datatype: "html",
						data:{'ProductID': iBalmProdID, 'qty': 1},												
						success: function(sReturnHtml) //sReturnHtml represent the return html from the rpc
						{
							$('#ModalBasket .inner').html(sReturnHtml);
						}	
					}				
				); //End ajax
			
				$('#ModalBasket').modal({onOpen: modalOpen, onClose: modalClose, width: 500, position: ["160px"]});
				closeModalWithOverlay();
			}
		); //End Click
		
		$(".addToBasketPurchaseHistory").click(
			function(objEvent) {
				objEvent.preventDefault();	
				
				//The id of each product is entered into a span in the parent containing element ('.productDetails span').
				//Due to the fact that there may be many instances of the span and containing element. parent is used to 
				//select the parent element of the button that was clicked so that the correct id is extracted from the 
				//correct element. var $productDetailsElement = $(this).parent().parent().parent().parent().parent(); 
				var $productDetailsElement = $(this).parent().parent().parent().parent().parent(); 
				var productID = $productDetailsElement.find('span').html();
				
				//alert(productID);
				//return;
				
				$.ajax(
					{
						url: $(this).attr('href'),
						type: 'post',
						datatype: "html",
						data:{'ProductID': productID},												
						success: function(sReturnHtml) //sReturnHtml represent the return html from the rpc
						{									
							$('#ModalBasket .inner').html(sReturnHtml);
						}	
					}				
				); //End ajax	
			
				$('#ModalBasket').modal({onOpen: modalOpen, onClose: modalClose, width: 500, position: ["160px"]});
				closeModalWithOverlay();
			}
		); //End Click
		
		function modalOpen(dialog) {
			dialog.overlay.fadeIn('fast', function () {
				dialog.container.slideDown('fast', function () {
					dialog.data.show().fadeIn('fast', function(){
					});	 
				});
			});
		}
		function modalClose(dialog) { 
			dialog.data.fadeOut('fast', function() {
				dialog.container.fadeOut('fast', function () {
					dialog.overlay.fadeOut('fast', function () {
						$.modal.close();
					});
				});						
			});
		}	
		function displayDebugLine(sDebugLine) {
			$("#debug").append(sDebugLine + "<br />")
		}
		function renderBasket(sJson) {
				
		}
		function closeModalWithOverlay() {
			$("#simplemodal-overlay").bind("click", 
				function(objEvent) {
					$.modal.close();
				}							
			);  	
		}
		
	});	// End Document Load
//}); // End Google JQuery API Load
