// jQuery enhanced menu
function MainMenu(){
	this.navLi = $('#nav li').children('ul').hide().end();

	$('#nav li li:has(ul)').append('<span class="hasChildren"></span>');
	
	this.navLi.hover(function() {
		// Mouse over
		$(this).find('> ul').stop(true, true).slideDown('slow', 'easeOutBounce');
		}, function() {
		// Mouse out
		$(this).find('> ul').stop(true, true).hide(); 		
	});
}



// On page load
$(document).ready(function() {
	// Contact form validation
	$('#submit-button').click(function(){
		// Getting variables
		var input_name = $('input:eq(0)').val(),
			input_email = $('input:eq(1)').val(),
			input_website = $('input:eq(2)').val(),
			input_message = $('textarea').val(),
			
			ajax_loader = '<img src="img/preloader.gif" alt="Loading..." />';
			
		// Hide any previous text
		$("#negativeResponse").hide();
		
		// Show the loader
		$("#submitLoader").html(ajax_loader);
		
		$.post("sendMail.php", {name: input_name, email: input_email, website: input_website, message: input_message}, function(data){
			// Hide the loader

			$("#submitLoader img").fadeOut(function(){
				if(data=='<h2 class="dark">Your message has been sent</h2><p>Your message has been successfully sent. We will get back to you as soon as we can !<br /><br />Thanks!</p>') {
					$('#negativeResponse').hide();
					
					$('#contact-form fieldset').fadeOut(function(){
						$('#positiveResponse').html(data);
						$('#positiveResponse').fadeIn();
					});
				}
				else {
					$('#negativeResponse').html(data);
					$('#negativeResponse').fadeIn();
				}
			});
		});
		
		return false;
	});



	// Blog comment form validation
	$('#comment-submit-button').click(function(){
		// Getting variables
		var input_name = $('input:eq(0)').val(),
			input_email = $('input:eq(1)').val(),
			input_website = $('input:eq(2)').val(),
			input_comment = $('textarea').val(),
			
			ajax_loader = '<img src="img/preloader.gif" alt="Loading..." />';
			
		// Hide any previous text
		$("#positiveResponse").hide();
		$("#negativeResponse").hide();
		
		// Show the loader
		$("#submitLoader").html(ajax_loader);
		
		$.post("submitComment.php", {name: input_name, email: input_email, website: input_website, comment: input_comment}, function(data){
			// Hide the loader

			$("#submitLoader img").fadeOut(function(){
				if(data=='<p>Your comment has been submitted and it might be waiting for approval before it shows up in the comment list.<br /><br />Thanks!</p>') {
					$('#negativeResponse').hide();
					
					$('#positiveResponse').html(data);
					$('#positiveResponse').fadeIn();
					
					// Reset the form
					$('#blog-comment-form')[0].reset();
				}
				else {
					$('#negativeResponse').html(data);
					$('#negativeResponse').fadeIn();
				}
			});
		});
		
		return false;
	});



	// Rollover function for the form submit element
    $("input:image[src$=img/subscribeButton.png]").hover(function() {
        $(this).attr("src", "img/subscribeButtonOver.png")
    }, function(){
        $(this).attr("src", "img/subscribeButton.png")    
    });

    $("input:image[src$=img/submitButton.png]").hover(function() {
        $(this).attr("src", "img/submitButtonOver.png")
    }, function(){
        $(this).attr("src", "img/submitButton.png")    
    });



	// Tipsy Init
	$(function() {
		$('ul.socialNav li a').tipsy({fade: true, gravity: 'n'});
		$('div#contactInformation a').tipsy({fade: true, gravity: 'n'});
		$('div#sharePost ul li a').tipsy({fade: true, gravity: 'n'});
	});
	
	
	
	// Activate the tabs
	$(".tabContent").hide(); //Hide all content
	$("ul.tabs li:first").addClass("active").show(); //Activate first tab
	$(".tabContent:first").show(); //Show first tab content



	//On click event
	$("ul.tabs li").click(function() {

		$("ul.tabs li").removeClass("active"); //Remove any "active" class
		$(this).addClass("active"); //Add "active" class to selected tab
		$(".tabContent").hide(); //Hide all tab content

		var activeTab = $(this).find("a").attr("href"); //Find the href attribute value to identify the active tab + content
		$(activeTab).fadeIn(); //Fade in the active ID content
		return false;
	});
	
	
	
	// Call the menu function
	MainMenu();
	
	
	
	// Activate image preloading
	$(".preloadImg").preloader();
});
