document.observe("dom:loaded", init);

	var favoritos = [];

function init()
{
	if ($('platos')) {
		$('platos').childElements().each(function(li, index)
		{
			li.onclick = function() { return false; }

			li.observe('mouseover',mouseover);
			li.observe('mouseout',mouseout);
			li.observe('click',voto);
		});
	}


	if ($('voto-form')) {
		$('bt_enviar').onclick = function() {
			var valid;

			if (!$('nombre').present()) { $('infoNombre').update('<span class="alert">No puede quedar en blanco</span>') } else { $('infoNombre').update("") };
			if ($F('correo').search(/^[^@]+@[^@]+.[a-z]{2,}$/i) == -1) { $('infoCorreo').update('<span class="alert">ingresa una dirección de correo válida</span>') } else { $('infoCorreo').update("") };
			if (!$('ciudad').present()) { $('infoCiudad').update('<span class="alert">No puede quedar en blanco</span>') } else { $('infoCiudad').update("") };

			// are all fields valid
			valid = $('nombre').present() && ($F('correo').search(/^[^@]+@[^@]+.[a-z]{2,}$/i) != -1) && $('ciudad').present();

			if (valid) {
				$('voto-form').request({
					onComplete: function() {
						$('voto-form-wrapper').style.height = $('voto-form-wrapper').getHeight()+'px';
						$('voto-form-wrapper').update('<div style="font-size:120%; text-align:center; margin-top:50px;"> <img src="img/Checked.png" style="margin-bottom:20px;" /><br>Gracias por votar, <br> Visita <a href="#" onclick="window.parent.document.location.replace(\'/\')" style="text-decoration:none;">7maravillasgastronomicas.com</a> y sigue apoyando<br> a tu favorito</div>');
						new Effect.Opacity('voto-form-wrapper', { from: 0, to: 1 });
						//$('voto-form-wrapper').morph('height:150px;');
					}
				})
				$('bt_enviar').disable();
				return true
			}
			return false
		}
	}

	if ($('bt_confirmar')) {
		$('bt_confirmar').onclick = function()
		{
			if(favoritos.size() < 7){ //menos de 7
				alert('Aún te quedan '+(7-favoritos.size())+' plato(s) por elegir')
			}
			else	{
				$('favs').value = favoritos.join(',');
				$('favs-form').submit();
			}

		}
	}
}

function mouseover(event){
  element= event.element();
  ancho  = element.getWidth();
  alto   = element.getHeight();
	offset = element.cumulativeOffset();
  posX = offset.left + ancho - 70;
  posY = offset.top + alto;
  $('tip').setStyle({left:posX+'px',top:posY+'px'});
  $('thumb').src = element.rel;
  $('enlace').href = element.href;
  $('tip').show();
}
function mouseout(event){
  element = event.element();
  //element.setStyle({ backgroundColor:'#fff'});
}

function voto(event){
	element = event.element();

	plato = element.getAttribute('_index');
	if (favoritos.indexOf(plato) < 0) { //no esta
		if(favoritos.size() < 7){ //añado uno nuevo
			favoritos.push(plato)
			element.setStyle({ backgroundColor:'#FF9C59', color:'#ffffff'}); //poner estilo
		}
		else{ //no puedo añadir mas
				alert('solo puedes elegir 7 platos')
		}
	}
	else{ //esta
		favoritos = favoritos.without(plato)
		element.setStyle({ backgroundColor:'#F1F5FA', color:'#666666'}); //quitar estilo
	}
}



