

HkAddAutoTestimonial = function(url)
{
	this.url = url;
	this.cacheModels = new Object();
	this.cacheModels[""] = new Array();

	this.idSeachMark = 'idATAB';
	this.idSeachModel = 'idATAM';
	
	this.isModelsUpdating = false;
}

HkAddAutoTestimonial.prototype.GetModels = function(markId)
{
	if (this.isModelsUpdating) return;

	if (!this.cacheModels[markId])
	{
		this.isModelsUpdating = true;
		jQuery('#'+this.idSeachModel).attr('disabled', 'disabled');
		this.GetModelsFromServer(markId);
		return;
	}

	this.RenderSearchModel(markId);
}

HkAddAutoTestimonial.prototype.GetModelsFromServer = function(markId)
{
	var selfObject = this;

	jQuery.getJSON(
		this.url + 'get_models_versions_testimonial/' + markId + '/',
		function(data)
		{
			selfObject.cacheModels[markId] = new Array();

			jQuery.each(data, function(i,item){
				//fill cache here
				selfObject.cacheModels[markId].push({id: item.id, title: item.title, url:item.url});
			});

			selfObject.RenderSearchModel(markId);
			selfObject.isModelsUpdating = false;
		}
	);	
}


HkAddAutoTestimonial.prototype.RenderSearchModel = function(markId)
{
	jQuery('#'+this.idSeachModel).empty();
	
	jQuery('#'+this.idSeachModel).removeAttr('disabled');
	
	var opts = this.cacheModels[markId];
	if (opts == undefined || !opts.length || opts.length<1)
	{
		jQuery('#'+this.idSeachModel).append('<option value="" selected="selected">Модель</option>');
		jQuery('#'+this.idSeachModel).change();
		return;
	}
	else
	{
		jQuery('#'+this.idSeachModel).append('<option value="" selected="selected">Модель</option>');
		for(var i=0; i<opts.length; i++)
		{
			jQuery('#'+this.idSeachModel).append('<option value="' + opts[i].url + '">' + opts[i].title +  '</option>');
		}
		jQuery('#'+this.idSeachModel).change();
	}
}


function ToggleAddTestimonial()
{
	jQuery('#add-testimonial div.at-panel').toggle();
}


var hkATA = null;

jQuery(document).ready(function(){

	hkATA = new HkAddAutoTestimonial(hkBaseUri + 'auto_helper/');

	//init brands form search select
	jQuery('#idATAB').html(jQuery('#idSearchMark').html());
	
	
	//add mark handler
	jQuery('#idATAB').change(function(){
		hkATA.GetModels(this.value);
	});

	//add submit handler
	jQuery('a.at-add-auto').click(function(e){ 
		e.preventDefault();

		var markId = jQuery('#'+hkATA.idSeachMark).val();
		//var modelId = jQuery('#'+hkATA.idSeachModel).val();
		var versionUrl = jQuery('#'+hkATA.idSeachModel).val();

		if (markId<1 || versionUrl=='')
		{
			alert('Выберите марку и модель автомобиля!');
			return;
		}
		
		location.href = hkBaseUri + 'auto/' + versionUrl + 'testimonials';
	});

});





/* -------------  moto  ------------ */






HkAddMotoTestimonial = function(url)
{
	this.url = url;
	this.cacheMarks = new Object();
	this.cacheMarks[""] = new Array();

	this.cacheModels = new Object();
	this.cacheModels[""] = new Array();
	
	this.idSeachCategory = 'idATMC';
	this.idSeachMark = 'idATMB';
	this.idSeachModel = 'idATMM';
	
	this.isMarksUpdating = false;
	this.isModelsUpdating = false;
}

HkAddMotoTestimonial.prototype.GetMarks = function(categoryId)
{
	if (this.isMarksUpdating) return;

	if (!this.cacheMarks[categoryId])
	{
		this.isMarksUpdating = true;
		jQuery('#'+this.idSeachMark).attr('disabled', 'disabled');
		jQuery('#'+this.idSeachModel).attr('disabled', 'disabled');
		this.GetMarksFromServer(categoryId);
		return;
	}

	this.RenderSearchMark(categoryId);
}

HkAddMotoTestimonial.prototype.GetMarksFromServer = function(categoryId)
{
	var selfObject = this;

	jQuery.getJSON(
		this.url + 'get_marks/' + categoryId + '/',
		function(data)
		{
			selfObject.cacheMarks[categoryId] = new Array();

			jQuery.each(data, function(i,item){
				//fill cache here
				selfObject.cacheMarks[categoryId].push({id: item.id, title: item.title});
			});

			selfObject.RenderSearchMark(categoryId);
			selfObject.isMarksUpdating = false;
		}
	);	
}


HkAddMotoTestimonial.prototype.RenderSearchMark = function(categoryId)
{
	jQuery('#'+this.idSeachMark).empty();
	
	jQuery('#'+this.idSeachMark).removeAttr('disabled');
	jQuery('#'+this.idSeachModel).removeAttr('disabled');
	
	var opts = this.cacheMarks[categoryId];
	if (opts == undefined || !opts.length || opts.length<1)
	{
		jQuery('#'+this.idSeachMark).append('<option value="" selected="selected">Марка</option>');
		jQuery('#'+this.idSeachMark).change();
		return;
	}
	else
	{
		jQuery('#'+this.idSeachMark).append('<option value="" selected="selected">Марка</option>');
		for(var i=0; i<opts.length; i++)
		{
			jQuery('#'+this.idSeachMark).append('<option value="' + opts[i].id + '">' + opts[i].title +  '</option>');
		}
		jQuery('#'+this.idSeachMark).change();
	}
}

HkAddMotoTestimonial.prototype.GetModels = function(markId)
{
	if (this.isModelsUpdating) return;

	if (!this.cacheModels[markId])
	{
		this.isModelsUpdating = true;
		jQuery('#'+this.idSeachModel).attr('disabled', 'disabled');
		this.GetModelsFromServer(markId);
		return;
	}

	this.RenderSearchModel(markId);
}

HkAddMotoTestimonial.prototype.GetModelsFromServer = function(markId)
{
	var selfObject = this;

	jQuery.getJSON(
		this.url + 'get_models_versions_testimonial/' + markId + '/',
		function(data)
		{
			selfObject.cacheModels[markId] = new Array();

			jQuery.each(data, function(i,item){
				//fill cache here
				selfObject.cacheModels[markId].push({id: item.id, title: item.title, url:item.url});
			});

			selfObject.RenderSearchModel(markId);
			selfObject.isModelsUpdating = false;
		}
	);	
}

HkAddMotoTestimonial.prototype.RenderSearchModel = function(markId)
{
	jQuery('#'+this.idSeachModel).empty();
	
	jQuery('#'+this.idSeachModel).removeAttr('disabled');
	
	var opts = this.cacheModels[markId];
	if (opts == undefined || !opts.length || opts.length<1)
	{
		jQuery('#'+this.idSeachModel).append('<option value="" selected="selected">Модель</option>');
		jQuery('#'+this.idSeachModel).change();
		return;
	}
	else
	{
		jQuery('#'+this.idSeachModel).append('<option value="" selected="selected">Модель</option>');
		for(var i=0; i<opts.length; i++)
		{
			jQuery('#'+this.idSeachModel).append('<option value="' + opts[i].url + '">' + opts[i].title +  '</option>');
		}
		jQuery('#'+this.idSeachModel).change();
	}
}


var hkATM = null;

jQuery(document).ready(function(){

	hkATM = new HkAddMotoTestimonial(hkBaseUri + 'moto_helper/');
	
	//add category handler
	jQuery('#idATMC').change(function(){
		hkATM.GetMarks(this.value);
	});

	//add mark handler
	jQuery('#idATMB').change(function(){
		hkATM.GetModels(this.value);
	});

	//add submit handler
	jQuery('a.at-add-moto').click(function(e){ 
		e.preventDefault();

		var categoryId = jQuery('#'+hkATM.idSeachCategory).val();
		var markId = jQuery('#'+hkATM.idSeachMark).val();
		var versionUrl = jQuery('#'+hkATM.idSeachModel).val();

		if (categoryId<1 || markId<1 || versionUrl=='')
		{
			alert('Выберите категорию, марку и модель мототехники!');
			return;
		}
		
		location.href = hkBaseUri + 'moto/' + versionUrl + 'testimonials';
	});

});
