// Method for fading in out the gallery item titlles
$(document).ready(function(){
	$('div.gallerypost .fadeTarget').mouseenter(function(){
		$('.title', this).stop().fadeTo(200, 1.0);
		$('.titlebg', this).stop().fadeTo(200, .8);
	}).mouseleave(function(){
		$('.title', this).stop().fadeTo(400, 0);
		$('.titlebg', this).stop().fadeTo(200, 0);
	});

});


// Comments form validation
$(function() {
	$('.error').hide();
	$("form#commentform").submit(function() {				
		$('.error').hide();
		var author = $("#author").val();
		if (author == "") {
			$("#authorError").fadeIn(200);
			$("#author").focus();			
			return false;
		}
		var email = $("#email").val();
		if (email == "" || !isValidEmail(email)) {
			$("#emailError").fadeIn(200);
			$("#email").focus();
			return false;
		}
		var comment = $("#comment").val();
		if (comment == "") {
			$("#commentError").fadeIn(200);
			$("#comment").focus();
			return false;
		}
	});
});


// Contact Us form validation and AJAX submit
$(function() {
	$('.error').hide();
	$("form#contactform").submit(function() {				
		$('.error').hide();
		disableForm(true);
		var author = $("#author").val();
		if (author == "") {
			$("#authorError").fadeIn(200);
			$("#inputSubmit").removeAttr("disabled");
			$("#author").focus();
			disableForm(false);
			return false;
		}
		var email = $("#email").val();
		if (email == "" || !isValidEmail(email)) {
			$("#emailError").fadeIn(200);
			$("#inputSubmit").removeAttr("disabled");
			$("#email").focus();
			disableForm(false);
			return false;
		}
		var comment = $("#comment").val();
		if (comment == "") {
			$("#commentError").fadeIn(200);
			$("#inputSubmit").removeAttr("disabled");
			$("#comment").focus();
			disableForm(false);
			return false;
		}
		var url = $("#url").val();   
		
		var dataString = 'author='+author+'&email='+email+'&url='+url+'&comment='+comment;
		
		$.ajax({
			type: "POST",
			url: "/wp/wp-content/themes/SilverlightDesign/lib/contact-submit.php",
			data: dataString,
			success: function() {
				$('#formContent').fadeOut(300, function() {$('#thanksContent').fadeIn(300);});
			}
		});
		return false;
	});
});
function disableForm(s){
	if(s){
		$("#contactSubmit").attr("disabled", "disabled");
		$("#contactSubmit").attr("value", "Sending...");
	}else{
		$("#contactSubmit").removeAttr("disabled");
		$("#contactSubmit").attr("value", "Submit");
	};	
};


function isValidEmail(email, required) {
    if (required==undefined) {
        required=true;
    }
	if (email==undefined){ //A hack because wordpress can sometimes hide the email field for logged in users
		return true;
	}
    if (email==null) {
        if (required) {
            return false;
        }
        return true;
    }
    if (email.length==0) {  
        if (required) {
            return false;
        }
        return true;
    }
    if (! allValidChars(email)) {
        return false;
    }
    if (email.indexOf("@") < 1) {
        return false;
    } else if (email.lastIndexOf(".") <= email.indexOf("@")) {
        return false;
    } else if (email.indexOf("@") == email.length) {
        return false;
    } else if (email.indexOf("..") >=0) {
	return false;
    } else if (email.indexOf(".") == email.length) {
	return false;
    }
    return true;
}
function allValidChars(email) {
  var parsed = true;
  var validchars = "abcdefghijklmnopqrstuvwxyz0123456789@.!#$%&'*+-/=?^_`{|}~";
  for (var i=0; i < email.length; i++) {
    var letter = email.charAt(i).toLowerCase();
    if (validchars.indexOf(letter) != -1)
      continue;
    parsed = false;
    break;
  }
  return parsed;
}