// JavaScript Document

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}
function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

function ahah(url, target, funcion) {
  //document.getElementById(target).innerHTML = '<div class="NegritaChiquita">Loading...</div>';
  if (window.XMLHttpRequest) {
    req = new XMLHttpRequest();
  } else if (window.ActiveXObject) {
    req = new ActiveXObject("Microsoft.XMLHTTP");
  }
  if (req != undefined) {
    req.onreadystatechange = function() {ahahDone(url, target, funcion);};
    req.open("GET", url, true);
    req.send("");
  }
}  

function ahahDone(url, target, funcion) {
  if (req.readyState == 4) { // only if req is "loaded"
    if (req.status == 200) { // only if "OK"
      document.getElementById(target).innerHTML = req.responseText;
	  eval(funcion);
    } else {
      document.getElementById(target).innerHTML="Error:\n"+req.statusText;
    }

  }
}

function selecciona(cual, valor, texto) {
	
	dtexto = document.getElementById("txt_"+cual);
	dinput = document.getElementById(cual);
	dtexto.innerHTML = texto;
	
	dinput.value = valor;
	
	if (cual=="b_marca") {
		ahah("ah_cmb_modelo.php?marca="+valor, "cmb_modelo", '$("#cmb_modelo").show("fast"); activatedhtmlselect("b_modelo", "159px", "179px")');
		$('#cmb_version').hide('fast');
		$('#boton_send').hide('fast');
	}
	if (cual=="b_modelo") {
		ahah("ah_cmb_version.php?modelo="+valor, "cmb_version", '$("#cmb_version").show("fast"); activatedhtmlselect("b_version", "159px", "179px");');
	}
	if (cual=="b_version") {
		$('#boton_send').show('slow');
	}
}

function selecciona2(cual, valor, texto) {
	
	dtexto = document.getElementById("txt_"+cual);
	dinput = document.getElementById(cual);
	dtexto.innerHTML = texto;
	
	dinput.value = valor;
	
	if (cual=="b_marca2") {
		ahah("ah_cmb_modelo2.php?marca="+valor, "cmb_modelo2", 'activatedhtmlselect("b_modelo2", "126px", "146px");');
		$('#cmb_version2').hide('fast');
		$('#btn_comp2').hide('fast');
	}
	if (cual=="b_modelo2") {
		ahah("ah_cmb_version2.php?modelo="+valor, "cmb_version2", '$("#cmb_version2").show(); activatedhtmlselect("b_version2", "126px", "146px");');
		
	}
	if (cual=="b_version2") {
		$('#btn_comp2').show();
	}
}

function selecciona3(cual, valor, texto) {
	
	dtexto = document.getElementById("txt_"+cual);
	dinput = document.getElementById(cual);
	dtexto.innerHTML = texto;
	
	dinput.value = valor;
	
	if (cual=="b_marca2") {
		ahah("ah_cmb_modelo2.php?marca="+valor, "cmb_modelo2", 'activatedhtmlselect("b_modelo2", "126px", "146px");');
	}
	if (cual=="b_modelo2") {
		ahah("ah_cmb_version2.php?modelo="+valor, "cmb_version2", '$("#cmb_version2").show(); activatedhtmlselect("b_version2", "126px", "146px");');	
	}
}

function sendForm() {
	document.getElementById("formCompare").submit();	
}

function sendComparador(valor) {
	window.open('comparador.php?modelo='+valor, '_blank');
}


function sendForm2() {
	document.getElementById("formCompare2").submit();	
}

function cargaArchivo(target) {
	if (document.getElementById(target).innerHTML == "") {
		ahah("ah_archivo.php", target, '$("#'+target+'").treeview({ collapsed: true }); $("#'+target+'").slideToggle("slow");');
	} else {
		$("#"+target).treeview({ collapsed: true }); 
		$("#"+target).slideToggle("slow");
	}
}

function actualizaModelo(cual, valor) {
	ahah("ah_fichas_cmb_mod.php?marca="+valor+'&numero='+cual, 'cmb_marca_'+cual);
}
function actualizaVersion(cual, valor) {
	ahah("ah_fichas_cmb_version.php?version="+valor+'&numero='+cual, 'cmb_version_'+cual);
}
function actualizaVehiculo(cual, valor) {
	ahah('ah_ficha2.php?numero='+cual+'&version='+valor, 'cambio_ficha_'+cual);
}
function initAgregar() {
	$('.btn_comparar').click(function () {
		$(this).effect('transfer', { to: '.vehiculo_comp2', className: 'ui-effects-transfer' }, 500, function  () { 
				if ($('#container_comparador').find('#vehiculo_'+$(this).attr('rel')).get(0)) {
					$('#container_vehiculos').children('#vehiculo_'+$(this).attr('rel')).remove();
				} 
				$('.vehiculo_comp2').after($('#vehiculo_'+$(this).attr('rel')));
				$(this).replaceWith('<div class="btn_volver" rel="'+$(this).attr('rel')+'"><img src="elementos/bot_quitar.jpg" /></div>');
				initQuitar();
				
		});
	});
	$('.vehiculo_comp').draggable({ revert: 'invalid' });
	$('#elpaginado a').click(function (evento) {
			evento.preventDefault();
			$.get('inc_buscarv.php'+$(this).attr('href'), function (data) {
				$('#container_vehiculos').html(data);
				initAgregar();
			});
	});
}
function initQuitar() {
	$('.btn_volver').click(function () {
		$(this).effect('transfer', { to: '#container_vehiculos .vehiculo_comp:last', className: 'ui-effects-transfer' }, 500, function  () { 
			$('#vehiculo_'+$(this).attr('rel')).appendTo("#container_vehiculos");
			$('#elpaginado').appendTo("#container_vehiculos");
			$(this).replaceWith('<div class="btn_comparar" rel="'+$(this).attr('rel')+'"><img src="elementos/bot_comparar.jpg" /></div>');
			initAgregar();
		});
	});
}
$(document).ready(function(){
	activatedhtmlselect("b_marca", "159px", "179px");
	$('#cmb_version').hide();
	$('#cmb_modelo').hide();
	//$('#combos').hide();
});
