var MAG = Object.extend({}, MAG || {});

MAG.Compare = Class.create(
{
	initialize: function(options)
	{
		this.options = {};
		Object.extend(this.options, options || {});

		this.options.action_url = _site_url.replace('/products', '/mobile');
		this.options.ajax_url = this.options.action_url.replace('/compare', '/xml_http_request');

		this.options.first_load = true;

		/**
		 * can be 'primary' or 'secondary', and is used to set from which dropdown
		 * was made the request and respectively which model dropdown to be filled
		 */
		this.options.dropdown_id_suffix = null;


		if($('creator_primary'))
		{
			$('creator_primary').observe('change', this.getModels.bind(this, 'primary'));
			this.getModels('primary');
		}

		if($('creator_secondary')){
			$('creator_secondary').observe('change', this.getModels.bind(this, 'secondary'));
		}


		if($('model_primary'))
		{
			$('model_primary').observe('change', function()
			{
				if($F('model_primary') != 0)
				{
					if($F('model_secondary') != 0) {
						window.location = this.options.action_url + '/' + $F('model_primary') + ',' + $F('model_secondary');
					}
					else{
						window.location = this.options.action_url + '/' + $F('model_primary');
					}
				}
			}.bind(this));
		}

		if($('model_secondary'))
		{
			$('model_secondary').observe('change', function()
			{
				if($F('model_secondary') != 0) {
					window.location = this.options.action_url + '/' + $F('model_primary') + ',' + $F('model_secondary');
				}
			}.bind(this));
		}

		this.setModel_ids();

	},


	getModels: function(dropdown_id_suffix)
	{
		this.options.dropdown_id_suffix = dropdown_id_suffix;
		var creator_id = $F('creator_' + dropdown_id_suffix);

		new Ajax.Request(this.options.ajax_url,{
			method: 'get',
			parameters: 'do=get_models&creator=' + creator_id,
			onComplete: this.proceedModels.bind(this)
		});
	},


	proceedModels: function(req) {
		var json = req.responseText.evalJSON();
		this.fillSelect(json.models);
	},


	fillSelect: function(values)
	{
		current = $F('current_model_' + this.options.dropdown_id_suffix);
	    el = $('model_' + this.options.dropdown_id_suffix);

	    el.innerHTML = "";

		if(values.length == 0) {
			if($F('creator_' + this.options.dropdown_id_suffix) != 0) {
				el.options.add(new Option('Няма намерени модели', ''));
			}
			else {
				el.options.add(new Option('Изберете марка', ''));
			}
		}
		else {
	    	el.options.add(new Option('Изберете модел...', ''));
		}

	    if (values.length)
	    {
		    for (var i = 0; i < values.length; i++)
		    {
	    		el.options[el.options.length] = new Option(values[i].title, values[i].id);
	    		el.options[el.options.length-1].selected = current == values[i].id;
		    }
	    }

	    //on page load
	    if(this.options.dropdown_id_suffix == 'primary' && this.options.first_load) {
	    	this.getModels('secondary');
	    	this.options.first_load = false;
	    }
	},


	setModel_ids: function()
	{
		ids = window.location.href.replace(this.options.action_url + '/', '');
		ids = ids.split(',', 2);

		this.options.primary_model_id = parseInt(ids[0]);
		this.options.secondary_model_id = parseInt(ids[1]);

	}
});

document.observe('dom:loaded', function(){
	new MAG.Compare();
});