﻿initNavIE = function() {
	if (document.all && document.getElementById) {
		nav_root = document.getElementById("nav").firstChild;
		for (i = 0; i < nav_root.childNodes.length; i++) {
			node = nav_root.childNodes[i];
			if (node.id && node.id.indexOf("nav_") != -1) {
				node.onmouseover = function() {
					this.className += " over";
					// hack for IE6 and the page_top select
					if (this.id == "nav_about") if (pt = document.getElementById("page_top")) pt.style.visibility = "hidden";
				}
				node.onmouseout = function() {
					this.className = this.className.replace(" over", "");
					// hack for IE6 and the page_top select
					if (this.id == "nav_about") if (pt = document.getElementById("page_top")) pt.style.visibility = "visible";
				}
			}
		}
	}
}

// rollover event handlers
function rollover(e, args) { e.target.src = args.rimg.src; }
function rollout(e, args) { e.target.src = args.img.src; }

// add rollover handler for any img and input elements with the 'rollover' class
window.addEvent("domready", function() {
	var rollovers = $$("img.rollover, input.rollover");
	for (var x = 0; x < rollovers.length; x++) {
		if (rollovers[x].src.indexOf(".png") != -1) {
			var left = rollovers[x].src.substring(0, rollovers[x].src.indexOf(".png"));
			var right = rollovers[x].src.substring(rollovers[x].src.indexOf(".png"), rollovers[x].src.length);
			
			var img = new Image();
			img.src = rollovers[x].src;
			var rimg = new Image();
			rimg.src = left + "-over" + right;

			rollovers[x].addEvent("mouseenter", rollover.bindWithEvent(rollovers[x], { rimg: rimg }));
			rollovers[x].addEvent("mouseleave", rollout.bindWithEvent(rollovers[x], { img: img }));
		}
	}
	
	var tweets_to_show = 1;
	new Request.Twitter('SenJeffMerkley', {
		data: { count: 20 }, // count is the number it grabs.  there's a bug with retweets, hence the tweets_to_show
		onSuccess: function(tweets){
			for (var i = tweets_to_show; i--; )
				new Element('div', {
					'class': 'tweet',
					'html': tweets[i].text
				}).inject('tweets', 'top');
		}
	}).send();
});

// newsletter signup functions

function validateNewsletterEmail() {
	n = $("notice");
	n.style.display = "none";
	n.innerHTML = "";
	
	var e = document.getElementById("newsletter_email");

	if (e.value == "") {
		//alert("Please enter an email address in the field provided!");
		n.innerHTML = "Please enter an email address!";
		n.style.display = "block";
		return false;
	} else if (e.value.indexOf("@") == -1 || e.value.indexOf(".") == -1) {
		//alert("Please enter a valid email address in the field provided!");
		$("notice").innerHTML = "Please enter a valid email address!";
		n.style.display = "block";
		return false;
	} else {
		return true;
		/*
		e.disabled = true;
		$("sign_up").disabled = true;
		var s = $("signup_indicator");
		s.innerHTML = "Please wait...";
		s.style.display = "block";

		new Ajax(application_webroot + "cfc_extensions/com/creativengine/newsletter.cfc", {
			postBody: "method=signup&email=" + encodeURIComponent(e.value),
			onComplete: addedEmail,
			evalScripts: true
		}).request();
		*/
	}
	//return false;
}

function addedEmail(request) {
	$("signup_indicator").style.display = "none";
	$("newsletter_email").disabled = false;
	$("sign_up").disabled = false;
	
	$("confirmed_email").innerHTML = request;
	$("signup_text").style.display = "none";
	$("form_newsletter").style.display = "none";
	$("signup_confirmation").style.display = "block";
}

// misc window opening functions

//leaving the senate servers so we have to display this message:
function openWin(urlToOpen) {
	window.open("http://www.senate.gov/cgi-bin/exitmsg?url=" + urlToOpen);	
}

//leaving the current site, but staying on senate servers.
function openSenateWin(urlToOpen) {
	window.open(urlToOpen);	
}


function address(which) {
	document.getElementById("addresses").className = "sel_" + which;
	document.getElementById("office-location").className = "sel_" + which;
	document.getElementById("location-photo").className = "sel_" + which;
}

// Twitter

Request.Twitter = new Class({

	Extends: Request.JSONP,
	
	options: {
		linkify: true,
		url: 'http://twitter.com/statuses/user_timeline/{term}.json',
		data: {
			count: 10
			}
	},
	
	initialize: function(term, options){
		this.parent(options);
		this.options.url = this.options.url.substitute({term: term});
	},
	
	success: function(data, script){
		if (this.options.linkify) {data.each(function(tweet){
			tweet.text = this.linkify(tweet.text);
			}, this);
		}
		
		if (data[0]) this.options.data.since_id = data[0].id; // keep subsequent calls newer

		this.parent(data, script);
	},
	
	linkify: function(text){
		// modified from TwitterGitter by David Walsh (davidwalsh.name)
		// courtesy of Jeremy Parrish (rrish.org)
		//return text.replace(/(https?:\/\/[\w\-:;?&=+.%#\/]+)/gi, '<a href="$1">$1</a>')
		return text.replace(/(https?:\/\/[\w\-:;?&=+.%#\/]+)/gi, '<a href="$1">$1</a>')
				   .replace(/(^|\W)@(\w+)/g, '$1<a href="http://twitter.com/$2">@$2</a>')
				   .replace(/(^|\W)#(\w+)/g, '$1#<a href="http://search.twitter.com/search?q=%23$2">$2</a>');
	}
	
});


