$(function() {
	$('a.zoom').fancyZoom();

	//form loading gif
	$('form').live('submit', function(e) {
	  $(this).children('button').remove();
	  if($(this).hasClass('small-throbber'))
	   $(this).append("<img src='../images/small-ajax-loader.gif' />");
	  else
 		  $(this).children('button').replaceWith("<img src='../images/ajax-loader.gif' />");
	});

	mail_chimp_setup();

	//Import form
	$('input#import_new_group_true').focus(function(){
		$(this).siblings("input[type=text]").focus();
	});
	$('input#import_new_group_false').focus(function(){
		$(this).siblings("select").focus();
	});
	$('div.group-import input[type=text], select').focus(function(){
		$(this).siblings('input[type=radio]').attr('checked', true)
	})

	//clear text
	$(".cleartext").focus(function(srcc) {
		if ($(this).val() == $(this)[0].title)
			$(this).removeClass("active");
	    $(this).val("");
	 });

	 $(".cleartext").blur(function(){
	   if ($(this).val() == "") {
		   $(this).addClass("active");
       $(this).val($(this)[0].title);
	   }
	 });

	//Chore forms
	$('select.network-focus').live("change", function() {
		$('div.secondary-sub').html($("#" + $(this).val()).parent().html());
	})

	$("input#test").val("0");

	$('a.send-test-email').live('click', function(e) {
		e.preventDefault();
		var contain = $(this).parents("div.secondary-email-test")
		$.get($(this).attr('href'), function(response) {
			contain.append("<p style='margin-left:25px;display:none;color:#3F320A;' id='test-sent'>Test email sent</p>")
			$("p#test-sent").fadeIn()
			setTimeout(function(){ $("p#test-sent").fadeOut('fast'); }, 5000);
			setTimeout(function(){ $("p#test-sent").remove(); }, 6000);
		})
	})

	$(".sm-scope-chk").live('change', function() {
		var chore = $(this).parents("div.chore")
		if($(this).attr('checked') == true) {
			chore.find(".has-sn-focus").val(true).end().find(".sn-selection").fadeIn('fast');
		}
		else {
			chore.find(".has-sn-focus").val(false).end().find(".sn-selection").fadeOut('fast');
		}
	})

	$("a.change-interval").live('click', function(e){
		e.preventDefault();
		$(this).parents('div.chore').find('div.interval-set').hide().end().find('div.interval-edit').show();
	})

	$("a.update-interval").live('click', function(e){
		e.preventDefault();
		var chore = $(this).parents('div.chore');
		chore.find('span.current-interval').html("Day " + chore.find('input.interval-input').val())
		chore.find('div.interval-set').show().end().find('div.interval-edit').hide();
	})

	$("a.delete-chore").live('click', function(e){
		e.preventDefault();
		if($(this).attr('href') == "#")  // unsaved chore
			$(this).parents('div.chore').remove();
		else {
			$.get($(this).attr('href'), function(response) {
				$("div#chores").replaceWith(response)
			})
		}
	})

	$("a#add-chore").live('click', function(e){
		e.preventDefault();
		$.get($(this).attr('href'), function(response) {
			$("div#chores").replaceWith(response)
		})
	})

	$("a.cancel-compose").live('click', function(e){
		e.preventDefault();
		var chore = $(this).parents('div.chore')
		$.get($(this).attr('href'), function(response) {
				$("div#chores").replaceWith(response)
		})
	})

	$('form.chore_form').live('submit', function(e){
		e.preventDefault();
		var chore = $(this)
		$('div.error').remove();
		chore.find('p.saving').show().end().find('div.save-msg').hide();
		$.post(chore.attr('action'), chore.serialize(), function(response) {
			chore.find('p.saving').hide().end().find('div.save-msg').show();
			if(response.split("|")[0] != "fail") {
				$("div#chores").replaceWith(response)
			} else {
				chore.prepend("<div class='error' style='padding:10px'>" + response.split("|")[1] + "</div>")
			}
		})
	})

	$(".edit-view").live('click', function(e) {
		e.preventDefault();
		$("div.edit").hide();
		$("div.closed-message").show();
		$(this).parents('div.chore').find('div.closed-message').hide().end().find('div.edit').show();
	})


	//autofills
	$("input[type='text']#import_new_group_name").example("Name the new group")
	$("textarea#import_bulk").example("Jane, jane_doe@yahoo.com\r\nJohn, john.doe@gmail.com\r\nSam Smith, sam_smith@gmail.com\r\njack_jameson@hotmail.com")
	$("input[type='text']#ttdldt-ttdldt").example("Enter your email address")
	$("input#single-import").example("Enter an email address")
})

function set_focus_check(){
	if($('#has-sn-focus').val() == "1"){
		$(".sm-scope-chk").attr('checked', true)
		$("#sn-selection").fadeIn('fast');
	}
	else{
		$(".sm-scope-chk").attr('checked', false)
		$("#sn-selection").fadeOut('fast');
	}
}

function mail_chimp_setup() {
  $("a.connect-to-mailchimp").click(function(e) {
		e.preventDefault();
		$("form#chimp-key-form").submit();
	})

	$("form#chimp-key-form").submit(function(e) {
		e.preventDefault();
		if($("div#mailchimp-key").css("display") == "none") {
		  if($("input#mailchimp_user").val() == "" || $("input#mailchimp_password").val() == "") {
			  $("p#chimp-instructions").html("MailChimp Login - <span style='color:red;'>Please enter a username and password</span>")
			  return;
			}
		} else if($("div#mailchimp-login").css("display") == "none") {
		  if($("input#mailchimp_api_key").val() == "") {
		    $("p#chimp-key-instructions").html("MailChimp API Key - <span style='color:red;'>Please enter a MailChimp API key</span>")
		    return;
		  }
		}
		$(this).find('button').replaceWith("<img style='margin-left:-7px;' src='../images/ajax-loader.gif' />");
		$.post($(this).attr('action'), $(this).serialize(), function(response) {
			$("div#chimp-ui").replaceWith(response)
			mail_chimp_setup();
		})
	})
}

