var MAG = function(ajax_url)
{
	this.ajax_url = ajax_url;
	this.cache = {
		get_resorts: {},
		//get_programs: {},
		get_transports: {}
	};

	this.ajax_queue = [];
	this.ajax_is_loading = false;
	this.extra_ajax_data = {};
}

MAG.prototype = {
	init_events: function()
	{
		var selects = [];
		$('select[class]').each(function(i)
		{
			var selects_from_class = $('#'+$(this).attr('class').split(/ +/).join(',#'));

			if(selects_from_class.length == 1)
			{
				var select_id = selects_from_class.attr('id');
				$(this).unbind('change');
				$(this).change(function(e){
					if($MAG.ajax_is_loading) {
						$MAG.ajax_queue.push(this);
						return;
					}
					if(select_id.indexOf('resort_id') > -1) {
					   var data = {id: $(this).val(), method: 'get_resorts', country_id: $(this).val(), 'classes': this.className};
					   $MAG.get_data(data, '#'+select_id);
					}
//					else if(select_id.indexOf('program_id') > -1) {
//						$MAG.get_data({id: $(this).val(), method: 'get_programs', resort_id: $(this).val()}, '#'+select_id);
//                  }
					else if(select_id.indexOf('transport_type') > -1) {
						$MAG.get_data({id: $(this).val(), method: 'get_transports', resort_id: $(this).val()}, '#'+select_id);
					}
				});
				selects.push(this);
			}
		});
		$(selects).trigger('change');
	},

	get_data: function(data, select)
	{
		if(!this.cache[data.method][data.id])
		{
			var _self = this;
			_self.ajax_is_loading = true;
			$.extend(data, this.extra_ajax_data);
			$.ajax({
				url: this.ajax_url,
				data: data,
				dataType: 'json',
				success: function(response){
					//alert(1);
					_self.cache[data.method][data.id] = response;
					//alert(2);
					_self.get_data_complete(response, select);
					//alert(3)
					_self.ajax_is_loading = false;
				//	alert(4);
					_self.process_queue();
				}
			});
		}
		else {
			this.get_data_complete(this.cache[data.method][data.id], select);
		}
	},

	get_data_complete: function(data, select)
	{
		var sel = $(select);
		var selected_id = sel.val();

		var el = sel, i = 0;
		do {
			el[0].options.length = i++ == 0 ? 0 : 1;
			if(!el.attr('class')) break;
			el = $('#'+el.attr('class').split(/ +/).join(',#'));
		} while(el.length == 1);

		$.each(data, function(i, txt) {
			var option = $('<option />').val(i).text(txt);
			option.appendTo(select);
		});

		if(!$.browser.msie)
		{
			var sel = sel.get(0);
			var opt = $('option', sel).clone();
			sel.options.length = 0;
			$(opt).sort(function(a,b){
				return a.text.toString() == b.text.toString() ? 0 : (a.text.toString() > b.text.toString() ? 1 : -1);
			}).appendTo(sel);
		}

		/** -  workaround - **/

		if(select.indexOf('transport') != -1)
		{
			selected_id = $(select).attr('selected_val');
		}

		/** - end workaround -  **/

		$(select+" [value="+selected_id+"]").attr('selected', 'selected');
	},

	process_queue: function()
	{
		var el = this.ajax_queue.shift();
		if(el) $(el).trigger('change');
	}
};

$(document).ready(function() {
	$('.has_dirt').append('<span class="dirt"></span>');
	$('.has_more_dirt').append('<span class="more_dirt"></span>');

	// If we ever decide to stop supporting IE6, remove this. It combines multiple class selectors
	// into one single class names, so that IE6 will actually recognize them.
	// Ex.: class="first article" becomes class="first article first-article article-first"
	$('body *[class]').each(function(i, el) {
		if((/\w+\s+\w+/).test(el.className)) {
			el.className += ' '+(el.className.replace(/\s+/, '-'))+' '+el.className.split(' ').reverse().join('-');
		}
	});
});