function echeck(str) {
	var at="@";
	var dot=".";
	var lat=str.indexOf(at);
	var lstr=str.length;
	var ldot=str.indexOf(dot);
	if (str.indexOf(at)==-1){
	   return false;
	}
	
	if (str.indexOf(at)==-1 || str.indexOf(at)===0 || str.indexOf(at)==lstr){
	   return false;
	}
	
	if (str.indexOf(dot)==-1 || str.indexOf(dot)===0 || str.indexOf(dot)==lstr){
		return false;
	}
	
	 if (str.indexOf(at,(lat+1))!=-1){
		return false;
	 }
	
	 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		return false;
	 }
	
	 if (str.indexOf(dot,(lat+2))==-1){
		return false;
	 }
	
	 if (str.indexOf(" ")!=-1){
		return false;
	 }
	
	 return true;			
}

$.fn.exists = function(){
	return jQuery(this).length>0;
};

function isIE() {
	return /msie/i.test(navigator.userAgent) && !/opera/i.test(navigator.userAgent);
}

$(document).ready(function(){
	$('form #firstname').focus(function(){
		if( 'First Name' == $(this).val() ) { $(this).val(''); }
	}).blur(function(){
		if( '' == $(this).val() ) { $(this).val('First Name'); }
	});
	
	$('form #lastname').focus(function(){
		if( 'Last Name' == $(this).val() ) { $(this).val(''); }
	}).blur(function(){
		if( '' == $(this).val() ) { $(this).val('Last Name'); }
	});
	
	$('form #email').focus(function(){
		if( 'Email Address' == $(this).val() ) { $(this).val(''); }
	}).blur(function(){
		if( '' == $(this).val() ) { $(this).val('Email Address'); }
	});
	
	$('form #email-news').focus(function(){
		if( 'Email Address' == $(this).val() ) { $(this).val(''); }
	}).blur(function(){
		if( '' == $(this).val() ) { $(this).val('Email Address'); }
	});
	
	if ( false === isIE() ) {
		$('#mast-box li.graphic:not(.on)').fadeTo(0,.4);
	}
	
	
	$('#mast-box li.graphic').hover(function(){
		if ( false === $(this).hasClass('on') ) {
			$('#mast-box *:not(li.graphic)').stop();
			
			var next = $(this).attr('id').substring(5);
			var bar = $('#green-bar');
			var corner = $('#green-corner');
			var scroller = $('#scroller');
			
			var top = $(this).offset().top - $('#mast-box').offset().top - 2;
			var width;
			var scrollTo;
			
			if ( 'find' == next ) { width = 200; scrollTo = 0; }
			else if ( 'understand' == next ) { width = 425; scrollTo = -401; }
			else if ( 'relate' == next ) { width = 250; scrollTo = -802; }
			
			corner.animate({top: top + 'px'},250,'cubicEaseIn');
			bar.animate({top: top + 'px', width: width + 'px'},250,'cubicEaseIn');
			scroller.animate({top: scrollTo + 'px'},500,'cubicEaseIn');
			
			if ( false === isIE() ) {
				$('#mast-box li.on').fadeTo(50,.4);
				$(this).fadeTo(50,1);
			}
			$('#mast-box li.on').removeClass('on');
			$(this).addClass('on');
		}
	});
	
	$('form #submit').click(function(){		
		$('form #firstname').css('background','#fff');
		$('form #lastname').css('background','#fff');
		$('form #email').css('background','#fff');
				
		if ( '' == $('form #firstname').val() || 'First Name' == $('form #firstname').val() ) {
			$('form #firstname').css('background','#ffe5e6');
		}
		else if ( '' == $('form #lastname').val() || 'Last Name' == $('form #lastname').val() ) {
			$('form #lastname').css('background','#ffe5e6');
		}
		else if ( '' == $('#email').val() || 'Email Address' == $('#email').val()  || false === echeck($('#email').val()) ) {
			$('#email').css('background','#ffe5e6');
		}
		else if ('' == $('#color').val() ) {
			$.ajax({
			  url: 'php/postalworker.php',
			  type: 'post',
			  data: ({ firstname : $('#firstname').val(), lastname : $('#lastname').val(), email : $('#email').val(), color: $('#color').val() }),
			  dataType: 'text',
			  success: function(data) {
				if( 'true' == data ) {
					window.location="thank-you.php";
				}
				else if( 'false - old' == data ) {
					window.location="thank-you.php?old";
				}
			  }
			});
		}
		
	});
	
	$('form #submit-news').click(function(){		
		$('form #email-news').css('background','#f1f2f2');
						
		if ( '' == $('#email-news').val() || 'Email Address' == $('#email-news').val()  || false === echeck($('#email-news').val()) ) {
			$('#email-news').css('background','#ffe5e6');
		}
		else if ('' == $('#color-news').val() ) {
			$.ajax({
			  url: 'php/newsletter.php',
			  type: 'post',
			  data: ({ email : $('#email-news').val(), color: $('#color-news').val() }),
			  dataType: 'text',
			  success: function(data) {
				if( 'true' == data ) {
					$('#form-newsletter').text('Thank you for registering.');
				}
				else {
					$('#form-newsletter').text('Something went wrong - please try again later.');
				}
			  }
			});
		}
		
	});
	
	$('#submit-heard').click(function(){
		$.ajax({
		  url: 'php/heard.php',
		  type: 'post',
		  data: ({ heard : $('#how-heard').val() }),
		  dataType: 'text',
		  success: function(data) {	  
			if( 'true' == data ) {
				$('#heard-box').text('Thank you.');
			}
			else {
				$('#heard-box').text('Something went wrong - please try again later.');
			}
		  }
		});		
	});
	
	$('div.privacy').slideUp(0);
	
	$('.slider').click(function(){
		if ( false === $(this).hasClass('on') || false === $('div:animated').exists ) {
			var oldH3 = $('h3.on');
			var newH3 = $(this);
			var oldP = $('div.on');
			var newPname = newH3.attr('id') + '-div';
			var newP = $('#' + newPname );
			var height = newP.outerHeight();
			
			oldH3.removeClass('on');
			oldP.slideUp(200).removeClass('on');
			newH3.addClass('on');
			newP.slideDown(200).addClass('on');
		}
	});
		
	$('.p-hover').css('opacity','0').hide();
		
	//glowMonster();
	$('.glow').each(function(){
		glowMonster($(this));
	});
	
	$('.glow').hover(function(){
		var hisName = $(this).attr('id').substr(6);
		var hisP = $('#p-' + hisName);
		hisP.show().animate({opacity:'.9',left:'+=10px'},500);
	},function(){
		var hisName = $(this).attr('id').substr(6);
		var hisP = $('#p-' + hisName);
		hisP.animate({opacity:'0',left:'-=10px'},500,function(){$(this).hide();});
	});
	
	// SPECIALS
	$('#track-group-drop').hover(function(){
		$('#special-group-drop').css('opacity',0).animate({height:'146px',opacity:'1'},{duration:500,queue:false});
	},	
	function(){
		$('#special-group-drop').animate({height:'0',opacity:'0'},{duration:500,queue:false});
	});
	
	$('#track-org-updates').hover(function(){
		$('#special-org-updates').css('opacity',0).animate({height:'147px',opacity:'1'},{duration:500,queue:false});
	},	
	function(){
		$('#special-org-updates').animate({height:'0',opacity:'0'},{duration:500,queue:false});
	});
	
	$('#track-tell-friends').hover(function(){
		$('#special-tell-friends').css('opacity',0).animate({height:'196px',opacity:'1'},{duration:500,queue:false});
	},	
	function(){
		$('#special-tell-friends').animate({height:'0',opacity:'0'},{duration:500,queue:false});
	});
	
	$('#track-settings-view').hover(function(){
		$('#special-settings-view').css('opacity',0).animate({height:'350px',opacity:'1'},{duration:500,queue:false});
	},	
	function(){
		$('#special-settings-view').animate({height:'0',opacity:'0'},{duration:500,queue:false});
	});
	
	$('#track-message-pop').hover(function(){
		if ( false === isIE() ) {
			$('#special-message-pop').css('opacity',0).animate({height:'300px',opacity:'1'},{duration:500,queue:false});
		}
		else {
			$('#special-message-pop').animate({height:'300px'},{duration:500,queue:false});
		}
	},	
	function(){
		if ( false === isIE() ) {
			$('#special-message-pop').animate({height:'0',opacity:'0'},{duration:500,queue:false});
		}
		else {
			$('#special-message-pop').animate({height:'0px'},{duration:500,queue:false});
		}
	});
	
	$('#track-attach-pop').hover(function(){
		$('#special-attach-pop').css('opacity',0).animate({height:'390px',opacity:'1'},{duration:500,queue:false});
	},	
	function(){
		$('#special-attach-pop').animate({height:'0',opacity:'0'},{duration:500,queue:false});
	});
	// END SPECIALS

	$('#page2 .glow').hide();

	$('#message-block').click(function(){
		//$(this).css('cursor','auto').animate({width:'205px'},500,'cubicEaseOut');
		$('#page2 .glow').show();
		$('#message-first').fadeOut(300);
		$('#cover').fadeOut(300);
	});

	$('#nav-start').click(function(){
		if (false === $('#demo-stage div').hasClass('animated') ) {
			var oldguy = $('#demo-stage .current');
			var newName = oldguy.attr('id').substr(4);
			newName++;
			var newGuy = $('#demo-stage #page' + newName);
			newGuy.addClass('animated');
			
			$(this).hide();
			$('#nav-left').show();
			$('#nav-right').show();
			//alert(newGuy.attr('id'));
			//newGuy.rotate('25deg');
			//newGuy.css({'top':'325px','left':'1084px'});
			newGuy.css({'top':'-30px','left':'1084px'});
			//oldguy.animate({top:'-325px',left:'-1084px',rotate:'25deg'},2000,'cubicEaseOut', function(){
			oldguy.animate({left:'-1084px'},1500,'cubicEaseOut', function(){
				$(this).removeClass('current');
			});
			//newGuy.animate({top:'-30px',left:'-70px',rotate:'0deg'},2000,'cubicEaseOut', function(){
			newGuy.animate({left:'-70px'},1500,'cubicEaseOut', function(){
				$(this).addClass('current').removeClass('animated').rotate('0deg');
				/*
				alert($('.glow',$(this)).attr('id'));
				$('.glow',$(this)).first().trigger('mouseover');
				*/
			});
		}
	});
	$('#nav-right').hide().click(function(){
		if (false === $('#demo-stage div').hasClass('animated') ) {
			var oldguy = $('#demo-stage .current');
			var newName = oldguy.attr('id').substr(4);
			newName++;
			var newGuy = $('#demo-stage #page' + newName);
		
			if ( 2 == (newName-1) && false === $('#page2').hasClass('changed') ) {
				$('#page2').addClass('changed');
				$('#page2 .glow').show();
				$('#message-first').fadeOut(300);
				$('#cover').fadeOut(300);
			}
			else {

			newGuy.addClass('animated');
			
			if ( true === newGuy.hasClass('last') ) {
				$(this).hide();
			}
			else if ( false === newGuy.hasClass('first') ) {
				$('#nav-left').show();
			}
			else {
				$(this).show();
			}
			//alert(newGuy.attr('id'));
			//newGuy.rotate('25deg');
			//newGuy.css({'top':'325px','left':'1084px'});
			newGuy.css({'top':'-30px','left':'1084px'});
			//oldguy.animate({top:'-325px',left:'-1084px',rotate:'25deg'},2000,'cubicEaseOut', function(){
			oldguy.animate({left:'-1084px'},1500,'cubicEaseOut', function(){
				$(this).removeClass('current');
			});
			//newGuy.animate({top:'-30px',left:'-70px',rotate:'0deg'},2000,'cubicEaseOut', function(){
			newGuy.animate({left:'-70px'},1500,'cubicEaseOut', function(){
				$(this).addClass('current').removeClass('animated').rotate('0deg');
				/*
				alert($('.glow',$(this)).attr('id'));
				$('.glow',$(this)).first().trigger('mouseover');
				*/
			});
			
			}
		}
	});
	$('#nav-left').hide().click(function(){
		if (false === $('#demo-stage div').hasClass('animated') ) {
			var oldguy = $('#demo-stage .current');
			var newName = oldguy.attr('id').substr(4);
			newName--;
			var newGuy = $('#demo-stage #page' + newName);
			
			if ( 2 == (newName+1) && true === $('#page2').hasClass('changed') ) {
				$('#page2').removeClass('changed');
				$('#page2 .glow').hide();
				$('#message-first').fadeIn(300);
				$('#cover').fadeIn(300);
			}
			else {
			
			newGuy.addClass('animated');
			
			if ( true === newGuy.hasClass('first') ) {
				$(this).hide();
				$('#nav-right').hide();
				$('#nav-start').show();
			}
			else if ( false === newGuy.hasClass('last') ) {
				$('#nav-right').show();
			}
			else {
				$(this).show();
			}
			//alert(newGuy.attr('id'));
			//newGuy.rotate('25deg');
			//newGuy.css({'top':'-325px','left':'-1084px'});
			newGuy.css({'top':'-30px','left':'-1084px'});
			//oldguy.animate({top:'100px',left:'-1080px', rotate:'-90deg'},2000,'cubicEaseIn');
			//oldguy.animate({top:'325px',left:'1084px',rotate:'25deg'},2000,'cubicEaseOut', function(){
			oldguy.animate({left:'1084px'},1500,'cubicEaseOut', function(){
				$(this).removeClass('current');
			});
			//newGuy.animate({top:'-30px',left:'-70px',rotate:'0deg'},2000,'cubicEaseOut', function(){
			newGuy.animate({left:'-70px'},1500,'cubicEaseOut', function(){
				$(this).addClass('current').removeClass('animated').rotate('0deg');
			});
			}
		}
	});
});

function glowMonster(obj) {
	//obj.css('opacity',1);
	obj.animate({opacity:'.2'},1000,function(){
		$(this).animate({opacity:'.7'},1000,function(){
			glowMonster($(this));
		});
	});
}
