/**
 * @author Coolmen
 */

/**
 * Селектор
 */
function selectorClass () {

	this.init = function () {
		// Добавление опшна
		var append = function ( text, value ) {
			var o = document.createElement('option');
			this.appendChild(o);
			o.text=text;
			o.value=value;
		}
		// Очистка селекта
		var clear = function () {
			while ( this.options.length > 0 ) {
				this.removeChild( this.options[0] );
			}
		}

		if ( ! $('region-selector') ) return false;

		$('region-selector').appendOption = append;
		$('date-selector').appendOption = append;
		$('length-selector').appendOption = append;
//		if ( !date ) return;
		$('region-selector').clearSelect = clear;
		$('date-selector').clearSelect = clear;
		$('length-selector').clearSelect = clear;
		this.build();
	};

	/**
	 * Генерация селектов
	 */
	this.build = function () {
		$('region-selector').clearSelect();
		$('region-selector').appendOption( 'where?', 0);
		for (k in date) {
			$('region-selector').appendOption(date[k].title, k);
		}
		$('region-selector').onchange = function () { selector.dates() };
		$('region-label').className='cool'; $('date-label').className=''; $('length-label').className='';

		$('date-selector').clearSelect();
		$('date-selector').appendOption( 'when?', 0);
		if ($('region-selector').value != 0) {
			var dates = date[$('region-selector').value].dates;
			for (k in dates) {
				$('date-selector').appendOption(dates[k].title, dates[k].value);
			}
			$('date-selector').disabled = 0;
		}
		else {
			$('date-selector').disabled = 1;
		}

		$('length-selector').clearSelect();
		$('length-selector').appendOption( 'for long?', 0);
		if ( $('date-selector').value != 0 ) {
			var lengths = date[$('region-selector').value].dates[$('date-selector').value].lengths;
			for (k in lengths) {
				$('length-selector').appendOption(lengths[k].title, lengths[k].value);
			}
			$('length-selector').disabled = 0;
		}
		else {
			$('length-selector').disabled = 1;
		}
	}

	/**
	 * 
	 */
	this.dates = function () {
		$('date-selector').clearSelect();
		$('date-selector').appendOption( 'where?', 0);
		if ($('region-selector').value != 0) {
			var dates = date[$('region-selector').value].dates;
			for (k in dates) {
				$('date-selector').appendOption(dates[k].title, dates[k].value);
			}
			$('date-selector').disabled = 0;
			$('region-label').className='';
			$('date-label').className= $('region-selector').value == 0 ? '' : 'cool';
		}
		else {
			$('date-selector').disabled = 1;
			$('region-label').className='cool'; $('date-label').className='';

		}
		$('date-selector').onchange = function () { selector.lengths() };
		this.lengths();
	}


	/**
	 * 
	 */
	this.lengths = function () {
		$('length-selector').clearSelect();
		$('length-selector').appendOption( 'for long?', 0);
		if ( $('date-selector').value != 0 ) {
			var lengths = date[$('region-selector').value].dates[$('date-selector').value].lengths;
			for (k in lengths) {
				$('length-selector').appendOption(lengths[k].title, lengths[k].value);
			}
			$('length-selector').disabled = 0;
			$('length-label').className='cool';
			$('date-label').className='';

		}
		else {
			$('length-selector').disabled = 1;
			$('length-label').className='';
			$('date-label').className= $('region-selector').value == 0 ? '' : 'cool';
		}
		$('length-selector').onchange = function () { selector._submit() };
	}

	
	/**
	 * Субмит формы 
	 */
	this._submit = function () {
		if ($('length-selector').value != 0) {
			$('selector-form').submit();
		}
	}

}


window.onload = function () {
	if ( selector = new selectorClass() ) selector.init();
}

// а-ля Prototype framework 
window.$ = function (id) {
	if ( !id ) return null;
	 return document.getElementById(id);
} 
