jQuery(document).ready(function($){
	setup_top_nav($);
	setup_home_tabs($);
	setup_pdf_links($);
	setup_search_box($);
	setup_accordion($);
	// setup_splash_rotator_slide($);
	setup_splash_rotator_fade($);
	setup_remove_sharethis_link_functionality($);
	
	Cufon.replace("#main-body .inner-content h1, #main-body .roles .content h1");
});

var setup_login_button = function($)
{
	$('.login-button-right-bg').toggle(function() {
		$("fieldset#signin_menu").show();
		$(".login-button-right-bg").addClass("menu-open");
	}, function() {
		$("fieldset#signin_menu").hide();
		$(".login-button-right-bg").removeClass("menu-open");
	});	

	$("fieldset#signin_menu").mouseup(function() {
		return false;
	});
	
	$(document).mouseup(function(e) {
		$(".login-button-right-bg").removeClass("menu-open");
		$("fieldset#signin_menu").hide();	
	});	
}

var setup_language_button = function($)
{
	$('.language-button-right-bg').toggle(function() {
		$("fieldset#lang_menu").show();
		$(".language-button-right-bg").addClass("menu-open");
	}, function() {
		$("fieldset#lang_menu").hide();
		$(".language-button-right-bg").removeClass("menu-open");
	});	

	$("fieldset#lang_menu").mouseup(function() {
		return false
	});

	$(document).mouseup(function(e) {
		$(".language-button-right-bg").removeClass("menu-open");
		$("fieldset#lang_menu").hide();	
	});	
}

var setup_top_nav = function($)
{
	setup_language_button($);
	setup_login_button($);
}

var setup_home_tabs = function ($)
{
	$("#main-body .roles .navigation ul li a").click(function(e){
		e.preventDefault();
		var current_index = $(this).parent().index();
		$("#main-body .roles .content > ul > li.active").removeClass("active");
		$("#main-body .roles .content > ul > li").eq(current_index).addClass("active");
		$(this).parent().addClass("active").siblings(".active").removeClass("active");
		$("#main-footer .bucket .awards").css("display", "none").css("display", "block"); /* fsck ie7 */
	});
}

var setup_pdf_links = function($)
{
	$("#main-body .main-content .inner-content a[href$='pdf']").each(function(){
		if (!$(this).parent().hasClass("view-more") && !$(this).parent().hasClass("read-more") && !$(this).parent().hasClass("pdf-link"))
		{
			$(this).addClass("pdf");
		}
	});
}

var setup_search_box = function($)
{
	var search_textbox_input = $(".search-textbox input");
	$(search_textbox_input).focus(function(){
		var title = $(this).attr("title"),
			value = $(this).attr("value");
		if (value == title)
		{
			$(this).attr("value", "");
		}
	});
	$(search_textbox_input).blur(function(){	
		var title = $(this).attr("title"),
			value = $(this).attr("value");
		if (value == "")
		{
			$(this).attr("value", title);
		}
	});
}

var setup_accordion = function($)
{
	var $ = jQuery;
	$(".accordion ul").wrap('<div class="active" />');
	$(".accordion .accordion-title p").each(function(){
		$(this).prepend("<span>+</span>");	
	});
	$(".accordion .accordion-title").click(function(e){
		$(this).siblings(".accordion-content").slideToggle("fast");
		if (!$(this).hasClass("active"))
		{
			$(this).addClass("active").find("span").text("-");
		}
		else
		{
			$(this).removeClass("active").find("span").text("+");
		}
	});
}

var setup_splash_rotator_fade = function($)
{
	var splash_nav_anchors = $("#main-body .splash .navigation ul li a");
	var splash_item_container = $("#main-body .splash .content ul");
	var splash_rotator = {
		init: function() {
			
		}
	};
	
	$(splash_nav_anchors).unbind("click").click(function(e){
		e.preventDefault();
		$(this).parent().addClass("active").siblings(".active").removeClass("active");
		
		var current_index = $(splash_nav_anchors).index(this);
		splash_rotator.current_index = current_index;
		$(splash_item_container).animate({
			left: splash_width * current_index * -1
		}, {
			duration: 250
		})
	});
	splash_rotator.init();
	
	$(splash_item_container).hover(function(e){
		window.clearInterval(splash_rotator.interval_id);
	}, function(e){
		splash_rotator.interval_id = splash_rotator.start_interval();
	});
};

var setup_splash_rotator_slide = function($)
{
	var splash_element = $("#main-body .splash-rotator-slide");
	if (splash_element.length == 0)
	{
		return;
	}
	var splash_nav = $(splash_element).find(".navigation");
	var splash_nav_anchors = $(splash_nav).find("ul li a");
	var splash_item_container = $(splash_element).find(".content ul");
	var splash_width = 940;
	var splash_rotator = {
		min_index: 0,
		max_index: 4,
		current_index: null,
		interval_id: null,
		init: function() {
			if (this.current_index == null)
			{
				this.current_index = this.min_index;
			}
			this.interval_id = this.start_interval();
		},
		start_interval: function() {
			return window.setInterval(function(){
				splash_rotator.render();
			}, 5000);
		},
		increment_index: function() {
			if (++this.current_index == this.max_index)
			{
				this.current_index = this.min_index;
			}
			
			return this.current_index;
		},
		render: function() {
			var current_index = this.increment_index();
			$(splash_nav_anchors).eq(current_index).click();
		}
	};
	
	$(splash_nav_anchors).unbind("click").click(function(e){
		e.preventDefault();
		$(this).parent().addClass("active").siblings(".active").removeClass("active");
		
		var current_index = $(splash_nav_anchors).index(this);
		splash_rotator.current_index = current_index;
		$(splash_item_container).animate({
			left: splash_width * current_index * -1
		}, {
			duration: 250
		})
	});
	splash_rotator.init();
	
	$(splash_element).hover(function(e){
		window.clearInterval(splash_rotator.interval_id);
	}, function(e){
		splash_rotator.interval_id = splash_rotator.start_interval();
	});
}

var setup_splash_rotator_fade = function($)
{
	var splash_element = $("#main-body .splash-rotator-fade");
	if (splash_element.length == 0)
	{
		return;
	}
	var splash_nav = $(splash_element).find(".navigation");
	var splash_nav_anchor_list = $(splash_nav).find("ul li a");
	var splash_item_container = $(splash_element).find(".content ul");
	var splash_item_list = $(splash_item_container).find("li");
	var splash_rotator = {
		min_index: 0,
		max_index: 3,
		current_index: null,
		interval_id: null,
		auto_click: false,
		init: function() {
			if (this.current_index == null)
			{
				this.current_index = this.min_index;
			}
			this.interval_id = this.start_interval();
		},
		start_interval: function() {
			return window.setInterval(function(){
				splash_rotator.render();
			}, 5000);
		},
		increment_index: function() {
			if (++this.current_index > this.max_index)
			{
				this.current_index = this.min_index;
			}
			this.auto_click = true;
			
			return this.current_index;
		},
		get_current_index: function(clicked_element) {
			if (this.auto_click)
			{
				return this.current_index;
			}
			
			return $(splash_nav_anchor_list).index(clicked_element);
		},
		get_previous_index: function(clicked_element) {
			if (this.auto_click)
			{
				var previous_index = this.current_index - 1;
				if (previous_index == -1)
				{
					previous_index = this.max_index;
				}
				return previous_index;
			}
			
			return this.current_index;
		},
		render: function() {
			var current_index = this.increment_index();
			$(splash_nav_anchor_list).eq(current_index).click();
		}
	};
	
	$(splash_nav_anchor_list).unbind("click").click(function(e){
		e.preventDefault();
		$(this).parent().addClass("active").siblings(".active").removeClass("active");
		
		var current_index = splash_rotator.get_current_index(this),
			previous_index = splash_rotator.get_previous_index(this);
		splash_rotator.auto_click = false;
		splash_rotator.current_index = current_index;
		
		var current_item = $(splash_item_list).eq(current_index),
			previous_item = $(splash_item_list).eq(previous_index);
		$(previous_item).fadeOut(250);
		$(current_item).fadeIn(250);
	});
	splash_rotator.init();
	
	$(splash_element).hover(function(e){
		window.clearInterval(splash_rotator.interval_id);
	}, function(e){
		splash_rotator.interval_id = splash_rotator.start_interval();
	});
}

var setup_remove_sharethis_link_functionality = function($)
{
	$("#share-this").click(function(e){
		e.preventDefault();
	});
}
