jQuery(document).ready(function($) {
	
	/* Ajax Contact form validation and submit */
	$('form#contactForm').submit(function() {
		$('form#contactForm .error').remove();
		var hasError = false;
		$('.requiredField').each(function() {
			$(this).parent().parent().find('.field-error').remove();
			if(jQuery.trim($(this).val()) == '') {
				$(this).addClass('input-error');
				hasError = true;
			} else if($(this).hasClass('email')) {
				var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
				if(!emailReg.test(jQuery.trim($(this).val()))) {
					$(this).addClass('input-error');
					hasError = true;
				}
			}
		});
		if(!hasError) {
			$('form#contactForm #submit').fadeOut('normal', function() {
				$(this).parent().append('<p class="highlight">Sending your message...</p>');
			});
			var formInput = $(this).serialize();
			$.ajax({
				type: "POST",
			   	url: $(this).attr('action'),
			   	data: formInput,
			   	success: function(data){
					$('#contact-form').fadeOut("normal", function() {				   
						$(this).before('<p class="highlight"><strong>Thanks!</strong> Your email was successfully sent. I check my email all the time, so I should be in touch soon.</p>');
					});
			   	},
				error: function(data){
					$('#contact-form').fadeOut("normal", function() {
						$(this).before('<p class="highlight"><strong>There was an error sending your message.</strong> Please try again later.</p>');
					});
			   	}
			});
		}
		
		return false;
		
	});
	
	$('.requiredField').blur(function() {
		if(jQuery.trim($(this).val()) != '' && !$(this).hasClass('email')) {
			$(this).removeClass('input-error');
		} 
	});
	
	$('.email').blur(function() {
		var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
		if(emailReg.test(jQuery.trim($(this).val()))) {
			$(this).removeClass('input-error');
		} 
	});

	
	/* Font Replacement */
	Cufon.replace('.menu .nav > li > a', { hover: true });
	Cufon.replace('#slider-full-width h2, #slider h2', { hover: true });
	Cufon.replace('#home-3-sections h3', { hover: true });
	Cufon.replace('#sub-header h2');
	Cufon.replace('.sidebar h3');	
	Cufon.replace('#pro h3, #contact-left h3, #contact-right h3');		
	Cufon.replace('.page h1, .page h2, .page h3, .page h4, .page h5, .page h6', { hover: true });
	Cufon.replace('.page-text h1, .page-text h2, .page-text h3, .page-text h4, .page-text h5, .page-text h6', { hover: true });
	Cufon.replace('#page-media-detail h2');
	Cufon.replace('#footer-widgets h3');
	Cufon.now();
		
		
// Image preloading and display a open icon when mouse hover lightbox images
$('#content img').not("#content #slider-full-width img").each( function() {	
	
	$(this).wrap("<div class='image-holder' />");
	
	var imagePath = $(this).attr('src');
	
	$(this).removeAttr('src');
	$(this)
		.css("opacity", "0.0")
        .load( function() {
			$(this)
				.css("opacity", "0.0")
				.animate({opacity: 1.0}, 500)
				//changed 1 above and .show to fix firefox fade problem
				.show({opacity: 1.0}, 500, function(){
				    $(this).parent().replaceWith($(this));						
					
					var $lightbox = $(this).parent("a[rel^='lightbox']");	
					
					if ($lightbox.length > 0) {
						$(this).css("position", "relative");
						
						var $class = '';
						
						if($lightbox.attr('href').match(/(jpg|gif|jpeg|png|tif)/)) {
							$class = 'lightbox-image';
						} else {
							$class = 'lightbox-video';
						}
							
						if ($(this).length > 0)	{	
							var $span = $("<span class='" + $class + " ie6fix'></span>").appendTo($lightbox);
							
							$(this).bind('mouseenter', function(){
								$height = $(this).height();
								$width = $(this).width();
								$pos =  $(this).position();		
								$span.css({
									height:$height, 
									width:$width, 
									top:$pos.top, 
									left:$pos.left					
								});
							});
						}
					}
					
				});
		}).attr('src', imagePath);
});
	
	
	/* PrettyPhoto */
	$("a[rel^='lightbox']").prettyPhoto({
		theme: 'light_square' /* light_rounded / dark_rounded / light_square / dark_square */		
	});
	
	
	/* Reduce opacity when mouse hover media images */
	//$(".media a img").fadeTo(0, 1.0); // This sets the opacity to 100% when the page loads

	$("a[rel^='lightbox'] img").hover(function(){
		$(this).stop().fadeTo("slow", 0.6); // This sets the opacity to 60% on hover
	},function(){
		$(this).stop().fadeTo("slow", 1.0); // This sets the opacity back to 100% on mouseout
	});
	
	$(".opacify").hover(function(){
		$(this).stop().fadeTo("slow", 0.7); // This sets the opacity to 70% on hover
	},function(){
		$(this).stop().fadeTo("slow", 1.0); // This sets the opacity back to 100% on mouseout
	});	
	
	/* Fix IE7 z-index issue */	
    if($.browser.msie && $.browser.version.substring(0,1) == '7') {
		$(function() {
			var zIndexNumber = 1000;
			$('#header-container div').each(function() {
				$(this).css('zIndex', zIndexNumber);
				zIndexNumber -= 10;
			});
		});
    }	

});
