/*
Western Pennsylvania Conservancy
General JavaScript file
15 November 2007
*/

/**
 * Interval variable to be used for custom section page menu behavior.
 */
var submenuInterval;

var wwAddLoadEvent = function(func) {
	var oldonload = window.onload;
	if (typeof window.onload != 'function') {
		window.onload = func;
	} else {
		window.onload = function() {
			if (oldonload) {
				oldonload();
			}
			func();
		}
	}
};

/**
 * document.ready
 */
$(function(){
	// enable W|W custom menu.
	$("#wrapper").filter(".home, .section, .interior").each(function(){
		$("#menu").wwMenu({
			mode: 'vertical'
		});
	});
	
	// turn on hints for all search boxes.
	$(".search_box").hint();
	
	// wrap the left and right columns so we can slide them out
	$("#wrapper").filter(".home").each(function(){
		$('#navigation').add($("#menu > ul.main > li > a")).add($(".submenu")).hover(navOverFunction, mainOutFunction);
	});
});

window.onresize = function(){
	// make sure we reposition the current menu on window resize.
	$("#wrapper").filter(".section").each(function(){
		$("a.currentSection").each(function(){
			showCurrent($(this));
		});
	});
};

wwAddLoadEvent(function(){
	$("#wrapper").filter(".section").each(function(){
		// show section menu on load.
		$("a.currentSection").each(function(){
			showCurrent($(this));
		});
		
		// behavior to make section menu reappear.
		$("#menu > ul.main > li > a").bind('mouseover', function(){
			clearInterval(submenuInterval);
		});
		// make it so the current section's submenu doesn't disappear when you mouse off it.
		// unbinds the default mouseout behavior from nav.js
		$("a.currentSection").each(function(){
			var id = $(this).parent('li').attr('id').split('_link')[0];
			$("#"+id).unbind('mouseout');
		});
		// bind custom interval-based behavior to submenu and menu-link mouseout events.
		$(".submenu").add($("#menu > ul.main > li > a")).bind('mouseout', showCurrentMenu);
	});
});

/**
 * 
 */
var showCurrentMenu = function(){
	// clear any existing pauses on the interval.
	clearInterval(submenuInterval);
	// set the interval.
	submenuInterval = setInterval(function(){
		if(!($(".submenu").filter(function(){ return $(this).css("display") != 'none'; }).length)){
			$("a.currentSection").each(function(){
				showCurrent($(this));
			});
			// clear the interval once we're done with it.
			clearInterval(submenuInterval);
		}
	}, 400);
};

/**
 * Show current section's menu cross-browser.
 * 
 * @param jQueryNode currentLink 
 */
var showCurrent = function(currentLink){
	var id = currentLink.parent('li').attr('id').split('_link')[0];
	var menu = $("#"+id);
	// alert(id);
	if($.browser.msie && parseFloat($.browser.version) <= 6){ // ie6
		menu.each(function(){
			$(this).trigger('mousemove');
		});
	}
	else{ // all others
		currentLink.trigger('mouseover');
	}
};

/**
 * add timeline functionality
 */
$(function(){
	// $('.timeline_details').hide().css({height:331, overflow:'auto'});
	$('.timeline_group').each(function(){
		// properly wrap the interior content
		$(this).next('.timeline_details').wrapInner("<div class='details_wrapper'></div>").append("<div class='closebox'>&nbsp;</div>").prepend($(this).children('h2').clone().addClass('timeline_details_header'));
		$('.closebox').click(function(){
			tb_remove();
		}).hover(
			function(){
				$(this).css({cursor: 'pointer'});
			},
			function(){
				$(this).css({cursor: 'normal'});
			}
		);
		$(this).hover(
			function(){
				$(this).css({cursor: 'pointer'});
			},
			function(){
				$(this).css({cursor: 'normal'});
			}
		).click(function(){
			tb_show("", '#TB_inline?height=345&width=555&modal=true&inlineId='+$(this).next('.timeline_details').attr('id'), false);
		});
	});
});

/**
 * home page behavior to fade out side columns.
 */
var navOverFunction = function(){
	var options = {
		opacity: 0
	};
	// $('#left_column').stop().fadeOut();
	$('#left_column').stop().animate(options, 150);
	$('#right_column').stop().animate(options, 150);
	$('.flexcroll').stop().animate(options, 150);
};

/**
 * home page behavior to fade in side columns.
 */
var mainOutFunction = function(){
	var options = {
		opacity: 1
	};
	$('#left_column').stop().animate(options, 150);
	$('#right_column').stop().animate(options, 150);
	$('.flexcroll').stop().animate(options, 150);			
};

