var CONFIRM_LEAVE_SITE = "Exit Notice: The appearance of external hyperlinks does not constitute endorsement by the U.S. Army of this Web site or the information, products, or services contained therein. For other than authorized activities such as military exchanges and MWR sites, the U.S. Army does not exercise any editorial control over the information you may find at these locations. Such links are provided consistent with the stated purpose of this Web site.";

var VISIBLE_INTRANET_LINK = "~~~~";
var REAL_INTRANET_LINK = "https://intranet.tatrc.org/";



var ROLLOUT_DELAY = 300;
var FADEIN_TIME = 500;
var FADEOUT_TIME = 500;
var GLOW_CLASS = "div.nav_glow";


var href;
var hideNavInterval;
var curLI;

jQuery(document).ready(function(){

	trace("1 (document).ready");
	
	jQuery("a.navLink, .subNav").hover(
		function(event){
			//var text = jQuery(this).text();
			//trace("Rolled over "+text);
			clearInterval(hideNavInterval);
			var parentLI = jQuery(this).parent();
			showNav(parentLI);
		},
		function(event){
			clearInterval(hideNavInterval);
			hideNavInterval = setInterval("hideCurNav()",ROLLOUT_DELAY);
		}
	);
	
	
	jQuery(GLOW_CLASS).hover(
		function(event){
			var parentLI = jQuery(this).parent();
			
			var shadows = getShadows(parentLI);	
			shadows.hide();
			
			//jQuery(this).css("display","none");
			hideNav(parentLI);		
		}
	);
	
	
	jQuery("a").click(function(event){
        href = jQuery(this).attr("href");
		trace(href);
		
		
		if(href.indexOf(VISIBLE_INTRANET_LINK) != -1)
		{
			event.preventDefault();
			window.open(REAL_INTRANET_LINK);
			return;
		}
		
		var isExternal = (href.indexOf("http") != -1);
		var isMilitary = (href.indexOf(".mil") != -1);
		var isGov = (href.indexOf(".gov") != -1);
		var isTATRC = (href.indexOf("tatrc.org") != -1);

        if(isExternal && !isMilitary && !isGov && !isTATRC)
        {
			try{
			
				jConfirm(CONFIRM_LEAVE_SITE, 'Leave www.tatrc.org?', function(r) {
					trace('Confirmed: ' + r, 'Confirmation Results');
					if(r)
						window.open(href);
				});

				event.preventDefault();
			}
			catch(e)
			{
				var answer = confirm(CONFIRM_LEAVE_SITE);
				if (!answer)
					event.preventDefault();
		     }
        }
    });

	if(jQuery("table.zebra tr:odd"))
		jQuery("table.zebra tr:odd").addClass("oddrow");
	
	if(!(isHome()))
		jQuery.localScroll();
	
	//trace("(gallery).ready");
	if(jQuery(".gallery").length)
	{
		//trace('gallery');
		try{
		jQuery(".gallery a[rel^='prettyPhoto']").prettyPhoto({theme:'dark_rounded'});
		}catch(e){trace("gallery fail")}
	}
	
	if(isIE6())
    {
		jQuery("#nav .links .subNav a ").css("float","left");
		jQuery("#nav .links .subNav a ").css("clear","both");
		
		jQuery("#nav .links li").hover(
          function () {
            jQuery(this).find(".subNav").css("display","block");
          }, 
          function () {
            jQuery(this).find(".subNav").css("display","none");
          }
        );   
    } 
});



function isIE6()
{
	var browser = jQuery.browser;
    var version = browser.version;
    return (version == "6.0" && browser.msie);
}

function isHome()
{
	return window.location.href.indexOf("?p=home")!= -1;
}


//pass in a jQuery(list item)
function showNav(jqLI)
{
	clearInterval(hideNavInterval);
	if(!jqLI)
		return;
	
	
	if(curLI && !navIsCurrent(jqLI))
	{
		//trace("showNav callling hideNav");
		hideNav(curLI);
	}
	curLI = jqLI;
	
	var shadows = getShadows(jqLI);
	//don't for IE (or make thinner 
	if(isIE6())
		return;
		
	if($.browser.msie)
        shadows.show();
	else		
		shadows.fadeIn(FADEIN_TIME);
}

function hideCurNav()
{		
	//trace("hideCurNav()");
	hideNav(curLI);
	curLI = null;
}

//pass in a jQuery(list item)
function hideNav(jqLI)
{
	//trace("hideNav()");
	clearInterval(hideNavInterval);
	if(!jqLI)
		return;
	var shadows = getShadows(jqLI);	
	if(isIE6())
		return;
	if($.browser.msie)
        shadows.hide();
	else
		shadows.fadeOut(FADEOUT_TIME);
}



function getShadows(jqLI)
{
	return jqLI.children(GLOW_CLASS);
}
function getSubNav(jqLI)
{
	return jqLI.children(".subNav");
}
function getText(jqLI)
{
	return jqLI.children("a.navLink").text();
}
function navIsCurrent(jqLI)
{
	return getText(curLI) == getText(jqLI);
}


function openLink()
{
	trace("openLink");
	window.open(href);
	// window.location = href;
}

function getHeight(id)
{
	var el = jQuery("#"+id);
	if(!el)
		return 0;
	var height= el.height();
	var pTop = el.css("padding-top");
	if(pTop)
		pTop = pTop.replace("px","");
	var pBot = el.css("padding-bottom");
	if(pBot)
		pBot = pBot.replace("px","");
	var tHeight = height + parseInt(pTop) + parseInt(pBot);
	return tHeight;
}


function trace(msg)
{
	try{
    if(console)
        console.log(msg);
	}
	catch(e)
	{
	}
}
