// JavaScript Document for Mums like us website.

// Other libraries are being used here, we don't want to cause any conflicts with jQuery.
jQuery.noConflict();

// Shows tooltip help text for 'forgotten username?' link. Not sure how this should be styled right now so commenting out temporarily.
/*jQuery(function() {
	jQuery('#forgot_username_link').tipsy({gravity: 'w'});
});*/

jQuery(document).ready(function() {
	// Header search box dropdown filter.
	// Opting for JS solution here to reveal dropdown list.
	// CSS only solution previously in place does not work in IE6.
	/*jQuery('.searchContainer').hover(
		function() {
			jQuery('#ctrSearch').show();
		},
		function() {
			jQuery('#ctrSearch').hide();
		}
	);*/
	
	// Highlight relevant labels white if associated checkbox is checked.
	jQuery('#sm li ul li input, #sm li ul li label').click(function() {
		if(jQuery(this).parent().children('input').is(':checked')) {
			jQuery(this).next().css('color', '#ffffff');
		}
		else {
			jQuery(this).next().css('color', '#0395CC');
		}
	});
	
	// Click function to reveal sign-in form. Was initially set up to show onclick and hide onmouseup from anywhere else on the doc. Not sure if this is still the intended behaviour.
	jQuery("#btn-signin").click(function() {
		//e.preventDefault();
		if (jQuery("fieldset#signin_menu").css('display') == 'none') {
			jQuery("fieldset#signin_menu").show();
			jQuery("#btn-signin").addClass("menu-open");
			jQuery("#btn-signin").removeClass("menu-closed");
		}
		else {
			jQuery("fieldset#signin_menu").hide();
			jQuery("#btn-signin").addClass("menu-closed");
			jQuery("#btn-signin").removeClass("menu-open");
		}
		return false;
	});

	/*jQuery("fieldset#signin_menu").mouseup(function() {
		return false;
	});*/
	
	/*jQuery(document).mouseup(function(e) {
		if(jQuery(e.target).parent("#btn-signin").length==0) {
			jQuery("#btn-signin").removeClass("menu-open");
			jQuery("#btn-signin").addClass("menu-closed");
			jQuery("fieldset#signin_menu").hide();
		}
	});*/
	
	// Applying active state of left nav items using JS temporarily. This is only relevant if there is a sublevel item active, so we also apply an active state to its parent. this really needs to be fixed server side.
	if (jQuery("ul").hasClass("left-nav-second")) {
		jQuery(".left-nav-second:eq(0)").siblings('a').addClass('active');
	}
	// Add horizontal arrow active state to menu items with no children.
	if (jQuery("#active_menu").length > 0 ) {
		if (jQuery("#active_menu").siblings().size() == 0) {
			jQuery("#active_menu").addClass('active-no-children');
		}
	}
	
	// Making the first item of the left nav bold.
	if (jQuery('#left-nav li:first a').text() == "Follow Our Mums" || jQuery('#left-nav li:first a').text() == "Expert Answers") {
		jQuery('#left-nav li:first a').css('font-weight', 'bold').addClass('active');	
	}
	
	// Little bit of cosmetic JS to ensure that all layout-list-type1 li elements appear to have the same height.
	// Also to ensure that intro images always sit flush with the bottom of the intro box.
	jQuery('#intro-img img').css('visibility', 'hidden');
	
	window.onload = function() {
		if (jQuery("ul").hasClass("layout-list-type1")) {
			var listItemHeight = 0;
			jQuery(".layout-list-content").each(function() {
				var h = jQuery(this).height();
				var mh = jQuery(this).prev().height();
				// Debugging -- alert('listItemHeight: ' + listItemHeight + '\nh: ' + h + '\nmh: ' + mh);
				if (h > listItemHeight && h > mh) {
					listItemHeight = h;
				}
			});
			jQuery(".layout-list-content").height(listItemHeight);
		}
		
		jQuery('#intro-img').height(jQuery('#intro-section').height() - (jQuery('#intro-section .section-top').height() + jQuery('#intro-section .section-btm').height()));
		jQuery('#intro-img img').css({position:'absolute', visibility:'visible'});
	}
	
	// Rollovers for various submit buttons. We can't do this with CSS.
	// Register.
	/*if (jQuery("input").hasClass("regbtn")) {
		jQuery(".regbtn").hover(
			function() {
				jQuery(this).css('background-position', '0 -34px');		
			},
			function() {
				jQuery(this).css('background-position', '0 0');
			}
		);
	}*/
	
	if (jQuery("input").hasClass("js-hover") || jQuery("button").hasClass("js-hover")) {
		jQuery(".js-hover").hover(
			function() {
				jQuery(this).css('background-position', '0 -24px');		
			},
			function() {
				jQuery(this).css('background-position', '0 0');
			}
		);
	}
	
	// Image scroller for follow our mums.
	if (jQuery('#lyr1').length > 0) {
		function init_dw_Scroll() {
			var wndo = new dw_scrollObj('wn', 'lyr1', 't1');
			wndo.setUpScrollControls('scrollLinks');
		}
		
		// if code supported, link in the style sheet and call the init function onload
		if ( dw_scrollObj.isSupported() ) {
			//dw_writeStyleSheet('css/scroll.css');
			dw_Event.add( window, 'load', init_dw_Scroll);
		}
	}
	
});



