/**
  * @file selectBrand.js
  * @author Jeannette Gaetan / Netwyse
  * @copyright Netwyse
  * @version 1.0
  */
var pageContextPath = "";
var widthCeil = 0;
var model = "";	

/* Callback for brand button */
function callback(result)
{
	var i;
	var aux;
	var maxWidth;
	var listSize;
	var carNumber = 0;
	var modelListSize = 0;
	var resultTable = "";
	
	/* Creation of the main container */
	modelListSize = result.length;
	if (-1 != Ext.get('car').dom.value)
		carNumber = Ext.get('car').dom.value.split('_')[1];
	if (!move)
	{
		if (null == Ext.get('car_selection'))
		{
			resultTable = '<div class="carousel_arrow"><input class="idle" type="button" id="arrow_prev" onclick="changeModel(1);" /></div>';
			resultTable += '<div id="carousel_cars"><div id="car_selection"></div></div>';
			resultTable += '<div class="carousel_arrow"><input type="button" id="arrow_next" onclick="changeModel(0);" /></div>';
			Ext.get('carousel-body').dom.innerHTML = resultTable;
		}
		/* resize the container div */
		listSize = widthCeil * modelListSize;
		
		/* init the car list */
		Ext.get('car_selection').setStyle('left', '0px');
		currentLeft = 0;
		carIndex = 0;
		if (listSize+'px' !=  Ext.get('car_selection').getStyle('width'))
			Ext.get('car_selection').setStyle('width', listSize+'px');
		/* new table creation */
		resultTable = "<table><tbody><tr>";
		for (i = 0; i < modelListSize; i++)
			resultTable += '<th class="table-vehicle-ceil"><span class="carousel_container"><a href="javascript:;" name="model_'+(i+1)+'" onclick="transmitData(\'model-'+result[i].id+'\', \'modelId\', \'select-element-car\', true);javascript:Ext.get(\'car\').dom.value = (this.name);"><img src="'+pageContextPath+'/espace_grandPublic/img/car/'+result[i].id+'.gif" alt="" /></a></span></th>';
		resultTable += "</tr><tr>";
	
		for (i = 0; i < modelListSize; i++)
			resultTable += '<td class="table-vehicle-name-ceil"><div id="model-'+result[i].id+'" class="carousel_container '+(result[i].id == selectedCar ? "select-element-car" : "")+'"><a name="model_'+(i+1)+'" href="javascript:;" onclick="transmitData(\'model-'+result[i].id+'\', \'modelId\', \'select-element-car\', true);javascript:Ext.get(\'car\').dom.value = (this.name);">'+ result[i].label + '</a></div></td>';
	
		resultTable += "</tr></tbody></table>";
		Ext.get('car_selection').dom.innerHTML = resultTable;
		
		/* User return to the result page */
		if ("" != model)
			transmitData(model, "modelId", "select-element-car", true);

		/* Show the selected car on the home page or the result page */
		if (0 != carNumber)
		{
			carIndex = Math.floor((carNumber - 1) / nbDiv);
			/* Count the number max of car */
			aux = Ext.get('car_selection').getStyle('width');
			aux = aux.split('px');
			maxWidth = aux[0];
			aux = aux[0] / widthCeil;
			if ((carNumber + nbDiv) <= aux)
				currentLeft = -carIndex * (widthCeil * nbDiv);
			else
				currentLeft = -maxWidth + (nbDiv * widthCeil);
			Ext.get('car_selection').setStyle('left', currentLeft+'px');
		}
	}
};

/* Change the class of an item */
function changeClass(src, oldClass)
{
	var i;	
	var bool = false;
	var classesName = "";
	var inputClass = "";
	var tableClass = new Array ();
	
	if (null != Ext.get(src))
	{
		classesName = Ext.get(src).dom.className;
		classesName = classesName.split(' ');
		for (i = 0; (i < classesName.length); i++)
		{
			if (classesName[i] != oldClass)
				inputClass += classesName[i]+' ';
			else
				bool = true;
		}
		Ext.get(src).dom.className = "";
		tableClass = inputClass.split(' ');
		for (i = 0; i < tableClass.length - 1; i++)
		{
			if (0 != tableClass[i].length)
				Ext.get(src).addClass(tableClass[i]);
		}
		if (!bool)
		{
			/* selected */
			Ext.get(src).addClass(oldClass);
			return (1);
		}
	}
	/* unselected */
	return (0);
};

/* Function uses for unselect modelId */
function removeClass(src, oldClass)
{
	var i, j, k;
	var bool = false;
	var row = new Array();
	var unselected = "";
	var classesName = "";
	var inputClass = "";

	inputClass = "";
	/* Get the last selected object */
	row = Ext.query("*[class*=" + oldClass + "]");
	/* The max size of j is 1 */
	j = row.length;
	for (i = 0; i < j; i++)
	{
		/* Get the id of the last sectected object */
		unselected = row[i].id;
		classesName = row[i].className;
		classesName = classesName.split(' ');
		for (k = 0; (k < classesName.length); k++)
		{
			/* remove the oldClass from the last object selected */
			if ((classesName[k] != oldClass) && ("" != classesName[k]))
				inputClass += classesName[k]+' ';
		}
		/* Give him back his others classes */
		row[i].className = inputClass;
	}
	/* If it was a new selection or the first one: put the oldClass style */
	if ("" == unselected || unselected != src)
	{
		if (null != Ext.get(src))
			Ext.get(src).addClass(oldClass);
		return (2);
	}
	/* Nothing had been selected */
	return (0);
}

/* url manager */
function addInput(targetId, inputId, value)
{
	var map = "map";

	if (null == Ext.get('map'))
		map = 'map_result';
	$j('#' + targetId).append("<input type='hidden' id='" + inputId + '-'+ value +"' name='" + inputId + "' value='" + value + "' />");
	if (false == $j(map +" a").hasClass() && inputId == map || inputId == 'regionId')
		$j('#' + map +" a").addClass('select-element');
}

/* suppression of the selected input */
function removeInput(id)
{
	var row = new Array();
	var map = "#map";
	
	if (null == Ext.get('map'))
		map = '#map_result';
	$j('#' + id).remove();
	
	/* if the selected input is map remove map->hover */
	row = Ext.query("*[name*=regionId]");
	if (0 == row.length)
		$j(map +" a").removeClass('select-element');
	
//	updateAdsNumber();
}

/* update input value */
function transmitData(src, dest, classes, modelClass, activation)
{
	var modelId = "";
	var changed = 0;
	var aux = "";
	if ("" != classes && null != classes)
	{
		/* If many object need the same classes or not */
		if (true == modelClass)
			changed = removeClass(src, classes);
		else
			changed = changeClass(src, classes);
		if ("regionId" == dest)
		{
			aux = src.split('map')[1];
			if (null != aux)
				src = aux;
		}

		if (0 == changed && !modelClass && (null != Ext.get(dest+'-'+src)))
			removeInput(dest + '-' + src);
		else if(0 == changed && null != Ext.get(dest))
			Ext.get(dest).dom.value = -1;
		else if (2 == changed && (null != Ext.get(src)))
		{
			/* Model Id section: modelId-xxx -> xxx */
			model = src;
			modelId = src;
			modelId = modelId.split('-');
			Ext.get(dest).dom.value = modelId[1];
		}
		else if (null == Ext.get(dest + '-' + src))
			addInput("input-list", dest, src);
	}
	else if (null != Ext.get(dest))
		Ext.get(dest).dom.value = src;
	if (null != classes && !activation)
		updateAdsNumber();
};

/* Erase car model */
function clearModel ()
{
	model = "";
	$j('#modelId').val(-1);
	$j('#car').val(-1);
	$j("#arrow_next").removeClass();
	
	updateAdsNumber();
};

function updateAdsNumber() {
	transmitPrice();
	var i = 0;
	var brandId = dwr.util.getValue('brandId');
	var modelId = dwr.util.getValue('modelId');
	var bodyId1 = (null != Ext.get('bodyId-1') ? dwr.util.getValue('bodyId-1') : -1);
	var bodyId2 = (null != Ext.get('bodyId-2') ? dwr.util.getValue('bodyId-2') : -1);
	var bodyId3 = (null != Ext.get('bodyId-3') ? dwr.util.getValue('bodyId-3') : -1);
	var bodyId4 = (null != Ext.get('bodyId-4') ? dwr.util.getValue('bodyId-4') : -1);
	var energyId1 = (null != Ext.get('energyId-6') ? dwr.util.getValue('energyId-6') : -1);
	var energyId2 = (null != Ext.get('energyId-20') ? dwr.util.getValue('energyId-20') : -1);
	var energyId3 = (null != Ext.get('energyId-316') ? dwr.util.getValue('energyId-316') : -1);
	var regionId = (0 != Ext.query("*[name*=regionId]").length ? Ext.query("*[name*=regionId]") : -1);
	var minPrice = dwr.util.getValue('minPrice');
	var maxPrice = dwr.util.getValue('maxPrice');
	
	var bodyIds = new Array();
	var energyIds = new Array();
	var regionIds = new Array();
	bodyIds[0] = bodyId1;
	bodyIds[1] = bodyId2;
	bodyIds[2] = bodyId3;
	bodyIds[3] = bodyId4;
	energyIds[0] = energyId1;
	energyIds[1] = energyId2;
	energyIds[2] = energyId3;
	if (regionId != -1)
	{
		for (i = 0; i < regionId.length; i++)
			regionIds[i] = regionId[i].value;
	}
	else
		regionIds[0] = regionId;
	
	AdService.getMatchingAdsNumber(brandId, modelId, energyIds, regionIds, minPrice, maxPrice, bodyIds, displayAdsNumber);
}

function displayAdsNumber(results) {
	if (null != document.getElementById('matchingAdsNumber')) {
		dwr.util.setValue('matchingAdsNumber', results);
		
		$j("#bg_site").effect('transfer',{ to: "#matchingAds", className: 'availableAdsEffect'},500,function(){
//			$j("#matchingAds").effect('pulsate',{},300,function(){
//				setTimeout(function(){
//					$j("#matchingAds:hidden").removeAttr('style').hide().fadeIn();
//				}, 700);
//			});
			flickerEffect(4, 200);
		});

	}
}

function flickerEffect(callDeep, timer)
{
 var inc = timer + 200;
 var aux = callDeep - 1;
 
 $j("#matchingAds").css('color', 'white');
 setTimeout('$j("#matchingAds").css("color", "#AB272D")', 200); 
 if (1 < callDeep)
	setTimeout("flickerEffect(" + aux + ", " + inc + ")", 400);
}

/* ERROR */
function transmitPrice()
{
	var minPrice = "";
	var maxPrice = "";
	var table = new Array ();
	
	minPrice = Ext.get('amount').getValue();

	if (minPrice) {
		table = minPrice.split('-');
		minPrice = table[0];
		maxPrice = table[1];
		minPrice = minPrice.split(' \u20AC');
		maxPrice = maxPrice.split(' \u20AC');
		minPrice = minPrice[0];
		maxPrice = maxPrice[0];
		/* remove + before maxPrice */
		
		if (null != Ext.get('minPrice'))
			Ext.get('minPrice').dom.value = String2Int(minPrice);
		if (null != Ext.get('maxPrice'))
			Ext.get('maxPrice').dom.value = String2Int(maxPrice);
	}
};

/* Action from cancel button */
function cancelAction(id)
{
	var i = 0;
	var row = new Array();
	if ('brandId' == id)
	{
		$j("#car_selection").remove();
		$j(".carousel_arrow").remove();
		$j('#brandId').val(-1);
		clearModel();
	}
	if ('bodyId' == id)
	{
		row = $j(".litlecars.select-element");
		for (i = 0; i < row.length; i++)
			transmitData(row[i].id, 'bodyId', 'select-element');
	}
	if ('energyId' == id)
	{
		row = $j(".carb_logo_res.select-element");
		for (i = 0; i < row.length; i++)
			transmitData(row[i].id, 'energyId', 'select-element');
	}
	if ('regionId' == id)
	{
		row = Ext.query("*[name*=regionId]");
		for (i = 0; i < row.length; i++)
			 removeInput(row[i].id);
	}
	if ('price' == id)
	{
		$j('#slider-range').slider("values", 0,0);
		$j('#slider-range').slider("values", 1,60000);
		$j("#amount").val(0 + ' \u20AC' +' - '+ 60000 + ' \u20AC');
	}
	updateAdsNumber();
}

/* map management */
function comment(id)
{
	if (0 == id.length)
		document.write("Aucun commentaire");
	else
		document.write(id);
}

/* Load selected region of the map */
function loadSelectedRegion()
{
	var row = new Array();
	var i = 0;
	
	row = Ext.query("*[name*=regionId]");
	if (0 < row.length)
	{
		for (i = 0; i < row.length; i++)
			$j("#map" + row[i].value).addClass("select-element");
	}
}

/* Load the main map */
function openWindow(img)
{
	var onfocus = "";
	
	if (jQuery.browser.msie && jQuery.browser.version === '6.0')
		onfocus = 'onfocus=\'this.blur();\'';
	var win = new Ext.Window({
		id: 	'select-region',
		title:	'Choisissez votre région',
		width:	440,
		height:	430,
		y: 60,
		html:	'<div id="map12" class="map-area"> </div>'+
				'<div id="map7" class="map-area"> </div>'+
				'<div id="map8" class="map-area"> </div>'+
				'<div id="map5" class="map-area"> </div>'+
				'<div id="map6" class="map-area"> </div>'+
				'<div id="map13" class="map-area"> </div>'+
				'<div id="map14" class="map-area"> </div>'+
				'<div id="map15" class="map-area"> </div>'+
				'<div id="map11" class="map-area"> </div>'+
				'<div id="map22" class="map-area"> </div>'+
				'<div id="map25" class="map-area"> </div>'+
				'<div id="map26" class="map-area"> </div>'+
				'<div id="map24" class="map-area"> </div>'+
				'<div id="map20" class="map-area"> </div>'+
				'<div id="map9" class="map-area"> </div>'+
				'<div id="map10" class="map-area"> </div>'+
				'<div id="map17" class="map-area"> </div>'+
				'<div id="map16" class="map-area"> </div>'+
				'<div id="map18" class="map-area"> </div>'+
				'<div id="map21" class="map-area"> </div>'+
				'<div id="map19" class="map-area"> </div>'+
				'<div id="map23" class="map-area"> </div>'+
				'<table id="hashmap" width="433" border="0" cellspacing="0" cellpadding="4">'+
				'<tr>'+
				'<td width="433">'+
				'<img src="'+img+'/img/main-greyMap.gif" width="433" height="404" border="0" usemap="#Map" title="Carte des régions" alt=""></td></tr></table>'+
				'<map name="Map">'+
				'<area '+ onfocus +'shape="poly" coords="131,109,124,113,120,106,96,103,89,108,76,93,31,103,30,125,41,135,92,151,104,147,118,140,127,138,133,132" href="javascript:transmitData(\'map17\', \'regionId\', \'select-element\');" target="_self" alt="" title="Bretagne">'+
				'<area '+ onfocus +' shape="poly" coords="140,118,140,139,99,157,99,180,119,201,131,210,145,202,137,176,163,169,181,150,188,130,173,125,157,118" href="javascript:transmitData(\'map16\', \'regionId\', \'select-element\');" target="_self" alt="" title="Pays de la Loire">'+
				'<area '+ onfocus +' shape="poly" coords="122,105,113,60,131,62,134,75,160,80,173,74,176,96,190,118,184,124,173,114,157,110,133,112" href="javascript:transmitData(\'map10\', \'regionId\', \'select-element\');" target="_self" alt="" title="Basse Normandie">'+
				'<area '+ onfocus +' shape="poly" coords="142,186,153,211,141,210,129,216,121,218,125,229,139,246,157,263,179,247,179,242,188,231,185,218,195,208,198,201,189,194,180,185,171,185,164,178" href="javascript:transmitData(\'map18\', \'regionId\', \'select-element\');" target="_self" alt="" title="Poitou Charentes">'+
				'<area '+ onfocus +' shape="poly" coords="127,240,117,271,118,304,103,336,142,353,151,324,145,316,136,307,179,300,201,267,198,248,184,238,177,237,155,264,138,251" href="javascript:transmitData(\'map19\', \'regionId\', \'select-element\');" target="_self" alt="" title="Aquitaine">'+
				'<area '+ onfocus +' shape="poly" coords="203,283,211,275,215,278,224,285,220,297,235,296,253,323,250,330,238,339,224,340,214,336,225,360,222,373,179,354,176,369,155,359,167,327,153,322,153,313,184,310" href="javascript:transmitData(\'map20\', \'regionId\', \'select-element\');" target="_self" alt="" title="Midi Pyr&eacute;n&eacute;es">'+
				'<area '+ onfocus +' shape="poly" coords="396,323,397,335,379,344,376,359,384,385,398,392,403,381,406,358,403,339,399,323" href="javascript:transmitData(\'map26\', \'regionId\', \'select-element\');" target="_self" alt="" title="Corse">'+
				'<area '+ onfocus +' shape="poly" coords="243,273,222,284,237,285,259,318,247,327,233,340,230,328,213,337,226,354,230,364,212,369,245,380,250,343,271,329,280,324,292,324,307,298,295,289,281,287,269,274" href="javascript:transmitData(\'map24\', \'regionId\', \'select-element\');" target="_self" alt="" title="Languedoc Roussillon">'+
				'<area '+ onfocus +' shape="poly" coords="307,293,313,308,296,325,353,346,363,335,379,328,392,305,403,297,400,293,387,300,375,289,378,267,366,260,364,261,364,262,360,261,333,284,339,298,322,294,311,294" href="javascript:transmitData(\'map25\', \'regionId\', \'select-element\');" target="_self" alt="" title="Provence Alpes C&ocirc;te d\'Azur">'+
				'<area '+ onfocus +' shape="poly" coords="280,216,277,222,282,240,301,253,287,268,282,285,310,293,332,291,329,285,328,282,350,260,362,250,378,244,364,227,372,220,365,197,344,213,336,210,326,212,317,211,311,213,302,213,297,213" href="javascript:transmitData(\'map22\', \'regionId\', \'select-element\');" target="_self" alt="" title="Rh&ocirc;ne Alpes">'+
				'<area '+ onfocus +' shape="poly" coords="232,203,242,223,241,227,238,250,228,260,222,271,234,277,236,269,254,270,265,265,275,280,293,248,273,239,275,223,275,196,262,193,250,193,244,193,235,200" href="javascript:transmitData(\'map23\', \'regionId\', \'select-element\');" target="_self" alt="" title="Auvergne">'+
				'<area '+ onfocus +' shape="poly" coords="192,209,222,205,237,216,238,221,235,239,220,275,210,272, 195,242,195,238,185,228,188,217" href="javascript:transmitData(\'map21\', \'regionId\', \'select-element\');" target="_self" alt="" title="Limousin">'+
				'<area '+ onfocus +'shape="poly" coords="375,157,371,165,355,189,338,210,325,206,326,189,324,161,325,157,328,154,343,135,349,132,358,138" href="javascript:transmitData(\'map15\', \'regionId\', \'select-element\');" target="_self" alt="" title="Franche Comt&eacute;">'+
				'<area '+ onfocus +' shape="poly" coords="189,106,192,119,188,134,174,152,166,169,181,176,199,201,228,199,249,184,246,164,246,128,235,128,221,124,208,99" href="javascript:transmitData(\'map9\', \'regionId\', \'select-element\');" target="_self" alt="" title="Centre">'+
				'<area '+ onfocus +' shape="poly" coords="253,132,256,190,274,194,282,213,306,213,311,202,325,202,324,155,310,154,305,139,280,143,269,124,256,125" href="javascript:transmitData(\'map11\', \'regionId\', \'select-element\');" target="_self" alt="" title="Bourgogne">'+
				'<area '+ onfocus +' shape="poly" coords="390,83,376,96,374,114,366,134,376,146,389,156,384,126,406,86" href="javascript:transmitData(\'map14\', \'regionId\', \'select-element\');" target="_self" alt="" title="Alsace">'+
				'<area '+ onfocus +' shape="poly" coords="375,90,375,90,375,90,375,93,370,101,361,140,331,125,310,100,310,95,316,70,344,73" href="javascript:transmitData(\'map13\', \'regionId\', \'select-element\');" target="_self" alt="" title="Lorraine">'+
				'<area '+ onfocus +' shape="poly" coords="283,53,285,79,273,81,265,117,284,137,306,135,313,149,327,145,335,137,300,95,315,63,296,52" href="javascript:transmitData(\'map6\', \'regionId\', \'select-element\');" target="_self" alt="" title="Champagne Ardenne">'+
				'<area '+ onfocus +' shape="poly" coords="211,12,234,5,245,18,252,18,264,31,267,34,280,39,282,49,251,46,235,40,210,34" href="javascript:transmitData(\'map12\', \'regionId\', \'select-element\');" target="_self" alt="" title="Nord Pas-de-Calais">'+
				'<area '+ onfocus +' shape="poly" coords="212,84,207,89,221,121,236,127,247,127,249,119,262,117,264,106,262,99,249,89,238,89,230,85" href="javascript:transmitData(\'map5\', \'regionId\', \'select-element\');" target="_self" alt="" title="Ile de France">'+
				'<area '+ onfocus +' shape="poly" coords="169,71,174,73,184,104,207,99,204,90,212,80,214,58,204,47,184,56,173,62" href="javascript:transmitData(\'map8\', \'regionId\', \'select-element\');" target="_self" alt="" title="Haute Normandie">'+
				'<area '+ onfocus +' shape="poly" coords="210,40,206,47,218,60,218,83,231,90,251,93,270,99,278,80,284,80,284,58" href="javascript:transmitData(\'map7\', \'regionId\', \'select-element\');" target="_self" alt="" title="Picardie">'+
				'</map>'+
				'</td>'+
				'</tr>'+
				'</table>'
	});
	win.show();
}

function checkResultNb() {
	if (dwr.util.getValue('matchingAdsNumber') == "0") {
		Ext.Msg.alert('Aucune annonce diponible', 'Il n\'y a pas de véhicule collaborateur correspondant &agrave; votre demande');
		return (false);
	} else {
		return true;
	}
}

