$(document).ready(function() {
	// more links
	$(".more").each(function() {
		var theP = document.createElement("p");
		theP = $(theP);
		var theLink = document.createElement("a");
		theLink.href="#";
		theLink = $(theLink);
		theLink.text($(this).attr("title"));
		var target = this;
		theLink.click(function() {
			$(target).slideToggle();
			return false;
		});
		theP.append(theLink);
		theP.addClass("more-toggle");
		$(this).before(theP);
		$(this).hide(0);
	});

	//location fields tabs
	$("#pod-tabs-nav").tabs({fx: {opacity: 'toggle'},
							  show: function(event, ui) {
								$(".ui-tabs-hide").find(":input").clearForm();
							  },
							  select: function(event, ui) {
								$(".ui-tabs-hide").find(":input").clearForm();
							  }
							});

	$(".service-category").wrap("<div>");
	$(".category-header").each(function() { $(this).nextAll("div:lt(2)").hide(0); });
	$(".service-category input:checkbox:checked").parents(".service-category").parent().show(0);
	$(".category-header").css("cursor", "pointer");
	$(".category-header").toggle(function() {
		if($(this).nextAll("div:lt(2):visible").length == 0) {
			$(this).click();
		}
		else {
			$(this).nextAll("div:lt(2)").slideUp();
		}
	},
	function() {
		$(this).nextAll("div:lt(2)").slideDown();
	});
	
	// new search link
	if($(".pod-search-column .begin").length == 0) {
		$("#refine-form").hide(0);
		var para = document.createElement("p");
		para = $(para);
		para.css("text-align", "right");
		var theLink = document.createElement("a");
		theLink.href="#";
		theLink = $(theLink);
		theLink.text("New Search");
		theLink.click(function() {
			$("#refine-form").slideToggle();
			return false;
		});
		para.append(theLink);
		para.addClass("new-link");
		$(".pod-search-column #refine-form").before(para);
	}

	//global category select all
	var cBox = document.createElement("input");
	$(cBox).attr("type", "checkbox");
	$(cBox).click(function() {
    	if($(this).attr("checked")) {
        	open_group($(this).parent().attr("class"));
		}
		else {
			close_group($(this).parent().attr("class"));
		}
	});
	$(".category-types div").prepend(cBox);
	
	$(".category-types div").each(function() {
		var theClass = $(this).attr("class");
		var isChecked = true;
		var theSelector = ".service-category td." + theClass + " input:checkbox";				
		$(theSelector).each(function() {
			isChecked = isChecked && this.checked;
		});
		$(this).children("input:checkbox").attr("checked", isChecked);
	});
	
	//individual category select all
	var cBox = document.createElement("input");
	$(cBox).attr("type", "checkbox");
	$(cBox).click(function() {		
		var checked_status = this.checked;				
		$(this).parent().parent().next("tr").find("input:checkbox").each(function() {
			this.checked = checked_status;
		});
	});
	$(".category-type").prepend(cBox);
	
	$(".category-type").each(function() {
		var isChecked = true;
		$(this).parent().next("tr").find("input:checkbox").each(function() {
			isChecked = isChecked && this.checked;
		});
		$(this).children("input:checkbox").attr("checked", isChecked);
	});
	
	$(".service-category td:not(.category-type)").each(function() {
		$(this).find("input:checkbox").click(function() {
			var container = $(this).parents("table:first").parent();
			var allChecked = true;
			container.find("input:checkbox").each(function() {
				allChecked = allChecked && this.checked;
			});
			container.parent().prev("tr").find("input:checkbox").attr("checked", allChecked);
		});
	});

	$(".btnSearch").click(function() {
		var textEntered = false;
		$(".pod-search-column").find("input:text, select.country").each(function() {
			textEntered = textEntered || ($(this).val() != "");
		});
		if(textEntered) {
			$("#dialog").css("display", "block");
			$("#dialog").dialog({ 
				modal: true, 
				overlay: { 
					opacity: 0.5, 
					background: "black" 
				},
				draggable: false,
				resizable: false,
				width: 350,
				height: 300,
				buttons: {
					Cancel: doStop
				}
			});
			$("#dialog").css("width", "300px");
		}
		else {
			alert("Please enter at least one field.");
			return false;
		}
	});
	
	$(".results-table tr:first").wrap("<thead>").parent().prependTo(".results-table");
	$(".results-table thead tr").clone().appendTo(".results-table").wrap("<tfoot>").parent().appendTo(".results-table");
	$(".results-table").tablesorter({widgets: ['zebra'], textExtraction: "complex", headers: { 0:{ sorter: false } }});
	
	
	// service description foldouts
	$("#organizing-codes h3 + div").hide(0);
	$("#organizing-codes h3").wrapInner('<a href="#"></a>').click(function() {
		$(this).next("div").slideToggle();
		return false;																	   
	});
	$("#organizing-codes h4 + p").hide(0);
	$("#organizing-codes h4").wrapInner('<a href="#"></a>').click(function() {
		$(this).next("p").slideToggle();
		return false;																	   
	});
	
	
	//paper & electronic organizing
	$(".service-RPE input:checkbox").click(function() {
		if (!this.checked) {
			$(".service-RE input:checkbox, .service-RPO input:checkbox").attr("checked", false);
		}
	});
	$(".service-BPE :checkbox").click(function() { 
		if (!this.checked) {
			$(".service-BE input:checkbox, .service-BPO input:checkbox").attr("checked",false);
		}
	});
	$(".service-RE :checkbox").click(function() { $(".service-RPE input:checkbox").attr("checked", this.checked); });
	$(".service-BE :checkbox").click(function() { $(".service-BPE input:checkbox").attr("checked", this.checked); });
	$(".service-RPO :checkbox").click(function() { $(".service-RPE input:checkbox").attr("checked", this.checked); });
	$(".service-BPO :checkbox").click(function() { $(".service-BPE input:checkbox").attr("checked", this.checked); });
});

// taken from http://www.learningjquery.com/2007/08/clearing-form-data
$.fn.clearForm = function() {
  return this.each(function() {
	var type = this.type, tag = this.tagName.toLowerCase();
	if (tag == 'form')
	  return $(':input',this).clearForm();
	if (type == 'text' || type == 'password' || tag == 'textarea')
	  this.value = '';
	else if (type == 'checkbox' || type == 'radio')
	  this.checked = false;
	else if (tag == 'select')
	  this.selectedIndex = -1;
  });
};



function open_group(theClass) {
	$(".service-category." + theClass).each(function() {
		$(this).parent().slideDown();
		$(this).parent().addClass("shown");
	});
}

function close_group(theClass) {
	$(".service-category." + theClass).each(function() {
		$(this).find("input:checkbox").each(function() {
			this.checked = false;
		});
		$(this).parent().slideUp();
		$(this).parent().removeClass("shown");
	});
}

function check_and_toggle() {
	$(".service-category").each(function() {
		var anyChecked = false;
		$(this).find("input:checkbox").each(function() {
			anyChecked = anyChecked || this.checked;
		});
		if(anyChecked && $(this).parent().is(":hidden")) {
			$(this).parent().slideDown();
		}
		else if(!anyChecked && $(this).parent().is(":visible")) {
			$(this).parent().slideUp();
		}
	});
}

function doStop() {
	if($.browser.msie) {
		document.execCommand("Stop");
	}
	else {
		window.stop();
	}
	$("#dialog").dialog("close");
}
