// JavaScript Document

$(function(){

	$("a[rel^='prettyPhoto']").prettyPhoto();
	
	$("#email_sign_up_container input[type=text]").bind("click", function(){
		$(this).focus();
		$(this).select();
	});
	
	$( "#accordion_faq" ).accordion({
   		 active: false,
   		 autoHeight: false,
   		 collapsible: true
         
	});

	$('#index_slideshow').jshowoff({
		controls: false,
		effect : "fade",
		links : false,
		changeSpeed : 1000,
		speed : 5000
	});
	
	$("ul.dropdown li ul li").hover(
  		function () {
    		$(this).closest("ul li:not(ul.children li)").addClass("active");
  		}, 
  		function () {
  	 		$(this).closest("ul li:not(ul.children li)").removeClass("active");
  		}
	);
  	
	
	$(".newspage:not(:first)").hide();
	
	$("#news_contents a, #side_news_nav li a").click(function(){
		var title = $(this).attr("title");
		$("#news_main_content").fadeOut('fast', function(){
			$(".newspage").hide();

			$("#newspage_" + title).show("fast", function(){
				$("#news_main_content").fadeIn("fast");
			});
		});		
	});
	
	
	setupthesideNavigationwithIE7Check();
	 
});

/**
 *  Checks to See if Browser is IE7 and Deactivates Accordion because IE sucks
 */
function setupthesideNavigationwithIE7Check(){
	if ($.browser.msie  && parseInt($.browser.version) == 7) {
		
	} else {
		$('.accordion .accordion_side_click').click(function() {
		$(this).next().toggle('fast');
		return false;
		}).next().hide();
	}
}

/**
 * Submits the Email Form
 */

function emailSignUp(){
	
	var theForm = document.getElementById("email_sign_up_form");
	theForm.submit();
	
}




function submitTheTestimonialForm() {
	
	if(validateTheForm("submit_testimonial")){	
		$ajaxPath = "_ajax/submitTestimonialForm.php";
		
		var Testimonial = new Object;
		Testimonial.name = $("input[name='testimonial_name']").val();
		Testimonial.email = $("input[name='testimonial_email']").val();
		Testimonial.content = $("textarea[name='testimonial_content']").val();
		
		JSONString = JSON.stringify(Testimonial);
		$.post($ajaxPath, {submitted_data : JSONString}, function(returned_data){
	
				var returned = JSON.parse(returned_data);
				
				if(returned.error_code == "0"){
					$("#form_error").show().html("<p>Your Testimonial was successfully submitted");
				}
				
		});
	}
}

function submitTheContactForm() {
	
	if(validateTheForm("tearpad_form")){	
		$ajaxPath = "_ajax/submitContactForm.php";
		
		var Testimonial = new Object;
		Testimonial.name = $("input[name='contact_name']").val();
		Testimonial.email = $("input[name='contact_email']").val();
		Testimonial.phone = $("input[name='contact_phone']").val();
		Testimonial.content = $("textarea[name='contact_request']").val();
		
		JSONString = JSON.stringify(Testimonial);
		$.post($ajaxPath, {submitted_data : JSONString}, function(returned_data){
	
				var returned = JSON.parse(returned_data);
				
				if(returned.error_code == "0"){
					$("#form_error").show().html("<p>We have received your contact request.  We will respond to your request asap.  Thank you.");
				}
				
		});
	}
}




function submitTheTearPadRequest(){
	
	if(validateTheForm("tearpad_form")){
	
		$ajaxPath = "_ajax/submitTearpadForm.php";
		
		var TearpadData = new Object;
		TearpadData.name = $("input[name='tearpad_name']").val();
		TearpadData.email = $("input[name='tearpad_email']").val();
		TearpadData.request = $("textarea[name='tearpad_request']").val();
		TearpadData.phone = $("input[name='tearpad_phone']").val();
		
		JSONString = JSON.stringify(TearpadData);
		
		$.post($ajaxPath, {submitted_data : JSONString}, function(returned_data){
	
				var returned = JSON.parse(returned_data);
				
				if(returned.error_code == "0"){
					$("#form_error").show().html("<p>Your WOCN Tearpad Request was successfully submitted");
				}
				
		});
		
	}
}

/**
 * Performs Validation on Form Object passed
 * @param frm - Form Object
 * @return Boolean
 */

function validateTheForm(frm){
	
	var error = false;
	$("#form_error").hide();
	$(".required").removeClass("form_highlight");
	
	var email_regex = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
	
	
	$(".required").each(function(){
		
		var field = $(this);
		if(field.val() == ""){
			field.addClass("form_highlight");
			error = true;
		}	
	});
	
	if(!error)
		return true;
	else{
		$("#form_error").show().html("<p>There was an error submitting your form. Please try the submission again");
		return false;
	}
}
