$(document).ready(function(){





		
		/*	sifr replacements	*/
		$("#main > h1").sifr({
			font: 'officina-sans-bold',
			path: '/assets/js/'
		});
					
		/* pathetic photo caption hack!	*/
		$(".photo").each(function(i) {
			$(this).width( $(this).children("img").width() );			
		});
		
		/* try to remove elements from stories/news?	*/
		$("div.appArea > table[cellspacing='0'] tr:first").remove();
		$("div.appArea > table[cellspacing='0'] td[width='20%']").remove();
		$("div.appArea > table[cellspacing='0'] td[width='78%']").attr("width","95%");
	
		/* override dropdowns on interior pages */	
		$("#secondary_nav ul").replaceWith($("li#current ul"));
		$("#secondary_nav li").hover(function() {
			$(this).addClass("hovered");
		}, function () {
			$(this).removeClass("hovered");
		});
						
		/* make sure hovers work	*/
		$("div.button").hover(function(){
			$(this).addClass('hover');
		}, function(){
			$(this).removeClass('hover');
		});
		
		/* dropdowns	*/
    $('ul.sf-menu').superfish({
    	delay:       400,
      animation:   {opacity:'show',height:'show'},
      speed:       'fast',
      autoArrows:  false,
      dropShadows: true
    }); 
    
		/* feature rotator controls */
		$("#slides").cycle({
			timeout:7000,
			speed:1000,
			pause:0,
			pager:'#controls',
			pagerAnchorBuilder: function(idx,slide) { return "<a href=\"#\"><div id=\"n" + (idx+1) +"\" class=\"control\"></div></a>" }
		});
		
		$("#controls #pause").click(function(){
			$("#slides").cycle('pause');
			$(this).hide();
			$("#controls #resume").show();
		});
		
		$("#controls > div").hover(function(){
			$(this).addClass('hover');
		}, function() {
			$(this).removeClass('hover');
		});

		$("#controls #resume").click(function(){
			$("#slides").cycle('resume');
			$(this).hide();
			$("#controls #pause").show();
		}); 

		/* form field resets */
		$("#signup #email").focus( function() {handleInput("email address","focus", $(this) )} ).blur( function() {handleInput("email address","blur",$(this) )});
		$("#signup #zip").focus( function() {handleInput("zip","focus",$(this) )} ).blur( function() {handleInput("zip","blur",$(this) )});
		$("#search #searchphrase").focus( function() {handleInput("search thinkcure!","focus",$(this) )} ).blur( function() {handleInput("search thinkcure!","blur",$(this) )});
	
		function handleInput( string, event, obj ) {
			if (obj.val() == string && event == 'focus') obj.val("");
			if (obj.val() == "" && event == 'blur') obj.val(string);
		}

		/*	form processing	*/
		$('#signup_submit').click(function() {
			if (isEmail($("#get_updates #email").val()) && isZip($("#get_updates #zip").val())) {
				$('form#get_updates').submit();
			}
		});

		$('#search_submit').click(function() {
			$('form#searchform').submit();
		});
		
		//checks for a valid zip code
		function isZip( theval ) {
			var filter = /^([0-9]{5})+$/;
			if (!filter.test( theval )) {
				alert("Please enter a valid zip code"); 
				return false;
			}
			return true;					
		}

		//checks for a valid email address   
		function isEmail( theval ) {
			var filter  = /^([a-zA-Z0-9_\.\-\+])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
			if ( !filter.test( theval ) || "" == theval ) {
				alert("Please enter a valid email address");
				return false;
			}
			return true;		
		}
			
		//make donate now box linkable 
		$("#donate_now").click(function() {
			path = $(this).children("a#donate_online_now").attr("href");
			window.location = $(this).children("a#donate_online_now").attr("href");
		});
			
	});


function setactivenav() {
	
	var url_to_element = new Array();
	url_to_element["about"] = "#about";
	url_to_element["stories"] = "#stories";
	url_to_element["events"] = "#events";
	url_to_element["research"] = "#research";
	url_to_element["support"] = "#support";	
	url_to_element["News2"] = "#stories";
	url_to_element["Survey"] = "#support";
	url_to_element["Donation2"] = "#support";
	
	current_path = window.location.pathname;
	dirs = current_path.split("/");
	
	if (typeof(url_to_element[dirs[3]]) != 'undefined') {
		the_element = url_to_element[dirs[3]];
		$("ul#nav "+the_element).attr("id","current");
	} else if (typeof(url_to_element[dirs[2]]) != 'undefined') {
		the_element = url_to_element[dirs[2]];
		$("ul#nav "+the_element).attr("id","current");	
	}
	
	//eliminate sub-subnav
	$("ul#nav #current li ul").remove();
	
}