var banner_timeout = 10000;
// Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults

function home_tab_nav(number, keep_rotating) {
	rotate = keep_rotating;
	// hide the banner
	$$('.home-toggle').each(function(item) {
    	item.hide();
	});
	//show all the links
	$$('.link-tab').each(function(item){ item.show(); });
	// hide all the tab text
	$$('.link-tab-text').each(function(item) { item.hide(); });
	
	// show the banner for the selected tab
	$('home-img' + number).show();
	$('home-title' + number).show();
	$('home-text' + number).show();

	// hide the anchor tag for the selected tab
  	$('link-tab-' + number).hide();

	// turn off the on state for the tabs
	$$('.link-tab-li').each(function(item) {
		item.removeClassName('on');
  	});

	// turn the selected tab to the 'on' state
  	$('link-tab-text-' + number).up().toggleClassName('on');
  
}

function document_tab_nav(number, in_or_out) {
  if(in_or_out == 'in') {
    $$('#keywords-' + number + ' li a').each(function(item) {
      item.addClassName('active');
    });
  } else {
    $$('#keywords-' + number + ' li a').each(function(item) {
      item.removeClassName('active');
    });
  }
}

function rotate_hometabs() {
	if( rotate ) {
		currentTab = currentTab + 1;
		if( currentTab > 4 ) {
			currentTab = 1;
		}
		home_tab_nav(currentTab, true);
		setTimeout(rotate_hometabs, banner_timeout);
	}
}

var submitLogin = false;
var rotate = true;
var currentTab = 1;

function toggle_login_box(evt) {
	$('login_form').toggle();
	$('user_session_login').focus();
}



document.observe( 'dom:loaded' , function() {
  if( document.getElementById("organization_login") ){
    document.getElementById("organization_login").focus();
  }

  if( $('search-all') ) {
	$('search-all').observe('click', function(evt) {
		if( $('search-all').checked ) {
			$$('.search-option').each(function(opt) {
				opt.checked = true;
			});
		}
	});
  }

  $$('.search-option').each(function(item) {
	item.observe('click', function(evt) {
		if( !item.checked ) {
			$('search-all').checked = false;
		}
	});
  });

	if( $( 'showLogin' ) ) {
		$( 'showLogin' ).observe( 'click', toggle_login_box);
	}
	
	if( $( 'link-tab-1') ) {
		// rotator for the homepage
		setTimeout(rotate_hometabs, banner_timeout);
	}
	
	if( $( 'loginForm' ) ) {

		$( 'loginForm' ).observe( 'submit', function(evt) {
			//alert('submit event');
			if( !submitLogin ) {
				//alert('stopping the event');
				evt.stop();
				new Ajax.Request('/check-for-org',
			    {
			      method: 'get',
			      parameters: Form.serialize('loginForm', true),
					onSuccess: function(transport) {
						var response = transport.responseText || "no response text";
						//alert('success');
						cms.lightbox.replace_content_and_open(response);
						
					},
					onFailure: function(transport){
						// if this fails it means there's no organization with this login/password combination
						//alert('failure');
						submitLogin = true;
						$('loginForm').submit();
					}
				});
			} else {
				submitLogin = false;
			}
			
		});
   
	
}  
});

