var vid;
var navSlideTimeout;

function enableAlphaImages(){
    if ($.browser.msie && (parseInt(jQuery.browser.version) < 7)) {
        var rslt = navigator.appVersion.match(/MSIE (\d+\.\d+)/, '');
        var itsAllGood = (rslt != null && Number(rslt[1]) < 7);
        if (itsAllGood) {
            for (var i=0; i<document.all.length; i++){
                var obj = document.all[i];
                var bg = obj.currentStyle.backgroundImage;
                var img = document.images[i];
                if (bg && bg.match(/\.png/i) != null) {
                    var img = bg.substring(5,bg.length-2);
                    var offset = obj.style["background-position"];
                    obj.style.filter =
                    "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='"+img+"', sizingMethod='crop')";
                    obj.rel = img;
                    obj.style.backgroundImage = "url('/common/images/spacer.gif')";
                    obj.style["background-position"] = offset; // reapply
                } else if (img && (img.src.match(/\.png$/i) != null || img.src.match(/fmt=png-alpha/i) != null)) {
                    var src = img.src;
                    img.style.width = img.width + "px";
                    img.style.height = img.height + "px";
                    img.style.filter =
                    "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='"+src+"', sizingMethod='crop')"
                    img.src = "/common/images/spacer.gif";
                }
            }
        }
    }
}

$(function() {
	$("#homepage-accordion").accordion({ header: 'div.homepage-accordion-header', collapsible:true, autoHeight:false });
	$("#homepage-accordion div.homepage-accordion-header").click(function() {
		$('.newstag').removeClass('newstag-active').addClass('newstag-inactive');
		$(this).prev('.newstag').removeClass('newstag-inactive').addClass('newstag-active');
		
		var clickedIconSrc = $(this).find('img').attr('src');
		if (clickedIconSrc.indexOf('minus') == -1) {
			$('div.homepage-accordion-header img').each(function() {
				var thisSrc = $(this).attr('src');
				$(this).attr('src', thisSrc.replace('minus', 'plus-off'));
			});
			$(this).find('img').attr('src', clickedIconSrc.replace('plus-on', 'minus'));
		} else {
			$(this).find('img').attr('src', clickedIconSrc.replace('minus', 'plus-on'));
		}
	});
	$('div.homepage-accordion-header').hover(function() {
		var thisSrc = $(this).find('img').attr('src');
		$(this).find('img').attr('src', thisSrc.replace('plus-off', 'plus-on'));
	}, function() {
		var thisSrc = $(this).find('img').attr('src');
		$(this).find('img').attr('src', thisSrc.replace('plus-on', 'plus-off'));
	});
	$('h3.goldhover').hover(function() {
		$(this).css('color', '#d18e09');
	}, function() {
		$(this).css('color', '#002F5F');
	});
	
	enableAlphaImages();

	$('#main1').hover(function() {
		doRollover(this.id);
		$('#nav-flyout-about').data('animating', 'open').stop(false, true).show("slide", { direction: "left" }, 300);
	}, function() {
		doRollover(this.id);
		if ($('#nav-flyout-about').data('animating') != 'close') {
			$('#nav-flyout-about').data('timeout', setTimeout('hideFlyoutNav(\'#nav-flyout-about\')', 10));
		}
	});
	
	$('#main2').hover(function() {
		doRollover(this.id);
		$('#nav-flyout-giving').data('animating', 'open').stop(false, true).show("slide", { direction: "left" }, 300);
	}, function() {
		doRollover(this.id);
		if ($('#nav-flyout-giving').data('animating') != 'close') {
			$('#nav-flyout-giving').data('timeout', setTimeout('hideFlyoutNav(\'#nav-flyout-giving\')', 10));
		}
	});
	
	$('#main4').hover(function() {
		doRollover(this.id);
		$('#nav-flyout-news').data('animating',' open').stop(false, true).show("slide", { direction: "left" }, 300);
	}, function() {
		doRollover(this.id);
		if ($('#nav-flyout-news').data('animating') != 'close') {
			$('#nav-flyout-news').data('timeout', setTimeout('hideFlyoutNav(\'#nav-flyout-news\')', 10));
		}
	});
	
	$('.nav-flyout').hover(function() {
		if ($(this).data('timeout') && $(this).data('animating') != 'close') {
			$(this).data('timeout', clearTimeout($(this).data('timeout')));
		}
	}, function() {
		if ($(this).data('animating') != 'close') {
			$(this).data('timeout', setTimeout('hideFlyoutNav(\'#' + $(this).attr('id') + '\')', 10));
		}
	});
	
	// Initialize section links on video page
	$('a.videosectionlink').click(function() {
		$('a.videosectionlink').removeClass('archive-year-active');
		$(this).addClass('archive-year-active');
	
		var sectionId = $(this).attr('id').split('-')[1];
		$('div.videosectioncontainer').hide();
		$('div#videosection-' + sectionId).show();
		return false;
	});
	
	// Initialize section links on newsletter page
	$('a.newslettersectionlink').click(function() {
		$('a.newslettersectionlink').removeClass('archive-year-active');
		$(this).addClass('archive-year-active');
	
		var sectionId = $(this).attr('id').split('-')[1];
		$('div.newslettersectioncontainer').hide();
		$('div#newslettersection-' + sectionId).show();
		return false;
	});
});

function hideFlyoutNav(id) {
	$(id).data('animating','close');
	$(id).stop(false, true).hide("slide", { direction: "left" }, 300);
}

function init() {
	if (document.getElementById('main3') != null){
		document.getElementById('main3').onmouseover = function() { doRollover(this.id); };
		document.getElementById('main3').onmouseout = function() { doRollover(this.id); };
	}
	if (document.getElementById('homelink1') != null){
		document.getElementById('homelink1').onmouseover = function() { doRollover(this.id); };
		document.getElementById('homelink1').onmouseout = function() { doRollover(this.id); };
	}
	if (document.getElementById('homelink2') != null){
		document.getElementById('homelink2').onmouseover = function() { doRollover(this.id); };
		document.getElementById('homelink2').onmouseout = function() { doRollover(this.id); };
	}
	
	//draw home flash if on home page	
	if (document.getElementById('home-flash') != null) {
	    var imageView  = new SWFObject("/common/flash/home.swf", "homeFlash", "768", "244", "8", "#00234a", true, "best");
		imageView.addParam('wmode', 'transparent');
		imageView.addParam('bgcolor', '#00234a');
		imageView.useExpressInstall('/common/flash/expressinstall.swf');
		imageView.write("home-flash");	
	}
	
	//imgs urls and ndx defined in functlib.js 
	//logostream defined on page
	if (document.getElementById('featuredsponsor') != null) {
		ary = logostream.split("|");
		imgs = ary[0].split(",");
		urls = ary[1].split(",");
		ndx = 0;
		rotate();
	} 
}

if (isDefined(window.addEventListener)) {
   window.addEventListener('load', init, false);
} else if (isDefined(window.attachEvent)) {
   window.attachEvent('onload', init);
}