jQuery(document).ready(function($) {
	
	//prevent default on links with # as href
	$('body a[href=#]').click(function(event) { event.preventDefault(); });
	
	//make columns same height
	function equalHeight(group) {
		tallest = 0;
		group.each(function() {
			thisHeight = $(this).innerHeight();
			if(thisHeight > tallest) {
				tallest = thisHeight;
			}
		});
		group.height(tallest);
	}
	equalHeight($('#body .eq1'));
	
	//scroll horz-box
	$('#body .horz-scroll-box').each(function() {
		var wrap = $(this);
		var prev = wrap.find('.prev'), next = wrap.find('.next');
		wrap.find('.hide').serialScroll({
										duration:1000,
										items:'li',
										prev:prev,
										next:next,
										cycle:false,
										axis:'x',
										start:0,
										easing:'easeOutQuart'
										});
	});
	
	//switch tabs
	$('#body .tabs-wrap').each(function() {
		var wrap = $(this);
		wrap.find('.tabs li:first').addClass('active');
		wrap.find('.tab:first').fadeIn(500);
		wrap.find('.tabs li').click(function(e) {
			//
			if(!$(this).hasClass('active')) {
				$(this).parent().find('li.active').removeClass('active');
				$(this).addClass('active');
				var i = $(this).closest('ul').find('li').index($(this));
				wrap.find('.tab:visible').fadeOut(500);
				wrap.find('.tab').eq(i).fadeIn(500);
			}
		});
	});
	
	//ad rotator
	$('#body .ad').cycle({timeout:6000, speed:1000, fx:'fade'});
	$('#head .ad').cycle({timeout:5000, speed:1500, fx:'fade'});
	
	/* ----------------------------------------------------- */
	
	//form validation
	$('.x-form').each(function() {
		var wrap = $(this);
		var error = false;
		
		//auto clear defaults
		wrap.find('input[type=text], input[type=password], 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); } });
		});
		
		//no empty
		jQuery.fn.v_noEmpty = function() {
			if($(this).val() == '' || $(this).val().length < 2) {
				$(this).setError();
			}
		}
		
		//email
		jQuery.fn.v_email = function() {
			if(!$(this).val().match('/^([a-zA-Z0-9_.-])+@([a-zA-Z0-9_.-])+\.([a-zA-Z])+([a-zA-Z])+/')) {
				$(this).setError();
			}
		}
		
		//error
		jQuery.fn.setError = function() {
			error = true;
			$(this).fadeOut('fast').fadeIn('fast');
		}
		
		wrap.find('.x-send').click(function(e) {
			
			wrap.find('.x-noempty').v_noEmpty();
			wrap.find('.x-email').v_email();
			
			if(!error) { wrap.submit(); }
			
		});
		
	});
	
});
