﻿
function setOptions($sel, options) {
	if ($sel.length) {
		$sel.find(':gt(0)').remove();
		$.each(options, function () {
			$sel.append($('<option/>').attr({ value: this.Id }).text(this.Name));
		});
	}
}

/* Field visibility */

function setVisible(id, mask, type) {
	var $parent = $(id).parent();
	if ($parent.hasClass('CustomSelect')) $parent = $parent.parent();
	if ((mask & type) == type) {
		$(id).removeAttr('disabled');
		$parent.removeClass('FormDisabled');
	} else {
		$(id).attr('disabled', 'disabled');
		$parent.addClass('FormDisabled');
	}
}

function setVisibleAll(typeId) {
	var type;
	switch (typeId) {
		case '': type = 63; break;
		case 'rvppdkyg': type = 1; break;
		case 'pv7pdkyg': type = 2; break;
		case 'sv3pdkyg': type = 4; break;
		case '3v2pdkyg': type = 8; break;
		case 'vvipdkyg': type = 16; break;
		case 'ivfpdkyg': type = 32; break;
	}
	setVisible('#ModelName', 1, type);
	setVisible('#Version', 1, type);
	setVisible('#ModelText', 2 + 4 + 8 + 16 + 32, type);
	setVisible('#MilesFrom', 1 + 2 + 8 + 16 + 32, type);
	setVisible('#MilesTo', 1 + 2 + 8 + 16 + 32, type);
	setVisible('#CaravanWeight', 4, type);
	setVisible('#Fuel', 1 + 2 + 8 + 16 + 32, type);
	setVisible('#Emission', 1, type);
	setVisible('#GearBox', 1 + 2 + 8 + 16 + 32, type);
	setVisible('#Effect', 1 + 8, type);
	setVisible('#Weight', 2 + 8, type);
	setVisible('#McEngine', 16, type);
	setVisible('#MopedClass', 32, type);
	setVisible('#Color', 1, type);
	setVisible('#Security', 1, type);
	setVisible('#Comfort', 1, type);
	setVisible('#EquipmentText', 1, type);
	setVisible('#Length', 4 + 8, type);
	setVisible('#Beds', 4 + 8, type);
	setVisible('#McVolume', 16, type);
	setVisible('#SingleBed', 4 + 8, type);
	setVisible('#DoubleBed', 4 + 8, type);
	setVisible('#ChildrensRoom', 4 + 8, type);
	setVisible('#Environment', 1 + 2, type);
}

/* Field load */

function loadBrands(type, used) {
    var minyear = $('#YearLimit').val();
	$.getJSON(JsonBrandsUrl + ''+ type + '-' + used + '-' + minyear, function (data) {
		setOptions($('select#Brand'), data.Brands);
		setOptions($('select#YearFrom'), data.Years);
		setOptions($('select#YearTo'), data.Years);
	});
}

function loadModels(brand) {
	$.getJSON(JsonModelsUrl + '' + brand, function (data) {
		setOptions($('select#ModelName'), data);
	});
}

$(function () {

	var currentType = '', currentUsed = '', currentBrand = '';

	$('#SearchType, .SearchType input:checked').each(function () { currentType = $(this).val(); });
	$('select#Used, .Used input:checked').each(function () { currentUsed = $(this).val(); });
	$('input#Brand[type=hidden]').each(function () { currentBrand = $(this).val(); });

	setVisibleAll(currentType);
	if (currentType != 'rvppdkyg' || currentUsed != 'sv3pdkyg') loadBrands(currentType, currentUsed);
	if (currentBrand != '') loadModels(currentBrand);

	$('.SearchType input:radio').click(function () { currentType = $(this).val(); setVisibleAll(currentType); loadBrands(currentType, currentUsed); });
	$('select#SearchType').change(function () { currentType = $(this).val(); setVisibleAll(currentType); loadBrands(currentType, currentUsed); });
	$('.Used input:radio').click(function () { currentUsed = $(this).val(); loadBrands(currentType, currentUsed); });
	$('select#Used').change(function () { currentUsed = $(this).val(); loadBrands(currentType, currentUsed); });
	$('#Brand').change(function () { loadModels($(this).val()); });

	$('a#SearchButton').click(function (e) {
		document.forms[0].submit();
		e.preventDefault();
	});

	/* Custom select */

	$('.CustomSelect :text').click(function () {
		$(this).next().toggle();
	}).blur(function () {
		if (!$(this).next().hasClass('hover')) $(this).next().hide();
	});
	$('.CustomSelect .items').hover(function () {
		$(this).addClass('hover');
	}, function () {
		$(this).removeClass('hover');
	});
	$('.CustomSelect :checkbox').click(function () {
		var $sel = $(this).closest('.CustomSelect');
		var $inp = $sel.find(':text');
		var $checks = $sel.find(':checkbox');
		var first = $checks.get(0) == this;

		if ($(this).attr('checked')) {
			if (first) {
				$checks.slice(1).removeAttr('checked');
			} else {
				$checks.first().removeAttr('checked');
			}
		} else {
			if (first) {
				$checks.first().attr('checked', 'checked');
			} else {
				if ($sel.find(':checked').length == 0) {
					$checks.first().attr('checked', 'checked');
				}
			}
		}

		$inp.focus().val($sel.find(':checked').next().map(function () {
			return $(this).text();
		}).get().join(', '));

	});

	/* Lead text input*/

	$('.LeadText input').each(function () {
		if ($(this).val().length) $(this).prev().hide();
	}).focus(function () {
		$(this).prev().hide();
	}).blur(function () {
		if (!$(this).val().length) $(this).prev().show();
	});

	/* View flipper */

	$('.FlipView .list').live('click', function () {
		$('.ListContainer').removeClass('GalleryView').addClass('ListView');
	});
	$('.FlipView .gallery').live('click', function () {
		$('.ListContainer').removeClass('ListView').addClass('GalleryView');
	});

	/* Sorting selector */

	$('#Sorting').change(function () {
		window.location.href = $(this).val();
	});

});

