// Submit Story Form

var storyForm = "";		// used to save form before it's changed via ajax so we can bring it back
						// when somone clicks the submit button again.
					
// Colorbox plugin options
var storyInlineObj =  {
		transition: "none",
		speed:0,
		opacity:0.6,
		width:"480",
		height:"500",
		inline:true, 
		href:"#submit-story-form",
		close: "Close",
		scrollbars:false
	};

// setup the forms
$(document).ready(function(){
	storyForm = ieInnerHTML(document.getElementById('submit-story-form'));
	$(".submit-story-inline").colorbox(
		storyInlineObj
	);
});
	
$().bind('cbox_open', function(){
        drawSubmitStoryForm();
});	


function drawSubmitStoryForm() {
	$("#submit-story-form").empty().append(storyForm);
	$(".submit-story-inline").colorbox(
		storyInlineObj
	);
}



// when form is loaded, we must attach our click
// events on our Labels so we can make them dissapear 
// and focus on the desired input.
$().bind('cbox_complete', function(){

	$("#lFirstName").click(function() {
		$(this).hide();
		$("#firstName").focus();
	});
	$("#firstName").focus(function() {
		$("#lFirstName").hide();
	});
	$("#firstName").blur(function() {
		if($("#firstName").val() == "") { $("#lFirstName").show(); }
	});
	$("#lLastName").click(function() {
		$(this).hide();
		$("#lastName").focus();
	});
	$("#lastName").focus(function() {
		$("#lLastName").hide();
	});
	$("#lastName").blur(function() {
		if($("#lastName").val() == "") { $("#lLastName").show(); }
	});
	$("#lEmail").click(function() {
		$(this).hide();
		$("#email").focus();
	});
	$("#email").focus(function() {
		$("#lEmail").hide();
	});
	$("#email").blur(function() {
		if($("#email").val() == "") { $("#lEmail").show(); }
	});
	$("#lPhone").click(function() {
		$(this).hide();
		$("#phone").focus();
	});
	$("#phone").focus(function() {
		$("#lPhone").hide();
	});
	$("#phone").blur(function() {
		if($("#phone").val() == "") { $("#lPhone").show(); }
	});
	$("#lVideoUrl").click(function() {
		$(this).hide();
		$("#videoUrl").focus();
	});
	$("#videoUrl").focus(function() {
		$("#lVideoUrl").hide();
	});
	$("#videoUrl").blur(function() {
		if($("#videoUrl").val() == "") { $("#lVideoUrl").show(); }
	});	
	$("#lImageUrl").click(function() {
		$(this).hide();
		$("#imageUrl").focus();
	});
	$("#imageUrl").focus(function() {
		$("#lImageUrl").hide();
	});
	$("#imageUrl").blur(function() {
		if($("#imageUrl").val() == "") { $("#lImageUrl").show(); }
	});
	$("#lStory").click(function() {
		$(this).hide();
		$("#story").focus();
	});
	$("#story").focus(function() {
		$("#lStory").hide();
	});
	$("#story").blur(function() {
		if($("#story").val() == "") { $("#lStory").show(); }
	});	
	
});	

// Submit Story
function submitStory() {
	$(".errorMsg").empty();
	var firstName = $("#firstName").val();
	var lastName = $("#lastName").val();
	var email = $("#email").val();
	var phone = $("#phone").val();
	var videoUrl = $("#videoUrl").val();
	var imageUrl = $("#imageUrl").val();
	var story = $("#story").val();
	
	var hasError = false;
	if(firstName == "") {hasError = true;}
	if(lastName == "") { hasError = true;}
	if(email == "" || !checkEmail(email)) { hasError = true; }	
	if(phone == "") { hasError = true; }
	if(story == "") { hasError = true; }

	if(hasError) {
		$(".errorMsg").append("Please fill out the required fields");
		$(".errorMsg").show(200);

		return false;
	} else {
		
	$.post("/wp-content/themes/nobu/forms/send-story.php", 
	{ firstName: firstName, lastName: lastName, email: email, phone: phone, videoUrl: videoUrl, imageUrl:imageUrl, story:story },
	function(data){
	 	$("#submit-story-form").empty().append(data);
	
	  	// resize modal
		$(".submit-story-inline").colorbox(
		{
			width:"480",
			height:"260",
			inline:true,
			href:"#submit-story-form",
			scrollbars:false,
			open:true
		}
		);
	  
	  
	});	
	
	//
	return false;
	}
	
}


