var Home = {
	'bloqueActual' : 'auto',
	'indiceActual' : 10,
	'indicesBloque' : 10, 
	'observarVehiculos' : function() {
		$$('.verVehiculo').each( function(e) {
			Event.observe(e, 'click', function() {			
				_target = e.id.toString();
				_id = _target.slice(5);
				_tipo = _target.slice(0,4);
				if(_tipo == 'auto') {
					Home.traerAuto(_id);
				} else {
					Home.traerMoto(_id);
				}
				$$('.verVehiculo').each( function(ex) { 
					ex.setStyle({color: '#fff'});
				});
				e.setStyle({color: '#212860'});
			})
		});	
	},
	
	'showButton' : function(){
		$('vermas').style.display = 'block';
	},

	'hideButton' : function(){
		$('vermas').style.display = 'none';
	},
	
	
	'traerAuto' : function(_id) {
		$('divFotoAutosNuevos').innerHTML = '<img src="' + _base_url + 'images/frontend/ajax-loader.gif" class="middle">';
		new Ajax.Request(_base_url+'home/devolverInfoAuto/'+_id,
				{
				onSuccess: function(transport, json){
					if(!json){
						json = transport.responseText.evalJSON();
					}

					if(json.estado_EST == 'Vendido'){
						$('vendido').style.display = 'block';
						$('vendido').innerHTML = '<img src="' + _base_url + 'images/frontend/vendido.jpg" alt="Vehiculo Vendido"/>';
					}else{
						$('vendido').style.display = 'none';
					}
					
					if(Home.bloqueActual != 'auto') {						
						// Actualizar los campos
						$('specDireccion').show();						
						$('specCaja').show();						
						$('specSegmento').show();				
						$('specCombustible').show();
						$('specCilindrada').hide();
						$('specMotor').hide();						
						Home.bloqueActual = 'auto';
					}
					
					/* Actualizar los campos*/
					$('specDireccion').down(1).update(json.direccion_DIRS);						
					$('specCaja').down(1).update(json.tipoCaja_TPSCJ+' ('+json.velocidades_TPSCJ+')');						
					$('specSegmento').down(1).update(json.segmento_SGMTS);				
					$('specCombustible').down(1).update(json.combustible_CMBS);
											
					/* Datos generales*/
					if(json.kilometraje > 0) {
						$('specKM').down(1).update(json.kilometraje+' KM');
					} else {
						$('specKM').down(1).update('0 KM');
					}		
					$('specAnio').down(1).update(json.anio);
					$('nombreVehiculo').down(0).update(json.marca_MRCS+' '+json.modelo_MDLS);
					$('precioVehiculo').update(json.signo_MONE+' '+json.precio);
											
					/* Actualizar los links*/
					$('masDetalles').down(0).href = _base_url+"vehiculos/detalleVehiculo/auto/"+_id;
					$('nombreVehiculo').down(0).href = _base_url+"vehiculos/detalleVehiculo/auto/"+_id;
					$('divFotoAutosNuevos').innerHTML = '<img height="250" width="485" src="'+ _base_url + 'asset/autos/auto_has_file/5.jpg">';
					
					$('vermas').innerHTML = '<a href="' + _base_url + 'vehiculos/detalleVehiculo/auto/'+ _id + '" ><img src="' + _base_url + 'images/frontend/vermas.jpg"></a>';
					/* Mostrar la foto*/
					if(json[0].filePath != 'noPhoto') {
						_file = json[0].filePath;
						_path = _file.slice(0,-4);
						_ext = _file.slice(-4);
						$('divFotoAutosNuevos').innerHTML = '<a onmouseover="Home.showButton();" onmouseout="Home.hideButton();" href="' + _base_url + 'vehiculos/detalleVehiculo/auto/'+ _id +'"><img height="250" width="485" src="'+ _base_url + 'asset/autos/auto_has_file/'+_path+'_485x250'+_ext+'"></a>';
					} else {
						$('divFotoAutosNuevos').innerHTML = '<img height="250" width="485" src="'+ _base_url + '/images/frontend/no-disponible.jpg">';
					}					
					$('divFotoAutosNuevos').style.margin = '0px 0px 2px 0px;';
				}
			}
		);
	},
	'traerMoto' : function(_id) {
		$('divFotoAutosNuevos').innerHTML = '<img src="' + _base_url + 'images/frontend/ajax-loader.gif" class="middle">';
		new Ajax.Request(_base_url+'home/devolverInfoMoto/'+_id,
				{
				onSuccess: function(transport, json){
					if(!json){
						json = transport.responseText.evalJSON();
					}
					if(Home.bloqueActual != 'moto') {
						//Actualizar los campos
						$('specDireccion').hide();						
						$('specCaja').hide();						
						$('specSegmento').hide();				
						$('specCombustible').hide();
						$('specCilindrada').show();
						$('specMotor').show();												
						Home.bloqueActual = 'moto';				
					}
					/*Datos generales*/
					if(json.kilometraje > 0) {
						$('specKM').down(1).update(json.kilometraje+' KM');
					} else {
						$('specKM').down(1).update('0 KM');
					}						
					$('specAnio').down(1).update(json.anio);	
					$('nombreVehiculo').down(0).update(json.marca_MRCS+' '+json.modelo_MDLS);
					$('precioVehiculo').update(json.signo_MONE+' '+json.precio);
					
					$('specCilindrada').down(1).update(json.cilindrada_MRSM+'cc');
					$('specMotor').down(1).update(json.tipoMotorMoto_TPMM);
						
					// Actualizar los links
					$('masDetalles').down(0).href = _base_url+"vehiculos/detalleVehiculo/moto/"+_id;
					$('nombreVehiculo').down(0).href = _base_url+"vehiculos/detalleVehiculo/moto/"+_id;
					
					$('divFotoAutosNuevos').innerHTML = '<img height="250" width="485" src="'+ _base_url + '/asset/autos/auto_has_file/5.jpg">';
					
					$('vermas').innerHTML = '<a href="' + _base_url + 'vehiculos/detalleVehiculo/moto/'+ _id + '" ><img src="' + _base_url + 'images/frontend/vermas.jpg"></a>';
					
					
					// Mostrar la foto						
					if(json[0].filePath != 'noPhoto') {
						_file = json[0].filePath;
						_path = _file.slice(0,-4);
						_ext = _file.slice(-4);
						$('divFotoAutosNuevos').innerHTML = '<a onmouseover="Home.showButton();" onmouseout="Home.hideButton();" href="' + _base_url + 'vehiculos/detalleVehiculo/moto/'+ _id +'"><img height="250" width="485" src="'+ _base_url + '/asset/motos/moto_has_file/'+_path+'_485x250'+_ext+'"></a>';
					}	else {
						$('divFotoAutosNuevos').innerHTML = '<img height="250" width="485" src="'+ _base_url + '/images/frontend/no-disponible.jpg">';
					}			
				}
			}
		);
	},	
	'showCartel' : function (){
		$('vendido').style.display = 'block';
	},
	'observarIndices' : function() {
		Event.observe('indexPrev', 'click', function() {
			_index = 1;
			
			$$('.verVehiculo').each( function(es) {
				es.up(0).hide();
			});
			Home.indiceActual = Home.indiceActual - Home.indicesBloque;
			$$('.verVehiculo').each( function(e) {
				if((_index > (Home.indiceActual - Home.indicesBloque)) && (_index < Home.indiceActual + 1)) {
					e.up(0).appear();
				}
				_index++;
				/*if(_index > Home.indiceActual + Home.indicesBloque) {
					$break;
				}*/
			});
			
			//console.log('Indice Actual: '+Home.indiceActual+'\nCantidad Vehiculos: '+cantidadVehiculos);
			
			if(Home.indiceActual > cantidadVehiculos) {
				$('divIndexNext').fade();
			}	else {
				$('divIndexNext').appear();
			}				
			if(Home.indiceActual > Home.indicesBloque) {
				$('indexPrev').appear();
			}	else {
				$('indexPrev').fade();
			}		
		});
		Event.observe('indexNext', 'click', function() {
	
				_index = 1;
				$$('.verVehiculo').each( function(e) {
					if(!(_index < ((Home.indiceActual + Home.indicesBloque) + 1) && _index > Home.indiceActual)) {
						e.up(0).hide();
					} else {
						e.up(0).appear();
					}
					_index++;
				});	
				Home.indiceActual += Home.indicesBloque;
				//console.log('Indice Actual: '+Home.indiceActual);
				//console.log('Indice Actual: '+Home.indiceActual+'\nCantidad Vehiculos: '+cantidadVehiculos);
				
				if(Home.indiceActual > cantidadVehiculos) {
					$('divIndexNext').fade();
				}	else {
					$('divIndexNext').appear();
				}				
				if(Home.indiceActual > Home.indicesBloque) {
					$('indexPrev').appear();
				}	else {
					$('indexPrev').fade();
				}			
			});
	}
}
window.onload = function() {
	Home.observarVehiculos();
	Home.observarIndices();
	Home.showCartel();
}
