$(document).ready(function(){

	
	$('#mymaillink').live("click", function(){
		$('#email_login_dialog').dialog('open'); 
		$(".ui-widget-overlay").bind("click", closeEmailLoginDialogWindowOnOverlayClick); 
		$("#employee_email_login").load(
				'/proxy.php?proxy_url=https://mail.byuh.edu/wm/eml/login.html',
				function() {
					$('#employee_email_login form[name="loginform"] > div').empty();
					$('#employee_email_login form[name="loginform"] > div').attr("class", "emaillogin_left");
					$('#employee_email_login form[name="loginform"] > div').removeAttr("style");
					//$('#employee_email_login form[name="loginform"]').removeAttr("onsubmit");
					
					var formaction = $('#employee_email_login form[name="loginform"]').attr("action");
					$('#employee_email_login form[name="loginform"]').attr("action", "https://mail.byuh.edu" + formaction);
					$('#employee_email_login form[name="loginform"] > div').load(frontpagepath + "employee_login.html");
				}
		);
		$("#student_email_login").load(frontpagepath + "student_login.html");
		
		

		return false;
	});	
	
	$('.email_button').live("mouseover", 
		function(){
			$(this).removeClass("email_button_normal");
			$(this).addClass("email_button_hover");
		});
	
	$('.email_button').live("mouseout",
		function(){
			$(this).removeClass("email_button_hover");
			$(this).addClass("email_button_normal");
		}	
	);
	
	$('.emaillogin_submit').live("mouseover", 
			function(){
				$(this).removeClass("email_button_normal");
				$(this).addClass("email_button_hover");
	});
		
	$('.emaillogin_submit').live("mouseout",
		function(){
			$(this).removeClass("email_button_hover");
			$(this).addClass("email_button_normal");
		}	
	);	
	
	$("#employee_email_login .email_button").live("click", function(){
		window.location = "http://mymail.byuh.edu";
	});

	$("#student_email_login .email_button").live("click", function(){
		window.location = "http://email.byuh.edu";
	});


	
	$("#email_login_dialog").dialog({ bgiframe: true, autoOpen: false,  width: 460, modal: true, resizable: false});
	
	closeEmailLoginDialogWindowOnOverlayClick = function(event){ 
		var closeButton = $(".ui-dialog:visible").find(".ui-dialog-titlebar-close"); 
		closeButton.trigger("click"); 
		$(".ui-widget-overlay").unbind("click", closeEmailLoginDialogWindowOnOverlayClick); 
	}	
	
	
	
	var overtoolbar = 0;

	$('#topbar').load(frontpagepath + 'topbar.html', function(){
		
		$('#toolbar #searchbox #searchtext input').bind('click', search_click );
		$('#toolbar #searchbox #searchtext input').bind('blur', search_blur );
		$('#toolbar #searchbox #search_buttons #searchclose').bind('click', toolbar_close);
		$('#toolbar #searchbox #search_buttons #searchclose').bind('mouseover', over_close);
		$('#toolbar #searchbox #search_buttons #searchclose').bind('mouseout', out_close);

		
		$("#searchform").submit(function() {
			var stype = $("input[name='searchtype']:checked").val();
			if (stype == "site") {
				$("#searchform").attr("action", "http://search.byuh.edu/search/form");
			}
			else if (stype == "people") {
				$("#searchform").attr("action", "http://search.byuh.edu/search/form");
			}
			return true;
		});
		
		$('#searchfield').autocomplete({
			serviceUrl1: '/proxy.php?proxy_url=http://search.byuh.edu/search/index/autocomplete',
			serviceUrl2: '/proxy.php?proxy_url=http://search.byuh.edu/search/directory/autocomplete',
			maxHeight: 400,
			width: 300,
			minChars: 3,
			zIndex: 9999
		});			
		
	});



	
	function search_blur() {
		add_search_text();
	}
	
	function add_search_text() {
		if( !$('#searchfield').val()) {
			$('#searchfield').val('Search');	
		}
	}
	
	function remove_search_text() {
		if( $('#searchfield').val() == "Search") {
			$('#searchfield').val('');	
		}
	}
		
	function search_click() {
		remove_search_text();
		show_search_buttons();
	}
		
	function show_search_buttons() {
		if( $( '#toolbar #searchbox form #search_buttons' ).css( 'display' ) == 'none' ) {
			$( '#topright #toolbar #urls' ).css( 'display', 'none' );
			$( '#toolbar #searchbox form #search_buttons' ).show();
		}
	}
		
	function hide_search_buttons() {
		if( $('#topright #toolbar #urls').css( 'display' ) == 'none' ) {
			$('#toolbar #searchbox form #search_buttons' ).css( 'display', 'none' );
			$('#topright #toolbar #urls').show();
		}
	}
		
	function over_close() {
		$('#toolbar #searchbox #search_buttons #searchclose').css( 'background', '#999' );	
		$('#toolbar #searchbox #search_buttons #searchclose').css( 'color', '#fff' );	
	}
	
	function out_close() {
		$('#toolbar #searchbox #search_buttons #searchclose').css( 'background', '');	
		$('#toolbar #searchbox #search_buttons #searchclose').css( 'color', '#999' );	
	}
		
	function toolbar_close() {
		add_search_text();
		hide_search_buttons();
	}
	
});	



