var currentUploadField = 1;

var currentAwardsFeature = 0;
var currentAwardsImage = 0;

sfHover = function() {
	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;
		});
	});
	
	
	//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);
			}
		});
	});
});

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); 
}

