/* $Id: program_search_form.js 55 2008-06-18 11:51:44Z edin.coralic $ */

var ProgramSearchForm = {
	
	run: function()
	{
		ProgramSearchForm.expander();
		ProgramSearchForm.company_ship();
	},

	expander: function()
	{
		var box           = $('program_search_fieldset_bottom');
		var expand_option = $('expand_option');
		var opened        = $('program_search_opened');

		if (box && expand_option && opened) {
			ProgramSearchForm._box           = box;
			ProgramSearchForm._opened        = opened;
			ProgramSearchForm._expand_link   = $('expand_link');
			ProgramSearchForm._collapse_link = $('collapse_link');

			ProgramSearchForm.expander_set_expand();
			ProgramSearchForm.expander_set_collapse();

			if (ProgramSearchForm.expander_is_opened()) {
				ProgramSearchForm._expand_link.hide();
			} else {
				ProgramSearchForm._collapse_link.hide();
				ProgramSearchForm._box.hide();
			}
		}
	},
	expander_set_expand: function()
	{
		ProgramSearchForm._expand_link.observe('click', ProgramSearchForm.expander_expand);
	},
	expander_expand: function(event) {
		event.stop();

		ProgramSearchForm._opened.value = '1';

		Effect.BlindDown(ProgramSearchForm._box, {
			afterFinish: function() {
				ProgramSearchForm._expand_link.hide();
				ProgramSearchForm._collapse_link.show();
			}
		});
	},
	expander_set_collapse: function()
	{
		ProgramSearchForm._collapse_link.observe('click', ProgramSearchForm.expander_collapse);
	},
	expander_collapse: function(event) {
		event.stop();

		ProgramSearchForm._opened.value = '0';

		Effect.BlindUp(ProgramSearchForm._box, {
			afterFinish: function() {
				ProgramSearchForm._expand_link.show();
				ProgramSearchForm._collapse_link.hide();
			}
		});
	},
	expander_is_opened: function()
	{
		if (ProgramSearchForm._opened.value > 0) {
			return true;
		}
		return false;
	},
	
	company_ship: function()
	{
		var company = $('program_search_company');
		var ship    = $('program_search_ship');

		if (company && ship) {
			ProgramSearchForm._company = company;
			ProgramSearchForm._ship = ship;

			ProgramSearchForm._ship_all_options = ship.select('option');

			ProgramSearchForm._company.observe('change', ProgramSearchForm.company_change);
			ProgramSearchForm._ship.observe('change', ProgramSearchForm.ship_change);
		}
	},
	company_change: function(event)
	{
		ProgramSearchForm._ship.innerHTML = '';
		var i = 0;
		ProgramSearchForm.selected_company = ProgramSearchForm._company.value;
		if (ProgramSearchForm.selected_company) {
			ProgramSearchForm._ship_all_options.each(function(input) {
				if (!i) {
					ProgramSearchForm._ship.options[i] = input;
					i++;
				}
				var rel = input.getAttribute('rel');
				if (ProgramSearchForm.selected_company == rel) {
					ProgramSearchForm._ship.options[i] = input;
					i++; 
				}
			});
		} else {
			ProgramSearchForm._ship_all_options.each(function(input) {
				ProgramSearchForm._ship.options[i] = input;
				i++; 
			});
		}
		if (event && ProgramSearchForm.selected_company) {
			ProgramSearchForm._ship.value = null;
		} else {
			ProgramSearchForm._ship.value = ProgramSearchForm.selected_ship;
		}
	},
	
	ship_change: function(event)
	{
		ProgramSearchForm.selected_company = 0;
		ProgramSearchForm.selected_ship = ProgramSearchForm._ship.value;
		for (var i in ProgramSearchForm._ship_all_options) {
			var input = ProgramSearchForm._ship_all_options[i];
			if (input.value == ProgramSearchForm.selected_ship) {
				ProgramSearchForm.selected_company = input.getAttribute('rel');
				break;
			}
		}
		if (ProgramSearchForm.selected_company) {
			ProgramSearchForm._company.value = ProgramSearchForm.selected_company;
			ProgramSearchForm.company_change(null);
		}
	}

}

new StartUp(ProgramSearchForm);
