var ajaxImage = "/media/11902/ajax-loader.gif";

// jQuery page load function
$(document).ready(function() {

	if( $('#boothSubmit').length > 0 )
	{
		$('#boothSubmit').click(function(){
			$.get("/view-booths/SearchForBooth.aspx", { b: $('#boothNumber').val() },
   				function(data){
     					$('.boothSelect').html(data);
			});
			return false;
		});
	}

	$("#boothNumber").keydown(function(e) {
		var code = (e.keyCode ? e.keyCode : e.which);
		if(code == 13) {
			$.get("/view-booths/SearchForBooth.aspx", { b: $('#boothNumber').val() },
   				function(data){
     					$('.boothSelect').html(data);
			});
			e.cancelBubble = true;
			return false;
		}
	}); 

	if( $('#vendorInformation').length > 0 )
	{
		$('area').click(function(){
			$.get("/getboothinfo", { booth: $(this).attr('title') },
   				function(data){
     					$('#vendorInformation').html(data);

						if( $('.addBoothToCart').length > 0 )
						{
							$('.addBoothToCart').click(function(){
								var thisEle = $(this);
								var thisId = thisEle.attr('id');
								thisEle.replaceWith("<img id='" + thisId + "' class='productAjaxLoaderImg' src='" + ajaxImage + "' />");
								$('.sidebarCart').after("<img id='sideBarLoaderImg' src='" + ajaxImage + "' />");
								$.get("/addtocart", { p: thisId, q: 1 },
   									function(data){
     										var totalItems = parseInt(data);
										if( totalItems == 1 )
										{
											$('.sidebarCart').html(totalItems + ' item');
										}
										if( totalItems > 1 )
										{
											$('.sidebarCart').html(totalItems + ' items');
										}
										$('#sideBarLoaderImg').replaceWith("");
										var productAjax = $('.productAjaxLoaderImg');
										var productAjaxId = productAjax.attr('id');
										productAjax.replaceWith("<span id='" + productAjax.attr('id') + "'>Added to Cart</span>");
										$("#" + productAjaxId).fadeOut("slow");									
   									});
								return false;
							});
						}

   				});
			return false;
		});
	}


	if( $('.addAccessoryToCart').length > 0 )
	{
		$('.addAccessoryToCart').click(function(){
			var thisEle = $(this);
			thisEle.after("<img class='productAjaxLoaderImg' src='" + ajaxImage + "' />");
			$('.sidebarCart').after("<img id='sideBarLoaderImg' src='" + ajaxImage + "' />");
			$.get("/addtocart", { p: thisEle.attr('id'), q: thisEle.siblings('.quantity').val() },
   				function(data) {
     					var totalItems = parseInt(data);
					if( totalItems == 1 )
					{
						$('.sidebarCart').html(totalItems + ' item');
					}
					if( totalItems > 1 )
					{
						$('.sidebarCart').html(totalItems + ' items');
					}

					if( $('.addAccessoryToCart').hasClass('gotoCart') )
					{
						window.location = '/cart.aspx';
					}
					$('#sideBarLoaderImg').replaceWith("");
					var productAjax = $('.productAjaxLoaderImg');
					productAjax.replaceWith("<span class='productaddedtocart'>Added to Cart</span>");
					$(".productaddedtocart").fadeOut("slow");									
   				});
			return false;
		});
	}


 	if( $('#vendorList').length > 0 )
	{
   		$('#vendorList').listnav({
			showCounts: false
   		});
	}
});
