function LibraryGlobalSearch(){
	this.el = $('#library-search');
	this.menu = $('#library-search-nav');
	this.title = $("#search-title");
	this.currentInputValue;
	
	this.askEl = $("#library-ask");
	
	this.setEvents = lgs_setEvents;
	
	this.__construct = function(){
		var obj = this;
		obj.setEvents();
		obj.el.children('form:gt(0)').hide();
	};
	
}

function lgs_setEvents(){
	var obj = this;
		
	obj.menu.children().click(function(){
		obj.menu.children().removeClass('on');
		$(this).addClass('on');
		obj.el.children('form').hide();
		obj.el.children('form:eq('+$(this).index()+')').fadeIn();
		obj.title.children('span:first').text($(this).text());
		obj.currentFocusValue = obj.el.find('form:visible input.input').val();
	});
	
	
	//set up the Ask/Chat button actions
	obj.askEl.find('.chat-librarian').attr('target',obj.askEl.find('.chat-librarian a').attr('href'));
	obj.askEl.find('.chat-librarian a').removeAttr('href');
	
	obj.askEl.children('.chat-librarian').click(function(){
		window.open($(this).attr('target'), 'urlibrary_chat', 'status=0,toolbar=0,location=0,menubar=0,height=470,width=270');
	});
	
	if( (typeof(mobile) != 'undefined') && (mobile == true) ){
		/* obj.menu.children().each().text().replace(/ /g, '<br/>');  /* Needs to be .each function */
		$('#library-ask').hide();
		obj.el.find('form#database-search ul').hide();
		obj.el.find('form#database-search').submit(function(){
			window.location.href = $(this).find('select').val();
			return false;
		});
	}
	
	obj.el.find('form#search-journal-titles').find('select').change(function(){
		window.location.href = $(this).val();
		return false;
	});
	
	obj.el.find('form#database-search').find('select').change(function(){
		window.location.href = $(this).val();
		return false;
	});
		
	obj.el.find('form input.input').focus(function(){
		if($(this).val().indexOf('Search') == 0){
			obj.currentFocusValue = $(this).val();
			$(this).val('');
		}	
	});
	/*obj.el.find('form:visible input.input').blur(function(){
		if($(this).val().indexOf('Search') == -1){
			$(this).val(obj.currentFocusValue);
		}	
	});*/
	
}

function lgs_json(opt, target, className){
	var obj = this;
	//this is to override browser style caching.
	var timestamp = new Date().getTime();
	
	if(typeof(className) == 'undefined'){
		className = 'a';
	};
	
	if(opt == 'header'){
		document.write('<link type="text/css" media="screen" rel="stylesheet" href="http://assets.richmond.edu/styles/kp3/fonts.css"/>'+
				'<link type="text/css" media="screen" rel="stylesheet" href="http://assets.richmond.edu/styles/kp3/default.css?id='+timestamp+'"/>');
	
		$.ajax({
			url: 'http://assets.richmond.edu/actions/set/library-json-global-search.php?class='+className,
			type: "GET",
			dataType: "jsonp",
			success: function(data){
				eval(data);
				target.html(data.libsearch);		
				//create a new instance of this class and construct it:
				var s = new LibraryGlobalSearch();
				s.__construct();
				if(className == 'a'){
					s.el.hide();
					//set the trigger event for show/hide:
					$('.library-search-toggle').click(function(){
						if($(this).is('.on')){
							$(this).removeClass('on');
							s.el.slideUp();
						}
						else{
							$(this).addClass('on');
							s.el.slideDown();
						}
						
						
					});
				}	
			},
			error: function(xhr,status,e){
				return false;
			}	
		});
	}
	else if(opt == 'footer'){
		$.ajax({
			url: 'http://assets.richmond.edu/actions/set/library-json-global-footer.php',
			type: "GET",
			dataType: "jsonp",
			success: function(data){
				eval(data);
				target.html(data.libfooter);		
			},
			error: function(xhr,status,e){
				return false;
			}	
		});
	}
};	
