// Fotos rotativas
var slideshowSpeed = 6500;
var photos = [ {
		"image" : "bg-foto1.jpg"
	}, {
		"image" : "bg-foto6.jpg"
	}, {
		"image" : "bg-foto4.jpg"
	}, {
		"image" : "bg-foto7.jpg"
	}, {
		"image" : "bg-foto5.jpg"
	}
];
photos.sort(function() {return 0.5 - Math.random()})

//
$(document).ready(function(){

/* PNG Fix (debe estar incluido el pngfix.js en todas las paginas)
if($.browser.msie) {
	if ($.browser.version == "6.0") {
		isIE = $.browser.msie;
		$(document).pngFix();
	}
}
*/

// Sidebar mail
$('.sidebar .mail').hover(function(){
	$(this).addClass('activo');
},function(){
	$(this).removeClass('activo');
}).focus(function(){
	$(this).addClass('activo');
}).blur(function(){
	$(this).removeClass('activo');
});


// Menu
$('.menu a:not(".activo")').hover(function(){
	$(this).stop().animate({paddingLeft: '12px'}, 200);
},function(){
	$(this).stop().animate({paddingLeft: '5px'}, 400);
});
	
/* sIFR */
/*
if ($.browser.version == "6.0") {
	// si es version "6.0" no hace nada
} else {
	$.sifr({	path: '/swf/',	save: true });
	$('.izq h1, .sidebar h4, .home h3').sifr({ font: 'palma' });
	//$('.sidebar h2').sifr({ font: 'mgopenmodatabold' });
}
*/

// Swap values
swapValues = [];
$(".swap").each(function(i){
	swapValues[i] = $(this).val();
	$(this).focus(function(){
		if ($(this).val() == swapValues[i]) {
			$(this).val("");
		}
	}).blur(function(){
		if ($.trim($(this).val()) == "") {
			$(this).val(swapValues[i]);
		}
	});
});

// Links externos en vent nueva
//$("a[href^='http']").attr({ target: "_blank" });

// Link IR ARRIBA
//$('#wrapper').append('<a href="#top" class="top">IR ARRIBA</a>');

//Scroll suave
var Velocidad = 900;
function filterPath(string) {
	return string
	.replace(/^\//,'')
	.replace(/(index|default).[a-zA-Z]{3,4}$/,'')
	.replace(/\/$/,'');
}
var locationPath = filterPath(location.pathname);
$('a[href*=#]').each(function() {
	var thisPath = filterPath(this.pathname) || locationPath;
	if (  locationPath == thisPath && (location.hostname == this.hostname || !this.hostname) && this.hash.replace(/#/,'') ) {
		var $target = $(this.hash), target = this.hash;
		if (target) {
			var targetOffset = $target.offset().top;
		$(this).click(function(event) {
			event.preventDefault();
			$('html, body').animate({scrollTop: targetOffset}, Velocidad, "easeOutExpo", function() {
				location.hash = target;
		    });
		});
	}
}
});

// FOTOS rotativas
	var interval;
	var activeContainer = 1;	
	var currentImg = 0;
	var animating = false;
	var navigate = function(direction) {
		// Check if no animation is running. If it is, prevent the action
		if(animating) {return;}
		
		// Check which current image we need to show
		if(direction == "next") {
			currentImg++;
			if(currentImg == photos.length + 1) {
				currentImg = 1;
			}
		} else {
			currentImg--;
			if(currentImg == 0) {
				currentImg = photos.length;
			}
		}
		
		// Check which container we need to use
		var currentContainer = activeContainer;
		if(activeContainer == 1) {
			activeContainer = 2;
		} else {
			activeContainer = 1;
		}
		
		showImage(photos[currentImg - 1], currentContainer, activeContainer);
		
	};
	
	var currentZindex = -1;
	var showImage = function(photoObject, currentContainer, activeContainer) {
		animating = true;
		
		// Make sure the new container is always on the background
		currentZindex--;
		
		// Set the background image of the new active container
		$("#headerimg" + activeContainer).css({
			"background-image" : "url(/images/" + photoObject.image + ")",
			"display" : "block",
			"z-index" : currentZindex
		});
		
		
		// Fade out the current container
		// and display the header text when animation is complete
		$("#headerimg" + currentContainer).fadeOut(1100, function() {
			setTimeout(function() {
				animating = false;
			}, 500);
		});
	};
	
	var stopAnimation = function() {
		// Clear the interval
		clearInterval(interval);
	};
	
	// We should statically set the first image
	navigate("next");
	
	// Start playing the animation
	interval = setInterval(function() {
		navigate("next");
	}, slideshowSpeed);

	
// MM Preload imgs
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];}}
}

// fin JS
});
