$(document).ready(function()
{

	/* Products Menu Button Onclick */
	$("#Category").click(function(event) {
		event.preventDefault();
		$("#category_menu").slideDown(); /* Show main menu */
		$('.submenu').hide(); /* Hide sub menus */
		$('#category-modal').hide(); /* Hide modal */	
		$("#view_cart_pop").fadeOut('fast'); 
		$("#auth_pop").fadeOut('fast'); 		
	});

	/* Products Menu Button Mouseover */
	$("#Category").mouseover(function(event) {
		event.preventDefault();
		$("#category_menu").slideDown();
		$("#btn_category").addClass('btn_category_hover');
		$("#view_cart_pop").fadeOut('fast');
		$("#auth_pop").fadeOut('fast'); 		
	});

	/* Menu Button Mouseovers (Excluding Products Button) */
	$('#nav_holder').children('div').each(function(index) {
		if(!$(this).hasClass('btn_category') && $(this).attr('id')!='category_menu'){
			$(this).mouseover(function(event) {
				event.preventDefault();
				$("#btn_category").removeClass('btn_category_hover'); /* Remove hover class from Products menu button */
			});
		}		
	});

	/* Main Category Menu - Close Button */
	$("#category_menu a.close").click(function(event) {
		event.preventDefault();
		$("#category_menu").slideUp(); /* Hide main menu */
	});

	/* Sub Category Menu - Close Button */
	$("#category_menu a.closesub").click(function(event) {
		event.preventDefault();
		$('.submenu').hide(); /* Hide sub menus */
		$('#category-modal').hide(); /* Hide modal */	
	});

	/* Main Category Menu Mouseout */
	$("#nav_wrapper").mouseover(function(event) {
		event.preventDefault();
		$("#category_menu").slideUp();	/* Hide main menu */
		$('.submenu').hide(); /* Hide sub menus */		
		$('#category-modal').hide(); /* Hide modal */
		$("#btn_category").removeClass('btn_category_hover'); /* Remove hover class from Products menu button */
			
	});

	/* Main Category Menu Item Click */
	$('#category_menu .category').each(function(index) {
		$(this).click(function(event) {
			var id = $(this).attr('id').replace('m-','');
			if($('#s-' + id).length){
				event.preventDefault();			
				$('#category-modal').show(); /* Show modal */			
				$('#s-' + id).slideDown(); /* Show submenu */
			}
		});		
	});


		
	/******************************************************************************************
	CART POPUPS 
	******************************************************************************************/
	
	var LoadCart = function()
	{
		//alert(baseDir);
		$.ajax({
			type: 'GET',
			url: 'cart.php',
			async: true,
			cache: false,
			dataType : "json",
			data: 'ajax=true&token=' + static_token,
			success: function(jsonData)
			{
				if(jsonData.products.length==0){
					var html = '<p align=\'center\'>Your shopping cart is empty.</p>';
					$("#view_cart_pop .foot").addClass('hidden');
				} else{
					var html = '<table>';
					$(jsonData.products).each(function(){
						if(this.quantity>0){
							html+= '<tr><td width=\'140\'><a href=\'' + this.link + '\'>' + this.name + '</a></td><td width=\'50\'>Qty: ' + this.quantity + ' </td><td>' + this.price + '</td></tr>';
						}
					});
					html+= '</table>';
					$("#view_cart_pop .foot").removeClass('hidden');
				}
				$("#view_cart_pop .product").html(html);
			},
			error: function(XMLHttpRequest, textStatus, errorThrown) {
				//alert(errorThrown);
			}
		});
	}

	/* Headers - Shopping Cart Link - Click */
	$("#view_cart_link, #nav_holder .btn_cart").click(function(event) {
		event.preventDefault();
		//$("#category_menu").slideUp('fast');
		//$("#view_cart_pop").fadeIn('fast'); 
		//$("#auth_pop").fadeOut('fast'); 	
		LoadCart();
		//Wrap("#view_cart_pop");
		//setTimeout ( '$("#view_cart_pop").fadeOut(\'slow\')', 5000 );
	});

	/* Headers - Shopping Cart Link - Mouseover */
	$("#view_cart_link, #nav_holder .btn_cart").mouseover(function(event) {
		$("#view_cart_link").click();
	});

	/* Headers - Login Link Hover */
	$("#nav_holder .btn_auth").hover(
	  function () {
		$("#auth_pop").fadeIn('fast'); 
	  },
	  function () {
		$("#auth_pop").fadeOut('fast'); 
	  }
	);

	/* Headers - Login Link */
	//$("#auth_link, #nav_holder .btn_auth").click(function(event) {
	//	event.preventDefault();
		//$("#category_menu").slideUp('fast');
		//$("#view_cart_pop").fadeOut('fast'); 
		//$("#auth_pop").fadeIn('fast');
	//});

	/* Headers - Login Link */
	//$("#auth_link, #nav_holder .btn_auth").mouseover(function(event) {
	//	$("#auth_link").click();		
	//});

	/* Headers - Login Cancel Button */
	//$("#auth_cancel").click(function(event) {
	//	event.preventDefault();
	//	$("#auth_pop").fadeOut('fast'); 		
	//});

	$("#auth_pop form").submit(function(event) {
		var regExpEmail = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/;
		if($("#email").attr('value') == '' || 
			$("#email").attr('value') == $("#email").attr('defaultValue') ||
			!regExpEmail.test($("#email").attr('value'))){
			alert("Please enter a valid email address");
			return false;
		}else if($("#passwd").attr('value') == '' || $("#passwd").attr('value') == $("#passwd").attr('defaultValue')){
			alert("Please enter your password");
			return false;
		}
	});

	

	/* Products Page - Add to Cart Button */
	$("#add_to_cart").click(function(event) {
		event.preventDefault();
		$("#add_to_cart_pop .msg").text(''); 
		$("#add_to_cart_pop .loading").fadeIn('fast'); 
		$("#add_to_cart_pop").fadeIn('fast'); 
		$.ajax({
			type: 'GET',
			url: baseDir + 'cart.php',
			async: true,
			cache: false,
			dataType : "json",
			data: 'add=1&ajax=true&token=' + static_token + '&id_product=' + $('#product_page_product_id').attr('value') + '&id_product_attribute=' + $('#idCombination').attr('value') + '&qty=' + $('#quantity_wanted').attr('value'),
			success: function(jsonData)
			{
				$('.ajax_cart_quantity').text(jsonData.nbTotalProducts);
				$('.ajax_cart_amount').removeClass('hidden');
				$('.ajax_cart_total').removeClass('hidden');
				$('.ajax_cart_total').text(jsonData.total);					
				if(jsonData.nbTotalProducts==1){
					$('.ajax_cart_product_txt').removeClass('hidden');
					$('.ajax_cart_product_txt_s').addClass('hidden');
				} else if(jsonData.nbTotalProducts>1){
					$('.ajax_cart_product_txt_s').removeClass('hidden');
					$('.ajax_cart_product_txt').addClass('hidden');
				}
				setTimeout ( '$("#add_to_cart_pop").fadeOut(\'slow\')', 5000 );
				if (jsonData.hasError)
				{
					var errors = 'error message';
					for(error in jsonData.errors)
						//IE6 bug fix
						if(error != 'indexOf')
							errors += jsonData.errors[error] + "\n";
					$("#add_to_cart_pop .loading").css('background-image','none'); 
					$("#add_to_cart_pop .msg").fadeIn('fast');
					$("#add_to_cart_pop .msg").text(errors);
				} else {
					$("#add_to_cart_pop .loading").fadeOut('fast'); 					
				}
			},
			error: function(XMLHttpRequest, textStatus, errorThrown) {
				setTimeout ( '$("#add_to_cart_pop").fadeOut(\'slow\')', 5000 );
				$("#add_to_cart_pop .loading").css('background-image','none'); 
				$("#add_to_cart_pop .msg").fadeIn('fast');
				$("#add_to_cart_pop .msg").text('An error has occurred. Please try again or contact customer service.');
			}
		});
	});

	

});

