<!--

/*
 * Newsletter
 */
function acrescenta( ) {
    els = $$('#ingredientes ul');

    reg = new RegExp( els.length - 1, "g" );
    $( 'ingredientes' ).insert( '<ul>' + els.pop( ).innerHTML.replace( reg,  els.length ) + '</ul>' );

    return false;
}

function processarNewsletter ( ) {
	$('nwait').removeClassName('wait');

	new Ajax.Request( '/subscribing', {
		method:'post',
		parameters: $('frmNewsletter').serialize( true ),

		onSuccess: function(transport){

		var strError = transport.responseXML.getElementsByTagName('error')[0].childNodes[0];
    if (( strError.data != '' )) {
    var tarea=document.createElement('textarea');
    tarea.innerHTML = strError.data.replace( /(\n)(.+)/g, "$2" );
    alert( "Erro: " + tarea.value );
 }
 else {
 alert( "Obrigado. Dentro de momentos receberá um email para confirmar a sua subscrição. Siga os passos indicados.");
 $( 'cxEmail' ).clear( );
 }
 $('nwait').addClassName ('wait');

 },

 onFailure: function( ) {
 alert( 'Ocorreu um erro. Tente novamente mais tarde...');
 }
 });
}

/*
 * Validations
 */

function isEmail(str) {
  var r1 = new RegExp("(@.*@)|(\\.\\.)|(@\\.)|(^\\.)");
  var r2 = new RegExp("^.+\\@(\\[?)[a-zA-Z0-9\\-\\.]+\\.([a-zA-Z]{2,3}|[0-9]{1,3})(\\]?)$");
  return (!r1.test(str) && r2.test(str));
}


/*
 * Recipe Pictures
 */

function changePicture(container, direction, nav) {
  var container;
  var e = $(container);
  if (e == null) return false;

  var imgs = $$('#'+ container +' img');
  if (imgs.length <= 1) return false;

  // first run
  if (typeof window[container] == 'undefined') {
    for (var i = 0; i < imgs.length; ++i) {
      if (imgs[i].style.display == 'block') {
        window[container] = i;
        break;
      }
    }
  }

  var container;
  var e = $(container);
  if (e == null) return false;

  var imgs = $$('#'+ container +' img');

  // first run
  if (typeof window[container] == 'undefined') {
    for (var i = 0; i < imgs.length; ++i) {
      if (imgs[i].style.display == 'block') {
        window[container] = i;
        break;
      }
    }
  }

  // apply direction
  if (direction == 'previous') {
    var tmp = window[container] - 1;
    if (tmp < 0) tmp = imgs.length - 1;
  } else {
    var tmp = window[container] + 1;
    if (tmp >= imgs.length) tmp = 0;
  }

  imgs[window[container]].fade({ duration: 0.5, from: 1, to: 0, queue: { position: 'end', scope: 'fotos'}});
  imgs[tmp].appear({ duration: 0.5, from: 0, to: 1, queue: { position: 'end', scope: 'fotos'}});
  
  // Controls
  if (typeof(nav) != 'undefined') {
    try {
      // height
      nav = $(nav);
      var h = imgs[tmp].readAttribute('height');
      if (h != null && parseInt(h) != parseInt(e.style.height)) {
        nav.hide({ queue: { position: 'end', scope: 'controls'}});
        nav.appear({ duration: 1.0, from: 0, to: 1, queue: { position: 'end', scope: 'controls'}});
        e.style.height = h + 'px';
      }
    } catch (e) {
    }
  }


  window[container] = eval(tmp);
  return false;
}

function showPrevious(container, nav) {
  changePicture(container, 'previous', nav);
  return false;
}

function showNext(container, nav) {
  changePicture(container, 'next', nav);
  return false;
}


/*
 * Top Menu
 */
function topMenu() {
  navRoot = document.getElementById("nav");
  for (i=0; i<navRoot.childNodes.length; i++) {
    node = navRoot.childNodes[i];
    if (node.nodeName == "LI") {
      node.onmouseover = function() {
        this.className+=" over";
      }
      node.onmouseout = function() {
        this.className=this.className.replace(" over", "");
      }
    }
  }
  
  // preload Images Here
  if (document.images) {
    var path     = 'http://imgs.sapo.pt/sabores/gfx/';
    var pictures = ['receitas_Hi.gif', 
                    'restaurantes_Hi.gif', 
					'aprender_Hi.gif', 
					'agora.gif', 
					'agora_Hi.gif', 
					'gourmet_pt_Hi.gif', 
					'gourmet_pt.gif',
					'foradecasa_Hi.gif', 
					'novidades_Hi.gif', 
					'vinho_cv_Hi.gif', 
					'vinho_Hi.gif',
					'gourmet_Hi.gif'];

    for (i = 0; i < pictures.length; i++) {
      var picture = [];
      picture[i]     = new Image(); 
	  picture[i].src = path + pictures[i];
    } 
  }
}

//-->

