
/* global state */

var REGION_WASHINGTON = 1;
var REGION_OTHER = 0;
var METHOD_AVA = 0;
var METHOD_CITY = 1;
var METHOD_NAME = 2;

var g_currentRegion = REGION_WASHINGTON;
var g_currentMethod = METHOD_AVA;
var g_currentSearchValue;
var g_showPublic = true;
var g_showPrivate = true;
/* class defs */

function IdName(id, name) {
	this.id = id;
	this.name = name;
}

/* data */

var rgWashingtonWineAVA = new Array();
var rgWashingtonWineCity = new Array();
var rgWashingtonWineName = new Array();

var rgOtherAVA = new Array();
var rgOtherCity = new Array();
var rgOtherName = new Array();

/* initialization */
function initWizard() {	
	var region 
	var method 
	var searchval 
	var showpub = true;
	var showpriv = true;
	
	var sURL = document.URL.toLowerCase();

	if (sURL.indexOf("searchresults.aspx")>0){
	$("closeSearchOptions").style.display =  "block";
	}else {
	$("closeSearchOptions").style.display =  "none";
	}
		
	if ($("refineRegion") != null) {
	region = $("refineRegion").value;
	} else {
	region = g_currentRegion;
	}
	
	if ($("refineMethod") != null) {
	method = $("refineMethod").value;
	} else {
	method = g_currentMethod;
	}
	
	if ($("refineSearchValue") != null) {
	searchval = $("refineSearchValue").value;
	} else {
	searchval = g_currentSearchValue;
	}
	if ($("showPublic2") != null) {
	showpub = $("showPublic2").checked;
	} else {
	showpub = g_showPublic
	}
	if ($("showPrivate2") != null) {
	showpriv = $("showPrivate2").checked;
	} else {
	showpriv = g_showPrivate;
	}
	
	//alert($("showPrivate").checked);
	//$("region1").checked = true;
	//$("method0").checked = true;
	
	$("region" + region).checked = true;
	$("method" + method).checked = true;
	g_currentRegion = region;
	g_currentMethod = method;
	g_currentSearchValue = searchval;
	g_showPrivate =	showpriv;
	g_showPublic = showpub;
	buildSelectionList();
	synchSelectionList();
}

/* helper functions */
function synchSelectionList() {
//if option public or private not checked then check showtastingoption
if (g_currentRegion == REGION_WASHINGTON) {
			Element.show("optionAVA");	
			Element.show("optionCity");
			Element.show("optionName");	
	
	}else if (g_currentRegion == REGION_OTHER) {
			Element.hide("optionAVA");	
			Element.hide("optionCity");
			Element.hide("optionName");
				selectChildInput($("optionAVA"),true);
				//g_currentMethod = METHOD_AVA;
				buildSelectionList();
	}
	
if (g_showPrivate  == false || g_showPublic == false) {

	$("showTastingOptions").checked= true;
	selectChildInput($("optionPublic"),g_showPublic);
	selectChildInput($("optionPrivate"),g_showPrivate);
	$("optionPublic").style.display = ($("showTastingOptions").checked ? "block" : "none");
	$("optionPrivate").style.display = ($("showTastingOptions").checked ? "block" : "none");

}
}
function buildSelectionList() {
	var region = g_currentRegion;
	var method = g_currentMethod;
	var msg = "";
	var rg = null;
	if (region == REGION_WASHINGTON) {
		if (method == METHOD_AVA) {
			msg = "Select a Viticultural Area";
			rg = rgWashingtonWineAVA;
		} else if (method == METHOD_CITY) {
			msg = "Select a City";
			rg = rgWashingtonWineCity;
		} else if (method == METHOD_NAME) {
			msg = "Select a Winery";
			rg = rgWashingtonWineName;
		}
	} else if (region == REGION_OTHER) {
		if (method == METHOD_AVA) {
			msg = "Select a Region";
			rg = rgOtherAVA;
		} else if (method == METHOD_CITY) {
			msg = "Select a City";
			rg = rgOtherCity;
		} else if (method == METHOD_NAME) {
			msg = "Select a Winery";
			rg = rgOtherName;
		}	
	}
	
	if (!rg) return;
	
	var sb = new Array();
	sb[sb.length] = "<select name=\"searchValue\">";
	//if (method == METHOD_CITY) {
	//	for (var i=0; i<rg.length; i++) {
	//		sb[sb.length] = "<option>" + rg[i].name + "</option>";
	//	}	
	//} else {
		for (var i=0; i<rg.length; i++) {
			if( rg[i].id == g_currentSearchValue ) {
			sb[sb.length] = "<option selected=true value=\"" + rg[i].id + "\">" + rg[i].name + "</option>";
			} else {
			sb[sb.length] = "<option value=\"" + rg[i].id + "\">" + rg[i].name + "</option>";
			}
		}
	//}
	sb[sb.length] = "</select>";
	
	$('wizardValue').innerHTML = msg;
	new Insertion.Bottom('wizardValue',sb.join("\n"));
}

function selectChildInput(o,f) {
	if (!o || !o.childNodes) return;
	for (var i=0; i<o.childNodes.length; i++) {
		if (o.childNodes[i].nodeName == "INPUT") {			
			o.childNodes[i].checked = f;
		}
	}						
}

/* page logic */		

var page_rules = {
	'#closeSearchOptions' : function(element) {
		element.onclick = function() {
			$('wizard').style.display = "none";
			return false;
		}
	},
	'#optionWineCountry' : function (element) {
		element.onclick = function() {
			this.childNodes[0].checked = true;			
			g_currentRegion = REGION_WASHINGTON;
			buildSelectionList();
			Element.show("optionAVA");	
			Element.show("optionCity");
			Element.show("optionName");		
		}					
	},

	'#optionOther' : function (element) {
		element.onclick = function() {
			selectChildInput(this,true);		
			g_currentRegion = REGION_OTHER;
			buildSelectionList();
			Element.hide("optionAVA");	
			Element.hide("optionCity");
			Element.hide("optionName");
			//if (g_currentMethod == METHOD_AVA) {
				selectChildInput($("optionAVA"),true);
				g_currentMethod = METHOD_AVA;
				buildSelectionList();
			//}
		}					
	},
	
	'#optionAVA': function (element) {
		element.onclick = function() {
			selectChildInput(this,true);
			g_currentMethod = METHOD_AVA;
			buildSelectionList();
		}					
	},
	
	'#optionCity': function (element) {
		element.onclick = function() {
			selectChildInput(this,true);
			g_currentMethod = METHOD_CITY;
			buildSelectionList();
		}					
	},
	
	'#optionName': function (element) {
		element.onclick = function() {
			selectChildInput(this,true);
			g_currentMethod = METHOD_NAME;
			buildSelectionList();
		}					
	},
	
	'#showTastingOptions': function(element) {
		element.onclick = function() {
			$("optionPublic").style.display = (element.checked ? "block" : "none");
			$("optionPrivate").style.display = (element.checked ? "block" : "none");
		}
	}	
	
}
Behaviour.addLoadEvent(initWizard);
Behaviour.register(page_rules);
    

