$(document).ready(function() {
	makeLinks();
	//$.address.strict(false);	//modo no estricto
	$.address.crawlable(true);	//Google ajax crawling
	if($.address.value() == "/") $.address.value("info");
});
function makeLinks() {							//**hacer links de menu y banderas**
	var menuitems = $("#menu img");
	var flaglinks = $("#flags area");
	menuitems.mouseover(function() {
		if ($(this).attr("name") == "Item") {
			var src = $(this).attr("src").match(/[^\.]+/) + "_s.jpg";
			$(this).attr("src", src);
		}
	});
	menuitems.mouseout(function() {
		if ($(this).attr("name") == "Item") {
			var src = $(this).attr("src").replace("_s", "");
			$(this).attr("src", src);
		}
	}); 
	menuitems.click(function(e) {
		if ($(this).attr("name") == "Item") {
			$.address.path(this.id);
		}
	});
	flaglinks.click(function(e) {
		e.preventDefault();
		var addr = $(this).attr("href").replace(new RegExp("\.[^.]*$"), "");
		$.address.path(addr);
	});
}
$.address.change(function(e) {				//**detectar cambios en la url y cargar páginas**
	$("#loading").show();
	$.ajax({
		url: "." + e.value+".html", //modo estricto
		cache: true,
		success: function(response) {
			var $response = $(response);
			var $content = (($response.children('body').length) ? $response.children('body').contents():
				($response.filter('body').length) ? $response.filter('body').contents():
				$response).not('style,title,script,meta,link');

			$("#content").html($content);
			
			//$("#content").html(response); // cargar sin quitar el body
			
			updatePaths($("#content img"));
			updatePaths($("#content a"));
			overrideContentLinks();
			
			if ($.address.value() == "/info") {
				if (typeof(playSlideshow) == 'undefined') playSlideshow = setInterval("slideSwitch()", 4500);
				
				$("#posters").mouseover(function() {
					clearInterval(playSlideshow);
				});
				$("#posters").mouseout(function() {
					playSlideshow = setInterval("slideSwitch()", 4500);
				});
			} else if (typeof(playSlideshow) !== 'undefined') {clearInterval(playSlideshow); delete playSlideshow;}
			$("#loading").hide();
		},
		error: function(xhr, ajaxOptions, thrownError) {
			$("#loading").hide();
		},
	});
});
function overrideContentLinks() {				//**funcionalidad de los links**
	var internalLnk = $("a:not([href^='http:'],[href^='https:'],[href^='mailto:'],[href*='#'],[href^='javascript:'])");
	var externalLnk = $("a[href^='http:'],a[href^='https:']");
	var mailtoLnk = $("a[href^='mailto:']");
	var anchorLnk = $("a[href*='#']");
	
	internalLnk.click(function(e) {
		e.preventDefault();
		if (this.target == "" || this.target == "_self") {
			var addr = $(this).attr("href").replace(new RegExp("\.[^.]*$"), "");
			$.address.path(addr);
			scrollFromTop(285);
		} else window.open($(this).attr("href"), this.target);
	});
	externalLnk.click(function(e) {
		e.preventDefault();
		window.open($(this).attr("href"), this.target);
	});
	mailtoLnk.click(function(e) {
		//comportamiento de links a E-mail
	});
	anchorLnk.click(function(e) {
		e.preventDefault();
		if ($(this).attr("href").search(/\b#/) > -1) {	//# despues de .xxx (.html#top)
			//var htag = $(this).attr("href").split("#")[1];
			var addr = $(this).attr("href").replace(new RegExp("\.[^.]*$"), "");
			$.address.path(addr);
			scrollFromTop(285);
		} else {
			var htag = $(this).attr("href").split("#")[1];
			var posy = $('a[name$="'+htag+'"]').offset().top;
			scrollFromTop(posy-32);
		}
	});
}
function updatePaths(element) {					//**actualizar links para nueva ruta**
	if (element.is("img")) {
		element.each(function() {
			var src = "." + $.address.path().replace(new RegExp("\/[^/]*$"), "") + "/" + $(this).attr("src"); //modo estricto
			$(this).attr("src", src);
		});
	}
	if (element.is("a")) {
		element.each(function() {
			if ($(this).attr("href")) {
				if ($(this).attr("href").search("http:") == "-1" && $(this).attr("href").search("https:") == "-1" && 
				$(this).attr("href").search("mailto:") == "-1" && $(this).attr("href").search("javascript:") == "-1") {
					var src2 = $(this).attr("href");
					var parenttimes = 0;
					while (src2.substring(0, 3) == "../") {
						src2 = src2.substring(3);
						parenttimes++;
					}
					var src1 = $.address.path().replace(/\/[^/]*$/, "");
					if (parenttimes > 0) {
						for (var i = 0; i < parenttimes; i++) {
							src1 = src1.replace(/\/[^/]*$/, "");
						}
					}
					$(this).attr("href", src1 + "/" + src2); //modo estricto
				}
			}
		});
	}
}

function scrollFromTop(y) {
	$('html, body').animate({
		scrollTop: y
	}, 400);
	
	//window.document.body.scrollTop = y;
	//window.document.documentElement.scrollTop = y;
}

function slideSwitch() {									//**Banners**
    var $active = $('#posters div.active');
    if ($active.length == 0) $active = $('#posters div:last');

    var $next = $active.next().length ? $active.next()
        : $('#posters div:first');

    $active.addClass('last-active');

    $next.css({opacity: 0.0})
        .addClass('active')
        .animate({opacity: 1.0}, 500, function() {
            $active.removeClass('active last-active');
        });
}
