/* 
megalodondesign.com functions
Author: Ben Fraser ben@megalodondesign.com
Date: April 2011
*/

//ALL FUNCTIONS
	// Turn red function
	function	redFunction	(value)	{
			$(value).addClass("red");
			$(value).removeClass("green");
			$(value).prev().addClass("red");
			$(value).prev().removeClass("green");
			$(value).next().html("<img src='images/red-cross.png' alt='cross' class='validation_icon'>");
	}
	
	// Turn green function
	function	greenFunction	(value)	{
			$(value).addClass("green");
			$(value).removeClass("red");
			$(value).prev().addClass("green");
			$(value).prev().removeClass("red");
			$(value).next().html("<img src='images/green-tick.png' alt='tick' class='validation_icon'>");
	}
	
	// Validate name
	function	validateName	(value)	{
		if($(value).val().length == "0")	{
				redFunction (value);
				$(value).prev().text("Name is empty");
			} 	else if ($(value).val().length > "50")	{
							redFunction (value);
							$(value).prev().text("Name is too long");
						} 	else	{
								greenFunction (value);
								$(value).prev().text("Name");
							}
		}
						
	// Email validation function	
	function emailValidation	(value) {
		if($(value).val().length == 0)	{
						redFunction (value);
						$(value).prev().text("Email is empty");
				} else if  ($(value).val().length > 200)	{
								redFunction (value);
								$(value).prev().text("Email is too long");
							} else if ($(value).val().indexOf(" ") >= 0)	{
									redFunction (value);
									$(value).prev().text("No spaces allowed!");
								}	else if ($("#email").val().indexOf("@") == -1)	{
										redFunction (value);
										$(value).prev().text("Email needs @");
									}	else if ($(value).val().indexOf("." , $("#email").val().indexOf("@")) == -1) 	{
											redFunction (value);
											$(value).prev().text("Email needs a valid domain name");
										} else {
												greenFunction (value);
												$(value).prev().text("Email");
										}
	}
	
	//Selection validation function
	function	validateSelect (value)	{
		if($(value).val() == "select")	{
				redFunction (value);
				$(value).prev().text("Select an option");
			} 	else 	{
					greenFunction (value);
					$(value).prev().text("Regarding:");
				}
		}
		
	//Move form up and down function if a label has 'red' class (ie is invalid)
	function moveForm () {	
		if ($("label").hasClass("red")) {
			$("form").css("position","relative");
			$("form").animate({"top":"-20px"},20, function()	{
				$("form").animate({"top":"0px"},20, function () {
					$("form").animate({"top":"-20px"},20, function() {
						$("form").animate({"top":"0px"},20);
					});
				});
			});
			return true // this value is used when calling the functions to test whther or not the form has validated
		} 
	}



$(document).ready(function(){
	
//WRAPPER FADE IN	
	$("#wrapper").hide().fadeIn(1000);


// LOGO COLOR CHANGE
	$("a.header").show();
	
	$("a.header").hover(function(){
		$("a.header img.mouth_red").fadeIn(300); 
		},	function() {
			$("a.header img.mouth_red").fadeOut(300); 
	});
	
// MENU COLOR CHANGE
	$("ul.menu a").not(".active").hover(function() {  
		$(this).stop().animate({ "color": "#CC0000"}, 300);  
	}, function() {  
		$(this).stop().animate({ "color": "#CCCCCC" }, 300);  
	}); 	
	
	
// Gallery opacity animate
	$("#content_web a.web_link dl").mouseover(function()	{
		$(this).children("dd").stop().animate({"opacity":"1"}, 400);
		$(this).children("dt").stop().animate({"opacity":"0.1"}, 400);
	}).mouseout(function()	{
		$(this).children("dd").stop().animate({"opacity":"0"}, 400);
		$(this).children("dt").stop().animate({"opacity":"1"}, 400);
	});
	
// WEB  GRAPHICS PAGE GALLERY FUNCTIONS
	$("a.close_web").click(function()	{
		$("div.web_info").hide();
		$("a.web_link").fadeIn();
		return false;
	});
	
	$("a.web_link").click(function()	{
		$("a.web_link").hide();
		$(this).next().fadeIn();
		return false;
	});
	
	$("a.next_web").click(function()	{
		$("div.web_info:visible").nextAll("div.web_info:first").fadeIn();
		$("div.web_info:visible").first().hide();
		return false;
	});
	
	$("a.prev_web").click(function()	{
		$("div.web_info:visible").prevAll("div.web_info:first").fadeIn();
		$("div.web_info:visible").last().hide();
		return false;
	})
	
	$(".prev_web:first").addClass("first");
	$(".next_web:last").addClass("last");

	$("a.nav").hover(function() {
	$(this).children(".off").hide();
	$(this).children(".on").show();
	},	function() {
		$(this).children(".off").show();
		$(this).children(".on").hide();
	});	
	

// FONT CHANGE
	$("a.fontsize_large").show();
	
	$("a.fontsize_large").click(function(){
		$("body").css("font-size", "15px");
		$("a.fontsize_large").hide(); 
		$("a.fontsize_small").show(); 
		return false;
	});
	
	$("a.fontsize_small").click(function(){
		$("body").css("font-size", "12px");
		$("a.fontsize_large").show(); 
		$("a.fontsize_small").hide(); 
		return false;
	});
	

// CONTENT HOME VIEW MORE / LESS PROFILE
	$("#profile").hide();
	$("a.more").show();
	$("h3.nojs").hide();
	
	$("a.more").click(function() {
		$("#profile").slideDown('Fast');
		$("a.less").show();
		$(this).hide();
		$("#skills").hide();
		$("#testimonials").hide();
		$("a.more-skills").show();
		$("a.less-skills").hide();
		$("a.more-test").show();
		$("a.less-test").hide();
		return false;
	});
	
	$("a.less").click(function() {
		$("#profile").slideUp();
		$("a.more").show();
		$("a.less").hide();
		return false;
	});
	
	
// CONTENT HOME VIEW MORE / LESS SKILLS	
	$("#skills").hide();
	$("a.more-skills").show();
	
	$("a.more-skills").click(function() {
		$("#skills").slideDown('Fast');
		$("a.less-skills").show();
		$(this).hide();
		$("#profile").hide();
		$("#testimonials").hide();
		$("a.more").show();
		$("a.less").hide();
		$("a.more-test").show();
		$("a.less-test").hide();
		return false;
	});
	
	$("a.less-skills").click(function() {
		$("#skills").slideUp();
		$("a.more-skills").show();
		$("a.less-skills").hide();
		return false;
	});
	
	
// CONTENT HOME VIEW MORE / LESS TESTIMONIALS	
	$("#testimonials").hide();
	$("a.more-test").show();
	
	$("a.more-test").click(function() {
		$("#testimonials").slideDown('Fast');
		$("a.less-test").show();
		$(this).hide();
		$("#profile").hide();
		$("#skills").hide();
		$("a.more").show();
		$("a.less").hide();
		$("a.more-skills").show();
		$("a.less-skills").hide();
		return false;
	});
	
	$("a.less-test").click(function() {
		$("#testimonials").slideUp();
		$("a.more-test").show();
		$("a.less-test").hide();
		return false;
	});


// EMAIL FORM VALIDATION
//Validate all clicking submit  
		$("#submit").click(function()	{
			validateName("#name");
			emailValidation("#email");
			validateSelect("#enquiry");
			moveForm();
			if (moveForm())	{
				return false //default action is prevented if moveForm returns true, ie the form has not validated
			}
			$("form").submit(); //otherwise the form will send
		});
	
		
//Validation on respective blur   
		$("#name").blur(function()	{
			validateName("#name");
		});
			
		$("#email").blur(function()	{
			emailValidation("#email");
		});
		
		$("#enquiry").blur(function()	{
			validateSelect("#enquiry");
		});
		

});
		   






