Event.observe(window,"load",function(ev) {

	
	// All the member login stuff.
	$$(".member_login").invoke("observe","click",function(ev) {
		ev.stop();
		new Effect.toggle("member_login", "appear", { duration: 0.5 });
	});

	$$("#member_login_form").invoke("observe","submit",function(ev) {
		ev.stop();
		$$("#member_login_form img")[0].show();
		new Ajax.Request("http://dev.fastspot.com/clients/idfa/modules/login.php", { method: "post", parameters: { form: $("member_login_form").serialize() }, evalScripts: true });
	});
	
	$$("#p_nav > li.members").invoke("observe","mouseenter",function(ev) {
		width = parseInt($$("#p_nav li.members")[0].getWidth());
		left = width - 211;
		$$("#p_nav li.members ul")[0].setStyle({ marginLeft: left + "px" });
	});
	$$("#p_nav > li.members").invoke("observe","mouseleave",function(ev) {
		$$("#p_nav li.members ul")[0].setStyle({ marginLeft: "-10000px" });
	});
	
	$$(".forgot_password").invoke("observe","click",function(ev) {
		ev.stop();
		user = this.previous("input",1).value;
		if (user == "")
			throwAlert("Please enter your Username and click the Forgot Password link again and we will email you your password.");
		else
			new Ajax.Request("http://dev.fastspot.com/clients/idfa/modules/forgot-password.php?user=" + escape(user));
	});
	
	// Add to favorites.
	$$(".favoritify").invoke("observe","click",function(ev) {
		ev.stop();
		new Ajax.Request("http://dev.fastspot.com/clients/idfa/modules/favoritify.php?data=" + escape(this.readAttribute("name")), { evalScripts: true });
	});
	
	// Clear defaults on input focus
	$$(".default").invoke("observe","focus",function(ev) { if (this.value == this.defaultValue) { this.value = ""; } });
	$$(".default").invoke("observe","blur",function(ev) { if (this.value == "") { this.value = this.defaultValue; } });
		
	// Embed custom dropdowns
	ProtoDrop.Embed();

	// Search dropdown
	$("searchlocation").observe("dropdown:change",function(ev) {
		if (this.value == "search")
			this.up("form").action = "http://dev.fastspot.com/clients/idfa/search/";
		if (this.value == "buyers")
			this.up("form").action = "http://dev.fastspot.com/clients/idfa/resource-center/directories/buyers-guide/";
		if (this.value == "companies")
			this.up("form").action = "http://dev.fastspot.com/clients/idfa/resource-center/directories/dairy-foods-companies/";
	});
	
	// Print Links
	$$(".print_link").invoke("observe","click",function(ev) {
		ev.stop();
		window.print();
	});
	
	// Email Links
	$$(".email_link").invoke("observe","click",function(ev) {
		ev.stop();
		if ($("email_window"))
			$("email_window").remove();
		ethis = new Element("span", { style: "position: absolute; padding-left: 5px; margin-top: -37px;" });
		ethis.id = "email_window";
		
		if (this.readAttribute("href") != "#")
			url = this.readAttribute("href");
		else
			url = document.location.href;
		
		ethis.innerHTML = '<div style="float: left; width: 10px;"><img src="http://dev.fastspot.com/clients/idfa/images/email-this-arrow.gif" alt="" style="margin-top: 32px;" /></div><div style="float: left; width: 218px; background: #0076C7; padding: 8px; color: #FFF; font-size: 16px; font-family: Georgia; margin-left: -2px;"><span style="float: left; padding-bottom: 8px; padding-left: 3px;">Email This</span><a style="display: block; width: 21px; margin: -3px 0 0 0; height: 21px; float: right;" onclick="$(\'email_window\').remove();"><img src="http://dev.fastspot.com/clients/idfa/images/close-email.jpg" alt="" /></a><div style="clear: both; width: 202px; background: #FFF; padding: 8px; color: #333; font-size: 12px; font-family: Arial;"><form method="post" onsubmit="emailThisForm(); return false;" id="email_form"><input type="hidden" name="url" value="' + url + '" />Your Name:<br /><input name="your_name" type="text" style="background: #F6F6F6; width: 194px; padding: 2px; margin: 3px 0 3px; border: 1px solid #83B9E6; display: block;" />Your Email:<br /><input name="your_email" type="text" style="background: #F6F6F6; width: 194px; padding: 2px; margin: 3px 0 3px; border: 1px solid #83B9E6; display: block;" />Recipient\'s Name:<br /><input name="friend_name" type="text" style="background: #F6F6F6; width: 194px; padding: 2px; margin: 3px 0 3px; border: 1px solid #83B9E6; display: block;" />Recipient\'s Email:<br /><input name="friend_email" type="text" style="background: #F6F6F6; width: 194px; padding: 2px; margin: 3px 0 3px; border: 1px solid #83B9E6; display: block;" />Your Message:<br /><textarea name="message" style="background: #F6F6F6; border: 1px solid #83B9E6; width: 194px; height: 64px; margin: 0; padding: 2px; margin-top: 3px;"></textarea><br /><input type="image" src="http://dev.fastspot.com/clients/idfa/images/submit_button.png" alt="Submit" style="width: auto; height: auto; border: 0; margin: 7px 0 0 0; padding: 0; float: right;" /></form><br class="clear" /></div></div>';
		
		this.insert({ after: ethis });
	});
	

	
	// Alerts
	$$("#news_alert img").invoke("observe","click",function(ev) {
		new Ajax.Request("http://dev.fastspot.com/clients/idfa/modules/ignore-alert.php?id=" + this.readAttribute("name"));
		new Effect.Fade(this.up("div"), { duration: 0.5 });
	});

	
});

function emailThisForm() {
	new Ajax.Request("http://dev.fastspot.com/clients/idfa/modules/send-to-friend.php?" + $("email_form").serialize());
}

function throwAlert(msg) {
	alert(msg);
}

// 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 });
	    }
	}
	

}