/* Build the list of stores. */

function Countries() {
	this.countries = new Array();
	this.addCountry = Countries_addCountry;
	this.write = Countries_write;
}

function Countries_addCountry(name,abbrev) {
	var country = new Country(name,abbrev);
	this.countries[this.countries.length] = country;
	return country;
}

function jumpToCountry()
{
	var listOfCountries = MM_findObj("listOfCountries");
	window.open("#" + listOfCountries.value,"_self");
}

function Countries_write()
{
	content = "";
	
	// Build the country selector
	content += "<form>";
	content += "<select id=\"listOfCountries\" onChange=\"jumpToCountry();\">";
	for(var i in this.countries) {
		var country = this.countries[i];
		var nstores = 0;
		
		for(var j in country.states.states) {
			var state = country.states.states[j];
			nstores += state.stores.length;
		}
		
		if(nstores > 0) {
			content += "<option value=\"" + country.abbrev + "\">" + country.name + "</option>";
		}
	}
	content += "</select>";
	content += "</form>";
	
	document.writeln(content);
	
	// Write them out
	for(var i in this.countries) {
		var country = this.countries[i];
		
		var nstores = 0;
		
		for(var j in country.states.states) {
			var state = country.states.states[j];
			nstores += state.stores.length;
		}
		
		if(nstores > 0) {
			/* Write country name */
			document.writeln("<a name=\"" + country.abbrev + "\"></a>");
			
			document.writeln('<div class="country">');
			document.writeln('<div class="countryname">' + country.name + '</div>');
		
			/* Write states */
			country.states.write();
			
			document.writeln('</div>');
		}
	}
}

function Country(name,abbrev) {
	this.states = new States(name,abbrev);
	this.name = name;
	this.abbrev = abbrev;
	
	this.addState = Country_addState;
}

function Country_addState(name,abbrev) {
	return this.states.addState(name,abbrev);
}

function States(name,abbrev) {	
	this.states = new Array();
	this.name = name;
	this.abbrev = abbrev;
	this.write = States_write;
	
	this.addState = States_addState;
}

function States_addState(name,abbrev) {
	var state = new State(name,abbrev);
	this.states[this.states.length] = state;
	return state;
}

function jumpToState(abbrev)
{
	var listOfStates = MM_findObj(abbrev + "_listOfStates");
	window.open("#" + listOfStates.value,"_self");
}

function States_write()
{
	var content = "";
	
	var iState, nStates = this.states.length;
	
	if(nStates > 1) {
		content += "<form>";
		content += "<select id=\"" + this.abbrev + "_listOfStates\" onChange=\"jumpToState('" + this.abbrev + "');\">";
		for(iState = 0;iState < nStates;iState++) {
			var state = this.states[iState];
	
			if(state.stores.length > 0) {
				content += "<option value=\"" + this.abbrev + "_" + state.abbrev + "\">" + state.name + "</option>";
			}
		}
		content += "</select>";
		content += "</form>";
	}
	
	for(iState = 0;iState < nStates;iState++) {
		var state = this.states[iState];
	
		if(state.stores.length > 0) {
			content += "<div class=\"state\">";
			
			if(state.name != "") {
				content += "<a name=\"" + this.abbrev + "_" + state.abbrev + "\"></a>";
				content += "<div class=\"statename\">" + state.name + "</div>";
			}
			
			var iStore = 0, nStores = state.stores.length;
			var iCol = 0, nCol = 2;
			
			while(iStore < nStores) {
				content += "<div class=\"row\">";
				
				iCol = 0;
				while(iCol < nCol && iStore < nStores) {
					
					content += "<div class=\"store\">";
					
					var store = state.stores[iStore];
					var text = store.text;
					for(var j = 0;j < text.length;j++) {
						if(text[j].substring(0,4) == "www.") {
							content += "<a href=\"http://" + text[j] + "\">" + text[j] + "</a>";
						}
						else if(text[j].substring(0,7) == "http://") {
							content += "<a href=\"" + text[j] + "\">" + text[j].substring(7,text[j].length) + "</a>";
						}
						else {
							content += text[j];
						}

						if(j < text.length-1) {
							content += "<br>";
						}
					}
					
					content += "</div>";
					
					iCol += 1;
					iStore += 1;
				}
				
				content += "</div>";
			}

			content += "</div>";
		}
	}
	
	document.writeln(content);
}


function State(name,abbrev) {
	this.stores = new Array();
	this.name = name;
	this.abbrev = abbrev;
	
	this.addStore = addStore;
}

function addStore()
{
	var s = new Store();
	s.text = new Array();
	
	for(var i = 0;i < addStore.arguments.length;i++) {
		s.text[i] = addStore.arguments[i];
	}
	
	this.stores[this.stores.length] = s;
}

function Store()
{
	this.text = new Array();
}

// Global list of stores
var countries;

countries = new Countries();