jQuery(document).ready(function($) {
	
	//prevent default on links with # as href
	$('body a[href=#]').click(function(event) { event.preventDefault(); });
	
	$('#nav li ul.hover').each(function() {
		var w = $(this).parent('li').innerWidth();
		$(this).css({'min-width':w});
	});
	
	//dd
	$('#nav ul li').hover(function() {
		$(this).find('ul.hover').show();
	}, function() {
		$(this).find('ul.hover').hide();
	});
	
	function equalHeight(group) {
		tallest = 0;
		group.each(function() {
			thisHeight = $(this).outerHeight(true);
			if(thisHeight > tallest) {
				tallest = thisHeight;
			}
		});
		group.height(tallest+50);
	}
	
	$('#content .text .cols').each(function() {
		equalHeight($(this).find('.col'));
	});
	
	//eq height blog
	equalHeight($('#posts, #sidebar1, #sidebar2'));
	
	//comments scroll
	$('#posts .comments').jScrollPane({scrollbarWidth:11, showArrows:false});
	
	//blog form switch
	$('#posts .post').each(function() {
		var wrap = $(this).find('.post-actions');
		
		//add class to jScrollPane
		wrap.find('.jScrollPaneContainer').addClass('x-tab');
		
		wrap.find('.x-action').click(function(e) {
			e.preventDefault();
			
			var i = wrap.find('.x-action').index($(this));
			var tab = wrap.find('.x-tab').eq(i);
			
			if(tab.css('display') == "none") {
				wrap.find('.x-tab:visible').hide();
				tab.show();
			}
			
		});
		
	});
	
	//auto clear form fields
	$('.i1, input[type=text], textarea').each(function() {
		var d = $(this).val();
		$(this).focus(function() { if($(this).val() == d) { $(this).val(''); } });
		$(this).blur(function() { if($(this).val() == '') { $(this).val(d); } });
	});
		
});

jQuery().ready(function($) {


var global_formError = false;

// RX_formValidate : 12/08/09
// - simple form validator
jQuery.fn.RX_formValidate = function() {
	function validateNoempty(text) {
    if (!text || text.length < 3) { return false;	}
		return true;
	}
	//

	function validateText(text) {
    if (!validateNoempty(text)) { return false; }
		var re_text = /[^A-Za-z0-9\.,\s]/;
		if (text.match(re_text) != null ) { return false; }
		return true;
	}
	//

	function validateEmail(email) {
		if (!validateNoempty(email)) { return false; }

		var splitted = email.match("^(.+)@(.+)$");
		if (splitted == null) return false;
		if (splitted[1] != null ) {
			var regexp_user=/^\"?[\w-_\.]*\"?$/;
			if (splitted[1].match(regexp_user) == null) { return false; }
		}

		if(splitted[2] != null) {
			var regexp_domain=/^[\w-\.]*\.[A-Za-z]{2,4}$/;
			if (splitted[2].match(regexp_domain) == null) {
				var regexp_ip =/^\[\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\]$/;
				if (splitted[2].match(regexp_ip) == null) { return false; }
			}
			return true;
		}
		return false;
	}
	//

	function validateSelect(select) {
		if (select != -255) {
			return true;
		}
		return false;
	}
	//

	function validateInteger(number) {
		var numRegExp  = /\d+$/;
	  return numRegExp.test(number);
	}
	//

	return this.each(function() {
		$(this).click(function(e) {
			e.preventDefault();
			error = false;

			function set_error(node) {
				node.animate( { opacity: 0.2 }, 175 ).animate( { opacity: 1 }, 150 );
				error = true;
			}

			function set_errorSelect(node) {
				if (node.parents('.rx-js-select').hasClass('rx-select-on') ) {
					node.parents('.rx-select-on').animate( { opacity: 0.2 }, 175 ).animate( { opacity: 1 }, 150 );
					error = true;
				} else {
					set_error(node);
				}
			}

			$(this).parents('form').find('.v-noempty, .v-text, .v-email, .v-select, .v-integer').each( function() {
				if ($(this).hasClass('v-noempty')) {
					if (!validateNoempty($(this).val())) {
						set_error($(this));
					}
				} else if ($(this).hasClass('v-text')) {
					if (!validateText($(this).val())) {
						set_error($(this));
					}
				} else if ($(this).hasClass('v-email')) {
					if (!validateEmail($(this).val())) {
						set_error($(this));
					}
				} else if ($(this).hasClass('v-select')) {
					if (!validateSelect($(this).val())) {
						set_errorSelect($(this));
					}
				} else if ($(this).hasClass('v-integer')) {
					if (!validateInteger($(this).val())) {
						set_error($(this));
					}
				}
			});

			global_formError = error;

			if (!error) {
				
				//ajax ??
				var tab = $(this).closest('.x-tab');
				
				if(tab.hasClass('comment-form')) {
					var form = tab.find('form');
					
					$.ajax({
						url:global_ajaxurl,
						type:'POST',
						data:form.serialize(),
						success:function(data) {
							
							var comments = tab.closest('.post-actions').find('.comments');
							var author = form.find('input[name=author]').val();
							var comment = form.find('textarea').val();
							
							var chtml = '<ul><li><strong>'+author+'</strong>: '+comment+'</li></ul>';
							
							if(comments.length == 0) {
								comments.html(chtml);
							}else {
								comments.find('ul').append(chtml);
							}
							
							//clear form
							form.find('input[type=text], textarea').val('');
							
							tab.closest('.post-actions').find('.x-action:first').click();
							
						}
					});
					
				}else {
					$(this).parents('form').submit();
				}
			}

		});

	});
	//
}
//




	$('a.x-submit').RX_formValidate();

});

