// Email Updates Slidedown

$(document)
.ready(function(){
	var moreInfoDiv = $('#moreInfoDiv');
	var wrapDiv = $('.wrap');
	var bgDiv = $('#bg');
	$('.moreInfo')
	.click(function(){
		
		
		if( moreInfoDiv.data("opened") != "yes" ){
			moreInfoDiv.data("opened", "yes")
			/*
			.stop()
			.animate({top:0},100);*/
			
			bgDiv
			.stop()
			.animate({top:55},1500,'easeInOutExpo');
			/*wrapDiv
			.stop()
			.animate({paddingTop:555},100);*/
			
		}
		else{
			moreInfoDiv.data("opened", "no")
			/*.stop()
			.animate({top:-400},100);
			*/
			bgDiv
			.stop()
			.animate({top:5},1500,'easeOutBounce');
			/*wrapDiv
			.stop()
			.animate({paddingTop:155},100);*/
			
		};
		
		return false;
	});
	

});


// JQuery Twitter

$(document).ready(function() {
	$("#twitter").getTwitter({
		userName: "ussoccerfndn",
		numTweets: 1,
		loaderText: "Loading tweets...",
		slideIn: false,
		showHeading: true,
		headingText: "Latest From Twitter",
		showProfileLink: true
	});
});


// Cufon

Cufon.replace(('.navCategory'), { hover:true, separate:'none' });
Cufon.replace(('.navCategorySelected'), { hover:true, separate:'none' });
Cufon.replace(('#support a'), { hover:true, separate:'none' });
Cufon.replace(('#section h2'), { fontFamily: 'apex sans', separate:'none' });
Cufon.replace(('#navSubTitle'), { fontFamily: 'apex sans', separate:'none' });
Cufon.replace(('#butSee'), { hover:true, separate:'none' });
Cufon.replace(('#butBecome'), { hover:true, separate:'none' });
Cufon.replace(('.content h3'), { separate:'none' });


// JQuery Tabs
$(function () {
	var tabContainers = $('div.tabs > div');
	tabContainers.hide().filter(':first').show();
	
	$('div.tabs ul.tabNavigation a').click(function () {
		tabContainers.hide();
		tabContainers.filter(this.hash).show();
		$('div.tabs ul.tabNavigation a').removeClass('selected');
		$(this).addClass('selected');
		return false;
	}).filter(':first').click();
});


// Open link in a new window/tab
$(function(){
	$('a.windowNew').click(function(){
		window.open(this.href);
		return false;
	});
});

// Open link in a new window/tab
$(function(){
	$('#twitter a').click(function(){
		window.open(this.href);
		return false;
	});
});

// Open link in a new window/tab
$(function(){
	$('.flickr_badge_image a').click(function(){
		window.open(this.href);
		return false;
	});
});

// Clear functions

function clickclear(thisfield, defaulttext)
{
	if (thisfield.value == defaulttext)
	{
		thisfield.value = "";
	}
}
function clickrecall(thisfield, defaulttext)
{
	if (thisfield.value == "")
	{
		thisfield.value = defaulttext;
	}
}


// Displaying the h3 over the left nav dynamically

$(document).ready(function() {
	var title = $('.navCategorySelected').text();
	$('#navSubTitle').html(title);
	Cufon.refresh('#navSubTitle');	
});


// Navigation highlight state fix
$(function(){
	$('a.navCategory').mouseover(function(){
		$(this).css("color", "#f2cc0d");		
		Cufon.refresh('.navCategory');			
	});
});
$(function(){
	$('a.navCategorySelected').mouseover(function(){
		$(this).css("color", "#f2cc0d");		
		Cufon.refresh('.navCategorySelected');			
	});
});
$(function(){
	$('a.navCategory').mouseleave(function(){
		$(this).css("cssText", "");		
		Cufon.refresh('.navCategory');			
	});
});
$(function(){
	$('a.navCategorySelected').mouseleave(function(){
		$(this).css("cssText", "");		
		Cufon.refresh('.navCategorySelected');			
	});
});
$(function(){
	$('.dir ul').mouseleave(function(){
		$("a.navCategory").css("color", "#ffffff");		
		Cufon.refresh('.navCategory');			
	});
});
$(function(){
	$('.dir ul').mouseleave(function(){
		$("a.navCategorySelected").css("color", "#ffffff");		
		Cufon.refresh('.navCategorySelected');			
	});
});	


// Navigation dropdown fix

$(document).ready(function() {
	$(".dir li:last-child").css({display:"none"});
	$("#navSub li ul li:last-child").css({display:"none"});
	$("#navigation li.dir:eq(4)").addClass("rtl");
});




function emailCheck (fieldname,blankOK,emailStr) {

var emailPat=/^(.+)@(.+)$/;
var specialChars="\\(\\)<>@,;:\\\\\\\"\\.\\[\\]";
var validChars="\[^\\s" + specialChars + "\]";
var quotedUser="(\"[^\"]*\")";
var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/;
var atom=validChars + '+';
var word="(" + atom + "|" + quotedUser + ")";
var userPat=new RegExp("^" + word + "(\\." + word + ")*$");
var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$");

if(emailStr=="") {
	if (blankOK==true) {
		return true;
	} else {
		alert(fieldname + " cannot be blank");
		return false;
	}
}
	emailStr = emailStr.replace(/\s/g,"");
var matchArray=emailStr.match(emailPat);
if (matchArray==null) {
	alert(fieldname + ": Seems incorrect (check @ and .'s)")
	return false
}
var user=matchArray[1]
var domain=matchArray[2]

// See if "user" is valid 
if (user.match(userPat)==null) {
    // user is not valid
    alert(fieldname + ": The username doesn't seem to be valid.")
    return false
}

/* if the e-mail address is at an IP address (as opposed to a symbolic
   host name) make sure the IP address is valid. */
var IPArray=domain.match(ipDomainPat)
if (IPArray!=null) {
    // this is an IP address
	  for (var i=1;i<=4;i++) {
	    if (IPArray[i]>255) {
	        alert(fieldname + ":Destination IP address is invalid!")
		return false
	    }
    }
    return true
}

// Domain is symbolic name
var domainArray=domain.match(domainPat)
if (domainArray==null) {
	alert(fieldname + ":The domain name doesn't seem to be valid.")
    return false
}

/* domain name seems valid, but now make sure that it ends in a
   two or more letter domain or country (allowing for new >3 domains), and that there's a hostname preceding 
   the domain or country. */

/* Now we need to break up the domain to get a count of how many atoms
   it consists of. */
var atomPat=new RegExp(atom,"g")
var domArr=domain.match(atomPat)
var len=domArr.length
if (domArr[domArr.length-1].length<2 ) {
   // the address must end in a two or more letter domain name .
   alert(fieldname + ":The address must end in a two or more letter domain (e.g., com, edu, org).")
   return false
}

// Make sure there's a host name preceding the domain.
if (len<2) {
   var errStr=": This address is missing a hostname!"
   alert(fieldname + errStr)
   return false
}

// If we've gotten this far, everything's valid!
return true;
}

function notBlank(fieldname,value){

if (value == "") {
	alert(fieldname + " can't be blank");
	return false;
}
return true;
}
function notNull(fieldname,value){

if (value == null) {
	alert(fieldname + ": a value must be selected");
	return false;
}
return true;
}


$(document).ready(function(){

	/*check for  a newsletter signup contact form*/
	if ($("#ContactForm"))
	{
		/*alert("yep -- there is a contact form");*/
		
		/*check for a querystring*/
		if (window.location.search)
		{			
			/*alert("yep -- there is a querystring");*/
			
			/*check for an email address name/value pair*/
			if (window.location.search.indexOf("em")>-1)
			{			
				/*alert("yep -- there is an email address in the querystring");*/		
				
				/*extract the email address from the querystring by splitting it into substrings*/
				
				var email = window.location.search.split("em=")[1]; 
				/*alert(email);*/

				
				/*check for additional name value pairs, chop them if they exist, and de-urlencode email address*/
				if (email.indexOf("&")>0)
				{
					var email = email.split("&")[0];
					var email = unescape(email);
					/*alert(email);*/
					
					
					/*Now find the email input field, and populate with the extracted, properly formatted email address*/
					
					$("input[name='email_address']").val(email)
					
				}
				
			}	
			
			if (window.location.search.indexOf("inputZipcode")>-1)
			{			
			
				var zipcode = window.location.search.split("inputZipcode=")[1]; 			
	
				if (zipcode.indexOf("&")>0)
				{
					var zipcode = zipcode.split("&")[0];
					var zipcode = unescape(zipcode);
					/*alert(zipcode);*/
						
						
					/*Now find the email input field, and populate with the extracted, properly formatted email address*/
					if (zipcode!="Zip+Code") {		
						$("input[name='zip']").val(zipcode)
						
					}
				}
			}

		}
		
	}
										   
})





// Random stage load

$(document).ready(function() {
	//Get random number:1 to 3	
	var rand_no = Math.floor((3)*Math.random());
	// Div id array
	var div_id=new Array("stageLearn","stageGrow","stagePlay");

	//Show current div
	document.getElementById(div_id[rand_no]).style.display="block";
});