/*
	Son of Suckerfish
	http://htmldog.com/articles/suckerfish/dropdowns/
*/
sfHover = function() {
	var sfGlobalNav = document.getElementById("global-nav");
	if (sfGlobalNav){
		var sfEls = document.getElementById("global-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"), "");
			}
		}
	}
}
if (window.attachEvent) window.attachEvent("onload", sfHover);


$(document).ready(function(){
	//Do we have a white sidebar?
	//This is a bit yucky, but will work until a better solutions is found. This is
	//done because the DIV cannot natively stretch vertically to match the height of
	//the div.content-body. So, we fake it...
	if ($('div.site-profiles').length > 0){
		$('div.container').addClass('white-sidebar');
	}

	//The Footer pages have content shorter than the sidebar, this somewhat yucky
	//fix ensures that the white of the content stretches to the bottom of the page.
	//Specifies the height of the footer-page element in EM so that it hold together
	//with a text resize! Yeah!
	if ($('#footer-page').length > 0){
		$('#footer-page').css('height', ($('#site-content').height()/12)+'em');
	}

	//This fixes the IE height bug with the corners script. This bug only seems to
	//happen when the height of the element being cornered is an an even number, so
	//we make it odd.
	$('#home-clusters div').each(function(){
		var el = $(this);
		if (el.height()%2==0) el.height(el.height()+.1);
	});
	
	//Add opacity filter to "active" elements.
	//$('#learn div.cluster div.interviews ul li.active').css('opacity', 0.4);

	$('div.fieldset ul li.active img').css('opacity', 0.4);

	//Add corners.
	var clusterParentBackground = $('div.cluster').parent().css('background-color');
	$('#site-header div ul').cornerz({corners:'tl'});
	$('div.content-body').cornerz({corners:'tl tr', radius:20, background:'#e1eef7'});
	$('div.site-profiles').cornerz({corners:'tl', radius:20, background:'#e1eef7'});
	$('#practice-learn-see-do li.active a span').cornerz({corners:'tl tr', radius:10});
	$('div.cluster div.fieldset ul li.active').cornerz();
	$('div.tabbed-cluster div.pad').cornerz({corners:'tr', radius:20, background:clusterParentBackground});
	$('div.tabbed-cluster ul.tabs li').cornerz({corners:'tl tr', radius:10, background:clusterParentBackground});
	$('div.home div.col-1-a').cornerz({
		corners:'tl',  radius:20, background:'#e7e7e7'
	});
	$('div.home div.col-0 div.cluster, div.arra div.col-0 div.cluster').cornerz({radius:10, background:'#e7e7e7'});
	//$('#home-clusters li div').cornerz({radius:20, background:'#999999'});
	//$('#home-clusters li').cornerz({radius:20});
	$('div.arra div.col-1').cornerz({
		corners:'tl',  radius:20, background:'#e7e7e7'
	});
	$('div.arra div.cluster.first').cornerz({radius:20, background:'#E0EEF7'});
	$('div.arra div.cluster.last').cornerz({corners:'tl tr', radius:20, background:'#E0EEF7'});
	$('div.arra div.empty-media-short').cornerz({radius:20});
	$('#practice').cornerz({corners:'tr', radius:20});
	$('#practice div.cluster').cornerz({corners:'tl tr', radius:20, background:clusterParentBackground});
	$('#see').cornerz({corners:'tl tr', radius:20});
	$('#see div.cluster').cornerz({radius:20, background:clusterParentBackground});
	$('#see div.cluster-links').cornerz({radius:20, background:clusterParentBackground});
	$('#learn').cornerz({corners:'tl tr', radius:20});
	$('#learn div.cluster').cornerz({radius:20, background:clusterParentBackground});
	$('#do').cornerz({corners:'tl', radius:20});
	$('#ideas-for-action').cornerz({corners:'tr br bl', radius:20, background:clusterParentBackground});
	$('#do div.cluster').cornerz({radius:20, background:clusterParentBackground});
	$('#do div.empty-media-short').cornerz({corners:'tl tr'});
	$('#do #cluster-1').prev('div.tabbed-cluster').cornerz({
		corners:'bl br', radius:20, background:clusterParentBackground
	});
	$('#topic').cornerz({corners:'tl tr', radius:20});
	$('#topic div.cluster').cornerz({radius:20, background:clusterParentBackground});
	$('#site-profile, #site-selection').cornerz({corners:'tl tr', radius:20});
	$('#site-profile div.cluster, #site-selection div.cluster').cornerz({
		corners:'tl tr', radius:20, background:clusterParentBackground
	});
	$('#site-profile div.details').cornerz({radius:15});
	$('#site-profile ul.tabs li').cornerz({corners:'tl tr', radius:10});
	$('div.tools-and-about-content div.col-1-a').cornerz({
		corners:'tl',  radius:20, background:'#e7e7e7'
	});
	$('div.tools-and-about-content div.col-1-b div.pad div').cornerz({radius:10, background:'#FCE8B1'});
	$('#site-content.tools-and-about-content ul.tabs li').cornerz({corners:'tl tr', radius:10});
	$('div.subscribe-container, div.share-container').cornerz({radius:20, background:'#FCE8B1'});
	$('div.site-tools div.col-1, div.search div.pad, div.advanced-search div.pad').cornerz({
		radius:20, corners:'tl', background:'#E7E7E7'
	});
	$('div.site-tools div.col-0 li.active').cornerz({radius:7, background:'#e7e7e7'});
	$('div.advanced-search div.cluster').cornerz({radius:20});
	$('div.workshop div.cols').cornerz({corners:'tl', radius:20, background:'#e7e7e7'}); 
	$('#site-content.ideas .col-0').cornerz({corners:'tl tr', radius:20, background:'#e7e7e7'});
	$('.ideas-pop-container').cornerz({radius:20, background:'#ffffff'});
	$('div.inaction').cornerz({radius:15, background:'#E0EEF7'});

	//Each time a top level #global-nav is hovered...
	if ((jQuery.browser.msie && jQuery.browser.version>=7) || !jQuery.browser.msie){ //Do not let IE6 do this.
		$('#global-nav li:has(ul)').hover(
			function(){
				var ul = $(this).find('ul');
				if (ul.length > 0){
					ul = $(ul[0]);

					//ensure dropdown menu is never less wide than the parent menu option.
					if (ul.width() < ul.parent().width())ul.width(ul.parent().width());

					//ensure dropdown menu never hits or goes off the right side of the content.
					var contentOffsetRightEdge = $('#site-content').offset().left + $('#site-content').width();
					var ulRightEdge = ul.offset().left + ul.width() + 10; //add 10 for good measure
					if (ulRightEdge > contentOffsetRightEdge){
						var contentPositionRightEdge = $('#site-content').position().left + $('#site-content').width();
						ul.css(
							'right',
							contentPositionRightEdge - ul.parent().position().left - ul.parent().width()
						);
					}
				}
			},
			function(){
				$(this).find('ul').css('right', null);
			}
		);
	}
	
	//Functionality for home page news bits.
	$('#home-news-bits ul.expandable').addClass('collapsed');
	var expandomarkup = '<a href="#" class="arrow-link expand"><span class="down">More</span></a>';
	$(expandomarkup).insertAfter('#home-news-bits ul.expandable');
	$('#home-news-bits a.expand').click(function(ev){										 
		ev.preventDefault();

		var a = $(this);
		var span = a.find('span');
		var ul = $(this).prev();
		if (ul.hasClass('collapsed')){
			ul.removeClass('collapsed');
			span.text('Minimize');
			span.removeClass('down');
			span.addClass('up');
		}
		else{
			ul.addClass('collapsed');
			span.text('More');
			span.addClass('down');
			span.removeClass('up');
		}
	});

	//Add functionality to site-map.
	var expandoMarkup = '<a href="#" class="expand">expand</a>';
	$('div.site-map dd').hide();
	$('div.site-map dl, div.site-map .dl-header').hide();
	$(expandoMarkup).insertAfter('div.site-map div.topic');
	$(expandoMarkup).insertBefore('div.site-map dt a');
	$('div.site-map a.expand').click(function(ev){
		ev.preventDefault();

		var self = $(this)
		var els = null;
		if (self.parent()[0].nodeName.toLowerCase()=='li')
			els = self.parent().find('.dl-header, dl');
		else
			els = self.parent().next();

		if (els.is(':visible')){
			self.removeClass('collapse');
			els.hide();
		}
		else{
			self.addClass('collapse');
			els.show();
		}
	});

	//Add hover effect to image buttons.
	$('#site-search-submit, #button-subscribe, #button-unsubscribe, #button-send').hover(
		function (){
			$(this).addClass('hover');
		},
		function () {
			$(this).removeClass('hover');
		}
	);
	
	//Add popup links to ideas page.
	var ideaLinks = $('#site-content.ideas li a');
	if (ideaLinks.length > 0){
		ideaLinks.popupWindow({
			width:750, 
			height:475, 
			centerBrowser:1, 
			windowName:'IdeasPopup'
		});
	}
	
	//Print link.
	$('#print-link').click(function(ev){
		ev.preventDefault();
		
		window.print();
	});
});

