$(document).ready(function(){
	init_marshals_table();
	init_country_menu();
	init_home_banners();
	init_client_login();
});//end ready


function gotoAnchor(selected){
	window.top.location.href = selected.options[selected.selectedIndex].value;
	selected.options[0].selected = "selected";
}


function init_marshals_table()
{
	$("#fire-marshals-table tr").each(function(){
		$tr = $(this);
		$tr.mouseover(function(){
			if(!$(this).hasClass("hover")){
				$(this).addClass("hover");
			}
		});//end mouseover
		
		$tr.mouseout(function(){
			$(this).removeClass("hover");
		})
		
	});//end each
}//end init_marshals_table



function init_country_menu()
{
	var anim_speed = 300;
	
	$("#country_nav a.slidable").mouseover(function(){
		$a_over = $(this);
		if(!$a_over.hasClass("animating_out") && !$a_over.hasClass("selected")){
			$a_over.animate({'height':'122px'}, anim_speed);
		}//end if
	});//end mouseover


	$("#country_nav a.slidable").mouseout(function(){
		$a_out = $(this);
		
		if(!$a_out.hasClass("selected")){
		
			$a_out.addClass("animating_out");
			$a_out.animate({'height':'33px'}, anim_speed, function(){
				$a_out.removeClass("animating_out");
			});//end animate
		
		}//end if
		
	});//end mouseout
	
}//end init_country_menu



var banner_timer;
var banner_time = 4000; //time for each banner slide
var banner_anim_speed = 2000; //time for animation to take place
var total_banners;

function init_home_banners(){
	
	total_banners = $("#home_banners a").length;
	
	if(total_banners <= 0) return;
	
	$("#home_banners a").css("display", "block");
	
	$("#pager_block a").click(function(e){
		e.preventDefault();
		handle_pager_click($(this));
	});
	
	banner_timer = setTimeout("fade_banner()", banner_time);

}//end init_home_banners



function reset_banner_timer(){
	clearTimeout(banner_timer);
	banner_timer = setTimeout("fade_banner()", banner_time);
}//end reset_banner_timer



function fade_banner(){
	
	$banners_left = $("#home_banners a.off");
	$last_off = $("#home_banners a.off:last");
	
	
	if($banners_left.length == 1){
		
		//instead of fading out the LAST one, we need to fade IN the first one
		$first_on = $("#home_banners a.on");
		activate_pager_button( $first_on.attr("id") );
		$first_on.fadeIn(banner_anim_speed, function(){
			$("#home_banners a").show();
			$("#home_banners a").addClass("off");
			reset_banner_timer();
		});

	}else{
		
		//we need to get the NEXT button & activate it
		var id = Number($last_off.attr("id").replace("banner_", ""));
		var next_id = id + 1;
		activate_pager_button("banner_" + next_id);
		
		$last_off.fadeOut(banner_anim_speed, function(){
			$last_off.removeClass("off");
			reset_banner_timer();
		});
		
	}//end else
	
	
}//end fade_banner



function activate_pager_button(href){

	$("#pager_block a").removeClass("selected");
	$("#pager_block a[href='#" + href + "']").addClass("selected");
	
}//end activate_pager_button



function handle_pager_click(clicked_obj){
	
	$a = clicked_obj;
	
	var href = $a.attr("href");
	var id = Number(href.replace("#banner_", ""));
	
	var turn_off = new Array();
	var to_enable = new Array();
	
	for(var i=id-1; i > 0; i--)
	{
		turn_off.push("#banner_" + i);
	}
	var ids_to_hide = turn_off.join(", ");


	for(var j=id; j <= total_banners; j++)
	{
		to_enable.push("#banner_" + j);
	}
	var ids_to_show = to_enable.join(", ");
	

	if(ids_to_hide != "") $(ids_to_hide).hide().removeClass("off");
	if(ids_to_show != "") $(ids_to_show).show().addClass("off");
	activate_pager_button("banner_" + id);
	reset_banner_timer();
	
}//end handle_pager_click


var clientLoginVisible = false;
function init_client_login()
{
	
	$("#client_login").click(function(e){
		e.preventDefault();
		if(!clientLoginVisible)
		{
			$("#client_login_form").slideDown();
			clientLoginVisible = true;
			
		} else {
			
			$("#client_login_form").slideUp();
			clientLoginVisible = false;
			
		}//end if
	});
	
}//end init_client_login


function checkLoginForm()
{
	var vals = [];
	vals[0] = $("#subdomain").val();
	vals[1] = $("#email").val();
	vals[2] = $("#password").val();

	var pass = true;

	for(var i=0; i < vals.length; i++)
	{
		if(vals[i] == "")
		{
			pass = false;
		}
	
	}//end for loop

	
	if(pass == false){
		alert("All fields are required");
		return false;
	} else {
	
		var actionUrl = "https://" + vals[0] + ".lsssitesurveyor.com/en-US/session";
		$("#software_login_form").attr("action", actionUrl);
		$("#software_login_form input#submit").val("Please wait...");
		return true;
	}
	
	return false;
	
}//end function
