/**
 * main.js
 * @author klokie
 * validate email address for submission
 */
var animating = false;
function validate(field){
	var illegalChars = /[\(\)\<\>\,\;\:\\\/\"\[\]]/, emailFilter = /^.+@.+\..{2,6}$/ , txt = field.val();
	//if( txt.match(illegalChars) != null || !emailFilter.test(txt) ){
	if ((txt == field[0].defaultValue) || (txt == '')) {
		document.getElementById('form-email-error').style.display='block';
		document.getElementById('form-reg-error').style.display='none';
		document.getElementById('form-success').style.display='none';
		return false;
	}
	return true;
}

function ropeEm(toggle) {
	if (animating) return;
	animating = true;
	
	//event.stopPropagation();
	var ropeOffset = '', formOffset = '', duration = 500;
	if (typeof toggle == 'undefined') {
		toggle = jQuery('.contact').toggleClass('open').hasClass('open');
	}
	
	if ( toggle ) {
		// pull rope down
		ropeOffset = '0';
		formOffset = '1';
		jQuery('a.pull.text').fadeOut();
		//jQuery('#contact-text').fadeIn(duration, function(){
			jQuery('#UserEmail').focus();
		//});
	} else {
		// pull rope up
		ropeOffset = '-106';
		formOffset = '-200';
		//jQuery('#contact-text').fadeOut(duration);
		jQuery('a.pull.text').fadeIn();
		//			jQuery('#contact-text').removeClass('success');
	}
	jQuery('#contact').animate({ top: ropeOffset +'px' }, duration, 'swing');
	jQuery('#contact-text').animate({ top: formOffset +'px' }, duration, 'swing', function() {
		if (!toggle) {
			if ( jQuery(this).hasClass('success') ) {
				jQuery(this).removeClass('success').find('h3').text("Let's Talk.").siblings('#form-success').hide();
			} else {
				jQuery('#form-email-error,#form-reg-error').hide();
			}
		}
		animating = false;
	});
}

// onload:
jQuery(document).ready(function() {
	// highlight mac-specific stuff
	var p = navigator.platform;
	if (/^Win/.test(p)) jQuery('.OS.win').show().siblings('.mac').hide();
	else if (/^Mac/.test(p))  jQuery('.OS.win').hide().siblings('.mac').show();

	jQuery('.contact a.pull').mousedown(function(event){
		ropeEm();
	});
	
	// contact form
	jQuery('#UserEmail').focus(function(){
		if (jQuery(this).val() == this.defaultValue )
			jQuery(this).val('').removeClass('default');
	}).blur(function(){
		if (jQuery(this).val()=='') 
			jQuery(this).val( this.defaultValue ).addClass('default'); 
	});

	jQuery('#contact-text a.close').click(function(){
		ropeEm();
		return false;
	});
	
	/*
	// support browser history
	if (self.location.hash && (self.location.hash.indexOf('contact') > 0) && !jQuery('#contact').hasClass('open')) {
		ropeEm( true );
	}
	*/
	if (jQuery('#contact-text').hasClass('open')){
		ropeEm( true );
	}

	if (jQuery('#contact-text').hasClass('success')) {
		self.setTimeout(function(){
			ropeEm( false );
		}, 2000);
	}
	
	jQuery('#UserRegisterForm').submit(function(){
		return validate(jQuery('#UserEmail'));
	});
});
