
jQuery.fn.flutter = function(){
	e=this;
	this.fadeIn("slow", function(){ setTimeout('e.fadeOut("slow");',2000); });
	};
		

jQuery.searchSite = function(){
	 q = jQuery("#stext");
	 if(q.val() == "" || q.val() == null || q.val() == "Enter Keyword Here"){
		 jQuery("#snotify").html("Please enter a valid search term.");
		 jQuery("#snotify").flutter("slow",2000);
		 return;
	 }
	 page=q.val();
	 page=page.replace(/ /g,'+');
	 page=encodeURI(page);
	 window.location = '/search/'+page;					  
};

jQuery.refreshCart = function(){
	jQuery.post('/inc/ajax/cartinfo.php',
		   {action:"load"},
		   function(data){
			  cart = jQuery.json.decode(data);
			  jQuery("#cartinfo").html(cart.data);
		});
};

jQuery.reloadCart = function(){
	jQuery.post('/inc/ajax/cartinfo.php',
		   {action:"cartReload"},
		   function(data){
			  d = jQuery.json.decode(data);
			  jQuery("#scContents").html(d.data);
		});
};

jQuery.freshCart = function(){
	jQuery.post('/inc/ajax/cartinfo.php',
		   {action:"cartReload"},
		   function(data){
			  d = jQuery.json.decode(data);
			  jQuery("#scContents").html(d.data);
			  jQuery.post('/inc/ajax/cartinfo.php',
		   					{action:"load"},
		   					function(data){
			 			 cart = jQuery.json.decode(data);
			  			jQuery("#cartinfo").html(cart.data);
				});
			  jQuery.post('/inc/ajax/cartinfo.php',
		   					{action:"load"},
		   					function(data){
			 			 cart = jQuery.json.decode(data);
			  			jQuery("#cartinfo").html(cart.data);
				});
		});
};

jQuery.loadDiscounts = function(){
	jQuery.post('/inc/ajax/cartinfo.php',
		   {action:"cartReload"},
		   function(data){
			  d = jQuery.json.decode(data);
			  if(d.discounts!=''&&$("#discountList")){
				  jQuery("#discountList").html(d.discounts);
				  jQuery("#discountList").show();
			  }
		});
};


 

$(document).ready(function(){
	
	$.refreshCart();
	$("#offer").click(function(event){
							   event.preventDefault();
							   
							   $.post('/inc/ajax/tracking.php',
									  {action:'specialOffer'},
									  function(data){
										  	return;
										  });
							   
							   $("#offer_ad").fadeIn("slow");
							   $("#offer_hide").unbind();
							   $("#offer_hide").click(function(event){
										$("#offer_ad").fadeOut("slow");
															   });
							   $("#offer_sub").unbind();
							   $("#offer_sub").click(function(event){
										$.post('/inc/ajax/login.php',
											   {action:'referral',
											   referrer:$("#referrer").val(),
											   email1:$("#remail1").val(),
											   email2:$("#remail2").val(),
											   email3:$("#remail3").val(),
											   email4:$("#remail4").val(),
											   email5:$("#remail5").val(),
											   refname:$("#refname").val()},
											   function(data){
												 
												  d=$.json.decode(data);
												   alert(d.response);
												   if(d.status=='success'){
													   $("#offer_ad").fadeOut("slow",function(){
															$("#referrer").val("");
															$("#remail1").val("");
															$("#remail2").val("");
															$("#remail3").val("");
															$("#remail4").val("");
															$("#remail5").val("");
															$("#refname").val("");
														});
													   
												   }
												   else{
													   return;
												   }
												   
												  
												   });
									});
							   });
	
	$.post('/inc/ajax/login.php',
				   {action:'checkstatus'},
					   function(data){
						   d=$.json.decode(data);
						   if(d.status=='true'){
							   $("#logout_t").append(' | <a href="/logout.php" class="darklnk">Log Out</a>');
							}
						});
	
	
	$(".lv").click(function(event){
							window.location = $(this).attr("title");
							});
	
	$("#signin").click(function(event){
									event.preventDefault();
									$.post('/inc/ajax/login.php',
										   {action:'checkstatus'},
										   function(data){
											   d=$.json.decode(data);
											   if(d.status=='true'){
												   window.location='account.php';
												}
												else{
													$("#login").fadeIn("slow");
												}
											});
									
									
							});
	
	$("#button_signin").click(function(event){
							   username = $("#uname").val();
							   password = $("#pcode").val();
							   if(username == "" || password == ""){
								   $("#logon_alert").text("Please enter your email & password before continuing.");
								   return;
							   }
							   $("#logon_alert").html('<img src="/img/loading.gif" height="10" />&nbsp;<span style="color:#000;">Logging in...</span>');
							   $.post('/inc/ajax/login.php',
									  { email : username,
									  passcode : password,
									  action : "signin"},
									  function(data){
										  result = $.json.decode(data);
										  if(result.status == "error"){
											  $("#logon_alert").text(result.response);
											  return;
										  }
										  else if(result.status == "success"){
											  window.location = "/account.php";
										  }
									   });
							   });
	
	$("#button_cancelsignin").click(function(event){
											 $("#login").fadeOut("slow", function(){
																				  $("#logon_alert").text("");
																				  });
											 });
	
	$("#stext").focus(function(){
								   text = $("#stext").val();
								   value = (text == "Enter Keyword Here") ? "" : text;
								   $("#stext").val(value); 
								   $("#stext").css("color","#000");
								   });
	$("#stext").blur(function(){
								  text = $("#stext").val();
								  value = (text == "") ? "Enter Keyword Here" : text;
								  $("#stext").val(value);
								  if(value == "Enter Keyword Here"){
									  $("#stext").css("color","#999");
								  }
								  });
	
	$("#sbutton").click(function(event){
								$.searchSite();
								 });
	$("#stext").keyup(function(e){
							   if(e.keyCode == 13){
								   $.searchSite();
							   }
							   });
	
	$("#newsletter").focus(function(){
									if($(this).val() == "Your Email"){
										$(this).val("");
									}
									});
	
	$("#nsignup").click(function(event){
								 if($(this).val() == "Your Email" || $(this).val() == ""){
										$("#nnotify").html("Please enter a valid email address.");
										$("#nnotify").flutter("slow",2000);
										return;
									}
								$.post('/inc/ajax/login.php',
									   {action : "newsletter", email : $("#newsletter").val()},
									   function(data){
										   result = $.json.decode(data);
										   if(result.status == "success"){
											   $("#nmessage").html('<br />'+result.response);
											   $("#nbox").fadeOut("slow",function(){
																			$("#nmessage").fadeIn("slow"); 
																			 });
										   }
										   else{
											   $("#nnotify").html(result.response);
											   $("#nnotify").flutter("slow",2000);
										   }
										   });
								 });
	$("#addItem").hover(
						function(){
							$("#addItem").attr("src","/img/cart_add_LG_h.jpg");
							},
						function(){
							$("#addItem").attr("src","/img/cart_add_LG.jpg");
							}
						);
	
	
	$(".detailsButton").hover(
						function(){
							$(this).attr("src","/img/details_h.jpg");
							},
						function(){
							$(this).attr("src","/img/details.jpg");
							}
						);
	
	$("#addItem").click(function(event){
								 
								 $("#cartLoader").fadeIn("slow");
								 $.get('/inc/ajax/cartinfo.php',
									   {id:$("#prodID").val(),
									   amount:$("#prodQTY").val(),
									   action:'addItem'},
									   function(data){
										   d=$.json.decode(data);
										   if(d.status=='error'){
											   $("#cartLoader").fadeOut("slow");
										   		$("#addInfo").html(d.response);
										   		$("#addInfo").flutter("slow",2000);
										   }
										   $.refreshCart();
										   $("#cartLoader").fadeOut("slow", function(){
																	$("#addInfo").html(d.response);
										   							$("#addInfo").fadeIn("slow");
																});
										   });
								 });
	
	
	$("#sameShip").click(function(){
								  $("#sfname").val($("#fname").val());
								  $("#slname").val($("#lname").val());
						$("#saddress").val($("#address").val());
						$("#scity").val($("#city").val());
						$("#sstate").val($("#state").val());
						$("#sstate_alt").val($("#state_alt").val());
						if($("#country").val()=="1"){
							$("#sstate1").show();
							$("#sstate2").hide();
						}
						else{
							$("#sstate1").hide();
							$("#sstate2").show();
						}
						$("#spostalcode").val($("#postalcode").val());
						$("#scountry").val($("#country").val());
						});
	
	
	$("#country").change(function(){
									  if($("#country").val()==1){
										$("#state1").show();
										$("#state2").hide();
									  }
									  else{
										  $("#state1").hide();
										  $("#state2").show();
									  }
									  });
		
		$("#scountry").change(function(){
									  if($("#scountry").val()==1){
										$("#sstate1").show();
										$("#sstate2").hide();
									  }
									  else{
										  $("#sstate1").hide();
										  $("#sstate2").show();
									  }
									  });
	
	
	$("#fpassword").click(function(event){	
								   event.preventDefault();
								   $("#fp_alert").fadeIn();
								   });
	
	$("#fp_send").click(function(event){
								   $.post('/inc/ajax/login.php',
										  {action:'retrieve_p',
										  email:$("#fp_email").val()},
										  function(data){
											  d=$.json.decode(data);
											  $("#fp_notify").html(d.response);
											  if(d.status=='success'){
												 setTimeout('$("#fp_alert").fadeOut("slow",function(){$("#fp_notify").html("Please enter your email.");}); ',2000);
											  }
											  });
								   });
	
	$("#fp_cancel").click(function(event){
								   $("#fp_alert").fadeOut("slow", function(){
													$("#fp_notify").html("Please enter your email.");
											});
								   });
	
			
});


