$(function() {
	$("table:not(.nobg) tr:odd").css("background-color", "#CCC");
	
	$(".thide").showhide();

	$('.filter').filterable({
		affects: 'tbody tr',
		queryLabel: 'Filter:'
	});

	$("#sosearchsubmit").click(function(){
		var sexoffmap = getFlashMovie("sexoffendermap");
		
		sexoffmap.searchaddress($("#sosearchaddy").val(), $("#socity").text(), $("#sostate").text());
	});
});

(function($){
	$.fn.showhide = function()
	{
		var too = $(this);
		
		var stop;
		return this.each(function(){
			var target = $(this);
			
			target.hide();
			
			var showbutton = $("<a class='showhide'>Show/Hide Table</a>").click(function(){
				target.toggle();
			});
			
			target.prev().after(showbutton);
			
			
			var stop;
		});
	};
	
})(jQuery);


(function($) {
  // Extend all jQuery objects with the filterable method.
  $.fn.filterable = function(options) {
    var o = $.extend({}, $.fn.filterable.defaults, options);

    return this.each(function() {
      var target = $(this);

      // Create the query div, and insert it into the DOM.
      var div = $('<div class="' + o.queryCss + '">');
      switch (o.queryPosition) {
        case 'before': div.insertBefore(target); break;
        case 'after': div.insertAfter(target); break;
      }

      // Create the query input field, possibly with a label in front if it.
      var txt = $('<input type="text" value="search by name or zip"/>').appendTo(div);
      if (o.queryLabel) div.prepend('<label>' + o.queryLabel + '</label>');
	  
		var spanid = target.prev().prev().prev().children("span").attr("id") + "_map";
		var hasmap = false;
		var map = null;
		if($("#" + spanid).length)
		{
			hasmap = true;
			map = getFlashMovie(spanid);
		}

      // Define the filtering function.
      var fn = function() {
        var query = txt.val().toLowerCase();
		/*
        target.find(o.affects).each(function() {
          var item = $(this);
          if (item.text().toLowerCase().indexOf(query) >= 0) item.show();
          else item.hide();
        });
		*/
		target.show();
		$.uiTableFilter(target, query);
		 
		 
		if(hasmap)
		{
			if($.browser.msie)
				var trs = $(target).children().children().children().children(":visible");
			else
				var trs = $(target).children().children().children(":visible");
				
			if(trs.length <= 10)
			{
				map.clearmap();
				
				
				trs.each(function(){
					
					var name = $(".normallink", this).text();
					var link = $(".normallink", this).attr("href");
					var address = $(".address", this).text();
					var id = $(".id", this).text();
			
					if(id != "")
					{
						map.addBusiness(id, address, name, link);
						$(this).mouseover(function(){map.businessmouseover(id);});
						$(this).mouseout(function(){map.businessmouseout(id);});
						$(this).click(function(){map.businessclick(id);});
					}
				});
				
			}
			else
				map.clearmap();
		}
		
		
      };

		
		/*
		var showbutton = $("<a class='showhide'>Show/Hide Table</a>").click(function(){
			target.toggle();
		});

		if(target.hasClass('thide'))
			div.prepend(showbutton);
		*/

      // Attach the function to the input text field (onKeyUp) or to a button (onClick).
      if (o.queryButton)
		$('<input type="button" value="' + o.queryButton + '" />').appendTo(div).click(fn);
      else
		txt.keyup(fn);

	  $('<div class="clear">').insertAfter(div);

    });
  };

  // Declare default options.
  $.fn.filterable.defaults = {
    affects: '> *',
    queryLabel: '',
    queryButton: '',
    queryPosition: 'before',
    queryCss: 'ui-filterable-query'
  };
})(jQuery);


/*
 * Copyright (c) 2008 Greg Weber greg at gregweber.info
 * Dual licensed under the MIT and GPL licenses:
 * http://www.opensource.org/licenses/mit-license.php
 * http://www.gnu.org/licenses/gpl.html
 *
 * documentation at http://gregweber.info/projects/uitablefilter
 *
 * allows table rows to be filtered (made invisible)
 * <code>
 * t = $('table')
 * $.uiTableFilter( t, phrase )
 * </code>
 * arguments:
 *   jQuery object containing table rows
 *   phrase to search for
 *   optional arguments:
 *     column to limit search too (the column title in the table header)
 *     ifHidden - callback to execute if one or more elements was hidden
 */
jQuery.uiTableFilter = function(jq, phrase, column, ifHidden){
  var new_hidden = false;
  if( this.last_phrase === phrase ) return false;

  var phrase_length = phrase.length;
  var words = phrase.toLowerCase().split(" ");

  var success = function(elem) { elem.show() }
  var failure = function(elem) { elem.hide() }

  if( column ) {
    var index = null;
    jq.find("thead > tr:last > th").each( function(i){
      if( $(this).text() == column ){
        index = i;
        return false;
      }
    });
    var iselector = "td:eq(" + index + ")";

    var search_text = function( ){
      var elem = jQuery(this);
      jQuery.uiTableFilter.has_words( jQuery(elem.find(iselector)).text(), words ) ?
        success(elem) : failure(elem);
    }
  }
  else {
    var search_text = function(){
        var elem = jQuery(this);
        jQuery.uiTableFilter.has_words( elem.text(), words ) ? elem.show() : elem.hide();
    }
  }

  // if added one letter to last time,
  // just check newest word and only need to hide
  if( (words.size > 1) && (phrase.substr(0, phrase_length - 1) ===
        this.last_phrase) ) {

    if( phrase[-1] === " " )
    { this.last_phrase = phrase; return false; }

    success = function(elem) { elem.hide(); new_hidden = true; }
    failure = function(elem) {;}
    var words = words[-1];
    jq.find("tbody tr:visible").each( search_text )
  }
  else {
    new_hidden = true;
    jq.find("tbody > tr").each( search_text );
  }

  last_phrase = phrase;
  if( ifHidden && new_hidden ) ifHidden();
  return jq;
};
jQuery.uiTableFilter.last_phrase = ""

// not jQuery dependent
// "" [""] -> Boolean
// "" [""] Boolean -> Boolean
jQuery.uiTableFilter.has_words = function( str, words, caseSensitive )
{
  var text = caseSensitive ? str : str.toLowerCase();
  for (var i=0; i < words.length; i++) {
    if (text.indexOf(words[i]) === -1) return false;
  }
  return true;
}


function statsmapready()
{
	var mapt = getFlashMovie("schools_k_12_map");
	
	if(mapt != null)
		mapt.clearmap();
		
	var maptt = getFlashMovie("schools_college_map");
	
	if(maptt != null)
		maptt.clearmap();
	
	var sexoffmap = getFlashMovie("sexoffendermap");
	
	if(sexoffmap != null)
		sexoffmap.clearmap();
}

function jsalert(txt)
{
	alert(txt);
}
