/**
 *
 * jQuery for sameAs.org
 *
 */
$(document).ready(function(){	

	//  set default text for input boxes
	$('#q').doDefaultText('Enter a literal (text) search...');
	$('#uri').doDefaultText('Enter a Linked Data URI...');

	//  apply fancy list for orderd lists
	$('ol').fancyList();

});



//
//  do focus/onblur default text
//
(function($s) {
	$.fn.doDefaultText = function(defaultText) {
		return this.each(function() {
			$(this).focus(function () {
				if($(this).val() == defaultText) $(this).val('');
			});
			$(this).blur(function () {
				if($(this).val() == '') $(this).val(defaultText);
			});
			if ($(this).val() == '') $(this).val(defaultText);
		});
	}
})(jQuery);



//
//  apply fancy formatting and collapse long orderd lists
//
(function($) {
        $.fn.fancyList = function() {
                return this.each(function() {

			//  get ul and li items
			var $list = $(this);
			var $items = $("> li", this);

			//  if a single item list use special background
			if ($items.length == 1) {
				$list.addClass('single');
			}  else {

				//  set first and last item backgrounds
				$list.addClass('brace');
				$($items[0]).addClass('first');
				$($items[$items.length - 1]).addClass('last');
			}

			//  if more than five items hide the excess
			if ($items.length > 4) {
				$items.filter(":gt(2)").hide();
				$items.filter(":last").show();
				var $reveal = $('<li class="reveal"><a href="#" class="reveal">Show ' + ($items.length - 4) + ' more</a></li>');
				$items.filter(":last").before($reveal);
				$reveal.click(function() {

					//  show the hidden items
					if ($items.length > 50) {
						//  don't animate if lots of items
						$reveal.hide();
						$items.filter(":hidden").show();
					} else {
						$reveal.hide('slow');
						$items.filter(":hidden").show('slow');
					}

					//  add link to collapse again
					var $hide = $('<span> &nbsp;&middot;&nbsp; <a href="#">show fewer items</a></span>');
					$('p', $list.parent()).append($hide);
					$hide.click(function() {
						if ($items.length > 50) {
							//  don't animate if lots of items
							$items.filter(":gt(2)").filter(":not(:last)").hide();
							$reveal.show();
						} else {
							$items.filter(":gt(2)").filter(":not(:last)").hide('slow');	
							$reveal.show('slow');
						}
						$hide.hide();
						return false;
					});
					return false;
				});
			}
		});
	}
})(jQuery);

