var currentUploadField = 1;

var currentAwardsFeature = 0;
var currentAwardsImage = 0;
var busy;
var pg_scroll_current = 0;

sfHover = function() {
	if(document.getElementById("nav"))
	{
		var sfEls = document.getElementById("nav").getElementsByTagName("LI");
		for (var i=0; i< sfEls.length; i++) {
			sfEls[i].onmouseover=function() {
				this.className += " sfhover";
			}
			sfEls[i].onmouseout=function() {
				this.className=this.className.replace(new RegExp(" sfhover\\b"), "");
			}
		}
		$('nav').immediateDescendants().each(function(e,index) {
			Event.observe(e,'mouseover',function(){
				$(e).addClassName('sfhover'+(index+1));
				});
			Event.observe(e,'mouseout',function(){
				$(e).removeClassName('sfhover'+(index+1));
				});
			});
	}
}


Event.observe(window, 'load', sfHover);
Event.observe(window,"load",function(ev) {
	$$("a").each(function(el) {
		if (el.protocol == "mailto:") {
			el.observe("click",function(ev) {
				pageTracker._trackPageview("/mailto/" + this.href);
			});
		}
		if (el.hostname == location.host) {
			var path = el.pathname + el.search;
			var isDoc = path.match(/\.(?:doc|eps|jpg|png|svg|xls|ppt|pdf|xls|zip|txt|vsd|vxd|js|css|rar|exe|wma|mov|avi|wmv|mp3)($|\&|\?)/);
			if (isDoc)
				el.observe("click",trackExternalLinks);
		} else {
			el.observe("click",trackExternalLinks);
		}
	});
	
	
	$$("#awards_submission a.add").each(function(el) {
		el.observe("click",function(ev) {
			Event.stop(ev);
			el.hide();
			
			currentUploadField ++;
			$$("#awards_submission .image_" + currentUploadField).invoke("removeClassName", "hide");
			
			$("total_uploads").value = currentUploadField;
		});
	});
	
	if (Prototype.Browser.IE) {
		function bodySelectClick(ev) {
			if (ev.element().tagName != "SELECT") {
				$$(".ie_select").invoke("setStyle",{ width: "88px" });
				$$("body")[0].stopObserving("click",bodySelectClick);
			}
		}
		// Fix some select boxes in IE
		$$(".ie_select").invoke("observe","mousedown",function(ev) {
			this.setStyle({ width: "auto" });
			$$("body")[0].observe("click",bodySelectClick);
		});
		$$(".ie_select").invoke("observe","change",function(ev) {
			this.setStyle({ width: "88px" });
			$$("body")[0].stopObserving("click",bodySelectClick);
		});
	}
	
	//TargetBlank plugin ported from jquery:
	$$("a._blank").each(function(el) {
		el.observe("click",function(ev) {
			Event.stop(ev);
			window.open(el.href);
		});
	});
	
	$$(".awards_feature_container a.prev, .awards_feature_container a.next").each(function(el) {
		el.observe("click",function(ev) {
			Event.stop(ev);
			if($$(".awards_feature_container .detail").length > 1)
			{
				currentAwardsFeature = (el.hasClassName("next")) ? currentAwardsFeature + 1 : currentAwardsFeature - 1;
				if(currentAwardsFeature > $$(".awards_feature_container .detail").length - 1)
				{
					currentAwardsFeature = 0;
				}
				if(currentAwardsFeature < 0)
				{
					currentAwardsFeature = $$(".awards_feature_container .detail").length - 1;
				}
				$$(".awards_feature_container .detail.selected")[0].removeClassName("selected");
				$$(".awards_feature_container .detail")[currentAwardsFeature].addClassName("selected");
				
				$$(".awards_feature_container .current .number")[0].update(currentAwardsFeature + 1);
			}
		});
	});
	
	$$("#category_select").each(function(el) {
		el.observe("change", function(ev) {
			window.location = this.value;
		});
	});
	
	$$(".awards_photo_gallery a.prev, .awards_photo_gallery a.next").each(function(el) {
		el.observe("click",function(ev) {
			Event.stop(ev);
			if($$(".awards_photo_gallery .images img").length > 1)
			{
				currentAwardsImage = (el.hasClassName("next")) ? currentAwardsImage + 1 : currentAwardsImage - 1;
				if(currentAwardsImage > $$(".awards_photo_gallery .images img").length - 1)
				{
					currentAwardsImage = 0;
				}
				if(currentAwardsImage < 0)
				{
					currentAwardsImage = $$(".awards_photo_gallery .images img").length - 1;
				}
				$$(".awards_photo_gallery .images img.selected")[0].removeClassName("selected");
				$$(".awards_photo_gallery .images img")[currentAwardsImage].addClassName("selected");
				
				var newCaption = $$(".awards_photo_gallery .images img")[currentAwardsImage].readAttribute("alt");
				$$(".awards_photo_gallery .caption")[0].update(newCaption);
				
				$$(".awards_photo_gallery .current .number")[0].update(currentAwardsImage + 1);
			}
		});
	});
	
	// RESEARCH ARCHIVE STUFF
	
	// Open / close the advanced search
	$$(".search_box .checkbox").click(function(ev) {
		if (this.checked) {
			$$(".ra_advanced").invoke("show");
		} else {
			$$(".ra_advanced").invoke("hide");
		}
	});
	$$(".ra_advanced .close").click(function(ev) {
		ev.stop();
		this.up("div").hide();
		$$(".search_box .checkbox")[0].checked = false;
	});
	
	
	//-----
	$$(".award_callout").each(function(el){
		el.observe("mouseover", function(ev) {
			el.down(".hidden").style.display = "block";
		});
		el.observe("mouseout", function(ev) {
			el.down(".hidden").style.display = "none";
		});
	});
	
	
	// !Photo Galleries
	$$(".pg_scroll_right").click(function(ev) {
		ev.stop();
		if (busy)
			return;
		count = $$(".photo_gallery li").length;
		if (count > ((pg_scroll_current + 1) * 4)) {
			busy = true;
			pg_scroll_current++;
			new Effect.Morph($$(".photo_gallery ul")[0], { style: "margin-left: -" + (pg_scroll_current * 452) + "px", duration: 0.5, afterFinish: function() { busy = false; } });
		}
	});
	$$(".pg_scroll_left").click(function(ev) {
		ev.stop();
		if (busy)
			return;
		if (pg_scroll_current > 0) {
			busy = true;
			pg_scroll_current--;
			new Effect.Morph($$(".photo_gallery ul")[0], { style: "margin-left: -" + (pg_scroll_current * 452) + "px", duration: 0.5, afterFinish: function() { busy = false; } });
		}
	});
	$$(".photo_gallery li a").click(function(ev) {
		ev.stop();
		$$(".photo_gallery li a.selected")[0].removeClassName("selected");
		this.addClassName("selected");
		$$(".photo_gallery .big_image img")[0].setAttribute("src",this.readAttribute("href"));
		if (this.readAttribute("title")) {
			$$(".photo_gallery .big_image p")[0].show().innerHTML = this.readAttribute("title");
		 } else {
		 	$$(".photo_gallery .big_image p")[0].hide();
		 }
	});
	
	$$(".clickshow").click(function(ev) {
		ev.stop();
		this.toggleClassName("clickshown");
		this.up().next("div").toggle();
	});
});

function trackExternalLinks (evnt) {
        var e = (evnt.srcElement) ? evnt.srcElement : this;
        while (e.tagName != "A") {
                e = e.parentNode;
        }
        var lnk = (e.pathname.charAt(0) == "/") ? e.pathname : "/" + e.pathname;
        if (e.search && e.pathname.indexOf(e.search) == -1) lnk += e.search;
        if (e.hostname != location.host) lnk = "/external/" + e.hostname + lnk;
        if (typeof(pageTracker) == "object") pageTracker._trackPageview(lnk); 
}


