/*  NAMESPACE: Namespaces the jquery "$" variable for the drupal environment. http://drupal.org/update/modules/6/7#javascript_compatibility */
(function ($) {

	/* Hides ce_submenu blocks and binds them to main-nav tabs.
	 * Losely based on 'Simple Tabs', See here for the genesis:
	 * http://www.sohtanaka.com/web-design/examples/tabs/ 
	 */
	
	$(document).ready(function() {
		initCeSubmenus();
	});
	
	
	function initCeSubmenus(){ 
		
		/* Instantiate the timer var. The timer var is needed 
			 for clearing on the initial mouseover since the
			 alternative (checking if timer is undefined) is not 
			 consistent between browsers. 
		*/
		var ce_closing_timer = setTimeout(function(){ /* nothing */ },0);
	
		
		// Tab's mouse-enter event
		$("#main-menu li").mouseenter(function() {
																 
			// Cancell any delayed fade out & 
			// just immediatley hide all submenus 
			clearTimeout(ce_closing_timer);
			$(".block-ce-submenu").hide(); 
			
			// Remove any "submenu-active" class
			$("#main-menu li a").removeClass("active-submenu");
			
			// Add "active-submenu" class to our newly selected tab
			$(this).find("a").addClass("active-submenu"); 
			
			
			// Convert the href to the corresponsing submen id
			var linktxt = $(this).find("a").text(); 
			var submenu = convertToSubmenuId(linktxt); 
			
			// Fade in the active content  
			$(submenu).show();
			 
			return false;
		});
		
		// Tab's mouseleave event
		$("#main-menu li").mouseleave(function() {	   
			ce_closing_timer = setTimeout( function() { 
						$(".block-ce-submenu").fadeOut('slow');
						$("#main-menu li a").removeClass("active-submenu");
					},75);
		});
		
		// Submenu's  mouseleave event
		$(".block-ce-submenu").mouseleave(function() {		   
			ce_closing_timer = setTimeout( function() { 
						$(".block-ce-submenu").fadeOut('slow'); 
						$("#main-menu li a").removeClass("active-submenu");
					},75);
		});
		
		// Submenu's mouseenter event
		$(".block-ce-submenu").mouseenter(function() {
			// Cancell any delayed fade out since
			// they are engaging the submenu
			clearTimeout(ce_closing_timer);								   
		});
		
	}
	
	// Converts a link's text to oue submenu ID format
	function convertToSubmenuId(linktxt){ 
		// All lowercase
		linktxt = linktxt.toLowerCase();
		
		// Remove ampersands
		linktxt = replaceAll(linktxt, " &", "");
		linktxt = replaceAll(linktxt, " &amp;", "");
		 
		// Dashify spaces
		linktxt = replaceAll(linktxt, " ", "-");  
		
		// Ad the common appendage we namspaced our classes with 
		return '#ce-submenu-' + linktxt;
	}
	
	// Replaces all instances of the given substring.
	function replaceAll(str, replaceStr, replaceWithStr){
		var intIndexOfMatch = str.indexOf( replaceStr );
		while (intIndexOfMatch != -1){ 
			str = str.replace( replaceStr, replaceWithStr )
			intIndexOfMatch = str.indexOf( replaceStr );
		} 
		return str;
	}

 
})(jQuery);
;
(function ($) {

/**
 * Open Mollom privacy policy link in a new window.
 *
 * Required for valid XHTML Strict markup.
 */
Drupal.behaviors.mollomPrivacy = {
  attach: function (context) {
    $('.mollom-privacy a', context).click(function () {
      this.target = '_blank';
    });
  }
};

/**
 * Attach click event handlers for CAPTCHA links.
 */
Drupal.behaviors.mollomCaptcha = {
  attach: function (context, settings) {
    // @todo Pass the local settings we get from Drupal.attachBehaviors(), or
    //   inline the click event handlers, or turn them into methods of this
    //   behavior object.
    $('a.mollom-switch-captcha', context).click(getMollomCaptcha);
  }
};

/**
 * Fetch a Mollom CAPTCHA and output the image or audio into the form.
 */
function getMollomCaptcha() {
  // Get the current requested CAPTCHA type from the clicked link.
  var newCaptchaType = $(this).hasClass('mollom-audio-captcha') ? 'audio' : 'image';

  var context = $(this).parents('form');

  // Extract the Mollom session id and form build id from the form.
  var mollomSessionId = $('input.mollom-session-id', context).val();
  var formBuildId = $('input[name="form_build_id"]', context).val();

  // Retrieve a CAPTCHA:
  $.getJSON(Drupal.settings.basePath + 'mollom/captcha/' + newCaptchaType + '/' + formBuildId + '/' + mollomSessionId,
    function (data) {
      if (!(data && data.content)) {
        return;
      }
      // Inject new CAPTCHA.
      $('.mollom-captcha-content', context).parent().html(data.content);
      // Update session id.
      $('input.mollom-session-id', context).val(data.session_id);
      // Add an onclick-event handler for the new link.
      Drupal.attachBehaviors(context);
      // Focus on the CATPCHA input.
      $('input[name="mollom[captcha]"]', context).focus();
    }
  );
  return false;
}

})(jQuery);
;

