var cargando = false;
function enlacesRecursivos(){
	$("a").click(function(event){
		// alert($(this).click);
		if(!cargando){
			url = $(this).attr("href").split("###");
			if(url.length == 2 && document.getElementById(url[1])){
				cargando = true;				
				$.ajax({
  					url: url[0]+'&cache=' +Math.random(),
					success: function(html){
						$("#contenido").removeClass("rayado");
						$("#contenido").css('overflow','auto');
						document.getElementById("contenido").innerHTML = html;
						html.ejecutaCodigo();
						// $("#contenido").html(html);
						cargando = false; enlacesRecursivos();
  					}
				});				
				//$("#contenido").load(url[0]+'&cache=' +Math.random(), {}, function(){cargando = false; enlacesRecursivos();});
				event.preventDefault();				
			}
		}	   		
	});
	$("area").click(function(event){
		// alert($(this).click);
		if(!cargando){
			url = $(this).attr("href").split("###");
			if(url.length == 2 && document.getElementById(url[1])){
				cargando = true;
				$("#contenido").removeClass("rayado");
				$("#contenido").css('overflow','auto');
				$("#contenido").load(url[0]+'&cache=' +Math.random(), {}, function(){cargando = false; enlacesRecursivos();});
				event.preventDefault();				
			}
		}	   		
	});
			
}



function centerPag(){
    $("#contenedor").hide();
    var w = $(this).width(); 
	var h = $(this).height();
	var ancho=(w>=1200)?1200:990;
	var alto =$("#contenedor").height(); //768	
	$("#contenedor").css('width',ancho);
	w=(w<ancho)? 0 : (w/2) - (ancho/2);
	h=(h<alto)? 0 : (h/2) - (alto/2);
	$("#contenedor").css("left",w + "px"); 
	$("#contenedor").css("top",h + "px");
	$("#contenedor").show();
}

$(window).resize(function(){
	centerPag();
});

String.prototype.ejecutaCodigo=function(){
   	var pat=/<script[^>]*>([\S\s]*?)<\/script[^>]*>/ig;
   	var pat2=/\b\s+src=[^>\s]+\b/g;
   	var elementos = this.match(pat) || [];
   	for(i=0;i<elementos.length;i++) {
       	var nuevoScript = document.createElement('script');
       	nuevoScript.type = 'text/javascript';
       	var tienesrc=elementos[i].match(pat2) || [];
       	if(tienesrc.length){
           	nuevoScript.src=tienesrc[0].split("'").join('').split('"').join('').split('src=').join('').split(' ').join('');
       	}else{
           	var elemento = elementos[i].replace(pat,'$1','');
           	nuevoScript.text = elemento;
       	}
       	document.getElementsByTagName('body')[0].appendChild(nuevoScript);
   	}
   	return this.replace(pat,'');
}


$(document).ready(function() {
	centerPag();
	$('#mycarousel').jcarousel();		
	// $("#scrollimagenes").load("scrollimagenes.inc.php", {}, function(){$('#mycarousel').jcarousel();});
	$("#lateraldecha").load("scrollvideos.inc.php", {}, function(){$('#mycarousel2').jcarousel({
        vertical: true,
        scroll: 2
    });enlacesRecursivos()});	
});

