﻿$(function(){
	$("#quickLinks")
		.hover(
			function(){ $("ul", this).hide().fadeIn("fast"); }, 
			function() { } 
		)
		.hoverClass("sfHover")
		.focus(function(){$(this).hover();})
		.each(function(){ $("ul", this).hide(); })
		;
		
	$('#header .ursaLoginUser').textBoxInstructions('Enter Username');
	$('#header .ursaLoginPass').textBoxInstructions('Enter Password');
	$('#header .ursaLoginUser').keypress(function(e){
		if (e.which == 13
			&& $('#header input.surrogate').css('display') == 'block')
		{
			$('#header input.surrogate').focus();
			return false;
		}
	});
	
	$('#topLogin h4')
		.click(function(){
			if ($('#topLoginLiner').hasClass('expanded'))
			{
				$('#topLoginLiner')
					.removeClass('expanded')
					.animate({right:'-286px'}, 200)
					;
			}
			else
			{
				$('#topLoginLiner')
					.addClass('expanded')
					.animate(
						{right:'-30px'}, 
						300, 
						'easeout', 
						function(){$('.ursaLoginUser', this).focus();}
					)
					;
			}
		})
		.click()
		;
	
	$('.searchTypeSelector #stSite').setSearchAction('http://www.googlesyndicatedsearch.com/u/uca', 'get', 'q');
	$('.searchTypeSelector #stPeople').setSearchAction('http://www.uca.edu/info/searchfs.php', 'post', 'search');
	$('.search .textBox').textBoxInstructions('Enter Keyword or Name');
	$('.newsletterSignup .textBox').textBoxInstructions('Enter Email Address');
});

$.fn.textBoxInstructions = function(text, instructionsClass)
{
    return this.each(function(){
		if (!instructionsClass) instructionsClass = 'instructions';
		if (this.type == 'password')
		{
			//replace the password input with a text input
			$(this)
				.hide()
				.after('<input type="text" class="surrogate ' + $(this).attr('class') + '" />')
				.blur(function(){
					if (this.value=='')
					{
						$(this).hide();
						$('.surrogate', this.parentNode).show();
					}	
				})
				;
			$('.surrogate', this.parentNode)
				.addClass(instructionsClass)
				.val(text)
				.focus(function(){
					$(this).hide();
					
					$(':password', this.parentNode)
						.show()
						.focus()
						;
				})
				;
		}
		else
		{
	        $(this)
				.addClass(instructionsClass)
				.focus(function(){
					if ($(this).hasClass(instructionsClass))
					{
						this.value='';
						$(this).removeClass(instructionsClass);
					}
				})
				.blur(function(){
					if(this.value=='')
					{
						this.value=text;
						$(this).addClass(instructionsClass);
					}
				})
				.each(function(){this.value=text;})
				;
		}
	});
}
$.fn.hoverClass = function(c) {
    return this.each(function(){
        $(this).hover( 
            function() { $(this).addClass(c);  },
            function() { $(this).removeClass(c); }
        );
    });
};
$.fn.setSearchAction = function(action, method, textBoxName) {
    return this.change(function(){
		if (this.checked)
		{
			$('#searchForm').each(function(){
				this.action=action;
				this.method=method;
			});
			$('#searchForm .textBoxAndButton .textBox').each(function(){
				this.name=textBoxName;
			});
		}
    });
};
