//<![CDATA[
//phonepattern = ;

emailReg = new RegExp(/^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/);
phoneReg = new RegExp(/^[2-9]\d{2}-\d{3}-\d{4}$/);
hasError = false;

$(document).ready(function() {
	/*	
	*	Animation on sub page
	*	For URL: contact.php, location.php	
	*/
	$('#lead').css( {backgroundPosition: "50% 0"} )
		.mouseover(function(){ $(this).stop().animate({backgroundPosition:"(30% 0)"}, {duration:1000}) })
		.mouseout(function(){
		$(this).stop().animate({backgroundPosition:"(50% 0)"}, 
		{duration:500, complete:function(){
		$(this).css({backgroundPosition: "50% 0"})
		}})
	})
	
	/*
	*	Set Cyle Plugin
	*	For url: index.php
	*/
	$('#slides').after('<div id="feature-nav">').cycle({ 
	    	fx:    'scrollRight',  
		delay: 10000,
		pager: '#feature-nav',
		cssBefore: { zIndex: 1 }, 
		cssAfter: { zIndex: -1  }
	});
/*	$('#feature-nav').addClass('prefix_6 grid_3');
	$('#feature-nav a').each( function(i) {
		$(this).wrap('<li class="feature-nav" id="feature-nav-'+(i+1)+'"></li>').wrapInner('<span></span>');
	});
	$('#feature-nav li').wrapAll('<ul></ul>'); */
	
	/*	
	*	Form Title adds default value
	*	For URL: contact.php
	*/
	if($('input[type="text"]'))
		$('input.text').each(function(e) {
		$(this).defaultValue = $(this).attr('title');					
		dv = $(this).defaultValue;
		v = $(this).value; 
		$(this).bind('focus', function() {
			$(this).css('text-transform', 'none');					 
			if( $(this).val().toLowerCase() ==  $(this).attr('title').toLowerCase() ) $(this).val('');
			
		});
		$(this).bind('blur', function() {		    
			if( v == '') $(this).val(dv);
			else if ($(this).val().toLowerCase() == $(this).attr('title').toLowerCase()) $(this).css('text-transform', 'Capitalize');
			//validate($(this));
		});
	});
	
	
	/*
	*	Form Validation
	*/
	
	$('form .format, form .error, #form-result').hide();
	$('#form-contact-us ul li #submit').click(function() {
		//input = $('#form-contact-us input[type!="submit"]');		
		
		femail = $('#contactEmail').val();
		fname = $('#contactName').val();
		fphone = $('#contactPhone').val();
		frentalsize = $('#rentalsize').val();
		fmessage = $('#message').val();
		
		// 
		if (femail == '' && femail !='Email') { triggerError('#contactEmail'); }
		else if(!emailReg.test(femail)) { triggerFormat('#contactEmail');	}
		if (fname == '' && fname !='Name') triggerError('#contactName');
		if (fphone == '' && fphone != 'Phone Number') triggerError('#contactPhone');
		else if(!phoneReg.test(fphone)) { triggerFormat('#contactPhone');	}		
		
		if (hasError == false) {
			$('#form-contact-us ul').hide();
			$('#form-contact-us .loading img').show();	
			$.get( "/include/validate.php", 
			{	
				contactEmail: femail, contactName: fname, rentalsize: frentalsize,
				message: fmessage, contactPhone: fphone, contactSubmit: true
			}, function(data) 	{
				$('#form-result:hidden').show();
//				console.log('Getting Data\n');
//				console.log(data);
				$('#form-contact-us .loading').hide('slow');
				$('#form-result').append(data); //.slideDown('slow');
			});	
		}
		
		hasError = false; // reset for next round
		return false;		
	});
	
	$('#request #request_submit').click(function() {
		//input = $('#form-contact-us input[type!="submit"]');		
		
		remail = $('#request_email').val();
		rname = $('#request_name').val();
		rphone = $('#request_phone').val();
		
		// 
		if (remail == '' && remail !='Email') { triggerError('#request_email'); }
		else if(!emailReg.test(remail)) { triggerFormat('#request_email');	}
		if (rname == '' && rname !='Name') triggerError('#request_name');
		if (rphone == '' && rphone != 'Phone Number') triggerError('#request_phone');
		else if(!phoneReg.test(fphone)) { triggerFormat('#request_phone');	}		
		
		if (hasError == false) {
			$('#request ul').hide();
			$('#request .loading img').show();	
			$.get( "/include/validate.php", 
			{	
				request_email: remail, request_name: rname, request_phone: rphone, 
				request_submit: true
			}, function(data) 	{
				$('#request-result:hidden').show();
//				console.log('Getting Data\n');
//				console.log(data);
				$('#request .loading').hide('slow');
				$('#request-result').append(data); //.slideDown('slow');
			});	
		}
		
		hasError = false; // reset for next round
		return false;		
	});

}); // END Document Ready

function triggerError(obj, format) {
	$(obj).next('label.error:hidden').css({color: '#cc0000', opacity: '0.1'})
	.animate({opacity: 1}, 500); 	// if value - default value
	$(this).focus();
	return hasError = true;
}
function triggerFormat(obj) {
	$(obj).next().next(':visible').hide();
	$(obj).next().next(':hidden').show("slow").css({color: '#cc0000', opacity: '0.1'})
	.animate({opacity: 1}, 500);	
	$(obj).next().next(':visible').animate({ opacity: '0'}, 1500, function() { $(this).hide(); });
	$(this).focus();
	return hasError = true;
}
//]]>
