$(document).ready(function(){
	
	
	//$.cookie('buckley', 'puppeh'); 
    // Check for cookie
	if (!$.cookie('jimmy')) {
			
		  //alert ("I got a cookie!");
			
		  //Put in the DIV id you want to display
		  launchWindow('#contactForm');
		  
		  //if main close button is clicked
		  $('.window #container-close').click(function () {
			  $('#mask').hide();
			  $('.window').hide();
		  });
		  
		  //if close button is clicked
		  $('.window #close').click(function () {
			  $('#mask').hide();
			  $('.window').hide();
		  });
		  
		  //if skip button is clicked
		  $('.window .skip').click(function () {
			  $('#mask').hide();
			  $('.window').hide();
			  $.cookie('jimmy', 'nope');
		  });
		  
		  //if mask is clicked
		  $('#mask').click(function () {
			  $(this).hide();
			  $('.window').hide();
		  }); 
	
	}  
		
		function launchWindow(id) {
	
		//Get the screen height and width
		var maskHeight = $(document).height();
		var maskWidth = $(window).width();
	
		//Set heigth and width to mask to fill up the whole screen
		$('#mask').css({'width':maskWidth,'height':maskHeight});
		
		//transition effect		
		$('#mask').fadeIn(1000);	
		$('#mask').fadeTo("slow",0.8);	
	
		//Get the window height and width
		var winH = $(window).height();
		var winW = $(window).width();
              
		//Set the popup window to center
		//$(id).css('top',  winH/2-$(id).height());
		//$(id).css('left', winW/2-$(id).width()/2);
	
		//transition effect
		$(id).fadeIn(2000); 

        }
		
	//if either ambassador is clicked in home gallery
	$('.go-popup').click(function () {
		launchWindow('#contactForm');
		return false;
	});
	 
	//run contact form when any contact link is clicked
	$(".contact").click(function(){contact()});
	
	//animation for same page links #
	$('a[href*=#]').each(function() {
		if (location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'')
		&& location.hostname == this.hostname
		&& this.hash.replace(/#/,'') ) {
		  var $targetId = $(this.hash), $targetAnchor = $('[name=' + this.hash.slice(1) +']');
		  var $target = $targetId.length ? $targetId : $targetAnchor.length ? $targetAnchor : false;
			if ($(this.hash).length) {
				$(this).click(function(event) {
					var targetOffset = $(this.hash).offset().top;
					var target = this.hash;
					event.preventDefault();			   
					$('html, body').animate({scrollTop: targetOffset}, 500);
					return false;
				});
			}
		}
	});



   //submission scripts
  $('.contactForm').submit( function(){
		//statements to validate the form	
		
		if (document.cform.name.value == "") {
			//$('.name-missing').show();
			alert ("Please enter your name");
		} else {$('.name-missing').hide();}
		
		var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
		var email = document.getElementById('e-mail');
			
		if (!filter.test(email.value)) {
			//$('.email-missing').show();
			alert ("Please enter your email address");
		} else {$('.email-missing').hide();}
		
		if (document.cform.phone.value == "") {
			//$('.message-missing').show();
			alert ("Please enter your phone number");
		} else {$('.phone-missing').hide();}
		
		//if (!$('input[name=ambType]').attr('checked')) {
        //  alert ("Do you want to connect with an ambassador or become one?")
        // }
		
		if (!$("input[@name='ambType']:checked").val()) {
              alert('Do you want to connect with an ambassador or become one?');
        }

		
		if (!$('#is18').attr('checked')) {
		   alert ("You must 18 or over")
		}
		
				
		if ((document.cform.name.value == "") || (!filter.test(email.value)) || (document.cform.phone.value == "") || (!$("input[@name='ambType']:checked").val()) || (!$('input[name=is18]').attr('checked')) ) {
			return false;
		} 
		
		if ( (document.cform.name.value != "") && (filter.test(email.value)) && (document.cform.phone.value != "") && ($("input[@name='ambType']:checked").val()) && ($('#is18').attr('checked')) )  {
			//hide the form
			$('.contactForm').hide();
		
			//show the loading bar
			$('.loader').append($('.bar'));
			$('.bar').css({display:'block'});
		
			//send the ajax request
			$.post('/mailer/community/process.php',{name:$('#name').val(),
							  email:$('#e-mail').val(),
							  ambType:$('.ambType').val(),
							  phone:$('#phone').val()},
		
			//return the data
			function(data){
			  //hide the graphic
			  $('.bar').css({display:'none'});
			  $('.loader').append(data);
			  
			  $('.window .close').click(function () {
		         $('#mask').hide();
		         $('.window').hide();
	          });
			  
			});
			
			//waits 2000, then closes the form and fades out
			//setTimeout('$("#backgroundPopup").fadeOut("slow"); $("#mask").fadeOut("slow"); $("#contactForm").fadeOut("slow")', 8000);
			$.cookie('jimmy', 'nope'); 
			
			//stay on the page
			return false;
		} 
  });
	//only need force for IE6  
	$("#backgroundPopup").css({  
		"height": document.documentElement.clientHeight 
	});  
});
