Event.observe(window,"load",function(ev) {

	// Clear default values for inputs on focus
	$$(".default").each(function(el) {
		el.observe("focus", function(ev) {
			if (this.value == this.defaultValue)
				this.value = "";
		});
		el.observe("blur", function(ev) {
			if (this.value == "")
				this.value = this.defaultValue;
		});
	});

	// Newsletter sign-up
	$$(".newsletter_signup").invoke("observe","submit", function(ev) {
		ev.stop();
		input = this.down("input");
		if (input.value == "" || input.value == input.defaultValue) {
			throwAlert("Please enter your email address to sign up.");
		} else {
			var regex = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
			if (regex.test(input.value))
				new Ajax.Updater($$(".newsletter_signup")[0],"http://www.familytreemd.org/modules/newsletter-signup.php?email=" + escape(input.value));
			else
				alert("Please enter a valid email address.");
		}		
	});

	// Embed audio players.
	try { AudioPlayer.Embed(); } catch (er) { atimer = setTimeout("AudioPlayer.Embed();",1000);	}

	// Embed dropdowns.
	ProtoDrop.Embed();
	
	// Start Photo Galleries
	PhotoGallery.Init();
	
	// Fix dropdowns in IE6/7
	if (Prototype.Browser.IE) {
		$$("#nav > ul > li").invoke("observe","mouseover",function(ev) {
			this.addClassName("hovered");
		});
		$$("#nav > ul > li").invoke("observe","mouseout",function(ev) {
			this.removeClassName("hovered");	
		});
	}
	
});

function throwAlert(msg) {
	alert(msg);
}

// Photo Gallery

var PhotoGallery = {

	Init: function() {
		$$(".photo_gallery a").invoke("observe", "click", function(ev) {
			ev.stop();
			if (this.hasClassName("pg_previous")) {
				// Handle scrolling left.
				if (this.hasClassName("enabled")) {
					inner = this.up().down(".pg_inner");
					left = parseInt(inner.getStyle("margin-left")) + 220;
					page = left / 220 * -1;
					this.up().down(".pg_next").addClassName("enabled");
					if (page == 0)
						this.removeClassName("enabled");
					new Effect.Morph(inner, { style: "margin-left: " + left + "px", duration: 0.5 });
				}
			} else if (this.hasClassName("pg_next")) {
				// Handle scrolling right.
				if (this.hasClassName("enabled")) {
					inner = this.up().down(".pg_inner");
					left = parseInt(inner.getStyle("margin-left")) - 220;
					page = left / 220 * -1;
					this.up().down(".pg_previous").addClassName("enabled");
					pages = Math.ceil(inner.childElements().length / 5);
					if ((page + 1) == pages)
						this.removeClassName("enabled");
					new Effect.Morph(inner, { style: "margin-left: " + left + "px", duration: 0.5 });
				}				
			} else {
				pg = this.up(".photo_gallery");
				pg.down(".large").src = this.readAttribute("href");
				pg.down(".caption p").innerHTML = this.readAttribute("title");
				pg.down("a.selected").removeClassName("selected");
				this.addClassName("selected");
			}
		});
	}

}

// Dropdown plugin for Prototype / Scriptaculous
var ProtoDrop = {

	Embed: function() {
		$$(".dropdown > a").invoke("observe","click", ProtoDrop.Drop); 
	},
	
	Drop: function(ev) {
		ev.stop();
	    // If the dropdown isn't visible...
	    if (this.next("ul").style.display == "none") {
	    	new Effect.toggle(this.next("ul"), "blind", { duration: 0.25 });
	    
	    	// Check the body for clicks, like a normal dropdown would
	    	Event.observe($$("body")[0],"click",function(ev) {
	    		$$(".dropdown > ul").each(function(el) { if (el.style.display != "none") { new Effect.BlindUp(el, { duration: 0.25 }); } });
	    		Event.stopObserving($$("body")[0]);
	    	});
	    	
	    	// Only observe the clicks on the currently dropped list.  That way any other click is going to cause the normal body click and close this dropdown.
	    	x = 0;
	    	while (this.next("ul").down("a",x)) {
	    		this.next("ul").down("a",x).observe("click",function(ev) {
	    			ev.stop();
	    			newLink = new Element("a", { href: "#" });
	    			newLink.observe("click", ProtoDrop.Drop);
	    			newLink.innerHTML = this.innerHTML;
	    			this.up("ul").previous("a").replace(newLink);
	    			this.up("ul").previous("input").value = this.readAttribute("href").substr(1);
	    			new Effect.toggle(this.up("ul"),"blind", { duration: 0.25 });
	    			this.up("ul").previous("input").fire("dropdown:change");
	    		});
	    		x++;
	    	}
	
	    } else {
	    	// Stop all the events and Blind Up.
	    	$$(".dropdown ul a").each(function(el) { Event.stopObserving(el); });
	    	Event.stopObserving($$("body")[0]);
	    	x = 0; while (this.next("ul").down("a",x)) { Event.stopObserving(this.next("ul").down("a",x)); x++; }
	    	new Effect.toggle(this.next("ul"), "blind", { duration: 0.25 });
	    }
	}
	

}

// SoundManager / Prototype / Scriptaculous audio player
soundManager.debugMode = false;
soundManager.url = www_root + 'swf/';
var sounds = new Array;
var AudioPlayer = {

	Embed: function() {
		$$(".audio_embed").each(function(el) {
			file = el.readAttribute("href");
			id = sounds.length + 1;
			var mySound = soundManager.createSound({ id: "player_" + id, url: file, volume: 100, whileplaying: AudioPlayer.UpdateProgress });
			sounds[sounds.length] = mySound;
					
			player = new Element("div")
			player.className = "ap_audio_player";
			player.id = "player_" + id;
			
			player.innerHTML = '<a href="javascript:AudioPlayer.PlayPause(' + id + ');" class="ap_play"></a><span class="ap_ctime">0:00</span><div class="ap_meter"><span class="ap_progress" style="width: 0px;"></span></div><span class="ap_etime">&mdash;</span><a href="javascript:AudioPlayer.MuteUnmute(' + id + ');" class="ap_mute"></a>';
			el.replace(player);
		});
	},

	PlayPause: function(id) {
		sound = sounds[id - 1];
		if (sound.playState && !sound.paused) {
			$$("#" + sound.sID + " .ap_play")[0].setStyle({ backgroundPosition: "center 0px" });
			sound.pause();
		} else {
			$$("#" + sound.sID + " .ap_play")[0].setStyle({ backgroundPosition: "center -15px" });
			sound.play();
		}	
	},
	
	MuteUnmute: function(id) {
		sound = sounds[id - 1];
		if (!sound.muted) {
			sound.mute();
			$$("#" + sound.sID + " .ap_mute")[0].setStyle({ backgroundPosition: "-0px -12px" });
			sound.muted = true;
		} else {
			sound.unmute();
			$$("#" + sound.sID + " .ap_mute")[0].setStyle({ backgroundPosition: "0px 0px" });	
		}
	},
	
	UpdateProgress: function() {
		progress = Math.round(this.position / this.duration * 1000) / 1000;
		w = Math.round(300 * progress) + "px";
		player = this.sID;
		$$("#" + player + " .ap_progress")[0].setStyle({ width: w });
		$$("#" + player + " .ap_ctime")[0].innerHTML = AudioPlayer.CalculateMinutes(this.position);
		$$("#" + player + " .ap_etime")[0].innerHTML = "-" + AudioPlayer.CalculateMinutes(this.duration - this.position);
	},
	
	CalculateMinutes: function(ms) {
		secs = Math.floor(ms / 1000);
		mins = Math.floor(secs / 60);
		while (secs >= 60)
			secs = secs - 60;
		if (secs < 10)
			return mins + ":0" + secs;
		else
			return mins + ":" + secs;
	}

};