// default
//
var tips;

$(function(){

	$('#contact_btn').click(function(){
		
		if(!$('#contact:visible').html()){
			$(this).toggleClass('sel');
			$('#about-us_btn').toggleClass('sel');
			
			$('#about-us>h2,#about-us>p').fadeOut(function(){
				$('#about-us>h1').slideUp(function(){
					$('#about-us').hide();
					
					var leftTemp 	= ($('#contact_btn').parent().offset().left - $('#mainContent').offset().left) + $('#contact_btn').parent().width();
					var rightTemp	= 760 - 3 - leftTemp;
					
					$('#left').animate({'width': leftTemp + 'px'});
					$('#right').show().animate({'width': rightTemp + 'px'}, function(){
						$('#contact').fadeIn();
						$('#right>div').fadeIn();
					});
				});
			});
		} else {
			// nothing
		}
		
		return false;
	});
	
	$('#about-us_btn').click(function(){
		
		if(!$('#about-us:visible').html()){
			$(this).toggleClass('sel');
			$('#contact_btn').toggleClass('sel');
			
			$('#contact,#right>div').fadeOut(function(){							   
				$('#left').animate({'width': '760px'});
				$('#right').animate({'width': '0px'}, function(){
					$('#about-us').show(function(){
						$('#about-us>h1').slideDown(function(){
							$('#about-us>h2,#about-us>p').fadeIn();
						});						 
					});
				});
			});
		} else {
			// nothing
		}
		
		return false;
	});
	
	$('#artwork').cycle({
        fx:      'fade',
        timeout:  5000,
        prev:    '#prev',
        next:    '#next'
	});
	
	tips = $('#validateTips');
	$('form').submit(function(){
		contactForm();
		return false;
	});
	
});

//
function updateTips(t) {
	tips.text(t);
}

function checkLength(o,n,min,max) {

	if ( o.val().length > max || o.val().length < min ) {
		o.addClass('error');
		updateTips("Length of " + n + " must be between "+min+" and "+max+".");
		return false;
	} else {
		return true;
	}

}

function checkRegexp(o,regexp,n) {

	if ( !( regexp.test( o.val() ) ) ) {
		o.addClass('error');
		updateTips(n);
		return false;
	} else {
		return true;
	}

}

//
function contactForm(){

	var bValid = true;
	
	$('#name').removeClass('error');
	$('#email').removeClass('error');
	$('#phone').removeClass('error');
	$('#comments').removeClass('error');

	bValid = bValid && checkLength($('#name'),"name",1,128);
	
	bValid = bValid && checkRegexp($('#email'),/^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/i,"Invalid Email eg. jonDoe@gmail.com");

	bValid = bValid && checkLength($('#comments'),"comments",1,2000);

	if (bValid) {
		$.getJSON('ajax.php',{'action':'contact', 'name':$('#name').val(), 'company':$('#company').val(), 'email':$('#email').val(), 'phone':$('#phone').val(), 'subject':$('#subject').val(), 'comments':$('#comments').val() },function(data){
			if(data.success == true){
				
				$('form').fadeOut('slow');
				$('#thankYou').fadeIn('slow');
				
			} else {
			
				updateTips('Error Submitting Contact Information');
										
			}
							
		});
	}

	return false;

}
