$(document).ready(function() {

	$("#frmLogin a.forgot_pwd").click(function() {
		$("#frmLogin").fadeOut("fast", function() {
			$("#frmForgotPwd").fadeIn();
		});
		return false;
	});

	$("#frmForgotPwd a.back_to_login").click(function() {
		$("#frmForgotPwd").fadeOut("fast", function() {
			$("#frmLogin").fadeIn();
		});
		return false;
	});

	/**
	 * Open all external links in a new window
	 */
	$("a[href^=http]").each(function() {
		if ((this.href.indexOf(location.hostname) == -1) && ($(this).attr('target') == '')) { 
			$(this).attr('target', '_blank');
		}
	});
	
	
	/* IE 6 Gradient need height */
	resizeContent = function() {
		$('body.home #sidebar ul.box li.content').each(function() {
			$(this).css('height', 'auto');
			$(this).css('height', $(this).height());
		});
		$('body.home #content ul.box li.content').each(function() {
			$(this).css('height', $('#sidebar').height() - 40);
		});
	}
	resizeContent();
	
	
	/**
	 * Homepage Ribbons
	 */
	$('.ribbons').each(function() {
		var container = $(this);
		var ribbons = container.find('.nav li');
		var active = 'ribbon-0';
		
		ribbons.mouseover(function() {
			container.removeClass(active);
			container.find('div#' + active + '-pane').hide();
			
			active = $(this).attr('id');
			container.addClass(active);
			container.find('div#' + active + '-pane').show();
			
			return false;
		});
	});
	
	
	/**
	 * Table of Contents
	 */
	$('ul.tableOfContents li.level-0 > a').click(function() {
		var li = $(this).parent();
		if (li.find('ul').length > 0) {
			if (li.hasClass('expanded')) {
				li.removeClass('expanded');
				li.find('ul').hide();
			} else {
				li.addClass('expanded');
				li.find('ul').show();
			}

			return false;
		}
	});
	
	
	/**
	 * Expanding Box
	 */
	$('div.expanding-box h2').click(function() {
		var box = $(this).parent();
		if (box.hasClass('expanded')) {
			box.removeClass('expanded');
			box.find('div.text').hide();
		} else {
			box.addClass('expanded');
			box.find('div.text').show();
		}
	});
	
	$('ul.rssfeed').each(function() {
		var items = $(this).find('li.item');
		var currentItem = 0;
		
		$(this).find('a.prev, ul.rssfeed a.next').click(function() {
			var nextItem = 0;
			if ($(this).hasClass('next')) {
				nextItem = currentItem + 1;
				if (nextItem > items.length - 1) {
					nextItem = 0;
				}
			} else {
				nextItem = currentItem - 1;
				if (nextItem < 0) {
					nextItem = items.length - 1;
				}
			}
			showItem(nextItem);
			return false;
		});
		
		showItem = function(nextItem) {
			$(items[currentItem]).hide();
			currentItem = nextItem;
			$(items[currentItem]).show();
			
			resizeContent();
		}
	});

});
