var agt = navigator.userAgent.toLowerCase();
var IE = agt.indexOf("msie") != -1;
var indice = 0;
var total  = 4;

function anterior(){
  if (indice > 0){
    selPagina(indice-1);
  }
}

function siguiente(){
   if (indice < total){
   	selPagina(indice+1);
  }
}

function selPagina(idPagina){
	
   _url = "/conestilo/data/"+idPagina+".html?p="+Math.random(); 
    Cargar(_url);
    if(idPagina!=indice){
    	var markCurrent = document.getElementById("mark"+idPagina);
    	var markActive = document.getElementById("mark"+indice);
    	if(IE){
    			markCurrent.setAttribute("className", "markActive");
    			markActive.setAttribute("className", "markNoActive");
    	}else{
    			markCurrent.setAttribute("class", "markActive");
    			markActive.setAttribute("class", "markNoActive");
    	}
    	indice = idPagina;
    	
    	if(indice==0){
    		if(IE)document.getElementById("styleNav-prev").setAttribute("className", "inActive-prev");
    		else document.getElementById("styleNav-prev").setAttribute("class", "inActive-prev");
    	}else{
    	 	if(IE)document.getElementById("styleNav-prev").setAttribute("className", "active");
    		else document.getElementById("styleNav-prev").setAttribute("class", "active");
    	}
    	
    	if(indice==total){
    		if(IE)document.getElementById("styleNav-next").setAttribute("className", "inActive-next");
    		else document.getElementById("styleNav-next").setAttribute("class", "inActive-next");
    	}else{
    	 	if(IE)document.getElementById("styleNav-next").setAttribute("className", "active");
    		else document.getElementById("styleNav-next").setAttribute("class", "active");
    	}
    	
    }
}

function NuevoAjax(){
	var xmlhttp=false;
	try{
		xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
	}catch(e){
		try{
			xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
	}catch(E){
			xmlhttp = false;
	}
}

if(!xmlhttp && typeof XMLHttpRequest!='undefined'){
	xmlhttp = new XMLHttpRequest();
}
	return xmlhttp;
}

function Cargar(url){
var contenido, preloader;
timestamp = (new Date()).getTime();

contenido = document.getElementById('styleConten');
preloader = document.getElementById('preloader');
ajax=NuevoAjax();
ajax.open("GET", url + '?t=' + timestamp,true);
ajax.onreadystatechange=function(){
	if(ajax.readyState==1){
		//preloader.innerHTML = "Cargando...";
		preloader.style.display = "block";
	}else if(ajax.readyState==4){
		if(ajax.status==200){
			contenido.innerHTML = ajax.responseText;
			preloader.style.display = "none";
			//preloader.style.background = "url('loaded.gif') no-repeat";
		}else if(ajax.status==404){
			preloader.innerHTML = "La página no existe";
		}else{
			preloader.innerHTML = "Error:".ajax.status;
		}
	}
}
ajax.send(null);
}

function iniCoverPic(xmlUrl, preview)
{
//	$(document).ready(function(){
	
		if (preview == null) preview = false;
		var galery = {
			setData : function(pos)
			{
				$("#picMenu-bt"+activeImg).removeClass("picMenu-on");
				$("#picMenu-bt"+pos).addClass("picMenu-on");
				$('.secondPic').attr({src: getContent('imagen', pos-1)}).fadeTo(700, 1);
		  	$('.firstPic').fadeTo(700, 0, newPic);
		  	$('#coverFirstPic h1 a').hide();
				$('#coverFirstPic p').hide();
				activeImg = pos;
			}
		};
		
		var xml;
		var activeImg = index = 1;
		var limite = 4;
		var intervalo; 
		
		$.ajax({
			type: 'get',
			url: xmlUrl,
			cache: true,
			dataType: 'xml', // Reconocemos el browser.
			success : function(data)
			{
				xml = data;
			  for (i=1; i< 5; i++) {
			  	$('#picMenu-bt'+i).click(function()
					{
						clearInterval(intervalo);
						galery.setData($(this).attr('id').substring(10));
						return false;
					});
				}
				intervalo = setInterval(timerPic, 7000);
			}
		});
		
		
		function timerPic(){
			if((activeImg*1)+1<=limite)galery.setData((activeImg*1)+1);
			else galery.setData(1);
			clearInterval(intervalo); 
		}
			
			
		function newPic()
		{
			$('#coverFirstPic h1 a').html(getContent('titulo', activeImg-1))
															.attr({href: getContent('enlace', activeImg-1)})
															.show();
			$('.firstPic').attr({src: getContent('imagen', activeImg-1)})
										.css({'opacity':'1', 'filter':'alpha(opacity=100)'});
	  	$('.coverLink').attr({href: getContent('enlace', activeImg-1)});
	  	$('#coverFirstPic p').html(getContent('texto', activeImg-1)).show();
			$('.secondPic').css({'opacity':'0', 'filter':'alpha(opacity=0)'});
			intervalo = setInterval(timerPic, 7000);
		}
			
		function getContent(tipo, id) {
			nodo = $(xml).find(tipo);
			return($(nodo[id]).text());
		}
		
//	});
}
