(function($) {
  $(document).ready(function(){
    // Add a link to submit the form
    $('#search-block-form').append(($('<a href="#" class="search-submit-link">Search</a>').click(function(e){e.preventDefault();$('#search-block-form').submit();})));

    $('#edit-search-block-form--2').watermark($('#edit-search-block-form--2').val()).autocomplete({
      source: function( request, response ) {
        $.getJSON( '/isos_search/autocomplete', {
          term: request.term
        }, response);
      },
      /*
      position: {
        'of': '#search'
      },
      open: function(event, ui) {
        console.log(event);
        var box = $('.ui-autocomplete');
        box.css({'top': (box.position().top - 10) + 'px'});
        $('#search').css({'z-index': box.css('z-index') + 1});
      },*/
      minLength: 3,
      delay:100,
      select: function(event, ui) {
        if (ui.item.auto_redirect != '' && ui.item.auto_redirect != null) {
          var form = $('#search-block-form');
          var hiddenEl = $('#isos_search_auto_redirect');

          if (hiddenEl.attr('id') != undefined) {
            $('#isos_search_auto_redirect').val(ui.item.auto_redirect);
          }
          else {
            form.append('<input type="hidden" name="isos_search_auto_redirect" id="isos_search_auto_redirect" value="' + ui.item.auto_redirect + '" />');
          }
        }
      }
    })
    .data('autocomplete')._renderItem = function( ul, item ) {
      var li = $( '<li></li>' ).data('item.autocomplete', item);

      if (item.cat_header != undefined) {
        li.append('<h4>' + item.cat_header + '</h4>');
      }
      return li.append('<a>' + item.label + '</a>').appendTo(ul);
    };
  });
})(jQuery);


(function($) {
	$.fn.watermark = function(opts) {
		return this.each(function(){
			var sValue = 'Text Here';
			
			if (typeof opts != 'undefined') {
				sValue = opts;
			}
			
			var obj = $(this);

			obj.addClass('watermarkOn').val(sValue);
			
			obj.focus(function() {
				$(this).filter(function() {

					// We only want this to apply if there’s not
					// something actually entered
					return $(this).val() == '' || $(this).val() == sValue;
				}).removeClass('watermarkOn').val('');
			});
		
			obj.blur(function() {
				$(this).filter(function() {

					// We only want this to apply if there’s not
					// something actually entered
					return $(this).val() == ''
				}).addClass('watermarkOn').val(sValue);
			});
			
			// Clear the watermark text when the form is submitted.
			obj.parents("form:first").submit(function() {
				obj.removeClass('watermarkOn');
			});
		});
	};
})(jQuery);


function dump(arr,level) {
	var dumped_text = "";
	if(!level) level = 0;

	//The padding given at the beginning of the line.
	var level_padding = "";
	for(var j=0;j<level+1;j++) level_padding += "    ";

	if(typeof(arr) == 'object') { //Array/Hashes/Objects
		for(var item in arr) {
			var value = arr[item];

			if(typeof(value) == 'object') { //If it is an array,
				dumped_text += level_padding + "'" + item + "' ...\n";
				dumped_text += dump(value,level+1);
			} else {
				dumped_text += level_padding + "'" + item + "' => \"" + value + "\"\n";
			}
		}
	} else { //Stings/Chars/Numbers etc.
		dumped_text = "===>"+arr+"<===("+typeof(arr)+")";
	}
	return dumped_text;
};

