function sortCZ(a, b) {
 a = a.charCodeAt(0);
 b = b.charCodeAt(0);
 var pred = [225,269,271,233,283,237,328,345,353,357,250,367,253,382,193,268,270,201,282,205,327,344,352,356,218,366,221,381];
 var po = [97.5,99.5,100.5,101.5,101.5,105.5,110.5,114.5,115.5,116.5,117.5,117.5,121.5,122.5,65.5,67.5,68.5,69.5,69.5,73.5,78.5,82.5,83.5,84.5,85.5,85.5,89.5,90.5];
 var code_a = pred.indexOf(a);
 code_a = code_a != -1 ? po[code_a] : a;
 var code_b = pred.indexOf(b);
 code_b = code_b != -1 ? po[code_b] : b;
 if(code_a > code_b) return 1;
 if(code_a < code_b) return -1;
 return 0;
}
if(!Array.indexOf){
   Array.prototype.indexOf = function(obj){
      for(var i=0; i<this.length; i++){
         if(this[i]===obj){
            return i;
         }
      }
      return -1;
   }
}

function cars() {
	this.getCarFamily = function(carMaker){ // returns: skoda, superb, fabia
		this.data = [];
		this.xml.find("vehicleManufacturer:contains('"+ carMaker +"')").each(function(){
				var el = jQuery(this).parent();
				var family = el.find('vehicleModel').text();
				if(window.cars.data.indexOf(family) == -1) window.cars.data.push(family);
		});
    this.data.sort(sortCZ);
		this.initSelBox(this.typSel, this.data);
		this.initSelBox(this.rokSel);
	}
	
	this.getCarTypes = function(carFamily){ // returns: (2004-) Benzin, (2006-) Diesel
		this.data = [[], [], carFamily];
		this.xml.find("vehicleModel:contains('"+ carFamily +"')").each(function(){
				var node = jQuery(this);
				if(node.text() == window.cars.data[2]) { // selector :contains returns 'felica' and 'felicia combi'
					node = node.parent();
					window.cars.data[0].push(node.find('modelYear').text());
					window.cars.data[1].push(node.find('id').text());
				}
		});
		this.initSelBox(this.rokSel, this.data[0], this.data[1]);
	}
	
	this.xmlLoaded = function(data){ // checks if all xml docs loaded
		this.xml = jQuery(data);
		// get car makers
		this.xml.find('vehicleManufacturer').each(function(){ // this.vyrobci = [manID, carID, manID...]
			var vyrobce = jQuery(this).text();
			if(window.cars.data.indexOf(vyrobce) == -1) {
				window.cars.data.push(vyrobce);
			}
		});
    this.data.sort(sortCZ);
		jQuery('document').ready(function(){window.cars.domload();});
	}
	
	this.domload = function() { // data created before domready, now is time to manipulate DOM
		this.znaSel = jQuery('#znacka');
		this.typSel = jQuery('#typ');
		this.rokSel = jQuery('#rok');

		this.initSelBox(this.znaSel, this.data);
		this.znaSel.change(function(){
			if(this.value == 'x') this.form.submit();
			else if(this.value != -1) {
				jQuery('#carForm .n2').attr('id', 'n2');
				jQuery('#carForm .n3').attr('id', 'n0');
				window.cars.getCarFamily(this.value);
			}
		});
		this.typSel.change(function(){
			if(this.value == 'x') this.form.submit();
			else if(this.value != -1) {
				jQuery('#carForm .n3').attr('id', 'n3');
				window.cars.getCarTypes(this.value);
			}
		});
		this.rokSel.change(function(){
			if(this.value == 'x') {
				this.form.submit();
			}
		});
		jQuery('#carForm').submit(function() {
			jQuery('#znacka,#typ', this.form).remove();
			if(this.teaserCode.value == 'x' || this.teaserCode.value == '-1') {
				jQuery('#rok', this.form).remove();
			}
		});
	}
	
	this.initSelBox = function(sel, names, vals) { // generates selectbox with data
		var html = '<option value="-1">Vyberte hodnotu</option>';
		if(names) {
			for(var i=0, len=names.length; i<len; i++) {
				html+='<option value="'+ (vals ? vals[i] : names[i]) +'">'+ names[i] +'</option>';
			}
		}
		html+='<option value="x">- jina -</option>';
		sel.empty();
		sel.html(html);
	}
	this.data = [];
	this.xml = null; // fetched xml data
}
window.cars = new cars;

jQuery.ajax({ // get xml
	success:function(data){window.cars.xmlLoaded(data);},
	dataType:'xml',
	url:'/assets/xml/teaser_cfg_client.xml'
});