// Do this really fast
document.write('<style type="text/css">#quicklaunch ul, .jump-list-box .jump-list, #carousel-wrapper .block {display:none;}</style>');

$(function() {
	$('#tools').buildTools();
	$('#quicklaunch ul').buildSelect();
	$('.jump-list-box .jump-list').buildSelect({text: 'Select...'});
	$('#carousel-wrapper').buildCarousel();
	
	$('table.striped tbody tr:odd').children().css('background-color', '#f8f8f8');
	$('a.popup').click(function(e) {
		popupWindow = window.open(this.href, 'popupWindow', 'scrollbars=yes,resizable=yes,width=400,height=400,left=100,top=105');
		popupWindow.focus();
		e.preventDefault();
		return false;
	});
	$('.popup-close a').show().click(function() { window.close(); });

	var search=$('#search');
	if(search) {
		search.each(function(){this.defaultValue=this.value;});
		search.focus(function(){if(this.value==this.defaultValue) this.value='';});
		search.blur(function(){if(this.value=='') this.value=this.defaultValue;});
	}
	$(".defaulted-content")
		.focus(function(){if(!this.defaultValue)this.defaultValue=this.value;if(this.defaultValue==this.value)this.value="";})
		.blur(function(){if(!this.value)this.value=this.defaultValue;});

});

$(document).ready(function() {
	$('.hover-list .hover-title').each(function() {
		var tipElement = $(this).siblings('.hover-text:first').eq(0);
		var tipText    = '<span class="htool">' + tipElement.text() + '</span>';
		
		$(this).mouseover(function() {
			Tip(tipText, BALLOON, true, ABOVE, true, OFFSETX, -50, WIDTH, 300, TEXTALIGN, 'left', FADEIN, 600, FADEOUT, 600, PADDING, 8);
		});
		
		$(this).attr('href', 'javascript:void(0);');
		$(this).removeClass('hover-hidden');
		tipElement.addClass('hover-hidden');
	});
	$("a[href^='/']:not([class~='tracked'])").each(function() {
		var node = this.parentNode;
		if (node!=null)
			node.innerHTML=node.innerHTML.replace(/<\/a>\s([^\s\d\w])/gi,'</a>$1');
	});
	// Add tracking to links that are internal (start with /, contain .fepblue.com, do not contain interstitial.jsp, do not contain #
	$('a[href^="/"]:not([href*="/interstitial.jsp"][href*="#"]):not([class~="tracked"])').click(function() { 
		_gaq.push(['_trackEvent', 'Internal', 'Site Links', this.href]);
	});
	// Add tracking to links that are external (do not start with / or contain fepblue.com
	$('a:not([href^="/"]):not([href*="#"]):not([class~="external"]):not([class~="tracked"])').click(function() {
		_gaq.push(['_trackEvent', 'Outbound', 'Site Links', this.href]);
	});
});


(function($) {
	$.fn.buildTools = function() {
		return this.each(function() {
			var basefont = 100,
				items = {
					print : {title: 'Print', icon: 'print', func: function() { print(); } },
					email : {title: 'Email', icon: 'email', func: function() { var page = window.location.href; window.location='mailto:?subject=Visit this page on fepblue.org&body='+escape('While exploring fepblue.org, I came across the following information. To access this page, click on the link below.\n\r')+page; } },
					ifont : {title: 'Increase Font Size', icon: 'ifont', func: function() { if(basefont < 300) { basefont += 10; $('#content').css('font-size', basefont+'%'); } }},
					dfont : {title: 'Decrease Font Size', icon: 'dfont', func: function() { if(basefont > 100) { basefont -= 10; $('#content').css('font-size', basefont+'%'); } }}
				},
				$ul = $('<ul />');
			
			$.each(items, function() {
				$('<li />').addClass('icon ' + this.icon).attr('title', this.title).text(this.title).bind('click', this.func).appendTo($ul);					   
			});
			
			$(this).prepend($ul);
		});
	};
		  
	$.fn.buildSelect = function(opts) {
		
		var defaults = {  
			text: 'Quicklink Launchpad',
			timeout: 200
		},
		opts =  $.extend(defaults, opts); 
		
		return this.each(function() {
			var	$obj = $(this).parent(),
				$cont = $('<div />').addClass('dropdown').attr('tabindex', 0).blur(function() { setTimeout(function() { $div.removeClass('active'); $ul.hide(); }, opts.timeout); }),
				$div = $('<div />').addClass('dropdown-select').text(opts.text).click(function() {
					$cont.focus();
					if($ul.is(':visible')) {
						$cont.blur()
					} else {
						$(this).addClass('active');
						$ul.show();	
					}
				}),
				$ul = $('ul', $obj).remove().clone().addClass('dropdown-list').hide();
			
			$cont.append($div).append($ul).appendTo($obj);
			$ul.css({position: 'absolute', left: $ul.offset().left, top: $div.outerHeight() - 1 })
				.children('li').hover(
					function() { $(this).addClass('hover'); },
					function() { $(this).removeClass('hover') }
				).click(function() {
					$('a[href^="/"]:not([href*="/interstitial.jsp"][href*="#"])', this).each(function() { 
						_gaq.push(['_trackEvent', 'Internal', 'Quick Links', this.href]);
					});
					$('a:not([href^="/"]):not([href*="#"]):not([class~="external"])', this).each(function() {
						_gaq.push(['_trackEvent', 'Outbound', 'Quick Links', this.href]);
					});
					window.location = $('a', this)[0].href;
				}).children('a').addClass('tracked');
		}).show();
	};
		
	$.fn.buildCarousel = function() {			
		return this.each(function() {
			var $obj = $(this),
				$carWrapper = $('<div />').attr('id', 'carousel'),
				$imageWrapper = $('<div />').attr('id', 'carousel-images').appendTo($carWrapper),
				$menuWrapper = $('<div />').attr('id', 'carousel-menu').appendTo($carWrapper),
				$menu = $('<ul />').appendTo($menuWrapper),
				$navHelper = $('<div />').attr('id', 'menu-helper').appendTo($menuWrapper),
				$upArrow = $('<div />').addClass('arrows').attr('id', 'uparrow').appendTo($menuWrapper),
				$downArrow = $('<div />').addClass('arrows').attr('id', 'downarrow').appendTo($menuWrapper), 
				$div = [], $li, $a, animating = false, timer;
			
			$upArrow.click(function() {
				var index = $('li', $menu).index($('.active', $menu).parent()) - 1;
				if(index < 0)
					index = $('li', $menu).length - 1;
				
				if(!animating)
					$('li:eq('+index+') a', $menu).trigger('click');
			}).mouseup(function() { if(timer) clearInterval(timer); });
			
			$downArrow.click(function() {
				var index = $('li', $menu).index($('.active', $menu).parent()) + 1;
				if(index == $('li', $menu).length)
					index =  0;

				if(!animating)
					$('li:eq('+index+') a', $menu).trigger('click');
			}).mouseup(function() { if(timer) clearInterval(timer); });

			
			$('.block:lt(5)', $obj).each(function(i) {
				$div[i] = $('<div>').addClass('image').attr('id', 'citem'+i).append($('img', this)).appendTo($imageWrapper);
				var $d = $('<div />').addClass('desc').append($('p', this)).appendTo($div[i]);
				
				if ($.browser.msie && $.browser.version.substr(0,1) < 7)
					$('p', $d).wrapAll('<div class="ie-desc" />');
			
				$a = $('<a />').attr({href: '#citem'+i, title: $('h2', this).text()})
					.html('<span class="span1">'+$('h2', this).text()+'</span><span class="span2">'+$('h2', this).text()+'</span>').click(function(e) {
						var $href = $(this);
						animating = true;
						e.preventDefault();
						
						if(!$href.hasClass('active')) {
							$('.active', $menu).removeClass('active').children('.span2').stop().fadeOut(100);
							$imageWrapper.stop().animate({top: -$div[i].position().top}, 250);
							$navHelper.stop().animate({top: $href.parent().position().top + 30}, 250, function() { animating = false; });
							$href.addClass('active').children('.span2').stop().fadeIn(250);
						}
					}).mouseup(function() { if(timer) clearInterval(timer); });
				
				$li = $('<li />').append($a).appendTo($menu);
			});
			$('a[href^="/"]:not([href*="/interstitial.jsp"]):not([href*="#"])', $imageWrapper).click(function() {
				_gaq.push(['_trackEvent', 'Internal', 'Carousel', this.href]);
			});
			$('a:not([href^="/"]):not([href*="#"]):not([class~="external"])', $imageWrapper).click(function() {
				_gaq.push(['_trackEvent', 'Outbound', 'Carousel', this.href]);
			});
			$('a', $imageWrapper).addClass('tracked');
			
			$obj.replaceWith($carWrapper);
			$('li:eq(0) a', $menu).trigger('click');
			timer = setInterval(function() { $downArrow.trigger('click'); }, 5000);
		}).show();
	};
})(jQuery);

