$(document).ready(function() {
	
	//Remove p tag margins from images.
	$('p a:has(img)').addClass('imgC');
	//$('a:has(img)').css('border-bottom' , 'none');

	$('a[rel="external"]').attr('target', '_blank');
});

Cufon.replace('.post.single h1, .post h2, h2.archives, .post h3, .post-detail h3, #comments h2, #menu a, .portfolio h2', { hover: true, hoverables: { li: true, a: true } });

/*----------------------------------------------------------------
	//Show/hide labels on forms
---------------------------------------------------------------- */

$(function(){
	$('#search-form input').focus(function(){
		$('label[for=' + $(this).attr('id') + ']').hide();
	});
	$('#search-form input').each(function(ele){
		if($(this).val().length > 0)
			$('label[for=' + $(this).attr('id') + ']').css({ display: "none" });
	});
	$('#search-form input').blur(function(){
		if($(this).val().length == 0)
			$('label[for=' + $(this).attr('id') + ']').show();
	});
});


/*----------------------------------------------------------------
	//Show image portfolio hover
---------------------------------------------------------------- */

$(function(){
	$('.image-container a.image').hover(function(){
		if ($.browser.msie && $.browser.version > 6 ) {
			$(this).parent().find('.image-hover').show();
		} else {
			$(this).parent().find('.image-hover').show();
		}
	}, function(){
		if ($.browser.msie && $.browser.version > 6 ) {
			$(this).parent().find('.image-hover').hide();
		} else {
			$(this).parent().find('.image-hover').hide();
		}
	});
});


/*----------------------------------------------------------------
	//Set body / content height
---------------------------------------------------------------- */

$(function(){
	var bodyHeight = $(this).height();
	$('#content').css('min-height',bodyHeight-70)
});


	
/*----------------------------------------------------------------
	//validate comments
---------------------------------------------------------------- */


function isValidEmailAddress(emailAddress) {
		var pattern = new RegExp(/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i);
		return pattern.test(emailAddress);
	}

$(function() {
	$("#commentform").submit(function() {
		var name = $("#author").val();
		var email = $("#email").val();
		var website = $("#url").val();
		var comment = $("#comment").val();
		
		if(name=='' || email=='' || comment=='' || !isValidEmailAddress(email)) {
			$('#commentform p').removeClass('error');
			$('p.error2').hide();
			$('p.error1').fadeIn();
			
			if (name=='') {  
				$("#author").parent().addClass('error'); 
			}
			if (email=='' || !isValidEmailAddress(email)) {  
				$("#email").parent().addClass('error'); 
			}
			if (comment=='') {  
				$("#comment").parent().addClass('error'); 
			}
			if(!isValidEmailAddress(email) && name!='' && comment!='') {
				$('p.error1').hide();
				$('p.error2').fadeIn();
				$("#email").parent().addClass('error'); 
			}
			return false;
		} else {
			return true;
		}
	}); 
});



/*----------------------------------------------------------------
	Remove error state if field full
---------------------------------------------------------------- */

$(function(){
	$('#commentform input').blur(function(){
		if($(this).val().length > 0)
			$(this).parent().removeClass('error');
	});
	$('#commentform textarea').blur(function(){
		if($(this).val().length > 0)
			$(this).parent().removeClass('error');
	});

});


/*----------------------------------------------------------------
	jQuery Quicksand
---------------------------------------------------------------- */

 $(function() {
	
	var overlay = $('<div>').attr('id', 'overlay-archive'),
        loader = $('<div>').attr('id', 'loader');
        
	$('body').append(overlay).append(loader);
	overlay.css({height: $('#content').height() + 'px'});
	
	Cufon.refresh();
	
	$('.article-count').text($('#article-count').text());
	
	var loadTimer;
	function showLoader() {
		clearTimeout(loadTimer);
		loadTimer = setTimeout(function() {
			loader.show();
			overlay.show();
		}, 100);
	};
	function hideLoader() {
		clearTimeout(loadTimer);
		loader.fadeOut('fast');
		//overlay.fadeOut('fast');
	};
	
	function stopEverything() {
		overlay.fadeOut();
		return false;
	};
	overlay.click(stopEverything);

	$('#filter-articles a.button').click(function(e) {
	
		showLoader();
        overlay.fadeIn('fast');
					
		var title = $(this).text();
		var title = title.toLowerCase();

		$('#filter-articles h3').html('<span>Archives</span> ('+title+')');
		Cufon.refresh();
		
		$.get( $(this).attr('rel'), function(data) {
			hideLoader();
			overlay.hide();
			$('#archive-articles').quicksand( $(data).find('li'), 	{ 
				enhancement: function() {
					Cufon.refresh();
					$('.article-count').text($('#article-count').text());
				}
			});
		});	
		e.preventDefault();	
	});
});

//Hover active states
 $(function() {
	$('#filter').hover(function(){
		$('#filter li:first-child').addClass('active');
	}, function(){
		$('#filter li:first-child').removeClass('active');
	});
});


