/*
 *	Title:		initialize.js
 *  Created:	Apr 10, 2009
 *	Author:		Cesar Gonzalez
 *	Company:	Ignited
 *
 *	This file initialized the JS actions throughout the site.  Eg, the drop-down login box, and
 *	the javascript code which allows dropdown menus to work in IE (courtesy of suckerfish).
 *
 */

// The Suckerfish Dropdown menu is the standar way to make IE play nice with drop downs.

var $j = jQuery.noConflict();
$j(document).ready(function() {
							
	try {
	 document.execCommand('BackgroundImageCache', false, true);
	} catch(e) {} 
	
	if($j.browser.msie && $j.browser.version=="6.0"){
		
	}
	else {
	
	$j('.menurow1').superfish({ 
		delay:       1000,                            // one second delay on mouseout 
		animation:   {opacity:'show',height:'show'},  // fade-in and slide-down animation 
		speed:       'fast',                          // faster animation speed 
		autoArrows:  false,                           // disable generation of arrow mark-up 
		dropShadows: false                            // disable drop shadows 
	});
	
	}
	
}); 


sfHover = function() {
	var sfEls = document.getElementById("sf-menu").getElementsByTagName("LI");
	for (var i=0; i<sfEls.length; i++) {
		sfEls[i].onmouseover=function() {
			this.className+=" sfhover";
		}
		sfEls[i].onmouseout=function() {
			this.className=this.className.replace(new RegExp(" sfhover\\b"), "");
		}
	}
}
if (window.attachEvent) window.attachEvent("onload", sfHover);



// Mootools code for login box.
window.addEvent('domready', function() {

	// SITEWIDE LOGIN FORM
	// Implement login box open & stay functionality 
	var loginSlide = new Fx.Style('login-box', 'height', { duration: 1000, transition:Fx.Transitions.Bounce.easeOut }).hide();
	var tmpHeight = 0;
	
	// Only worry about this if the user is not logged in.
	if ($('login-click') != null) {

		$('login-click').addEvent('click', function(e) {

			// Don't follow the link anywhere
			e = new Event(e);
			
			tmpHeight = $('login-box').style.height.replace(/px/,"");
			if (tmpHeight == 180) { 
				loginSlide.start(0).chain(function() {
					//$('login-box').setStyle('display', 'none');
					$$('.navaccount .item31 a').removeClass('selected');
				});
			} else if(tmpHeight == 0 ) {
				//$('login-box').setStyle('display', 'block');
				loginSlide.start(180).chain(function() {
					$$('.navaccount .item31 a').addClass('selected');
				});
			}
			e.stop(e);
		});
				
	} else {

		// If login-click is null, then the user is logged in, so hide the login box.
		$('login-box').setStyle('display', 'none');
	}

	// SIDEBAR LOGIN FORM
	// Only add these events if the form actually exists
	if ( $('sidebar-login') != null ) {
		$('sidebar-login').style.display="block";
		
		// Set up a vertical slide effect for the sidebar login form
		var sidebarLoginForm = new Fx.Slide('sidebar-login', {duration: 700}).show();
				
		$('show-login').addEvent('click', function(e) {
	
			e = new Event(e);
			sidebarLoginForm.toggle();			
			e.stop();
	
		});
	}

	
	// EMBEDDED LOGIN FORM
	// Only add these events if the form actually exists
	if ( $('embedded-login') != null ) {
		$('embedded-login').style.display="block";
		// Set up a vertical slide effect for the sidebar login form
		var embeddedLoginForm = new Fx.Slide('embedded-login', {duration: 700}).show();
				
		$('show-embedded-login').addEvent('click', function(e) {
	
			e = new Event(e);
			embeddedLoginForm.toggle();			
			e.stop();
	
		});
	}

	
	// CONTACT A SOLDIER FORM
	// On this form, clicking a radio button reveals the phone OR email input box.
	if ( $('contact-soldier') != null ) {

		// Hide both fields initially
		$('phone-field').setStyle('display', 'none');
		$('email-field').setStyle('display', 'none');

		// Now add events to show the respective fields when the radio buttons are clicked.
		$('Phone_Number0').addEvent('click', function(e) {

			// Toggle the fields
			$('phone-field').setStyle('display', 'block');
			$('email-field').setStyle('display', 'none');

			// Toggle class used for validation
			$('text_2').addClass('required');
			$('text_5').removeClass('required');			
		});

		$('Email_Address0').addEvent('click', function(e) {

			// Toggle the fields
			$('phone-field').setStyle('display', 'none');
			$('email-field').setStyle('display', 'block');

			// Toggle class used for validation
			$('text_2').removeClass('required');
			$('text_5').addClass('required');
		});
	}

	
	// COLLAPSIBLE SIDEBAR MODULES IN 'RESERVE THE CENTER'
	// Only add these events if we are at the page
	if ( $('collapsible-modules') != null ) {

		//create our Accordion instance
		var sidebarAccordion = new Accordion($('sidebar'), 'div.module-header', '#sidebar div.module-body', {
			opacity: false,
			onActive: function(toggler, element){
				element.setStyle('padding', '10px');
				toggler.addClass('open');
			},
			onBackground: function(toggler, element){
				element.setStyle('padding', '0px');
				toggler.removeClass('open');			
			}
		});
	
	}
	
});

