Category.prototype.constructor = Category;

function Category(curcat, typec, se_ids) {
	this.curcat = curcat;
	this.typec = typec;
	this.closeable = true;
	this.se_ids = se_ids;
	this.compt = 0;
	this.comptchecked = 0;
	this.IE = document.all ? true : false;
	this.Categorie = document.getElementById('SS_CATEGORIE');
	this.DivCategory = document.getElementById('div_category');
	this.id_div = document.getElementById('masqueIECat');
	this.checklist = new Array();
	this.list = new Array();
	this.Init();
}

Category.prototype.Init = function() {
	switch(this.curcat){
	case 'auto':
		switch (this.typec) {
		case 'ft':
			this.categorielist = new Array('40,41,42,43,44,45,46,47');
			this.checklist[0] = new Array('40', '41', '42', '43', '44', '45,46', '47');
			break;
		case 'cote':
			this.categorielist = new Array('40,41,42,43,44,45,46,47,48');
			this.checklist[0] = new Array('40', '41', '42', '43', '44',
					'45,46', '47', '48');
			break;
		case 're':
		case 'alert':
			this.categorielist = new Array('40,41,42,43,44,45,46,47,48,49');
			this.checklist[0] = new Array('40', '41', '42', '43', '44',
					'45,46', '47', '48', '49');
			break;
		}
		break;
	case 'moto':
		switch (this.typec) {
		case 'cote':
			this.categorielist = new Array(
					'60,61,62,63,64,65,66,67,68,69,70,71');
			this.checklist[0] = new Array('60', '61', '62', '63', '64', '65',
					'66', '67', '68', '69', '70', '71');
			break;
		case 're':
		case 'alert':
			this.categorielist = new Array('60,61,62,63,64,65,66,67,68,69,72');
			this.checklist[0] = new Array('60', '61', '62', '63', '64', '65',
					'66', '67', '68', '69', '72');
			break;
		}
		break;
	case 'utils':
		this.categorielist = new Array('80,81,82,83,84');
		this.checklist[0] = new Array('80', '81', '82', '83', '84');
		break;
	}
	
	this.fill_cat();
};

Category.prototype.fill_cat = function() {
	var k, i, j;
	var se_id = this.se_ids.split(',');
	this.comptchecked = 0;
	for (k = 0; k < this.categorielist.length; k++) {
		for (i = 0; i < se_id.length; i++) {
			if (document.getElementById(se_id[i]) != null) {
				this.Categorie.value += (this.Categorie.value != '' ? ',' : '')
						+ document.getElementById(se_id[i]).value;
				document.getElementById(se_id[i]).checked = true;
				this.comptchecked++;
			} else if (se_id[i] == '45'
					&& document.getElementById(se_id[i] + ',46') != null) {
				this.Categorie.value += (this.Categorie.value != '' ? ',' : '')
						+ document.getElementById('45,46').value;
				document.getElementById('45,46').checked = true;
				this.comptchecked++;
			}
		}

		var nb = 0;
		for (j = 0; j < this.checklist[k].length; j++)
			if (document.getElementById(this.checklist[k][j]).checked)
				nb++;
	}
	if (this.typec != 're') {
		var tmp = this.checklist[0];
		if (nb == tmp.length) {
			for (i = 0; i < se_id.length; i++) {
				if (document.getElementById(se_id[i]) != null)
					document.getElementById(se_id[i]).checked = false;
				else if (se_id[i] == '45'
						&& document.getElementById(se_id[i] + ',46') != null)
					document.getElementById('45,46').checked = false;
			}
		}
	}
};

Category.prototype.ChangeSousCategorie = function(id) {
	this.comptchecked = 0;
	var nb, k;
	var ss_cat = document.getElementById(id);
	if (!ss_cat)
		return;

	for (k = 0; k < this.categorielist.length; k++) {
		nb = 0;
		var i = 0;
		while (i < this.checklist[k].length) {
			if (document.getElementById(this.checklist[k][i]).checked)
				nb++;
			i++;
		}

		if (nb != i)
			this.Categorie.value = '';
		for (i = 0; i < this.checklist[k].length; i++) {
			if (this.checklist[k][i] == id && !ss_cat.checked)
				document.getElementById(this.categorielist[k]).checked = false;
		}
	}
	this.Categorie.value = '';
	for (k = 0; k < this.categorielist.length; k++) {
		if (this.checklist[k].length == 0
				&& document.getElementById(this.categorielist[k]).checked)
			this.Categorie.value += (this.Categorie.value != '' ? ',' : '')
					+ document.getElementById(this.categorielist[k]).value;
		for (i = 0; i < this.checklist[k].length; i++) {
			if (document.getElementById(this.checklist[k][i]).checked) {
				this.Categorie.value += (this.Categorie.value != '' ? ',' : '')
						+ document.getElementById(this.checklist[k][i]).value;
				this.comptchecked++;
			}
		}
	}
	if (this.Categorie.value == '')
		this.Categorie.value = this.categorielist.join(',');
	this.AfficheCat(this.Categorie.value);
};

Category.prototype.ChangeCategorie = function(id) {
	var nb = 0;
	var k, i;
	this.comptchecked = 0;
	this.Categorie.value = '';
	for (k = 0; k < this.categorielist.length; k++) {
		if (this.categorielist[k] == id) {
			for (i = 0; i < this.checklist[k].length; i++) {
				if (document.getElementById(this.categorielist[k]).checked) {
					document.getElementById(this.checklist[k][i]).checked = true;
					this.comptchecked++;
				} else
					document.getElementById(this.checklist[k][i]).checked = false;
			}
		}
	}
	for (k = 0; k < this.categorielist.length; k++)
		this.Categorie.value += (this.Categorie.value != '' ? ',' : '')
				+ document.getElementById(this.categorielist[k]).value;
};

Category.prototype.getnbCat = function() {
	var cat, Tcat;
	cat = this.categorielist.join(',');
	Tcat = cat.split(/,/);
	return Tcat.length;
};

Category.prototype.filter_ss_cat = function(ss_cat_found) {
	var cat, val, i, j, k;
	val = '';
	cat = this.categorielist.join(',');
	cat = cat.split(/,/);
	var T_cat_found = ss_cat_found.split(/;/);
	i = 0;
	this.comptchecked = 0;
	while (i < cat.length) {
		j = 0;
		if (cat[i] != '46' && cat[i] != '45')
			document.getElementById(cat[i]).checked = false;
		else if (cat[i] == '45')
			document.getElementById('45,46').checked = false;
		while (j < T_cat_found.length) {
			if (cat[i] == T_cat_found[j] && cat[i] != ''
					&& T_cat_found[j] != '') {
				if (cat[i] != '45' && cat[i] != '46') {
					document.getElementById('div_' + cat[i]).style.display = '';
					if (this.se_ids
							.match(new RegExp('(,|^)' + cat[i] + '(,|$)'))) {
						document.getElementById(cat[i]).checked = true;
						this.comptchecked++;
					}
					val += (val != '' ? ',' : '')
							+ document.getElementById(cat[i]).value;
				} else if ((cat[i] == '45' || cat[i] == '46')) {
					document.getElementById('div_45_46').style.display = '';
					if (!document.getElementById('45,46').checked)
						val += (val != '' ? ',' : '')
								+ document.getElementById('45,46').value;
					if (this.se_ids
							.match(new RegExp('(,|^)' + cat[i] + '(,|$)'))) {
						if (!document.getElementById('45,46').checked)
							this.comptchecked++;
						document.getElementById('45,46').checked = true;
					}
				}
			}
			j = j + 1;
		}
		i = i + 1;
	}
	T_cat_found = this.BubleSort(T_cat_found);
	this.compt = 0;
	this.checklist[0] = new Array();
	for (k = 0; k < T_cat_found.length; k++) {
		if (T_cat_found[k] == '45') {
			this.checklist[0][this.compt] = '45,46';
			this.compt++;
		} else {
			if (T_cat_found[k] != '46') {
				this.checklist[0][this.compt] = T_cat_found[k];
				this.compt++;
			}
		}
	}
	if (this.compt == this.comptchecked)
		document.getElementById(this.categorielist[0]).checked = true;
	this.AfficheCat(this.Categorie.value);
	document.getElementById(this.categorielist[0]).value = val;
	var T = this.Categorie.value.split(',');
	if (this.compt == this.comptchecked) {
		for (i = 0; i < T.length; i++)
			if (T[i] != 45 && T[i] != 46)
				document.getElementById(T[i]).checked = false;
			else
				document.getElementById('45,46').checked = false;
	}
};

Category.prototype.BubleSort = function(T) {
	var max_ = T.length - 1;
	var swap, i;
	var ok = true;
	var loop = 0;
	while (ok) {
		ok = false;
		for ( var i = loop; i < max_; i++) {
			if (T[i] > T[i + 1]) {
				swap = T[i + 1];
				T[i + 1] = T[i];
				T[i] = swap;
				if (!ok) {
					loop = (i - 1 > -1 ? i - 1 : i);
					ok = true;
				}
			}
		}
	}
	return T;
};

Category.prototype.display_div = function(val) {
	this.masqueForIe6();
	var display = this.DivCategory.style.display;
	if (display == 'none') {
		if (parent && parent.document
				&& parent.document.getElementById('home') != null)
			parent.document.getElementById('home').style.height = '350px';
		this.DivCategory.style.display = '';
	} else {
		if (parent && parent.document
				&& parent.document.getElementById('home') != null)
			parent.document.getElementById('home').style.height = '160px';
		this.DivCategory.style.display = 'none';
	}
	this.closeable = false;
};

Category.prototype.close_div = function() {
	if (this.closeable) {
		this.DivCategory.style.display = 'none';
		if (parent && parent.document
				&& parent.document.getElementById('home') != null)
			parent.document.getElementById('home').style.height = '160px';
	}
	this.closeable = true;
	this.masqueForIe6();
};

Category.prototype.masqueForIe6 = function() {
	var display = this.DivCategory.style.display;
	if (this.IE && this.id_div != null && display == 'none')
		this.id_div.style.display = '';
	if (this.IE && this.id_div != null && display == '')
		this.id_div.style.display = 'none';
};

Category.prototype.AfficheCat = function(sscat) {
	if (sscat == null)
		return;
	var chaineTexte = '';
	var laChaineFinale;
	ZeChaine = sscat.replace(/,/gi, '|'); // remplace toutes les virgules
	ZeChaine = ZeChaine.replace('45|46', '45,46'); // remet 45 et 46 bien
	document.getElementById("CpteurCatAuto").innerHTML = ZeChaine;
	ZeTab = ZeChaine.split("|"); // bon format tableau
	for (k = 0; k < ZeTab.length; k++) {
		chaineTexte += this.LaChaineEnTexte(ZeTab[k]);
	}
	if (ZeChaine.length == this.categorielist[0].length
			&& this.curcat == 'auto') {
		laChaineFinale = 'Toutes les automobiles';
	} else if (ZeChaine.length == this.categorielist[0].length
			&& this.curcat == 'moto') {
		laChaineFinale = 'Toutes les motos';
	} else if (ZeChaine.length == this.categorielist[0].length
			&& this.curcat == 'utils') {
		laChaineFinale = 'Toutes catégories';
	} else {
		laChaineFinale = chaineTexte.substr(0, 20) + "...";
	}
	if (document.getElementById("catcombo") != null)
		document.getElementById("catcombo").innerHTML = laChaineFinale;
	else
		document.getElementById("CategorieZeLink").innerHTML = laChaineFinale;
};

Category.prototype.LaChaineEnTexte = function(num) {
	var ZeChaine = '';
	switch (num) {
	case '40':
		ZeChaine = 'Citadine, ';
		break;
	case '41':
		ZeChaine = 'Moyenne, ';
		break;
	case '42':
		ZeChaine = 'Grande, ';
		break;
	case '43':
		ZeChaine = 'Break, ';
		break;
	case '44':
		ZeChaine = 'Monospace, ';
		break;
	case '45,46':
		ZeChaine = 'Coupé et cabriolet, ';
		break;
	case '47':
		ZeChaine = 'SUV, 4x4 & Crossover, ';
		break;
	case '48':
		ZeChaine = 'Voiture sans permis, ';
		break;
	case '49':
		ZeChaine = 'Voiture ancienne, ';
		break;
	case '60':
		ZeChaine = 'Custom, ';
		break;
	case '61':
		ZeChaine = 'Offroad, ';
		break;
	case '62':
		ZeChaine = 'Roadster, ';
		break;
	case '63':
		ZeChaine = 'GT, ';
		break;
	case '64':
		ZeChaine = 'Mini Moto, ';
		break;
	case '65':
		ZeChaine = 'Mobylette, ';
		break;
	case '66':
		ZeChaine = 'Supermotard, ';
		break;
	case '67':
		ZeChaine = 'Trail, ';
		break;
	case '68':
		ZeChaine = 'Sportive, ';
		break;
	case '69':
		ZeChaine = 'Side-cars, ';
		break;
	case '70':
		ZeChaine = 'Quad, ';
		break;
	case '71':
		ZeChaine = 'Scooter, ';
		break;
	case '72':
		ZeChaine = 'Motos anciennes et collections, ';
		break;
	case '80':
		ZeChaine = 'Voitures sociétés, commerciales,  ';
		break;
	case '81':
		ZeChaine = 'Fourgons (moins de 3,5t), ';
		break;
	case '82':
		ZeChaine = 'Bus et minibus, ';
		break;
	case '83':
		ZeChaine = 'Camions (plus de 3,5t), ';
		break;
	case '84':
		ZeChaine = 'Remorques seules ';
		break;
	default:
		ZeChaine = '';
		break;
	}
	return (ZeChaine);
};

