// ***************************************************************************
//   Memory Highway Ltd. Copyright (C) 2007. All Rights Reserved  $Revision: 1.15 $                     
// ***************************************************************************

$(function(){
 	$.ajaxSetup({cache: false});
 	
	var $type = $("#searchform input[@name='type']");
	var $mode = $("#searchform input[@name='mode']");
	var $fields = $("#searchtable :input");
	var $rangetypes = $("#searchform input[@name='criteria.rangeType']");
	var $starttimeframe = $("#starttimeframe");
	var $endtimeframe = $("#endtimeframe");
	var $advancedRows = $("#searchtable tr:not(.required)");
	var $tabLinks = $("#searchresultstabs a");

	$rangetypes.click(function(event){
		updateForm();
	})

	var updateForm = function() {
		if ($mode.val() == "SIMPLE") {
			$advancedRows.hide();
		} else {
			$advancedRows.show();
			if ($rangetypes.filter(":checked").val() == "ON") {
				$endtimeframe.hide()
			} else {
				$endtimeframe.show()
			}			
		}
	}	
				
	$("#searchtypelink").click(function(e) {
		$mode.val($mode.val() == "SIMPLE" ? "ADVANCED" : "SIMPLE");		
		$(this).text($mode.val() == "SIMPLE" ? "Advanced Find \273" : "\253 Simple Find");
		updateForm();
		e.preventDefault();
	});
	
	$tabLinks.click(function(e){
		var tabName = this.id.replace(/tablink/g,"");
		var $tab = $("#" + tabName + "searchresults");
		$tab.siblings().hide();
		$tab.show();
		$tabLinks.not($(this)).parent().removeClass("selected");
		$(this).parent().addClass("selected");
		e.preventDefault();
	});
	
	$(".searchresults a[@rel='history']").livequery(function(){
		$(this).remote($(this).parents(".searchresults")[0], function(){
			var tabName = this.title.replace(/^(\S+)\s.*/,"$1").toLowerCase();
			$("#" + tabName + "tablink").click();
		});
	});	
		
	var tabName = $("#searchresultstabs div.selected a")[0].id.replace(/tablink/g,"");
	var firstPagerLink = "#" + tabName + "searchresults .pager a:eq(0)";
	$.ajaxHistory.initialize(function(){
		$(firstPagerLink).click();
	});
	
	updateForm();
});
