var tempMSG = "If you have any specific instructions for the seller, write it here.";

// Login window dropdown
$(document).ready(function(){
	$("#dev").css({
		"display":"block",
		"top": ($(window).height()/2) - ($("#dev").height()/2),
		"opacity":.5
	})
	
	$("#nav-search a").click(function(){
		$("#nav-search .toggle").toggle("fast");
	});
	$("#nav-categories a").click(function(){
		$("#nav-categories .toggle").toggle("fast");
	});
	$("#account #cart").click(function(){
		$("#account #cart .toggle").toggle("fast");
	});
	$(".item-info .more, .item-info .toggle .button").click(function(){
		$(this).next(".item-info .toggle").toggle("fast");
	});
	
	// LOG OUT //
	$("#btn-logout").bind("click", function(){
		$.ajax({
			url: "/includes/auth.php",
			type: "POST",
			data: "action=log_out",
			context: document.body,
			success: function(xhr){
				if (xhr == "OK") { 
					$("#btn-register, #btn-login").show();
					$("#btn-myaccount, #btn-logout").hide();	
				}
				document.location.href="http://www.stylemychild.com/index.php";
			}
		});
		
	});
	
	// LOG IN //
	$("#loginWindow").bind("click",function(){
		
		if ($("#loginWindow").val() == "Reset") {
			$.ajax({
				url: "/includes/functions.php",
				type: "POST",
				data: "action=resetPassword&userName="+$("#loginName").val(),
				success: function (xhr) {
					if (xhr == "OK") {
						$(".wrong").slideUp(function(){
							$(".saved").css({
								"height":"auto",
								"padding":4
							}).html("Thank you. You will receive an email from us with a new temporary password. Please use this to login and immediately change your password to something you can remember").slideDown();
						});		
						$("#loginPass, label[for='loginPass']").slideDown();
						$("#loginWindow").val("login");
					} else {
						$(".saved").slideUp(function(){						
							$(".wrong").slideUp(function(){
								$(this).html("There is no account matching these credentials.<br/> Please try again.").slideDown()
							});
						});
					}
				}
			});
		} else {
			$.ajax({
				url: "/includes/auth.php",
				type: "POST",
				data: "username="+$("#loginName").val().toLowerCase()+"&password="+$("#loginPass").val()+"&action=log_in",
				context: document.body,
				success: function(xhr){
					if (xhr == "ERROR1" || xhr == "BAD") {
						$(".wrong").slideDown();
						return false;
					} else if (xhr == "OK") {
						$("#btn-login").hide();
						$("#btn-register").hide();
						$("#btn-logout").show();
						$("#btn-myaccount").show();
						$("#login-window").animate({
							"opacity":0
						}, function(){
							$(this).hide()
						})
						$("#lb-bkg").animate({
							"opacity":0
						}, function(){
							$(this).hide();
							$(".wrong").slideUp();
						});
						
						location.reload(true);
		
						return false;
					} else {
						alert(xhr);
						return false;
					}
				}
			});
		}
		return false;
	});
	
	
	$("#btn-login, .btn-login").live("click", function(){
		$.scrollTo("0px",500);
		$("#login-window").css({"opacity":0}).show().animate({
			"opacity":1,
		})
		$("#lb-bkg").css({"opacity":0}).show().animate({
			"opacity":1,
			"height":$(document).height()
		})
		return false;
		
	})
	
	$("#close, #lb-bkg").bind("click", function(){
		$("#login-window").animate({
			"opacity":0
		}, function(){
			$(this).hide()	
		})
		
		$(".wrong").slideUp();
		$("#loginPass, label[for='loginPass']").slideDown();
		$("#loginWindow").val("login");
		
		$("#lb-bkg").animate({
			"opacity":0
		}, function(){
			$(this).hide()	
		})
	})

	// lightbox
	$(".lb-btn").bind("click", function(){
		// loading the content within the lightbox
	
		$.ajax({
			url: $(this).attr("href"),
			type: "POST",
			success: function(xhr){
				$(".lightbox").html(xhr);
				
				// animate if success in loading
				$(".lightbox").css({"opacity":0}).show().animate({
					"opacity":1,
					//"margin-top":$(window).height()/2-$(".lightbox").height()/2
					"margin-top":'40'
				})
				$.scrollTo("0px",500);
				$("#lb-bkg").css({
					"opacity":0,
					"height":$(document).height()
				}).show().animate({
					"opacity":1
				})
			}
		})
		// animate the lightbox
		return false;
	})
	
	$("#close, #lb-bkg").live("click", function(){
		$(".lightbox").animate({
			"opacity":0
		}, function(){
			$(this).hide()	
		})
		$("#lb-bkg").animate({
			"opacity":0
		}, function(){
			$(this).hide()	
		})
	})
	
	//////////////////////////////////////
	//	 SWAP IMAGES ON PRODUCTS PAGE	//
	//////////////////////////////////////
	if ($("#item-th a").length > 1) {
		$('#product .image img').remove();
		var url = $('#product .image').css('background-image').replace('url(', '').replace(')', '').replace("'", '').replace('"', '').replace('"', '');
		var bgImg = $('<img />');
		bgImg.hide();
		$('#product .image').append(bgImg);
		bgImg.attr('src', url);
		bgImg.bind('load', function() {
		    var height = $(this).height();
		    if (height > 700) { height = 700; }
		   	$('#product .image').animate({
		   		"height":height
		   	});
		});
		
		$("#item-th a").bind("click", function(){
			var _this = $(this);
			var target = $(_this).css("background-image");
			target = target.replace("icon", "full");

			$('#product .image').animate({
				"opacity" : 0
			}, function(){
				$('#product .image').css('background-image',target).animate({"opacity":0},function(){
					$("#product .image").animate({
						"opacity" : 1
					}, function(){
						$('#product .image img').remove();
						var url = $('#product .image').css('background-image').replace('url(', '').replace(')', '').replace("'", '').replace('"', '').replace('"', '');
						var bgImg = $('<img />');
						bgImg.hide();
						$('#product .image').append(bgImg);
						bgImg.attr('src', url);
						bgImg.bind('load', function() {
						    var height = $(this).height();
						    if (height > 700) { height = 700; }
						   	$('#product .image').animate({
						   		"height":height
						   	});
						});
					});
			    }); 
		    })
		    
		});
	} else {
		$("#item-th").children().css({"cursor":"default"});
	}
	

	//////////////////////////////////////
	//	 LOAD CATEGORY BASED PRODUCTS	//
	//////////////////////////////////////
	$(".category-list ul li a").not(":last").click(function(){
		var category = $(this).attr("href");
		category = category.substring(0, category.lastIndexOf("/"));
		category = category.substring(category.lastIndexOf("/")+1);

		$.ajax({
			url: "/includes/functions.php",
			type: "POST",
			data: "category="+category+"&storeID="+$("#storeID").val()+"&pageNum=1",
			success: function(xhr){

				$("#storeContent").animate({
					"opacity" : 0
				}, function(){
					$(this).html(xhr).animate({
						"opacity" : 1
					});
					if (stButtons){stButtons.locateElements();}
				})		
			}
			
		});
		return false;
	});
	
	
	//////////////////////
	//	 PAGINATION		//
	//////////////////////
	$(".paginate a").live("click", function(){
		$.ajax({
			url: "/includes/functions.php",
			type: "POST",
			data: $(this).attr("href"),
			success: function(xhr){
				$("#storeContent").animate({
					"opacity" : 0
				}, function(){
					var storeSort = $("#store-sort").detach();
					$(this).html(xhr).animate({
						"opacity" : 1
					});
				})			
			}
			
		});
		return false;
	});
	
	
	//////////////////////////
	//	 RESULT SORTING		//
	//////////////////////////
	$("#store-sort a").live("click", function(){
		$.ajax({
			url: "/includes/functions.php"+$(this).attr("href"),
			type: "GET",
			success: function(xhr){
				$("#storeContent").animate({
					"opacity" : 0
				}, function(){
					$(this).html(xhr).animate({
						"opacity" : 1
					});
				})		
			}
			
		});
		return false;
	});


	$(".sellerMsg").bind("focus", function(){
		if ($(this).val() == tempMSG) { $(this).val(""); }
	});
	
	$(".sellerMsg").bind("focusout", function(){
		if ($(this).val() == "") {
			$(this).val(tempMSG);
		}
	});
	
	
	$("#forgotPass").live("click", function(){
		$(".wrong").slideUp();
		$("#loginPass, label[for='loginPass']").slideUp();
		$("#loginWindow").val("Reset");
		return false;
	});
	
	
	$("#storeFav").live("click", function(){
		$.ajax({
			url: "/includes/functions.php",
			type: "POST",
			data: "funct=storeFav&sid="+$(this).attr("href"),
			success: function(xhr){
				$("#storeFav").html(xhr);
				return false;
			}
		});
		return false;
	});
	
	$("#prodFav").live("click", function(){
		$.ajax({
			url: "/includes/functions.php",
			type: "POST",
			data: "funct=prodFav&pid="+$(this).attr("href"),
			success: function(xhr){
				$("#prodFav").html(xhr);
				return false;
			}
		});
		return false;	
	});
	
	tempNotifyURL = $("#return").val();
})

var tempNotifyURL;
////////////////////////////////////////////
// 	UPDATE MESSAGE TO SELLER ON CHECKOUT  //
////////////////////////////////////////////
function fixMessage(obj){
	if (tempMSG == $(obj).parent().parent().children(".left").children("p").children("textarea").val()) {
		$(obj).parent().parent().children(".left").children("p").children("textarea").val("");
	} else {
		$("#return").val(tempNotifyURL+"_"+encodeURI($(obj).parent().parent().children(".left").children("p").children("textarea").val()));
	}
	
	return false;
}

	
////////////////////////
// 		RESET FORM	  //
////////////////////////
$.fn.clearForm = function() {
	return this.each(function() {
		var type = this.type, tag = this.tagName.toLowerCase();
		if (tag == 'form')
			return $(':input',this).clearForm();
		if (type == 'text' || type == 'password' || tag == 'textarea')
			this.value = '';
		else if (type == 'checkbox' || type == 'radio')
			this.checked = false;
		else if (tag == 'select')
			this.selectedIndex = -1;
	});
};


////////////////////////
// NEWSLETTER SIGN UP //
////////////////////////
$(function(){
	$("#newsSignup").bind('click', function(){
		pass = true
		if (/^[_a-zA-Z0-9-]+(\.[_a-zA-Z0-9-]+)*@[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)*(\.[a-zA-Z]{2,4})$/.test($("#buy-newsletter #email").val()) == false) {
			$("#buy-newsletter #email").css({
				"background"	:	"#E0D5C6"
			});
			
			if ($(".wrong").length > 0 ){
				$(".wrong").html("Please enter a valid email address.").slideDown();
			}
			
			pass = false;
		} else {
			$(".wrong").slideUp(function(){
				var i = 0;
				$("#newsletter-selection input:checked").each(function(){
					i++;
				});
				if (i == 0) { $(".wrong").html("Please select a newsletter to subscribe to.").slideDown(); pass = false; }
			})	
		}
		
		if (pass == true) {

			$.ajax({
				url: "/includes/functions.php",
				data: "funct=newsletter&email="+$("#email").val()+"&name="+$("#firstName").val()+"&news="+$("#smc-news:checked").val()+"&babies="+$("#smc-babies:checked").val()+"&cause="+$("#smc-cause:checked").val(),
				type: "POST",
				success: function(xhr){
					$(".saved").css({"display":"block", "height":"auto"}).slideDown();
				},
				error: function(xhr){
					alert(xhr)
				}
			});
		}
	});
	
	$('#newsletter-selection input').bind('change', function () {
	   if ($(this).is(':checked')) {
		    $(this).val("1");
		    $(".wrong").slideUp();
		} else {
		    $(this).val("0");
		}
	});

	
});


