﻿window.StringTable = { 
	get: function( name ) { return this[name.toLowerCase()] || name; },
	set: function( name, value ) { this[name.toLowerCase()] = value; } 
};

$(document).ready(
     function() {

     	var formSubmit = function() {
     		//if( $('#region')[0].selectedIndex == 0 )
     		//return fullListClick();

     		if ($('#city')[0].selectedIndex == 0)
     			return fullListClick();

     		var form = $('#storesForm')[0];
     		form.submit();
     	};

     	var regionChange = function() {
     		var regionForm = $("#regionForm")[0];
     		regionForm.rid.value = this.value;
     		regionForm.submit();
     	};

     	var cityChange = function() {
     		//SelectSetup( $('#metro')[0], RegionInfo[2 * this.selectedIndex + 1] );
     		formSubmit();
     	};

     	var fullListClick = function() {
     		location.href = '/stores.sdf';
     	};

     	var voteSubmit = function() {
     		var form = $('#poll')[0];
     		if(form) form.submit();
     	};

     	// ============== INIT ============= //
     	//SelectsSetup();
     	$('#button').click(formSubmit);
     	//$('#region').change( regionChange );
     	$('#city').change(cityChange);
     	$('#full-list').click(fullListClick);

     	if ($('#votebutton')[0]) $('#votebutton').click(voteSubmit);

     	// ============== HELPERS ============= //  
     	function SelectSetup(sel, col) {
     		sel.options.length = col.length / 2;
     		for (var i = 0; i < col.length; i++) {
     			if (i % 2 == 0) {
     				sel.options[i / 2].text = col[i];
     				if (col[i] == 'Все')
     					sel.options[i / 2].value = 'all';
     			}
     		}
     	};

     	function SelectsSetup() {
     		var metroIndex = 0;
     		var cityIndex = 0;

     		if (cityParam)
     			for (var i = 0; i < RegionInfo.length; i += 2)
     			if (RegionInfo[i] == cityParam) {
     			cityIndex = i; break;
     		}

     		if (metroParam)
     			for (var i = 0; i < RegionInfo[cityIndex + 1].length; i += 2)
     			if (RegionInfo[cityIndex + 1][i] == metroParam) {
     			metroIndex = i; break;
     		}

     		SelectSetup($('#city')[0], RegionInfo);
     		$('#city')[0].selectedIndex = cityIndex / 2;

     		SelectSetup($('#metro')[0], RegionInfo[cityIndex + 1]);
     		$('#metro')[0].selectedIndex = metroIndex / 2;
     	};

     }
);

