var opt = {
    // Use POST
    method: 'post',
    // Send this lovely data
    postBody: '',
    // Handle successful response
    onSuccess: function(t) {
        datos = (t.responseXML);
    },
    // Handle 404
    on404: function(t) {
        alert('Error 404: location "' + t.statusText + '" was not found.');
    },
    // Handle other errors
    onFailure: function(t) {
        alert('Error ' + t.status + ' -- ' + t.statusText);
    }
}
 
new Ajax.Request('carga.php', opt);

function initialize() {
  // initialize RSH
  dhtmlHistory.initialize();
  
  // add ourselves as a listener for history
  // change events
  dhtmlHistory.addListener(handleHistoryChange);
  // now initialize our starting UI
  if (datos == null) {
	  imp_cuerpo_inicial();
	  }
  else{
	  updateUI("inicio", "imp_cuerpo_inicial()");
	  dhtmlHistory.add("inicio","imp_cuerpo_inicial()");
	  
  }
  _uacct = "UA-377371-2";
  urchinTracker();
}


/** A function that is called whenever the user
    presses the back or forward buttons. This
    function will be passed the newLocation,
    as well as any history data we associated
    with the location. */
function handleHistoryChange(newLocation,
                             historyData) {
  // use the history data to update our UI
  updateUI(newLocation, historyData);                           
}

/** A simple method that updates our user
    interface using the new location. */
function updateUI(newLocation,
                  historyData) {
	if (historyData != null) {				  
		eval (historyData);
		}
}


function imp_items(id) {
	secc = datos.getElementsByTagName('seccion');
	item_cont = $("trabajos");
	texto = "";
	trabajosv = secc[id].getElementsByTagName('trabajo');
	for (var i=0; i < trabajosv.length; i++) {
		texto += "<li><a href='JavaScript:imp_cuerpo("+id+","+i+");dhtmlHistory.add(\"trabs"+trabajosv[i].childNodes[0].firstChild.nodeValue+"\", \"imp_cuerpo("+id+","+i+")\");'>"+ trabajosv[i].childNodes[0].firstChild.nodeValue+"</a></li>";			   
	} 
	item_cont.innerHTML = texto;
	if(trabajosv.length<2){
		texto = trabajosv.length+" ITEM";
	}else{
		texto = trabajosv.length+" ITEMS";
	}
	$("numero").innerHTML = texto;
	texto = "<table height='100%' width='100%' border='0'><tr>"
	texto += "<td width='150' valign='top'><div class='wrap1'><div class='wrap2'><div class='wrap3'><div class='sombra'>";
	texto += "<a><div class='items_intro'>"+secc[id].childNodes[0].firstChild.nodeValue+"</div><img src='"+secc[id].getElementsByTagName('thumb_prin')[0].firstChild.nodeValue+"'/></a>";
	texto += "</div></div></div></div></td>";
	
	texto += "<td valign='top' align='center'><table border='0'><tr valign='top' align='left'>";
	
	for (var i=0; i < trabajosv.length; i++) {
		if( i%3 == 0 && i != 0){
				texto += "</tr><tr>";
			}
		texto += "<td height='80' width='120' valign='top'><div align='left' class='item_tr'>";
		texto += "<a  href='JavaScript:imp_cuerpo("+id+","+i+");dhtmlHistory.add(\""+id+"item"+secc[id].childNodes[0].firstChild.nodeValue+"\", \"imp_cuerpo("+id+","+i+")\");'>"+"<img src='"+trabajosv[i].getElementsByTagName('thumb')[0].firstChild.nodeValue+"'/>"+"<br/>";
		texto += trabajosv[i].childNodes[0].firstChild.nodeValue;
		texto += "</a></div></td>";
		
	}
	texto += "</tr></table></td></tr></table>";
	$("cuerpo").innerHTML = texto;
	$("camino").innerHTML = "<div class='camino_volver'><a href='JavaScript:imp_cuerpo_inicial();dhtmlHistory.add(\"inicio\",\"imp_cuerpo_inicial()\");'>VOLVER AL INICIO</A></div> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<B><< SELECCIONA TRABAJO</B>";
	
}

function imp_cuerpo(seccion,trabajo){
	trabajosv = secc[seccion].getElementsByTagName('trabajo');
	$("camino").innerHTML = "<div class='camino_volver'><a  href='JavaScript:imp_cuerpo_inicial();dhtmlHistory.add(\"inicio\",\"imp_cuerpo_inicial()\");'>VOLVER AL INICIO</A></div> &nbsp;&nbsp;<SMALL>Sección</SMALL> &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;<B><span class='camino_trab'><a  href='JavaScript:imp_items("+seccion+");dhtmlHistory.add(\"IT"+secc[seccion].childNodes[0].firstChild.nodeValue+"\", \"imp_items("+seccion+")\");'>"+secc[seccion].childNodes[0].firstChild.nodeValue+"</a></span></B> &nbsp;&nbsp;&nbsp; - &nbsp;&nbsp;&nbsp;  <SMALL>Item</SMALL>  &nbsp;&nbsp; <span class='camino_trab'><a  href='JavaScript:imp_cuerpo("+seccion+","+trabajo+");dhtmlHistory.add(\""+seccion+"item"+secc[seccion].childNodes[0].firstChild.nodeValue+"\", \"imp_cuerpo("+seccion+","+trabajo+")\");'>"+trabajosv[trabajo].childNodes[0].firstChild.nodeValue+"</a></span>";
	cuerpo_cont = $("cuerpo");
	
	texto = "<table height='100%' width='100%' border='0'><tr>";
	texto += "<td height='90%' width='80%' align='center' valign='middle'><div id='imagencentral'><div id='imgcontenedor'><div class='img-sombra'><img style='visibility:hidden;' id='foto'  src='"; 
	texto +=   trabajosv[trabajo].getElementsByTagName('imagen')[0].firstChild.nodeValue;
	texto += "'/></div></div></td><td><div align='left' id='comentario_txt'>";
	texto += trabajosv[trabajo].getElementsByTagName('comentario')[0].firstChild.nodeValue;
	texto += "</div></td></tr><tr><td align='center'><br/><font class='pics'><div id='viendo'>VIENDO IMÁGEN "+1+"/"+ trabajosv[trabajo].getElementsByTagName('imagen').length+"</div></font>";
	for (var i=0; i < trabajosv[trabajo].getElementsByTagName('imagen').length; i++) {
		texto += "<a href='JavaScript:cambia_imagen("+i+","+seccion+","+trabajo+");dhtmlHistory.add(\""+seccion+trabajo+"trabajo"+i+"\", \"cambia_imagen("+i+","+seccion+","+trabajo+")\");'><img src='imagenes/minimg.gif' border='0'/></a>&nbsp;&nbsp;";			   
	}
	text = "</td><td></td></tr></table>";
	cuerpo_cont.innerHTML = texto;
	initImage('foto');
}


function imp_cuerpo_inicial(){
	$("camino").innerHTML = "";
	$("numero").innerHTML = "selecciona trabajo";
	$("trabajos").innerHTML = "";
	cuerpo_cont = $("cuerpo");
	secc = datos.getElementsByTagName('seccion');
	texto = "<table  cellpadding='0' cellspacing='0' border='0' width='100%' height='100%' ><tr>";
	for (var id=0; id<secc.length; id++){
		texto += "<td width='150' valign='middle'><div class='wrap1'><div class='wrap2'><div class='wrap3'><div class='sombra'>";
		texto += "<a  href='JavaScript:imp_items("+id+");dhtmlHistory.add(\"IT"+secc[id].childNodes[0].firstChild.nodeValue+"\", \"imp_items("+id+")\");'>";
		texto += "<div class='items_intro'>"+secc[id].childNodes[0].firstChild.nodeValue+"</div><img src='"+secc[id].getElementsByTagName('thumb_prin')[0].firstChild.nodeValue+"'/></a>";
		texto += "</div></div></div></div></td>";
	}
	texto += "</tr></table>";

	cuerpo_cont.innerHTML = texto;

}


function cambia_imagen(id_img,seccion,trabajo){
	if($("imagencentral") == null) {
		imp_items(seccion);
		imp_cuerpo(seccion,trabajo);
		}
	$("imagencentral").innerHTML = "<div id='imgcontenedor'><div class='img-sombra'><img style='visibility:hidden;' id='foto' src='"+trabajosv[trabajo].getElementsByTagName('imagen')[id_img].firstChild.nodeValue+"'/></div></div>";
	$("viendo").innerHTML = "VIENDO IMÁGEN "+(id_img+1)+"/"+ trabajosv[trabajo].getElementsByTagName('imagen').length;
	initImage('foto');
}


function initImage(idf) {
  image = document.getElementById(idf);
  setOpacity(image, 0);
  image.style.visibility = 'visible';
  fadeIn(idf,0);
}
function setOpacity(obj, opacity) {
  opacity = (opacity == 100)?99.999:opacity;
  
  // IE/Win
  obj.style.filter = "alpha(opacity:"+opacity+")";
  
  // Safari<1.2, Konqueror
  obj.style.KHTMLOpacity = opacity/100;
  
  // Older Mozilla and Firefox
  obj.style.MozOpacity = opacity/100;
  
  // Safari 1.2, newer Firefox and Mozilla, CSS3
  obj.style.opacity = opacity/100;
}
function fadeIn(objId,opacity) {
  if (document.getElementById) {
    obj = document.getElementById(objId);
    if (opacity <= 100) {
      setOpacity(obj, opacity);
      opacity += 10;
      window.setTimeout("fadeIn('"+objId+"',"+opacity+")", 100);
    }
  }
}



