// Custom JavaScript functions

$(document).ready(function(){
	caja_blanca_height();
	menu_change();
	footer_top();
	back_img($('#bg_img').attr('class'))
	fondo();
	fondo_blanco($('#main_wrap').height());

});

$(window).scroll(function () { 
	menu_change(); 
});


mw_top = 180;
mw_min_height = 397;
menu_wrap_top = 577;
footer_wrap_top = 667;

function caja_blanca_height(){
		mw = $('#main_wrap');
		if(mw.height()<mw_min_height){ mw.height(mw_min_height) };
}

function menu_change(){
	fin_cajablanca = mw_top + $('#main_wrap').height();
	menu_pos = $('#menu_wrap').offset();
	menu_top = menu_pos.top;
	browser_height = $(window).height();
	menu_height = $('#menu_wrap').height();


	x=browser_height-menu_height+$(window).scrollTop();
	$('.texto').text($(window).scrollTop())

	if(fin_cajablanca>=x){
		$('#menu_wrap').css('position','absolute')
		$('#menu_wrap').stop();
		$('#menu_wrap').animate({
			top: x
		}, 'fast', function() {});
	}else{
		$('#menu_wrap').css('position','absolute');
		$('#menu_wrap').stop();
		$('#menu_wrap').animate({
			top: fin_cajablanca
		}, 'fast', function() {});
	}
}

function footer_top(){
	topdelfooter = mw_top + $('#main_wrap').height() + $('#menu_wrap').height();
	$('#footer_wrap').css('top',topdelfooter+'px');
}

function back_img(bgimg){
	for (var i = $('a').length - 1; i >= 0; i--) {
		$('a:eq('+i+')').attr('href',$('a:eq('+i+')').attr('href')+'?bg='+bgimg);
	};
}

function fondo(){
	win_h = $(window).height();
	mw_h = mw_top + $('#main_wrap').height()+$('#menu').height()+$('#footer_wrap').height();

	// alert(mw_top + $('#main_wrap').height());
	// alert($('#menu').height());
	// alert($('#footer_wrap').height());
	// alert(mw_h);

	var oo = (mw_h>=win_h)?mw_h:win_h;
	$('#bg_img div').height(oo)
}
function clean(que){
	if($('#'+que).attr('value')==''){
		switch(que)	{
			case 'f_nombreyapelido': mete_value('#'+que,'Nombre y Apellido'); break;
			case 'f_mail': mete_value('#'+que,'e-Mail'); break;
			case 'f_telefono': mete_value('#'+que,'Teléfono'); break;
			case 'f_consulta': mete_value('#'+que,'Consulta'); break;
		}
	}else{
			switch($('#'+que).attr('value'))	{
			case 'Nombre y Apellido': mete_value('#'+que,''); break;
			case 'e-Mail': mete_value('#'+que,''); break;
			case 'Teléfono': mete_value('#'+que,''); break;
			case 'Consulta': mete_value('#'+que,''); break;
		}	
	}
}

function mete_value(donde,cual){
	$(donde).attr('value',cual)
}

function fondo_blanco(alto){
	$('#main_wrap').height(0);
	$('#main_wrap').animate({
		height: alto
	}, 1000, function() {
		$('#main_wrap > div').css('visibility','visible')
		$('#main_wrap > div#tira_de_imagenes').css('opacity',0)
		$('#main_wrap > div#tira_de_imagenes').animate({
			opacity: 1
			}, 1000, function() {
				animar_marca();
			});
	});
}

function animar_marca(){
	marca_w = $('#header_marca');
	marca = $('#header_marca img');
	taco = $('#header_taco');

	marca_w.slideDown('5000', function() {
				marca.fadeIn('2000', function(){
					taco.slideUp('5000')
				});	
	});
}

