$(function(){
	$(".social input").each(function(){
		var network=this.id;
		var b = $(this).parent();
		b.addClass("button").css({
			background:"transparent url(images/icons/48/"+network+".png) no-repeat 0px 0px"
		});
		b.children().hide();
		b.append("<a href='#"+network+"' title='"+network+"' onclick='return false;'>"+network+"</a>");
		b.children("a").hover(function() {
			$(this).css({background:"transparent url(images/icons/48/"+network+"-over.png) no-repeat 0px 0px"});
		},function() {
			$(this).css({background:"transparent url(images/icons/48/"+network+".png) no-repeat 0px 0px"});
		}).click(function(){
			var c = $(this).parent();
			$(this).remove();
			c.fadeOut().queue(function(){
				$(this).removeClass("button").css({background:"none"}).appendTo(".webPresence > .left").show().children().fadeIn();
				resize(c);
				$(this).dequeue();
			});
		});
	});
	
	var a = $("#goals > div > ol");
	a.after('<input type="button" value="Add Another Goal" id="addGoal" />');
	$("#addGoal").click(function(){
		var e = $("#goals > div > ol");
		var n=e.children("li").length+1;
		e.append('<li><input type="text" class="text" name="goal'+n+'" style="display:none" /></li>');
		e.children("li:last").children().fadeIn();
		resize($(this));
	});
	a.after('<input type="button" value="Remove Last Goal" id="removeGoal"/>');
	$("#removeGoal").click(function(){
		$("#goals > div > ol > li:not(:first):last").children().fadeOut().queue(function(){$(this).parent().remove(); $(this).dequeue();});
		resize($(this));
	});
	
	$("#logo").click(function(){
		$("#logoURLwrapper").slideDown().queue(function(){resize($(this));$(this).dequeue();});
	});
	$("#noLogo").click(function(){
		$("#logoURLwrapper").slideUp().queue(function(){resize($(this));$(this).dequeue();});
	});

	
	$.validator.addMethod("phoneUS", function(phone_number, element) {
    phone_number = phone_number.replace(/\s+/g, ""); 
	return this.optional(element) || phone_number.length > 9 &&
		phone_number.match(/^(1-?)?(\([2-9]\d{2}\)|[2-9]\d{2})-?[2-9]\d{2}-?\d{4}$/);
}, "Please specify a valid phone number");

	
	$("#brief").validate({
		rules:{
			name:{required: true, maxlength:40},
			phone:{required: false, phoneUS: true},
			email:{required: false, email:true},
			website:{required: false, url:true},
			zip:{required:false, digits:true, maxlength:5},
			overview:{required: true},
			goal1:{required: true},
			audience:{required:true},
			competitors:{required:true},
			message:{required:true},
			timeline:{required:true},
			budget:{required:true}
		},
		groups: {
			fields: "name phone email"
		},
		errorPlacement: function(error, element) {
			error.appendTo("#done");
		},
		submitHandler: function(form) {
			var validator = $("#brief").validate();
			if(validator.element("#phone") || validator.element("#email")) form.submit();
			else validator.showErrors({"phone": "We need a phone number or email address to contact you!"});
		}
	});
	
});

function resize(c){
	var h = c.parents(".panel").attr("scrollHeight");
	$(".scroll").animate({height:h+"px"});
}