var sectionVisible = 'inicial';
var toogleAction = 'close';
var background = 0;
var background_delay = 5000;
var homeContentEnabled = 'suerte';
var home_delay = 6000;
var mapaEnabled = 'azul';
var mapa_delay = 1000;
var busy = false;
var backgrounds = new Object();

backgrounds["inicial"] = new Array();
backgrounds["suerte"] =  new Array();
backgrounds["desde"] = new Array();
backgrounds["contacto"] = new Array();
backgrounds["situanos"] = new Array();

backgrounds["inicial"][0] = "imagenes/imagen1_home.jpg";
backgrounds["inicial"][1] = "imagenes/imagen2_home.jpg";

backgrounds["suerte"][0] = "imagenes/imagen1_suerte.jpg";
backgrounds["suerte"][1] = "imagenes/imagen2_suerte.jpg";

backgrounds["desde"][0] = "imagenes/imagen1_desde.jpg";
backgrounds["desde"][1] = "imagenes/imagen2_desde.jpg";
backgrounds["desde"][2] = "imagenes/imagen3_desde.jpg";

backgrounds["contacto"][0] = "imagenes/imagen1_contacto.jpg";
backgrounds["contacto"][1] = "imagenes/imagen2_contacto.jpg";

backgrounds["situanos"][0] = "imagenes/imagen1_situanos.jpg";
backgrounds["situanos"][1] = "imagenes/imagen2_situanos.jpg";


$(function() {

	timer = setInterval("slideSwitch(1)", background_delay);

	timer2 = setInterval("slideHomeContent()", home_delay);

	timer3 = setInterval("slideMapaImage()", mapa_delay);


});


function slideMapaImage(){
	
	if(mapaEnabled == 'azul'){
	
		$('#link-situanos').css({ backgroundImage: 'url(imagenes/taco_negro_mapa_situanos.png)' });

		mapaEnabled = 'negro';

	}else{
		
		$('#link-situanos').css({ backgroundImage: 'url(imagenes/taco_azul_mapa_situanos.png)' });

		mapaEnabled = 'azul';

	}

}


function slideHomeContent(){
	
	if(toogleAction == 'close'){
		
		if(sectionVisible == 'home' || sectionVisible == 'suerte'){

			if(homeContentEnabled == 'suerte'){
			
				$('#suerte').fadeOut(5000);

				$('#main-container').load('content.php #home', function(){ 
					
					$('#home').fadeIn(5000);
					
				});

				homeContentEnabled = 'home';

			}else{
				
				$('#home').fadeOut(5000);

				$('#main-container').load('content.php #suerte', function(){ 
					
					$('#suerte').fadeIn(5000);
					
				});

				homeContentEnabled = 'suerte';
			}

		}

	}

}


function changeContent(section){
	
	if(toogleAction == 'open'){	// si el contenedor de seccion esta cerrado => la abrimos

		toogleContentVisibility();
	
	}
		
	if(section == 'desde' || section == 'suerte' || section == 'contacto' || section == 'situanos'){

		$('#nav-' + sectionVisible).css("background-image", "");

		$('#nav-' + section).css({ backgroundImage: 'url(imagenes/bg.png)' });

	}

	if(section == 'home' || section == 'legal' || section == 'situanos'){

		$('#nav-' + sectionVisible).css("background-image", "");

	}

	$('#inicial').fadeOut(500);

	$('#' + sectionVisible).fadeOut(500);

	sectionVisible = section;

	$('#main-container').load('content.php #' + sectionVisible, function(){ 
		
		$('#' + sectionVisible).fadeIn(500, function(){ slideSwitch(0); });
		
	});

}



function toogleContentVisibility(){	
	
	heigh_value = (toogleAction == 'open') ? '300px' : '20px';

	top_value = (toogleAction == 'open') ? '0px' : '250px';
	
	$('#right').animate({top: top_value, height: heigh_value}, 500, "swing", function(){ 		
		
		$('#main-container-controller').toggleClass('open');

		if(toogleAction == 'open'){

			$('#' + sectionVisible).show();

		}else{

			$('#' + sectionVisible).hide();

		}

		if(sectionVisible == 'home'){
		
			if(toogleAction == 'open'){

				$('#inicial').show();

			}else{

				$('#inicial').hide();

			}
		}


		toogleAction = (toogleAction == 'open') ? 'close' : 'open';	
		
	});

}



function slideSwitch(next_bg){

	function changeBackground(){

		if($("#background1").css('display') == 'none'){

			$("#background1").attr('src', backgrounds[sectionVisible][background]);

			$("#background2").fadeOut(1000);

			$("#background1").fadeIn(1000);			

		}else{

			$("#background2").attr('src', backgrounds[sectionVisible][background]);

			$("#background1").fadeOut(1000);

			$("#background2").fadeIn(1000);

		}

	}
	
/*	if(sectionVisible == 'home'){
		
		sectionVisible = 'suerte';

	}*/

	if(sectionVisible == 'inicial' || sectionVisible == 'home' || sectionVisible == 'desde' || sectionVisible == 'suerte' || sectionVisible == 'contacto' || sectionVisible == 'situanos'){

		if(!busy && backgrounds[sectionVisible].length > 0){
			
			busy = true;		

			clearTimeout(timer);

			timer = setInterval("slideSwitch(1)", background_delay);		

			if(!next_bg){

				background = (background==0) ? backgrounds[sectionVisible].length-1 : background-1;
				
			}else{

				background = (background==backgrounds[sectionVisible].length-1) ? 0 : background+1;

			}

			bckimg = new Image();

			bckimg.src = backgrounds[sectionVisible][background];
		
			// For IE:
			if($.browser.msie){			

				function testImg(){

					if(bckimg.complete != null && bckimg.complete == true){ 

						changeBackground();

						return;

					}			

					setTimeout(testImg, 300);

				}

				setTimeout(testImg, 300);	
				
			}

			else bckimg.onload = changeBackground;
			
			// Preload next bg!
			bckimgnew = new Image();

			bckimgnew.src = backgrounds[sectionVisible][background+1];
			
			busy = false;
			
		}else{

			clearTimeout(timer); 

			timer = setTimeout("slideSwitch(1)", 1500);

		}

	}

}



$(document).ready(function(){

	$('#accordion').accordion({

		autoheight: false,

		alwaysOpen: false

	});

	$('#main-container').load('content.php #inicial', function(){ 
		
		$('#inicial').fadeIn(500);
		
	});

	slideSwitch(0);

});
