/*
general.js

###############################
Creado en 2008/06/19
NOVEMBRO S.l.
Todos los derechos reservados.
###############################
*/

$(document).ready(function () {

	var modulos = $('div.Modulos');
	var contenedor = $('#contenedor');
	var fotos = $('.fotos');
	var mosaico = $('#mosaico');
	var index = contenedor.html();
	
	
	EstadoInicialModulos();
	
	// PARA SITEMAPS DE GOOGLE
	/*
	var peticion = window.location+''
	peticion = peticion.substring(peticion.indexOf('novembro.net/')+13);

	if (peticion.indexOf('_') == 6) {
		Modulo(0);
		$('#contenedor').load('./modulos/'+peticion);
	};
	*/
	// FIN PETICIONES GOOGLE
	
	var Titulos = ['Empresa','Fotografia','Taquio','Camping Riaño', 'Prodemel','Simelec','Videos']
	
	modulos.livequery('click', function(event) {
		
		var m = $('div.Modulos').index(this);
		if (m >= 0 && m <= 8) {
			Modulo(0);
			$('#contenedor').load('./modulos/modulo_'+m+'.htm');
			document.title='NOVEMBRO '+Titulos[m];
			return false;
		};

	});
	
	modulos.livequery('mouseover', function(event) {

		var m = $('div.Modulos').index(this);
		
		if (m > 0 && m <= 8) { 
			$(this)
				.css({'background-image':'url(./img/modulos/modulo'+m+'.png)'})
				.fadeTo(10, 1);
		};
	});
	
	modulos.livequery('mouseout', function(event) {
		var m = $('div.Modulos').index(this);
		
		if (m > 0 && m <= 8) {
			$(this)
				.css({'background-image':'url(./img/modulos/modulo'+m+'_i.png)'})
				.fadeTo(10, 0.70);
			
			//$(this).css({'background-image':''});
			//$(this).fadeTo(10, 0.30);
		};
	});
	
	// AGRANDAR LOS MOSAICOS
	fotos.livequery('click', function(event) {
		$('#mosaico').remove();
		var idt = $(this).attr('id');
		var ruta = './img/modulos/mosaico_'+idt+'.jpg';
		//$('#mode').append('<img id="mosaico" src="'+ruta+'" />')
		$('#mode').append('<div id="loader" class="loading"></div>');
		
		Imagen(ruta);
		
		mosaico.livequery('click', function(event) {
			$('#loader').remove();
		});
		
	});
	
	
	setTimeout('CargarFondo();',3000);
	
	// FUNCIONES
	
	// CARGAR DESCARGAR LOS MODULOS 
	function Modulo (remove) {
		
		if (remove == 1){
			$('#capaModulo').remove();
			contenedor.html(index);
			EstadoInicialModulos ();
			document.title = 'NOVEMBRO';
		} else {
			if (document.body.scrollHeight>$(window).height()) var h = document.body.scrollHeight
			else var h = $(window).height();
			var w = $(window).width();

			$('body').append('<div id="capaModulo"/>');
			$('#capaModulo').height(h);
			$('#capaModulo').width(w);
		};
		
		$('#capaModulo').click(function() { 
			Modulo(1);
			
			return false;	
		});
			
		$('.cerrar').livequery( 'click', function(event) {
			Modulo(1);
			return false;
		});
	};
	
	// CARGADOR DE IMAGENES
	// http://jqueryfordesigners.com/image-loading/
	function Imagen (imagen) {
		var img = new Image();

		// wrap our new image in jQuery, then:
		$(img)
			// once the image has loaded, execute this code
			.load(function () {
				// set the image hidden by default    
				$(this).hide();

				// with the holding div #loader, apply:
				$('#loader')
					// remove the loading class (so no background spinner), 
					.removeClass('loading')
					// then insert our image
					.append(this);

					// fade our image in to create a nice effect
				$(this).fadeIn();
			})

			// if there was an error loading the image, react accordingly
			.error(function () {
			// notify the user that the image could not be loaded
			})

			// *finally*, set the src attribute of the new image to our image
			.attr('src', imagen)
			.attr('id','mosaico')
	};
	
	function EstadoInicialModulos () {
		// ESTADO INICIAL DE LOS MODULOS
		jQuery.each($('div.Modulos'),function(i,val){
			if (i != 0)
				$(val)
					.css({'background-image':'url(./img/modulos/modulo'+i+'_i.png)'})
					.fadeTo(10, 0.7);
		});
	};
});

function CargarFondo () {
	var img = new Image();
	var rutaFondo = './img/fondo/back_full.jpg';
	// wrap our new image in jQuery, then:
	$(img)
		// once the image has loaded, execute this code
		.load(function () {
			// set the image hidden by default    
			$(this).hide();
			$('body').css({'background-image':'url('+rutaFondo+')'})
		})

		// if there was an error loading the image, react accordingly
		.error(function () {
		// notify the user that the image could not be loaded
		})
		.attr('src',rutaFondo)
	
};