var serv = {secciones: []};
var sec = 0;
var listaImg;
var alturaFijo;

var prop  =
{
	url : '',
	altura : '',
	margen : '',
	borde : '',
	visto : 'no',
	imagenes : [],
	agrega : function(url, borde, altura, margen, visto, imagenes)
	{
		
		resp = prop.busca(url);
			
		if(resp.enc=="no")
		{
			var nvaImgs = '"'+imagenes.join('","')+'"';
			
			prop.visto = visto;
			prop.url = url;
			prop.borde = borde;
			prop.altura = altura;
			prop.margen = margen;
			serv.secciones[sec] = jQuery.parseJSON( '{ "url":"'+prop.url+'" , "altura" : "'+prop.altura+'", "margen" : "'+prop.margen+'" ,"borde" : "'+prop.borde+'","visto" : "'+prop.visto+'", "imagenes":['+nvaImgs+'] }');
			sec ++;
		}
		
		if(resp.enc=="si" && serv.secciones[resp.pos].visto=='no' )
		{
			serv.secciones[resp.pos].visto = visto;
			serv.secciones[resp.pos].altura = altura;
			serv.secciones[resp.pos].margen = margen;
		}	
		
		return resp.pos;
	},
	busca : function(url)
	{
		var resp ={'enc':'no', 'pos':'0'};
		
		if(serv.secciones.length == 0)
		{
			return resp;
			
		}else{
				for(var i in serv.secciones)
				{
					
					if(serv.secciones[i].url == url)
					{			
						resp.enc = 'si';
						resp.pos = i;
						return resp;
						break;
					}
					
					
				}	
			
				return resp;
			}	
		
	}
};



$(document).ready(function() 
{		
	recuperaListaIni();
	
	$('#lista-servicios a').hover(entradaServ, salidaServ);
	
	
	verificaURL();
	
	$('#lista-servicios a').click(function(){
		alturaFijo = $('#serv-fijo').height();
		sec = $(this).attr('href').split('#')[1];
		
		muestraSeccion(sec);
	});
	
	cambiaFondo();
	
	$('#form_news').submit(function()
	{
		email = $('#email').val();
		if( validaCorreo(email) )
		{
			$.ajax({
				type: "POST",
				dataType: "html",
				url: 'admin/registraNewsletter.php',
				data: $('#form_news').serialize(),
				success: function(dat)
					{
						alert(dat);	
						//$('#form_contacto').html('');
						//$('#direccion').html('<label>'+dat+'</label>');
						$('#email').val('');
						
					}
				});
			
		}else alert('E-mail Incorrecto: '+email);
		
		return false;
	});
	
	$('#cierrraSec').click(cierraSec);
});

function validaCorreo(valor)
{
	var reg=/(^[a-zA-Z0-9._-]{1,30})@([0-9a-z][0-9a-z-]+\.)+[a-z]{2,3}$/;
	if(reg.test(valor)) return true;
	else return false;
}


function cierraSec()
{
	$('#cierrraSec').hide();
	url = window.location.href;
	if(url.indexOf('#') != -1)
	{
		sec = url.split('#')[1];
		alturaSec = $('#'+sec).height();
		
		$('#'+sec).animate({
			height : 0
			},
			'2000',function()
			{
				$('#'+sec).hide();
				$('#'+sec).css('height',alturaSec);
				
				
				//alert(altura);
				$('#serv-fijo').css('background','#000000');
				$('#serv-fijo').animate({
					height : alturaFijo
				},'1000',function()
				{
					$('#background').html(listaImg);
					cambiaFondo();
					$('#cont-serv').slideToggle();
					window.location.href="#";
					//$('#cont-serv').show(500);
				});
			});
		
	}else window.location.href="index.php";
}

function recuperaListaIni()
{
	listaImg = $('#background').html();
}

function entradaServ()
{
	//alert('entrada');
	url = $(this).attr('href').split('#')[1];
	
	$(this).parent().css('border-top','3px solid #000000');
	
	altura = $(this).height();
	margen = $(this).css('margin-top');
	$(this).removeClass('peque');
	$(this).addClass('grande');
	
	pos = prop.agrega(url, '', altura, margen, 'si','');

	$(this).css('border-top','3px solid '+serv.secciones[pos].borde);
		
	$(this).animate(
	{
		height : serv.secciones[pos].altura,
		marginTop : '-=10px'
	},'fast',function(){
		$(this).parent().css('border-top','3px solid #535355');
	});
	
	
}

function salidaServ()
{
	
	url = $(this).attr('href').split('#')[1];
	
	resp = prop.busca(url);
	
	$(this).css('border-top', '');
	$(this).animate(
	{
		height : serv.secciones[resp.pos].altura,
		marginTop : serv.secciones[resp.pos].margen
	}, "fast", "linear",function(){
		
		$(this).removeClass('grande');
		$(this).addClass('peque');
	});

}

function verificaURL()
{
	url = window.location.href;
	
	if(url.indexOf('#') != -1)
	{
		sec = url.split('#')[1];
		muestraSeccion(sec);
	}
}	

function muestraSeccion(sec)
{
	resp = prop.busca(sec);
	
	if(resp.enc=="si")
	{
		$("#background").mask("Procesando...");
		
		ajustaFijo(sec, resp.pos);
		
	
		$('#cont-serv').hide('fast');
		$('#'+sec).show('fast');
		$('#'+sec).find('div.desc').css('background',serv.secciones[resp.pos].borde);
		
		
	}else{
			window.location.href = "";
		}	
}

function ImgsFondo(pos)
{
	
	var listadoImgs = '';
	$(serv.secciones[pos].imagenes).each(function(i,v)
	{
		listadoImgs +='<img src="images/'+v+'" alt="'+v+'" />';
	});	

	$('#background').html(listadoImgs);
	$("#background").mask("Cargando im&aacute;genes...");	
	
	
	$('#background img').load(function()
	{
		cambiaFondo();
			
		setTimeout(function()
		{
			$("#background").unmask();	
		},3000);
		
	});	
	
	
}

function ajustaFijo(sec,pos)
{
	
	altura = $('#'+sec).height();
	//alert(altura);
	$('#serv-fijo').css('background','transparent');
	$('#serv-fijo').animate({
		height : altura + 10 //padding bottom 40px
	},2000,function()
	{
		$('#cierrraSec').show();
		ImgsFondo(pos);
	});
	
	
	
}

function cambiaFondo()
{	
	if(document.getElementById('fss-content'))
	{
	}
	else
	{
		//alert('no lo tiene');
		
		$('#background').fsslider({
			spw: 1,
			sph: 1,
			delay: 5000,
			/*delay: 5000,*/
			sDelay: 2000,
			effect: 'lineal',
			texture: 'strip1',
			navigation: false
		});
			
	}	
	
}


