var ANIM_SPEED_10 = 150;
var ANIM_SPEED_20 = 150;
var ANIM_SPEED_30 = 400;

/* The id of the 'Other' organization */
var ORG_ID_OTHER = 6;

function resetAllStates(isSlowFade) {
	
	// Hide any hovered menu items.
	jQuery("ul#menu span.to_hover").stop().animate({opacity: 0}, isSlowFade ? ANIM_SPEED_20 : 0);
	// Hide any hoverable subnav. 
	jQuery("ul.subnav.is_hidden").hide()
	// Show the select subnav.
	jQuery("ul.subnav.is_selected").show()
	// Hide any hovered subnav menu items.
	jQuery("ul.subnav span.to_hover").stop().animate({opacity: 0}, isSlowFade ? ANIM_SPEED_10 : 0);
}

function toggleOrgOther() {

	var org_id = jQuery('select#id_organization :selected').val();

    if (org_id == ORG_ID_OTHER) {
		jQuery('tr.org_other').show();
	} else {
		jQuery('tr.org_other').hide();
	}
}

jQuery(function() {

	// Reset menu hover layers on page load
	resetAllStates(isSlowFade=false);
	
	// Reshow the hover states (started hidden for IE6 compatibility.) 
	// All "to_hover" spans start with 'display:none' and are faded in and out as required using opacity settings.
	jQuery("span.to_hover").show()
	
	// main menu container, mouse out.	
	jQuery("div#menu_container").hover(

		null, function () {
			// Fade everything out.
			resetAllStates(isSlowFade=true);
		}
	);
	
	// menu item, mouse over
	jQuery("ul#menu span").hover(

		function () {
			// Fade everything out first
			resetAllStates(isSlowFade=true);
			// Fade in the selected menu item.
			jQuery(this).stop().animate({opacity: 1}, ANIM_SPEED_20);
			// Hide the selected subnav.
			jQuery("ul.subnav.is_selected").hide();
			// Fade in the relevant subnav.
			var index = jQuery("ul#menu span").index(this);
			var subnav = jQuery("ul.subnav:eq(" + index + ")")
			subnav.show();
		}
	);
	
	// subnav item, mouse over
	jQuery("ul.subnav span.to_hover").hover(

		function () {
			// Fade in the selected subnav item.
			jQuery(this).stop().animate({opacity: 1}, ANIM_SPEED_10);
		}, 
		function () {
			// Fade out the selected subnav item.
			jQuery(this).stop().animate({opacity: 0}, ANIM_SPEED_10);
		}
	);
	
	// Hide the ts and cs on the registration form.
	jQuery("div#bedingunen_content").hide();
	
	// Toggle the ts and cs  on the registration form.
	jQuery('a#bedingunen_link').click(function () {
    	jQuery("div#bedingunen_content").slideToggle("slow");
		return false;
    });
	
	// Hide the img edit help.
	jQuery("div#imgedit_content").hide();
	
	// Toggle the timg edit help.
	jQuery('a#imgedit_link').click(function () {
    	jQuery("div#imgedit_content").slideToggle("slow");
		return false;
    });
	
	// Set up the slideshow.
	jQuery('#slideshow').cycle({
		delay: 0000,
		timeout: 7000,
		speed: 1500,
		pause: 0})
	
	// Handle changing of organization on the registration form.
	jQuery('select#id_organization').change(toggleOrgOther);
	
	// Decrypt email address links.
	jQuery('a.to_decrypt').each(function(){
		var token="[atsign]";
		var href = jQuery(this).attr("href");
		if (href.indexOf(token) != -1) {
			var href_split = href.split(token);
			jQuery(this).attr("href", href_split[0] + "@" + href_split[1]);
		}
		var linktext = jQuery(this).html();
		if (linktext.indexOf(token) != -1) {
			var linktext_split = linktext.split(token);
			jQuery(this).html(linktext_split[0] + "@" + linktext_split[1])
		}
	});	
})
