jQuery(document).ready(function(){
	tabs();
	validate_form({form:".contact form"});
});

function tabs(){
	var o ={};
	o.main = jQuery(".tab_cont");
	o.mainTabs = jQuery(".tabs");
	o.pad = o.main.find(".pad");
	
	if(o.main.html() == null){return false;}
	o.mainTabs.html("");
	o.pad.each(function(){
		o.title = jQuery(this).attr("title");
		o.url = o.title.toLowerCase().split(' ').join('_');
		
		o.mainTabs.append('<a href="#'+o.url+'"><span class="left">'+o.title+'</span><span class="right">&nbsp;</span></a>');
		o.nWidth = o.mainTabs.find("span.left:last").outerWidth() + o.mainTabs.find("span.right:last").outerWidth()+10;
		o.mainTabs.find("a:last").css({width: o.nWidth});
		jQuery(this).attr("url", o.url);
	});
	
	o.obj = o.main.find("a");

	classes();
	
	function classes(){
		o.mainTabs.find("a:first").addClass("first");
		o.mainTabs.find("a:last").addClass("last");
		o.mainTabs.find(".active").prev("a").addClass("pre_active");

	}
	
	if(!location.hash){location.hash = o.main.find(".pad:first").attr("url");}
	var $state = jQuery('#state'), $custom = jQuery('#custom'), $overloading = jQuery('#overloading');	
	jQuery.History.bind(function(state){
		$state.text(state);
		open_tab(state);
	});
	
	function open_tab( obj ){
		o.aTag = jQuery("a[href='#"+obj+"']");
		o.obj.removeClass();
		o.aTag.addClass("active");
		classes();
		o.pad.hide();
		o.main.find(".pad[url='"+obj+"']").show();
	}
	
	
}


function validate_form(options){
	/*
	usage:	validate_form({form:" selector "});    the forms ID or CLASS
				category="validate"							to validate fields
				type="email"  									if it is an EMAIL field
				min="5"											to set the MINIMUM required characters
				max="5"											to set the MAXIMUM amount of characters
				
				
				.error 											that is the DEFAULT error class name
	
	by Henri Kokk
		www.henri.ee
	*/
	if(jQuery(options.form).html() !== null){
		
		var o = {
			type: "category",
			btn:	"saada",
			err_class: "error",
			object: options.form
		};
		
		jQuery("[rel='validate']").keyup(function(e){
			if(e.keyCode == 9) return false;
			jQuery(this).removeClass(o.err_class);
			validates(jQuery(this));
		});
		
		jQuery(o.object).submit(function(){
			o.counter=0;
			jQuery(o.object).find("[rel='validate']").each(function(){
				jQuery(this).removeClass(o.err_class);
				validates(jQuery(this));
				if(jQuery(this).attr("class") == o.err_class ){o.counter++;}
			});
			
			if(o.counter !== 0){return false;}
			
		});
	}
	
	function validates(obj){
		if(obj.attr(o.type) == "email"){
			check_mail(obj.val()) == false ? obj.addClass(o.err_class): obj.removeClass(o.err_class);
		}
		else{
			o.minimum = jQuery(obj).attr("min") || 1;
			o.maximum = jQuery(obj).attr("max") || 9999999999;
			obj.val().length < o.minimum ? obj.addClass(o.err_class): obj.removeClass(o.err_class);
			obj.val().length > o.maximum ? obj.val(obj.val().substring(0, o.maximum)) : "";
		}
		
	}
	
	function check_mail(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);
	}
}
