function toggleSelector(id){
	var oSel = document.getElementById(id);
	oSel.className=oSel.className=='on' ? 'off':'on'
}

function escribeMail( nom, dom, etiqueta){
   if( !etiqueta ){
       etiqueta = nom + "@" + dom;
   }
   document.write( '<a hr'+'ef="mai'+'lto:'+nom+'@'+dom+'">'+etiqueta+'</a>' );   
}

function loadExternalUrl(){
    if( !document.getElementsByTagName ){
        return;
    }
    var links=document.getElementsByTagName( 'a' );
    for( var i=0; i<links.length; i++ ){
    	if( links[i].rel=="external" ){
			links[i].target="_blank";
        }
    }
}

if( navigator.userAgent.indexOf('MSIE')!=-1 ){
    window.attachEvent('onload', loadExternalUrl );
}
if( navigator.userAgent.indexOf('Gecko')!=-1 ){
    window.addEventListener( 'load', loadExternalUrl, false );
}

jQuery(function() {
   //jQuery('a[@rel*=lightbox]').lightBox({
   jQuery('#secFotos a, a.popupBanner').lightBox({
	overlayBgColor: '#000000',
	overlayOpacity: 0.6,
	imageLoading: '/img/lb/lightbox-ico-loading.gif',
	imageBtnClose: '/img/lb/lightbox-btn-close.gif',
	imageBtnPrev: '/img/lb/lightbox-btn-prev.gif',
	imageBtnNext: '/img/lb/lightbox-btn-next.gif',
	imageBlank: '/img/lb/lightbox-blank.gif',
	containerResizeSpeed: 350,
	txtImage: 'Imagen',
	txtOf: 'de',
	keyToPrev: 'a',
	keyToNext: 's',
	//keyToClose: 's',
	keyToClose: 27
   });
   
});

function updateModels(oSel){

	var idMarca = oSel.options[oSel.selectedIndex].value;

	var oMod = document.getElementById('selectorModel');
	borrarSelect(oMod,'Todos los modelos');
	if (models[idMarca]){
		var mods = models[idMarca];
		for(clau in mods){
			oMod.options[oMod.options.length] = new Option(mods[clau].nom+' ('+mods[clau].num+')',mods[clau].id);					
		}
		oMod.options[0].selected = true;
	}
	
}

/**
 * si seleccionen un model no poden triar carroceria
 * @param oMod
 * @param oCar
 */
function updateCarroceries(oMod,oCar){
	if (oMod.options[oMod.selectedIndex].value!=''){
		oCar.disabled = true;
	}else{
		oCar.disabled = false;
	}
}


/**
 * Borra totes les opcions d'un select
 * @param {DOMobj} oSel
 */
function borrarSelect(oSel,defaultText){
    while (oSel.options.length) {
        oSel.options[0] = null;
    }
    oSel.options[0] = new Option(defaultText,"");
}
	

/**
 * Marca un element (o varis) d'un selector
 * @param {Object} oSel
 * @param string valor
 */
function marcaSelect(oSel,valor){
    for(i=0;i<oSel.options.length;i++){
        oSel.options[i].selected = (oSel.options[i].value == valor); 
    }
}

/**
 * Verifica la validesa del formulari de 
 * contacte del vehicle o del contacte genèric
 * @param {Object} oForm
 * @param {Boolean} isGeneric
 */
function checkFormContacte(oForm,isGeneric){
	
	var arrErrors = new Array();
	
	if (oForm.nombre.value==''){
		arrErrors.push({camp:'nombre',desc:'El <strong>nombre</strong> es obligatorio.'}); 
	}
	if (isGeneric && oForm.email.value==''){
		arrErrors.push({camp:'email',desc:'El <strong>email</strong> es obligatorio.'});
	}else if (!isGeneric && oForm.telefono.value=='' && oForm.email.value==''){
		arrErrors.push({camp:'email',desc:'Es obligatorio insertar el <strong>email</strong> o el <strong>teléfono</strong>.'});
	}
	if (oForm.email.value!='' && !oForm.email.value.match(/^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i)) {
		arrErrors.push({camp:'email',desc:'El <strong>email</strong> es incorrecto.'});
	} 
	
	/*if (!isGeneric && oForm.provincia && oForm.provincia.options[oForm.provincia.selectedIndex].value==''){
		arrErrors.push({camp:'provincia',desc:'La <strong>provincia</strong> es obligatoria.'}); 
	}*/

	if (!isGeneric && oForm.poblacion && oForm.poblacion.options[oForm.poblacion.selectedIndex].value==''){
            arrErrors.push({camp:'poblacion',desc:'La <strong>poblacion</strong> es obligatoria.'});
    }
	
	if (isGeneric && oForm.comentario.value==''){
		arrErrors.push({camp:'comentario',desc:'El <strong>comentario</strong> es obligatorio.'}); 
	}
	
	if (oForm.suma.value==''){
		arrErrors.push({camp:'suma',desc:'La <strong>suma</strong> es obligatoria.'}); 
	}
	
	if (arrErrors.length){
		
		var strErrors = "<p>Corrija los siguientes errores:</p><ul>";
		for(var i=0;i<arrErrors.length;i++){
			strErrors+="<li>"+arrErrors[i].desc+"</li>";
		}
		strErrors+="</ul>";
		
		jQuery('#popupContact').attr('className','');
		jQuery('#msg').html(strErrors);
		centerPopup();
		loadPopup();
		
		oForm[arrErrors[0].camp].focus();
		return false;
				
	}
	
	return true;
	
}

/***************************/
//@Author: Adrian "yEnS" Mato Gondelle
//@website: www.yensdesign.com
//@email: yensamg@gmail.com
//@license: Feel free to use it, but keep this credits please!					
/***************************/

//SETTING UP OUR POPUP
//0 means disabled; 1 means enabled;
var popupStatus = 0;

//loading popup with jQuery magic!
function loadPopup(){
	//loads popup only if it is disabled
	if(popupStatus==0){
		$("#backgroundPopup").css({
			"opacity": "0.7"
		});
		$("#backgroundPopup").fadeIn("fast");
		$("#popupContact").fadeIn("fast");
		popupStatus = 1;
	}
}

//disabling popup with jQuery magic!
function disablePopup(){
	//disables popup only if it is enabled
	if(popupStatus==1){
		$("#backgroundPopup").fadeOut("fast");
		$("#popupContact").fadeOut("fast");
		popupStatus = 0;
	}
}

//centering popup
function centerPopup(){
	//request data for centering
	var windowWidth = document.documentElement.clientWidth;
	var windowHeight = document.documentElement.clientHeight;
	var popupHeight = $("#popupContact").height();
	var popupWidth = $("#popupContact").width();
	//centering
	$("#popupContact").css({
		"position": "absolute",
		"top": windowHeight/2-popupHeight/2,
		"left": windowWidth/2-popupWidth/2
	});
	//only need force for IE6
	
	$("#backgroundPopup").css({
		"height": windowHeight
	});
	
}

//CONTROLLING EVENTS IN jQuery
$(document).ready(function(){
				
	//CLOSING POPUP
	//Click the x event!
	$("#popupContactClose").click(function(){
		disablePopup();
	});
	//Click out event!
	$("#backgroundPopup").click(function(){
		disablePopup();
	});
	//Press Escape event!
	$(document).keypress(function(e){
		if(e.keyCode==27 && popupStatus==1){
			disablePopup();
		}
	});
	
	//llistat
	/*
	$("#llistatCotxes tbody tr:even").addClass('par');
	$("#llistatCotxes tbody tr:odd").addClass('impar');
	*/
	$("#llistatCotxes tbody tr").mouseover(function(){
		$(this).addClass("over");
	});

	$("#llistatCotxes tbody tr").mouseout(function(){
		$(this).removeClass("over");
	});
	$("#llistatCotxes tbody tr").click(function(){
		var urlVehicle = $(this).attr('id');
		urlVehicle = urlVehicle.replace(/__*/g,'/');
		window.location.href=urlVehicle; 
	}); 

});

