// accordion arrows
var $openArrow = '<img src=\"/assets/images/accordion_arrow_open.gif\" alt=\"\" />';
var $closedArrow = '<img src=\"/assets/images/accordion_arrow_closed.gif\" alt=\"\" />';

$(document).ready(function() {
	// initialize submenus
	$('ul.jd_menu').jdMenu({ showDelay: 20, hideDelay: 20, disableLinks: false });
	
	// homepage tabs
	// Uncomment Lines 8 & 10 when 2nd tab content
	// is ready to be implemented
	$("#tabs div").hide();
	$("h2.tab a").click(function() {
		$("h2.tab").removeClass("active");
		$(this).parent().addClass("active");
		var $tabContent = $(this).attr("href");
		var $visibleContent = $("#tabs div:visible");
		if ("#"+$visibleContent.attr("id") !== $tabContent) {
			if ($visibleContent.length) {
				$visibleContent.hide();
				$($tabContent).toggle();
			} else {
				$($tabContent).toggle();
			}
		}
		return false;
	});
	
	// content tabs
	// hide all tab content
	$("#contentTabsContent div").hide();
	$(".contentTabs a").click(function() {
		// set tab state
		$(".contentTabs a").removeClass("active");
		$(this).addClass("active");
		// get id of clicked tab
		var $content = $(this).attr('href');
		var i = $content.indexOf("#");
		if (i>1)  {
			$content = $content.substring(i);
		}		
		// hide visible content
		$("#contentTabsContent div").hide();
		// show related content
		if (($.browser.msie) && ($.browser.version < 7)) {
			$("#contentTabsContent div"+$content).show();
		} else {
			$("#contentTabsContent div"+$content).fadeIn('normal');
		}
		return false;
	});
	
	// IE6 & general style fixes
	$(".globalNavigation ul li:eq(0)").css("background", "transparent");
	$(".globalNavigation ul li.navLogin a:eq(0)").css("color", "#cc092f");
	$("ul.jd_menu ul li:first-child a").css("border", "none");
	$("ul.jd_menu ul li:last-child a").css("padding-bottom", "0");
	$("#rail a.banner:first img").css("margin-top", "0");
	
	// overlay search input label
	$(".siteSearch label").labelOver('over');
	
	// accordion toggle
	if(!$.browser.msie){
		var accordionHeight = (($("#accordion h2").length * $("#accordion h2").height()) + $(".accordionPanel:first").height());
		$("#accordion").height(accordionHeight);
	}
	$('.accordionPanel').hide();
	$("#accordion h2:first").addClass("first");
	$("#accordion h2:last").addClass("last");
	$("#accordion .accordionPanel:last").addClass("last");
	$("#accordion h2").append($closedArrow);
	
	$('#accordion h2').hoverIntent({
		sensitivity: 1,
		interval: 100,
		over: 	function() {
					if(!$(this).next('.accordionPanel:visible').length) {
						$('.accordionPanel:visible').slideUp();
						$(this).next('.accordionPanel').slideToggle();
						$("#accordion h2").removeClass('active');
						$("#accordion h2 img").replaceWith($closedArrow);
						$(this).addClass('active');
						$(this).children('img').replaceWith($openArrow);
					}
				},
		timeout: 100,
		out: function() {}
	});
	
	// check all checkboxes
	$("a.checkAll").click(function() {
		$("input[@type=checkbox]").attr("checked", true);
		return false;
	});
	
	// uncheck all checkboxes
	$("a.uncheckAll").click(function() {
		$("input[@type=checkbox]").attr("checked", false);
		return false;
	});
	
	// add zebra table row class
	$("table.zebra tbody tr:even").addClass("even");
	
	// adjust overlay opacity for FF 2.x mac
	if (detectMacXFF2()) {
		$('body').addClass('mac-ff2');
	}
	
	// clear feature floats
	$(".featurePod").wrapAll('<div class="wrapper"></div>');
	
	// fix the IE6 background flickr on links
	try {
		document.execCommand("BackgroundImageCache", false, true);
	} catch(err) {}
});


$(window).load(function() {
	// initialize shadowbox
	var options = {
		flvPlayer: '/assets/flash/flvplayer.swf'
	};
	Shadowbox.init(options);
	
	// justify content containers
	$(".featurePod").vjustify();
	$(".contentTabs a").vjustify();
	$(".manager").vjustify();
	$("ul.videoThumbnails li").vjustify();
	
	// show initial tab content
	$("#tabs h2:first a").trigger('click');
	
	// load initial accordion panel
	$("#accordion h2:first").addClass('active');
	$("#accordion h2:first img").replaceWith($openArrow);
	$(".accordionPanel:first").show();
	
	// load first content tab
	$(".contentTabs a:first").trigger('click');
	
	// fix hyphenated text wrap for FF 2.x
	if (detectFF2()) {
		$("#sectionNavigation a:contains('-')").each(function() {
			var $this = $(this);
			var t = $this.text();
			$this.text(t.replace('-', '- '));
		});
	};
});

// label over
jQuery.fn.labelOver = function(overClass) {
	return this.each(function() {
		var label = jQuery(this);
		var f = label.attr('for');
		if (f) {
			var input = jQuery('#' + f);
			this.hide = function() {
				label.css({ textIndent: -10000 })
			}
			this.show = function() {
				if (input.val() == '') label.css({ textIndent: 0 })
			}
			// handlers
			input.focus(this.hide);
			input.blur(this.show);
			label.addClass(overClass).click(function(){ input.focus() });
			if (input.val() != '') this.hide(); 
		}
	});
}

// justify element heights
jQuery.fn.vjustify=function() {
	var maxHeight=0;
	this.each(function() {
		if (this.offsetHeight>maxHeight) { maxHeight=this.offsetHeight; }
	});
	this.each(function() {
		$(this).height(maxHeight + "px");
		if (this.offsetHeight>maxHeight) {
			$(this).height((maxHeight-(this.offsetHeight-maxHeight))+"px");
		}
    });
};

// Isolate Firefox 2.x for mac
function detectMacXFF2() {
	var userAgent = navigator.userAgent.toLowerCase();
	if (/firefox[\/\s](\d+\.\d+)/.test(userAgent)) {
		var ffversion = new Number(RegExp.$1);
		if (ffversion < 3 && userAgent.indexOf('mac') != -1) {
			return true;
		}
	}
}

function detectFF2() {
	var userAgent = navigator.userAgent.toLowerCase();
	if (/firefox[\/\s](\d+\.\d+)/.test(userAgent)) {
		var ffversion = new Number(RegExp.$1);
		if (ffversion < 3) {
			return true;
		}
	}
}