$(document).ready(function() {
	//  Main Nav
	function megaHoverOver() {
		$(this).find(".sub").stop().fadeTo('fast', .90).show();
		//Calculate width of all ul
		(function($) { 
			jQuery.fn.calcSubWidth = function() {
				rowWidth = 0;
				//Calculate row
				$(this).find("ul").each(function() {
					rowWidth += $(this).width(); 
				});	
			};
		})(jQuery); 
		if ( $(this).find(".row").length > 0 ) { //If row exists...
			var biggestRow = 0;	
			//Calculate each row
			$(this).find(".row").each(function() {
				$(this).calcSubWidth();
				//Find biggest row
				if(rowWidth > biggestRow) {
					biggestRow = rowWidth;
				}
			});
			//Set width
			$(this).find(".sub").css({'width' :biggestRow});
			$(this).find(".row:last").css({'margin':'0'});
		} else { //If row does not exist...
			$(this).calcSubWidth();
			//Set Width
			$(this).find(".sub").css({'width' : rowWidth});
		}
	}
	function megaHoverOut(){ 
	  $(this).find(".sub").stop().fadeTo('fast', 0, function() {
		  $(this).hide(); 
	  });
	}
	var config = {    
		 sensitivity: 1000, // number = sensitivity threshold (must be 1 or higher)    
		 interval: 1, // number = milliseconds for onMouseOver polling interval    
		 over: megaHoverOver, // function = onMouseOver callback (REQUIRED)    
		 timeout: 150, // number = milliseconds delay before onMouseOut    
		 out: megaHoverOut // function = onMouseOut callback (REQUIRED)    
	};
	$("ul#topnavSkinny li .sub").css({'opacity':'0'});
	$("ul#topnavSkinny li").hoverIntent(config);
	
	// generate URL link for email
	var pageURL = document.location.href;
	$('#emailLink').val(pageURL);
});


$(function() {
	// // Social - show and hide
	$("#sendLinkBut").click(function () {
		$("div#Email").slideToggle(150);
	});
	$("#sendTweetBut").click(function () {
		$("div#Twitter").slideToggle(150);
	});
	$("#sendFacebookBut").click(function () {
		$("div#Facebook").slideToggle(150);
	});
	
	$('.social').click(function() {
		var id = $(this).text();
		var $content = $('#' + id + ':not(:visible)');
		if ($('.drop').length === 0) {
			showContent($content)
		}
		else {
			$('.drop').slideUp(150, function() {
				showContent($content)
			});
		}
	});

	function showContent(content) {
		content.slideDown(150);
		$('.drop').removeClass('drop');
		content.addClass('drop');
	}

	
	//if email send button is clicked
	$('#sendLinkSubmit').click(function () {
		// validate form before processing.
		if (sendEmailValidate(document.getElementById('submitSendEmail'))) {
			//disabled all the text fields
			$('.text').attr('disabled','true');

			//show the loading sign
			$('#sendingMessage').show();

			var emailFrom = $('#emailFrom').val();
			var emailTo = $('#emailTo').val();
			var emailSubject = $('#emailSubject').val();
			var emailBody = $('#emailBody').val();
			var emailLink = $('#emailLink').val();

			var dataString = 'emailFrom=' + emailFrom + '&emailTo=' + emailTo + '&emailSubject=' + emailSubject + '&emailBody=' + emailBody + '&emailLink=' + emailLink;
			// alert(dataString); return false;

			//start the ajax

			$.ajax({
				type: 'POST',
				url: 'http://www.pixologic.com/navigation/sendEmail.php',
				data: dataString,
				cache: false,

				//success
				success: function (html) {
					if (html==1) {
						$('#submitSendEmail').fadeOut('slow');
						$('#done').fadeIn('slow');
					//if process.php returned 0/false (send mail failed)
					} else alert('Sorry, unexpected error. Please try again later.');
				}
			});
			//cancel the submit button default behaviours
			return false;
		}
	});
});

function clickClear(thisfield, defaulttext) {
	if (thisfield.value == defaulttext) {
		thisfield.value = "";
	}
}
function clickRecall(thisfield, defaulttext) {
	if (thisfield.value == "") {
		thisfield.value = defaulttext;
	}
}
