$(document).ready(function() {
	
						   
	/* MEGA MENU DROPDOWN
	-------------------------------------------------------------- */
	
	function megaHoverOver(){
		$(this).find(".dropdown").stop().fadeTo('fast', 1).show();
	}
	
	function megaHoverOut(){ 
	  $(this).find(".dropdown").stop().fadeTo('fast', 0, function() {
		  $(this).hide(); 
	  });
	}
 
	var config = {    
		 sensitivity: 2, 	  // number = sensitivity threshold (must be 1 or higher)    
		 interval: 0, 		  // number = milliseconds for onMouseOver polling interval    
		 over: megaHoverOver, // function = onMouseOver callback (REQUIRED)    
		 timeout: 90, 		  // number = milliseconds delay before onMouseOut    
		 out: megaHoverOut 	  // function = onMouseOut callback (REQUIRED)    
	};
 
	$("#mainmenu ul li .dropdown").css({'opacity':'0'});
	$("#mainmenu ul li").hoverIntent(config);
	
	
	
	/* OPACIFY
	-------------------------------------------------------------- */
	
	$(".opacify").fadeTo(0.85, 1);
	$(".opacify").hover(
		function () {
			$(this).stop().fadeTo("normal", 0.85);
		},
		function () {
			$(this).stop().fadeTo("normal", 1);
		}
	);
	
	/* JQUERY DELAY PLUGIN
	-------------------------------------------------------------- */
	$.fn.delay = function(time, callback){
    	// Empty function:
    	jQuery.fx.step.delay = function(){};
		
    	// Return meaningless animation, (will be added to queue)
    	return this.animate({delay:1}, time, callback);
	}
  	
	/* HOVER OVERLY ON PORTFOLIO THUMBS
	-------------------------------------------------------------- */

	$(".show").fadeTo(250, 0);
	$(".show").hover(
		function(){
			$(this).fadeTo(250, 1.0);
		},
		function(){
			$(this).fadeTo(250, 0);
		}
	);


	/* MAKE PORTFOLIO THUMB CLICKABLE
	-------------------------------------------------------------- */

	$(".portfolio_list li").click(function(){
		url = $(this).find("a").attr("href");
		window.location = url;
		return false;
	});
	
	/* MAKE PORTFOLIO PREV / NEXT BUTTONS CLICKABLE
	-------------------------------------------------------------- */

	$("#controlls .client_prev").click(function(){
		url = $(this).find("a").attr("href");
		window.location = url;
		return false;
	});
	
	$("#controlls .client_next").click(function(){
		url = $(this).find("a").attr("href");
		window.location = url;
		return false;
	});
	
	/* MAKE CHARACTER RISE AND FALL ON HOVER
	-------------------------------------------------------------- */
	
	$("#scroll_up").hover(
  		function () {
			$(this).animate({bottom:'-5px'},{queue:false,duration:500});
  	},
  		function () {
    		$(this).animate({bottom:'-32px'},{queue:false,duration:500});
  	});
	
	/* TOOLTIP PLUGIN. (TIP TIP)
	-------------------------------------------------------------- */
	
	$(".tooltip").tipTip();
	
	
	/* SCROLL TO THE TOP OF THE PAGE
	-------------------------------------------------------------- */
	
	$('#scroll_up').click(
		function(){
	 		$('html, body').animate({
				scrollTop:0
			}, {
    			duration: 2000,
    			specialEasing: {
      				width: 'linear',
      				height: 'easeOutBounce'
    			}
			});
	        return false;
	    }
	);
	
	
	
	/* CONTACT FORM FOCUS / BLUR TEXT SWAP
	-------------------------------------------------------------- */
	
  	var default_values = new Array();
	
  	$(".contact_form input, .contact_form textarea").focus(function() {
	  
		if (!default_values[this.id]) 
		{
			default_values[this.id] = this.value;
		}
	
		if (this.value == default_values[this.id]) 
		{
	  		this.value = '';
		}
	
		$(this).blur(function() {
			if (this.value == '') 
			{
				this.value = default_values[this.id];
			}
		});
	
  	});
	
	/* AJAX SUBMIT CONTACT FORM
	-------------------------------------------------------------- */
	
	$("[name=submit]").click(    
		function() 
		{ 				    
			 $.ajax({
			   type: "POST",
			   cache: false,
			   url: "/includes/sendMail_contact.php",
			   data: {from_name: 	$("[name=from_name]").val(),
			   		  from_phone: 	$("[name=from_phone]").val(),
					  from_email: 	$("[name=from_email]").val(),
					  from_msg: 	$("[name=from_msg]").val()},
			   success: function(data){
				   $('#err_name').empty();
				   $('#err_email').empty();
				   $('#err_msg').empty();
				   
				   //Print error message json data
				   
				   var obj = jQuery.parseJSON(data);
				   
				   if (obj != null)
				   {
						if (obj.err_name != "")
						{
							$('#err_name').append(obj.err_name).html(); 
							$('#from_name').addClass("error");
						}
						
						if (obj.err_mail != "")
						{
							$('#err_email').append(obj.err_mail).html(); 
							$('#from_email').addClass("error");
						}
						
						if (obj.err_msg != "")
						{
							$('#err_msg').append(obj.err_msg).html();
							$('#from_msg').addClass("error");
						}
				   } else {
					   
						/* MODAY OVERLAY POPUP WHEN EMAIL HAS BEEN SENT
						-------------------------------------------------------------- */
						$(".modal").overlay({
							
							// some mask tweaks suitable for modal dialogs
							mask: {
								color: '#fff',
								loadSpeed: 200,
								opacity: 1
								
							}, api:true
								
								
						}).load();  
						
				   }
			   },
			   error: function(){
					alert("failed");   
			   }
		});
	});
	
	/* AJAX ON THE FLY VALIDATION ON CONTACT FORM
	-------------------------------------------------------------- */
	
	$(".contact_form #from_name").blur(function() 
    { 
        var name_length; 
 
        name_length = $("#from_name").val().length; 
		
        $("#err_name").empty(); 
 
        if (name_length == 0 )
		{
            $("#err_name").append("Name is required");
			$(this).addClass("error");
		} else {
			$(this).removeClass("error");	
		}
    }); 
	
	$(".contact_form #from_msg").blur(function() 
    { 
        var msg_length; 
 
        msg_length = $("#from_msg").val().length;
		
        $("#err_msg").empty();
 
        if (msg_length == 0 )
		{
            $("#err_msg").append("Message is required");
			$(this).addClass("error");
		} else {
			$(this).removeClass("error");	
		}
    }); 
	
	$(".contact_form #from_email").blur(function() 
    { 
        var regex = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9-]+\.[a-zA-Z.]{2,5}$/;
		$("#err_email").empty();
		
   		if ( !regex.test($(this).val()) )
		{
			$("#err_email").append("Invalid Email Address");
			
			$(this).addClass("error");
			
		} else {
			$(this).removeClass("error");
		}
		
    }); 
	
	
	
	
	$('#peek').delay(15000, function(){
		$(this).animate({top:'0px',right:'0px'},{queue:false,duration:3000});
	});
	
	
});


/* MAKE MASCOT APPEAR AND DISAPPEAR DEPENDING ON PAGE SCROLL PERCENTAGE
-------------------------------------------------------------- */

$(document).scroll(function(e){
 
    // grab the scroll amount and the window height
    var scrollAmount = $(window).scrollTop();
    var documentHeight = $(document).height();
 
    // calculate the percentage the user has scrolled down the page
    var scrollPercent = (scrollAmount / documentHeight) * 100;
 
    if(scrollPercent > 6) {
        $('#scroll_up').animate({bottom:'-32px'},{queue:false,duration:500});
    }
	
	if(scrollPercent < 5) {
		$('#scroll_up').animate({bottom:'-108px'},{queue:false,duration:500});
	}
 
});




