$(document).ready(function(){
	//get screen size
	var thisWidth=$(window).width();
	var docWidth=$(document).width();
	var thisHeight=$(window).height();
	var docHeight=$(document).height();
	//alert(thisWidth+" \ "+docWidth+" \ "+thisHeight+" \ "+docHeight);
	//flash content
	if (thisWidth>1000) {
	var numRand = Math.floor(Math.random()*1000);
	flashembed("#flashEnabled", {
		src: 'index.swf?a=numRand',
		version:[10,0],
		width:974,
		height:600
		});
	}
	var headerArray=new Array();
	var mainArray=new Array();
	var imageArray=new Array();
	var footerArray=new Array();
	//parse the JSON file
	 $.getJSON("jsonContentAccordn.json",function(data)	{
		 
		var thePagesNumberLength=data.content.length;
		var theMenuLength=data.menu.length;		
		$.each(data.content, function(i,data){			
			var thisHeader=data.header;
			var thisMain=data.main;
			var thisImage=data.image;
			var thisFooter=data.footer;
			headerArray.push(thisHeader);
			mainArray.push(thisMain);
			imageArray.push(thisImage);
			footerArray.push(thisFooter);			
								  }); 
	
	$(".noJS").html("");
	$(".accordion2 h3").eq(0).html(headerArray[0]);
	$(".accordion2 h3").eq(0).addClass("active");
	$(".accordion2 p.jsonContent").eq(0).html(mainArray[0]);
	$(".accordion2 p").eq(0).show();
	//
	$(".accordion2 h3").eq(1).html(headerArray[1]);	
	$(".accordion2 p.jsonContent").eq(1).html(mainArray[1]);
	//
	$(".accordion2 h3").eq(2).html(headerArray[2]);
	$(".accordion2 p.jsonContent").eq(2).html(mainArray[2]);
	//
	$(".accordion2 h3").eq(3).html(headerArray[3]);
	$(".accordion2 p.jsonContent").eq(3).html(mainArray[3]);
	//
	$(".accordion2 h3").eq(4).html(headerArray[4]);
	$(".accordion2 p.jsonContent").eq(4).html(mainArray[4]);
	//click function
	$(".accordion2 h3").click(function(){
		$(this).next("p").slideToggle("slow")
		.siblings("p:visible").slideUp("slow");
		$(this).toggleClass("active");
		$(this).siblings("h3").removeClass("active");
	});
//end click function
	$("#contactor").css({
	'cursor':'pointer'	
		});
	$("#contactor").click(function() {
		//alert ("contactor clikt");
		webFormer();		
	});
	$("#postor").css({
	'cursor':'pointer'	
		});
	/*$("#postor").click(function() {
		//alert ("contactor clikt");
		goForum();		
	});*/
	var stopTheRecursionMadness=false;
		//overlay ends at line 373
		function webFormer() {
			if (stopTheRecursionMadness==false) {
				stopTheRecursionMadness=true;
			var idArray=new Array();
	var firstArray=new Array();
	var lastArray=new Array();
	var emailArray=new Array();
	
	var memberSelectedIndex=-1;
   		var docHeight = $(document).height();
		var docWidth = $(document).width();
		var fivePercent=(docWidth*.10)/2;
		var reFactored=fivePercent*20;
		var theseThings=docWidth+", "+fivePercent+", "+reFactored;		
		$("<div id='membersBG'></div>").appendTo("#mobileContent");
		$("#membersBG")
     		.height(docHeight)
     		.css({
				'opacity' : 0.8,
				'position': 'fixed',
				
      			'top': 0,
         		'left': 0,
         		'background-color': 'black',
         		'width': '100%',
         		'z-index': 5000
      		});
			$("#membersBG").show();			
			$("<div id='members'><h2>Select a Member</h2></div>").appendTo("#mobileContent");
			$("#members").css({
				'position': 'absolute',
				'width' : "90%",
				'background-color' : 'white',				
      			'top': fivePercent,
         		'left': fivePercent,
				'display' : 'block',
				'text-align':'center',
				'z-index': 5001,
				'padding-bottom':'25px',
				'opacity':'1'
			});
			$("<div id='close'></div>").appendTo("#mobileContent");
			$("#close").css ({
				'width' : '16px',
				'height' : '16px',
				'background-image' : 'url(\"images/close.gif\")',
				'background-repeat': 'no-repeat',
				'color':'white',
				'padding':'3px',
				'position': 'absolute',
				'top': fivePercent+3,
				'right': fivePercent+3,
				'cursor':'pointer',
				'z-index': 6000		
			});
			$("#close").show();
			//begin parse JSON			
			$.getJSON("contactMember.json",function(data)	{
		var theNumberOfMembers=data.content.length;
		$.each(data.content, function(i,data){			
			var thisId=data.id;
			var thisFirst=data.first;
			var thisLast=data.last;
			var thisEmail=data.email;
			idArray.push(thisId);
			firstArray.push(thisFirst);
			lastArray.push(thisLast);
			emailArray.push(thisEmail);			
								  });			
				//begin populate in names
				$.each(idArray, function (n, val) {
					var curNum=n+1;
					var thisFirst=firstArray[n];
					var thisLast=lastArray[n];
					var someButton=curNum+". "+thisFirst+" "+thisLast;
					$(".aButton").css ({
						'width' : '200px',
						'margin':'auto',
						'background-color':'#FF9',
						'border-color':'black',
						'border-style':'solid',
						'border-width': '1px',
						'padding':'10px',
						'margin-bottom': '15px',
						'cursor':'pointer'	
					});
					var thisMarkup="<div id=\""+n+"\" class=\"aButton\">"+someButton+"</div>";
					$("#members").append(thisMarkup);				
				});
				$(".aButton:last-of-type").hide();
				$(".aButton").click(function(){
					var myId = $(this).attr('id');
					memberSelectedIndex=myId;
					$("#members").hide(function(){
						$("<div id=\"mailFormDiv\" class=\"formEvent\"><h3 class=\"formEvent\">Compose a Message to <br />"+firstArray[memberSelectedIndex]+" "+lastArray[memberSelectedIndex]+"</h3><span id=\"eitherOr\">Please enter either a phone number<br />or an email address.</span></div>").appendTo("#mobileContent");
			$("#mailFormDiv").css({
				'position': 'absolute',
				'width' : "90%",
				'background-color' : 'white',				
      			'top': fivePercent,
         		'left': fivePercent,
				'display' : 'block',
				'text-align':'center',
				'z-index': 5002,
				'padding-bottom':'25px',
				'opacity':'1',
				'color' : 'black'
			});
			$(".label").css({
					'margin-top' : '50px'
				});
				$(".centerish").css({
					'position' : 'relative',
					'margin-left' : '70px'
				});
						memberSelectedIndex=parseFloat(memberSelectedIndex)+1;	
						$("#mailFormDiv").append("<script  type=\"text/javascript\">$(\"#contactForm\").validate();</script><div id=\"theForm\" class=\"formEvent\"><div class=\"formElement formEvent\"><form action= \"contactMember.php\" method=\"post\" id=\"contactForm\" name=\"contactForm\"><label id=\"nameLabel\">Your name: </label><br /><input id=\"thisName\" class=\"input required\" type=\"text\" name=\"theName\"><br /><label id=\"acLabel\">Your area code: </label><br /><input class=\"input\" type=\"text\" name=\"theAC\" id=\"thisAC\"><br /><label id=\"phoneLabel\">Your phone number: </label><br /><input class=\"input\" type=\"text\" name=\"thePhone\" id=\"thisPhone\"><br /><label id=\"emailLabel\">Your email address: </label><br /><input class=\"input required email\" type=\"text\" name=\"email\" id=\"thisEmail\"><br /><labelid=\"messageLabel\">Message:</label><br /><textarea  class=\"input\" name=\"message\" rows=\"6\" cols=\"40\" id=\"thisMessage\"></textarea><input type=\"hidden\" id=\"thisRecipient\" name=\"recipient\" value=\""+memberSelectedIndex+"\"><div class=\"centerish\" style=\"align:center\"> <input type=\"submit\" value=\"Send\" id=\"submit\"></div> </form></div></div>");
						//
						$("#theForm").css({
				'position': 'relative',
				'background-color' : 'white',	
				'top' : '7px',
				'display' : 'block',
				'text-align':'left',
				'z-index': 5003,
				'padding-bottom':'25px',
				'opacity':'1',
				'color' : 'black',
				'line-height' : '150%'
			});
			$(".formElement").css({
				'position': 'relative',
				'display' : 'block',
				'text-align':'left',
				'padding-left' :'3%',
				'z-index': 5004,
				'opacity':'1',
				'color' : 'black',
				
			});
			$(".input").css ({
				'width' : '93%',
				'margin-bottom' : '12px'
				});
				$("input#thisName").focus();
		$("#submit").click(function(){									 
			var entryName = $("input#thisName").val();
			var entryAC = $("input#thisAC").val();
			var entryPhone = $("input#thisPhone").val();
			var entryEmail = $("input#thisEmail").val();
			var entryMessage = $("textarea#thisMessage").val();
			var entryRecipient = $("input#thisRecipient").val();
			if (entryName == "") {
			$("#nameLabel").html("<strong><font color=\"red\">Please enter your name</font></strong>");
			$("input#thisName").focus();			
			return false;			
			}else{
				$("#nameLabel").html("Your name");
			}
			if (entryPhone == ""&&entryEmail=="") {
			$("#eitherOr").html("<strong><font color=\"red\">Please enter either a phone number<br />or an email address.</font></strong>");
			return false;
			}else{
				$("#eitherOr").html("Please enter either a phone number<br />or an email address.");
			}			
			//email validation here
			var email = $("#thisEmail").val();
			if(email != "")	{
				if(isValidEmailAddress(email)){
					$("#emailLabel").html("Your email address.");
			$("input#thisEmail").focus();		
			}else{
				$("#emailLabel").html("<strong><font color=\"red\">Please enter a valid email address.</font></strong>");				
				return false;
				}			
			}
			//AJAX here
				var dataString = 'theName='+ entryName + '&theAC='+ entryAC + '&thePhone='+ entryPhone + '&email=' + entryEmail + '&message=' + entryMessage + '&recipient=' + entryRecipient;
				$.ajax({
      type: "POST",
      url: "contactMember.php",
      data: dataString,	  
      success: function(msg) {
		  memberSelectedIndex=parseFloat(memberSelectedIndex)-1;
		  if (msg=="returnMessage=Thank you. Your message has been received.") {
        $("#eitherOr").html("<h2>Thank You</h2><h3>Your message has been sent to "+firstArray[memberSelectedIndex]+" "+lastArray[memberSelectedIndex]+"</h3>");
		//clear the form
			$("input#thisName").val("");
			$("input#thisAC").val("");
			$("input#thisPhone").val("");
			$("input#thisEmail").val("");
			$("textarea#thisMessage").val("");
			$("input#thisRecipient").val("");
		//end clear the form
		  }else{
			  $("#eitherOr").html("<h2><font color=\"red\">ERROR</font></h2><h3>Please contact the Webmaster, webmaster@sboakcrest.com</h3>");
		  }
      }
     });
	//end AJAX
	return false;			
	  	});	
					});
				});
				$("#close").click(function(){
					closeMembership();
					});
					//a function contained within populate in names to clear itself
				function closeMembership() {
					stopTheRecursionMadness=false;
					idArray="";
					firstArray="";
					lastArray="";
					emailArray="";
					thisId="";		 
					thisFirst="";
					thisLast="";
					thisEmail="";
					dataString ="";
					entryName ="";
			entryAC ="";
			entryPhone ="";
			entryEmail ="";
			entryMessage ="";
			entryRecipient ="";
			memberSelectedIndex =-1;
					
					$("#eitherOr").html("");
					$("#theForm").html("");
					$("#members").remove(".aButton");
					//remove the dynamically loaded in divs
					$("#members").fadeOut(500, function() { 
						$("#mobileContent").remove("#members"); 
					});
					$("#membersBG").fadeOut(500, function() { 
						$("#mobileContent").remove("#membersBG"); 					
					});	
						$("#mobileContent").remove("#mailFormDiv");;	
					//killAll
					$(".aButton").remove();
					$(".formElement").remove();
					$('.formEvent').remove();
					$('#mailFormDiv').remove();
					$("#membersBG").remove();
					$(".input").remove();
					$(".eitherOr").remove();
					$("#theForm").remove();
					$("#nameLabel").remove();
					$("#emailLabel").remove();
					$("#members").remove();
					//
					$("#close").fadeOut(500, function() { 
						$("#mobileContent").remove("#close"); 					
					});
					//end remove the dynamically loaded in divs			
				}			
				//end populate in names								 			  
			});
			//end stopTheRecursionMadness
			}
		}
			//end parse JSON
			//end overlay
		});
		//end member click function next line
	});

function isValidEmailAddress(emailAddress) {
 		var pattern = new RegExp(/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i);
 		return pattern.test(emailAddress);
	}
