/*********************Preload Menu ***************/
function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}
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];}}
}
function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}
function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
} 
//////////////////////////////////////////
var ver_mas = 'true';
var fondo = 1;
var perfil = 0;
var total_imagenes = 1;
var copies = {
		unete_nombre: 'Escribe tu nombre y haz parte de la magia de Noel',
		registro_nombre: 'Escribe tu nombre y empieza a vivir la magia de Noel',
		registro_apellido:'Escribe tu apellido y empieza a vivir la magia de Noel',
		registro_mail: 'Escribe tu mail y empieza a vivir la magia de Noel',
		registro_productos:'Selecciona uno de nuestros deliciosos productos',
		login_pregunta:'Escribe una respuesta a tu pregunta',
		titulo_receta: 'Ponle titulo a tu receta',
		foto_receta: 'Sube la foto de tu receta puede ser un archivo JPEG PNG GIF',
		ingredientes_receta: 'Escribe los ingredientes de tu receta',
		preparacion_receta: 'Como prep&aacute;ras tu receta',
		apublicacion:'Autoriza la publicaci&oacute;n de los contenidos'
};
function adcionarAnalitycs(url) {
	pageTracker._trackPageview(url);
}
function loading(){
	$('loading').setStyle("opacity",0);
	$('loading').setStyle("display","block");
	var myFx = $('loading').get('morph', {duration: 10});
	myFx.start({'opacity':[0,1]});
}
function ocultarCargador(){
	
	var loading = $('loading');
	var myFx = $('loading').get('morph', {duration: 200});
	myFx.start({'opacity':[1,0]}).chain(function(){$('loading').setStyle("display","none");});
}
function mostrarAlerta(msj)
{
	var Contenido = "<div id='msj'>"+msj+"</div>";
	var detalle_combo = new light_box({lightBoxOpacity:0.7, tema:'alerta' });
	detalle_combo.abrirLightBox(Contenido);
}

function mostrarLightboxHome()
{
	var home_lightbox = new frame_box({tema:'portada'});
	home_lightbox.abrirLightBox('?opcion=inicio&a=light_box');
	
}

function mostrarAlertaRegistro(msj)
{
	var Contenido = "<div id='msj_registro'><a href='?opcion=registro' onclick='' ><img src='imagenes/alerta_registro/bg_alert.png'></img></a></div>";
	var detalle_combo = new light_box({lightBoxOpacity:0.7, tema:'alerta_registro' });
	detalle_combo.abrirLightBox(Contenido);
}

function popup(page, height, width)
{
  var url = page;
  var h = height;
  var w = width;
  var left=(screen.width)?(screen.width - width)/2:100;
  var top=(screen.height)?(screen.height - height)/2:100;

 var popup = window.open(url,'popUp','toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=no,width=' + w + ',height=' + h + ',left='+left+',top='+top);
 popup.focus();
}
function terminos()
{
	popup("php/html/terminos.html", screen.height-150 , 570);
}
function mostrarScroll(content,scrollbar,handle,horizontal,ignoreMouse){
		
		var steps = (horizontal?(content.getScrollSize().x - content.getSize().x):(content.getScrollSize().y - content.getSize().y))
		var slider = new Slider(scrollbar, handle, {	
			steps: steps,
			mode: (horizontal?'horizontal':'vertical'),
			onChange: function(step){
// Scrolls the content element in x or y direction.
				var x = (horizontal?step:0);
				var y = (horizontal?0:step);
				content.scrollTo(x,y);
			}
		}).set(0);
		if( !(ignoreMouse) ){
			// Scroll the content element when the mousewheel is used within the 
			// content or the scrollbar element.
			$$(content, scrollbar).addEvent('mousewheel', function(e){	
				e = new Event(e).stop();
				var step = slider.step - e.wheel * 30;	
				slider.set(step);					
			});
		}
		// Stops the handle dragging process when the mouse leaves the document body.
		$(document.body).addEvent('mouseleave',function(){slider.drag.stop()});
}

var enviandoComentario = false;
function validarComentario(){
	
	if(!enviandoComentario)
	{
		enviandoComentario = true;
		var comentario = $('comentario').value;
		if (comentario == ''){
			mostrarAlerta("Debe escribir un comentario");
			enviandoComentario = false;
		}else {
			//$('loading').setStyle ('visibility', 'block');
			$('form_coment').set('send', { method: 'post',evalScripts:true, onComplete: resp_form});
			$('form_coment').send();
			return false;
		}
	}
	return false;
}
function validarReceta(){
	var texto = /^\S/;
	var imagen = $('img_receta').value;
	var titulo = $('form_titulo').value;
	var ingredientes = $('ingredientes_form').value;
	var preparacion = $('preparacion_form').value;
	var apublicacion = $('apublicacion');
	if (!(titulo.match(texto))|| titulo == ''){
		mostrarAlerta('Debe escribir un titulo');
		$('form_titulo').focus();
		return false;
	}else if (!/(\.jpg|\.jpeg|\.png|\.gif)$/i.test(imagen)){
		mostrarAlerta(copies.foto_receta);
		return false;
	}else if (!(ingredientes.match(texto))){
		mostrarAlerta(copies.ingredientes_receta);
		$('ingredientes_form').focus();
		return false;
	}else if (!(preparacion.match(texto))){
		mostrarAlerta(copies.preparacion_receta);
		$('preparacion_form').focus();	
		return false;
	}else if (apublicacion.checked == false){
		mostrarAlerta(copies.apublicacion);
		$('apublicacion').focus();
		return false;
	}else{
		//$('loading').setStyle ('visibility', 'block');
		$('form_coment').submit();
	}
	return false;
}
function validarBusqueda(){
	var texto = /^\S/;
	var buscar = $('buscar').value;
	
	if (!(buscar.match(texto))|| buscar == ''){
		mostrarAlerta('Debe escribir un criterio de busqueda');
		$('buscar').focus();	
		return false;
	}else
		return true;
	return false;
}
function resp_form(){
	//$('loading').setStyle ('visibility', 'hidden');
	$('form_coment').reset();
	enviandoComentario = false;
}
function calcular_edad(ano, mes, dia){

    var fechaActual = new Date();
    var numeros = /^([0-9])*$/;
	
	if (!(dia.match(numeros))){
		return 0;
	}
	if (!(ano.match(numeros))){
		return 0;
	}
	if (!(mes.match(numeros))){
		return 0;
	}
    edad=fechaActual.getFullYear()- ano - 1; //-1 porque no se si ha cumplido años ya este año
	//si resto los meses y me da menor que 0 entonces no ha cumplido años. Si da mayor si ha cumplido
    if (fechaActual.getMonth() + 1 - mes < 0)
		return edad		
		 
	if (fechaActual.getMonth() + 1 - mes > 0)
       return edad+1

	if (fechaActual.getUTCDate() - dia >= 0)
       return edad + 1
	
    return edad
} 

function addOverImg(container_carrusel, src)
{
	//alert (src);
	var fotos_total = $$('#'+container_carrusel+' .panel .infinito')[0].getChildren().length;
	var imagen = "";
	
	for (i=1; i<=fotos_total; i++){
		var imagen = $('marca_'+i).src.substring(src.length);
		if (imagen.substring(0,5) != "over_"){
			$('marca_'+i).addEvents({
				'mouseenter':overImagen.bindWithEvent(this, Array(i,src,imagen)),
				'mouseleave':outImagen.bindWithEvent(this,Array(i,src,imagen))
			});
		}	
	}	
}

function overImagen(obj,id,src,img){
	$('marca_'+id).src = src+"over_"+img;
}

function outImagen(obj, id,src, img){
	$('marca_'+id).src = src+img;
	//alert ($('marca_'+id).src);
}
function perfil_over(id){
	$('imagen'+id).setStyle('width', '319px');
	$('titulo_img'+id).setStyle('display', 'block');
	//tween('imagen'+id, 'width', '319px', 500);
}
function perfil_out(id){
	$('imagen'+id).setStyle('width', '110px');
	$('titulo_img'+id).setStyle('display', 'none');
	//tween('imagen'+id, 'width', '110px', 500);
}
function over(id, home){
	$('menu_'+id).src="imagenes/home_f2/menu/btn_"+id+"_over"+home+".png";
}
function out(id , home){
	$('menu_'+id).src="imagenes/home_f2/menu/btn_"+id+home+".png";
}
function over_recetas(id, activo){
	if(activo == "" )
		$('menu_recetas_'+id).src="imagenes/cocina2/nav_tipo"+id+"_over.jpg";
}
function out_recetas(id, activo){
	if(activo == "" )
		$('menu_recetas_'+id).src="imagenes/cocina2/nav_tipo"+id+".jpg";
}
function ocultar_titulos(cont){
	for(var i = 1; i<= cont; i++){
			$('titulo_img'+i).setStyle('display', 'none');	
		}
}
function rotarFondo(p,cant){
	perfil = p;
	total_imagenes = cant;
		$('header_tipo_seccion').setStyle('background-image', 'url(imagenes/home_f2/banner/'+perfil+"_"+fondo+'.jpg)');
		//$('banner_home').src = 'imagenes/home_f2/banner/'+perfil+"_"+fondo+'.jpg)';
	tween('header_tipo_seccion', 'opacity', '1', 1000);
	
	if(perfil > 2 || perfil == 0)
		setTimeout ("setPerfil();", 5000);
	
}
function setPerfil(){
/*	tween('header_tipo_seccion', 'opacity', '0',1000);
	$('header_tipo_seccion').setStyle('background-image', 'url(imagenes/home_f2/banner/'+perfil+"_"+fondo+'.jpg)');
	tween('header_tipo_seccion', 'opacity', '1', 1000);
*/	tween('header_tipo_seccion', 'opacity', '0', 300);
	if(fondo == total_imagenes)
		fondo = 1;
	else
		fondo++;
	//rotarFondo(perfil,total_imagenes);
	setTimeout ("rotarFondo('"+perfil+"','"+total_imagenes+"');", 300);
}

function setFondoMenu(){
	$('titulo_seccion').setStyle('opacity', '0');
	$('btns_menu').setStyle('opacity', '1');
}
function perfilar(uid){
	$('header_tipo_seccion').setStyle('background-image', 'url(imagenes/home_f2/banner/'+uid+'.jpg)');
	//$('banner_home').src = 'imagenes/home_f2/banner/'+uid+"_"+fondo+'.jpg)';
	// $('header_tipo_seccion').setStyle('z-index', '100');
}

function tween(id, estilo, valor, duracion){
	var over = $(id);
	var morph = over.get('morph', {link: 'cancel', duration: duracion, transition: Fx.Transitions.Sine.easeIn});
	morph.start({'opacity': [over.getStyle(estilo), valor]});
}
function shareFB(url){
    var surl = "http://www.facebook.com/sharer.php?u="+encodeURIComponent(url);
	popup(surl, 300, 756);
}
function verMas(id){
	if(ver_mas == 'false'){
		ver_mas = 'true';
		//tween(id, 'visibility', 'none', 5000);
		$(id).setStyle('height', '0');
		$(id).setStyle('display', 'none');
		$('img_ver_mas').src = 'imagenes/que_es_noel/historia/btn_ver_mas.jpg';
	}else{
		ver_mas = 'false';
		$(id).setStyle('height', 'auto');
		$(id).setStyle('display', 'block');
		$('img_ver_mas').src = 'imagenes/que_es_noel/historia/btn_ver_mas2.jpg';
		//$(id).setStyle('display', 'block');
		//tween(id, 'display', 'block', 5000);
	}
}
function over_btn_mas(ifover){
	if(ifover == '1'){
		if(ver_mas == 'false'){
			$('img_ver_mas').src = 'imagenes/que_es_noel/historia/btn_ver_mas_over2.jpg';
		}else
			$('img_ver_mas').src = 'imagenes/que_es_noel/historia/btn_ver_mas_over.jpg';
	}else{
		if(ver_mas == 'false'){
			$('img_ver_mas').src = 'imagenes/que_es_noel/historia/btn_ver_mas2.jpg';
		}else
			$('img_ver_mas').src = 'imagenes/que_es_noel/historia/btn_ver_mas.jpg';
	}	
}
function verFormCommentario(){
	$('contenedor_formulario').setStyle('display', 'block');
	tween('contenedor_texto', 'opacity', '0', 1000);
	tween('contenedor_formulario', 'opacity', '1', 1000);
	$('contenedor_texto').setStyle('display', 'none');
}
function ocultarFormCommentario(){
	$('contenedor_texto').setStyle('display', 'block');
	tween('contenedor_formulario', 'opacity', '0', 1000);
	tween('contenedor_texto', 'opacity', '1', 1000);
	$('contenedor_formulario').setStyle('display', 'none');
}
function cambiarComentario(){
	request("inicio&a=banner_pregunta", "contenido_comentarios");
}
function request(url_request,div)
{	
	new Request(
			{
			method: 'get',
			url: 'index.php?opcion='+url_request,
			onSuccess: function(texto){
				$(div).tween("opacity", "0", "1000");	
				$(div).innerHTML = texto;
				$(div).tween("opacity", "1", "2500");
			},
			evalScripts:true
			}).send();
}
var post_actual = 1;
function setPost(cant){
	post_actual = 1;
	if(cant == '0'){
		return;
	}	
	try{
		$('post_'+post_actual).setStyle('display', 'block');
		$('flecha_atras').setStyle('display', 'none');
	  }
	catch(err){
		//Handle errors here
	  }
	
	//mostrarScroll( $('posts_container'), $('scrollbar_post'), $('handle_post'), false);
}
function anteriorPost(total){
	if(post_actual > 1){
		$('post_'+post_actual).tween("opacity", "0", "1000");
		$('post_'+post_actual).setStyle('display', 'none');
		post_actual -= 1;
		$('post_'+post_actual).setStyle('display', 'block');
		$('post_'+post_actual).tween("opacity", "1", "3000");
		
		if(post_actual == 1){
			$('flecha_atras').setStyle('display', 'none');
		}
		$('flecha_delante').setStyle('display', 'block');
	}
}
function siguientePost(total){
	if(post_actual < total){
		$('post_'+post_actual).tween("opacity", "0", "1000");
		$('post_'+post_actual).setStyle('display', 'none');
		post_actual += 1;
		$('post_'+post_actual).setStyle('display', 'block');
		$('post_'+post_actual).tween("opacity", "1", "3000");
		
		if(post_actual == total){
			$('flecha_delante').setStyle('display', 'none');
		}
		$('flecha_atras').setStyle('display', 'block');
	}
}
