$(document).ready(function() {
	
	//External links 
	$(function() {
		$('a[rel=external]').attr('target', 'blank');
		$('a[rel=NOFOLLOW]').attr('target', 'blank');
	});
	
	//Add slash to post_categories last ul
	$("ul.sub").css('background-image','url(http://cssgem.com/wp-content/themes/cssgem/images/slash.gif)');
	$("ul.post_categories li > ul:last").css('background-image','none'); 
	
	//Sidebar Tabs
	$('#gem_tabs > ul').tabs({ 
		fx: { height: 'toggle', opacity: 'toggle' } 
	});
	
	//Fade In Preview Image	
	$('.top10').css({"opacity": "0"}).animate({"opacity": "1"}, 400).fadeIn(400);
	$('.thumbnail').fadeIn(400);
	
	//Profile Info Preview
	$('.preview_inner').hover(function(){
		$(this).find('.hidden_block').css({opacity: '0'}).animate({top:'0px',opacity:'1'},{queue:false,duration:400});
	}, function(){
		$(this).find('.hidden_block').animate({top:'173px',opacity:'0'},{queue:false,duration:400});
	});
	
	//Footer Popup 
	$(function () {
		$('#footer').each(function () {
			var distance = 10;
			var time = 250;
			var hideDelay = 200;
	
			var hideDelayTimer = null;
	
			var beingShown = false;
			var shown = false;
			var trigger = $('.trigger', this);
			var info = $('.popup', this).css('opacity', 0);
	
	
			$([trigger.get(0), info.get(0)]).mouseover(function () {
				if (hideDelayTimer) clearTimeout(hideDelayTimer);
				if (beingShown || shown) {
					// don't trigger the animation again
					return;
				} else {
					// reset position of info box
					beingShown = true;
	
					info.css({
						top: -30,
						left: 325,
						display: 'block'
					}).animate({
						top: '-=' + distance + 'px',
						opacity: 1
					}, time, 'swing', function() {
						beingShown = false;
						shown = true;
					});
				}
	
				return false;
			}).mouseout(function () {
				if (hideDelayTimer) clearTimeout(hideDelayTimer);
				hideDelayTimer = setTimeout(function () {
					hideDelayTimer = null;
					info.animate({
						top: '-=' + distance + 'px',
						opacity: 0
					}, time, 'swing', function () {
						shown = false;
						info.css('display', 'none');
					});
	
				}, hideDelay);
	
				return false;
			});
		});
	});
	
	// Comments Form
	$('#commentform input, #commentform textarea').each(function () {
		if ($(this).val() == '') {
			$(this).val($(this).attr('title'));
		}
	}).focus(function () {
		$(this).removeClass('inputerror');
		if ($(this).val() == $(this).attr('title')) {
			$(this).val('');
		}
	}).blur(function () {
		if ($(this).val() == '') {
			$(this).val($(this).attr('title'));
		}
	});
	$('#commentform').submit(function () {
		$('#submiterror').remove();
		var errors = 0;
		$(this).find('textarea, input').each(function () {
			if ($(this).val() == $(this).attr('title')) {
				if ($(this).attr('name') != 'comment_post_ID') {
					$(this).val('');
				}
			}
			if ($(this).hasClass('required') && $(this).val() == '') {
				$(this).addClass('inputerror');
				errors++;
			}
		});

		if (errors > 0) {
			$(this).find('textarea, input').each(function () {
				if ($(this).val() == '') {
					$(this).val($(this).attr('title'));
				}
			});
			$(this).prepend('<p id="submiterror">Please complete the highlighted fields.</p>');
			return false;
		}
		return true;
	});
	
});
