$(function() {
	var currentYear = new Date();
	currentYear = currentYear.getFullYear();
	
	
	$("#member-login a:first").bind("click.login", function(e) {
		$(this)
			.parent()
			.addClass("loginExpanded")
			.find("#login-links, #login-form, #submit-container")
				.fadeIn(300);
		$("#txtUsername").focus();
		e.preventDefault();
	});

	$("ul.sf-menu").superfish({
		dropShadows: false
	});

	$("a[rel^='video']").prettyPhoto({
		theme: 'facebook',
		modal: true,
		deeplinking: false,
		default_width: 640,
		default_height: 400
	});

	$(".video-container:last").css("border-right", "none");

	$('<img />').attr('src', '/assets/img/bob-greene-active.jpg');
	$('#bob-greene-video').hover(
		function(e) {
			$(this).attr('src', '/assets/img/bob-greene-active.jpg');
		},
		function(e) {
			$(this).attr('src', '/assets/img/bob-greene-inactive.jpg');	
		}
	);

	$("#top-main button, #sidebar-container button, #become-member button, #top-right button").click(function(e) {
		
		if ($(this).hasClass("free-membership")) {
			goFreeMembership();
		}
		else if ($(this).hasClass("full-membership")) {
			goFullMembership();
		}
		e.preventDefault();
	});
	
	if ($("#main").hasClass("full-membership") || $("#main").hasClass("free-membership")) {
		HideForMemberForm();
	}

	$("#footer-right ul li:not(:last)").append("|");
	
	
	$("#footer-right span").text(currentYear);
	
	$("#share-container img").tooltip();
	
	$("#community-button").click(function() {
		$(this).hide();
		$("#community-expanded").show();
	});
	
	$("#community-expanded").click(function() {
		$(this).hide();
		$("#community-button").show();
	});
	
	$("#enews").bind("click", function(event){
		sendToMailChimp("#txtEnewsEmail", "#newsletter", "#confirmation", event);
	});
	
	$("#enews-content").bind("click", function(event) {
		sendToMailChimp("#txtEnewsEmail-content", "#newsletter-content", "#confirmation-content", event);
	});
/*
	$("html").css("background-image", function() {
		var randomnumber=Math.floor(Math.random()*8)+1;
		
		return "url('/assets/img/home/background" + randomnumber + ".jpg')";
	});
*/	
	setTimeout("$('#community-expanded').click();", 10000);
	
	if ($(".success-story").length) {
		$(".image-container").hide();
		var randomnumber = Math.floor(Math.random()*5)+1;
		$("#story-" + randomnumber + ", #story-" + randomnumber + " .image-container").show();
	}
	
	
	$(".rollover").bind("mouseover", function(){
		var src = $(this).attr("src");
		var first = src.substr(0, src.indexOf("."));
		var last = src.substr(src.indexOf("."), src.length);
		$(this).attr("src", first + "-on" + last);
	}).bind("mouseout", function() {
		var src = $(this).attr("src");
		var first = src.substr(0, src.indexOf("-on"));
		var last = src.substr(src.indexOf("-on")+3, src.length);
		$(this).attr("src", first + last);
	});
	
	/*           Forget password events                 */
	$("#forget-link").bind("click.forget", function(e){
		var $forgetPasswordContainer = $("#forget-password-container");
		
		var details = {
			win_width: $(window).width(),
			win_height: $(window).height(),
			panel_width: $forgetPasswordContainer.width(),
			panel_height: $forgetPasswordContainer.height()
		};
		
		$("#forget-overlay, #forget-password-container").show()
		$forgetPasswordContainer
			.css("left", (details.win_width/2) - (details.panel_width/2))
			.css("top", (details.win_height/2) - (details.panel_height/2));
		e.preventDefault();
	});
	
	$("#forget-close").bind("click.forget", function() {
		$("#forget-password-container, #forget-overlay").hide();
	});
	
	$("button.submitPasswordReset").bind("click", function(e) {
		var email = $("#txtEmailResetPassword").val();
		$.ajax({
			type: "POST",
			url: "/assets/includes/userAccount/forgotPassword.php",
			data: { email : email },
			dataType: "html",
			success: function(html) {
				$("#passConfirm").html(html);
			}
		}); 
		$(this).submit();
		e.preventDefault();
	});
	/*           End Forget password events             */
	
	if ($("#year").length > 0) {
		for (var i=0; i<7; i++) {
			var loopYear = currentYear + i;
			$("#year").append("<option value=\"" + loopYear + "\">" + loopYear + "</option>");
		}
		if ($("#year option:eq(1)").text() == "") {
			$("#year option:eq(1)").remove();
		}
	}
	
	$("#learn-more").click(function(e){
		goFullMembership();
		e.preventDefault();
	});


	
	if ($("#approved-foods").length) {
		$("#approved-foods li > div").dialog({
			autoOpen: false,
			modal:    true,
			width:    540
		});
		$("#approved-foods li").click(function() {
			$("#" + $(this).data("dialog")).dialog("open");
		});
	}
	
	$(".twitter-share").click(function(){
		var url = $(this).attr("href");
		url += "?url=" + encodeURIComponent(window.location);
		url += "&text=" + encodeURIComponent(document.title);
		$(this).attr("href", url);
	});
	
	$(".facebook-share-button").click(function(){
		var url = $(this).attr("href");
		url += "?u=" + encodeURIComponent(window.location);
		url += "&t=" + encodeURIComponent(document.title);
		$(this).attr("href", url);
	});
	
	$(".email-share-button").click(function(){
		var url;
		url = "mailto:?subject=" + encodeURIComponent("I thought you would like this page: " + document.title);
		url += "&body=" + encodeURIComponent(document.title + " " + window.location);
		$(this).attr("href", url);
	});
	
	$("#username").change(function(){
		var t = this;
		$("#val-user").hide();
		$("#val-exist").hide();
		$.ajax({
			type: "POST",
			url: "/assets/includes/registration/validation.php",
			data: { username : t.value},
			success: function(html) {
				if (html == 'false') {
					var OK = false;
					$("#val-exist").show();
					
				}else {
					var OK = true;
				}
			},
			error: function(xhr, ajaxOptions, throwError) {
				alert(xhr.status);
				alert(thrownError);
			}
		});
	});

	$("#email").change(function(){
		var t = this;
		$("#val-email-exist").hide();
		$("#val-email-invalid").hide();
		$.ajax({
			type: "POST",
			url: "/assets/includes/registration/validation.php",
			data: { email : t.value},
			success: function(html) {
				if (html == 'false') {
					var OK = false;
					$("#val-email-exist").show();
				} else if (html == 'invalid') {
					var OK = false;
					$("#val-email-invalid").show();
				} else {
					var OK = true;
				}
			},
			error: function(xhr, ajaxOptions, throwError) {
				alert(xhr.status);
				alert(thrownError);
			}
		});
	});

	$('.books_page .book_container, .articles .article_container').each(function(index, el) {
		if (index%2) {
			$(el).addClass('right_column');
		}
	});

	var article_search = $('input[name=search_art]');
	var article_search_label = $('label[for=search_art]');
	if (article_search && article_search_label) {
		article_search.blur(function(e) {
			if($(this).val() == '') {
				$(this).val(article_search_label.text());
			}
		});

		article_search.focus(function(e) {
			if($(this).val() == article_search_label.text()) {
				$(this).val('');
			}
		});
		article_search.trigger('blur');
	}

});

$(window).load(function () {
		$(".image-container").each(function(){
			$(this).width($(this).find("img:visible").width());
			if (!$(this).find("p").text()) {
				$(this).css("border-bottom", "none");
			}
	});
});

function sendToMailChimp (email, newsletter, confirmation, event) {
	event.preventDefault(); 
	$.ajax({
		type: "POST",
		url: "/assets/includes/mailChimp/mailChimp.php",
		data: { email : $(email).val() },
		dataType: "html",
		success: function(html) {
			$(newsletter).hide();
			$(confirmation).html(html);
		}
	});
}

function HideForMemberForm () {
	$("#container header, #community, #main-container").hide();
	$("#member-form").show();
	
	var _member_login = $("#member-login")
								.removeClass("loginExpanded")
								.find("#login-links, #login-form, #submit-container")
									.hide()
										.end()
								.clone(true);
	$("#member_login").remove();
	_member_login.prependTo("#form-container");
	$(window).scrollTop(0);
}

function goFullMembership () {
	window.location = "/fullmembership.php";
}

function goFreeMembership () {
	window.location = "/freemembership.php";
}

$("#product-links").delegate("a", "click", function(e) {
	e.preventDefault();
	var $this = $(this),
			$id 	= $($this.attr("href"));
	if ($id.is(":not(.ui-dialog)")) {
		$id.dialog({
			title: 		$id.find("h3").hide().text(), 
			modal:    true,
			width:    540
		});
	} else {
		$id.dialog("open");
	}
});

$("#content-container").delegate("button#locator", "click", function(e) {
	e.preventDefault();
	e.stopPropagation();
	var zip = $(this).prevAll("input[type='text']").val(),
			zip_error = "<em>Please enter a valid zip code.</em>",
			zip_regex = /^\d{5}([\-]\d{4})?$/;
		if (zip) {
			if (zip.match(zip_regex)) { 
				$(this).nextAll("#locator-results").load("zip.php?zip=" + zip);
			}	else {
				$(this).nextAll("#locator-results").html(zip_error);
			}
		}
});














