function getUUID()
{
  return $('#uuid').val();
}

function getGameId()
{
  return $('#game_id').val();
}

function innerSearch(node, pat) {
  var skip = 0;
  if (node.nodeType == 3) {
    var pos = node.data.toUpperCase().indexOf(pat);
    if (pos >= 0) {
      skip = 1;
    }
  } else if (node.nodeType == 1 && node.childNodes && !/(script|style)/i.test(node.tagName)) {
    for (var i = 0; i < node.childNodes.length; ++i) {
      skip += innerSearch(node.childNodes[i], pat);
      //alert(skip);
    }
  }
  return skip;
}

function toggle_fields() {
  if($('#feedback_complain_false')[0] !== undefined && $('#feedback_complain_false')[0].checked === true) {
    $('#complain').hide();
  } else {
    $('#complain').show();
  }
}

function show_hide(items, item, parent)
{
  $(items).hide();
  $(items).parents().find(parent).hover(function () {
    $(this).children().find(item).show();
  }, function () {
    $(this).children().find(item).hide();
  });
}

//jgrowl announcements. ajax GET to hide ann/store cookie
$.jGrowl.defaults.closer = false;

function hide_announcements(){
  $.get(
    '/hide_announcements'
  );
  $("#announcements_box").fadeOut();
  return false;
}

$(document).ready(function() {
  $("#announcements_box").css("display","none");
  $("#announcements_box .announcement").each(function(){
    $.jGrowl($(this).html(),{ sticky:true, close:function(e,m,o){hide_announcements();} });
  });

  // Default facebox settings
  $.facebox.settings.opacity = 0.5;
  $.facebox.settings.loadingImage = '/images/facebox/loading.gif';
  $.facebox.settings.closeImage = '/images/facebox/closelabel.gif';

  // Barras de navegacion
  $("#navbar li a").mouseover(function(){
    //When mouse rolls over
    x = $(this).attr('rel')*90;

    $(this).css( { backgroundPosition: '-' + x + "px -102px" });

    $(this)
      .stop()
      .animate({
        marginTop: '-10px',
        paddingTop: '+84px'
       },{queue:false, duration:600, easing: 'easeOutBounce'});
  });

  $("#navbar li a").mouseout(function(){
    //When mouse is removed
    x = $(this).attr('rel')*90;
    $(this)
      .stop()
      .animate({
        marginTop: '0px',
        backgroundPosition: '-' + x + 'px 0px',
        paddingTop: '74px'
      },{queue:false, duration:600, easing: 'easeOutBounce'});
  });

  // Barra de busqueda
  $('#search_box #q').tips({ text: 'Buscar beneficios ...' });
  $('#user_phone_number').tips({ text: '011-4123-4567' });
  $('#user_cellphone_number').tips({ text: '011-1111-1111' });


  // Banners rotativos
  $('#news #cycle').cycle({
    pager: '.cycle_nav',
    pauseOnPagerHover: true,
    pagerEvent: 'mouseover'
  });

  $('#highlighted_games #cycle').cycle({
    pager: '.cycle_nav',
    pauseOnPagerHover: true,
    containerResize: false,
    fit: true,
    height: '263px',
    pagerEvent: 'mouseover'
  });

  // Busqueda de ganadores
  $('#winners .search_button').click(function () {
    v = $('#winners #search_winner').val().toUpperCase();
    if (v == '') {
      $('#winners ul li').show();
    } else {
      $('#winners ul li').hide();
      $('#winners ul').contents()
        .filter(function () {
          return innerSearch(this, v);
        }).show();
    }

    return false;
  });

  $('#stage_winners .search_button').click(function () {
    v = $('#stage_winners #search_winner').val().toUpperCase();

    if (v == '') {
      $('#stage_winners tr td').show();
    } else {
      $('#stage_winners tr td').hide();
      $('#stage_winners tr').contents()
        .filter(function () {
          return innerSearch(this, v);
        }).parent().children().show();
    }
    return false;
  });

  // Participaciones de usuarios no registrados
  $('#anonymous_participation_button').click(function() {
      $('#anonymous_participation_form').show();
  });

  // Formulario de sugerencia de beneficios
  $("#suggest a").facebox();
  $(document).bind('beforeReveal.facebox', function() {
    $('a.close').hide();
  });


  $(document).bind('reveal.facebox', function() {
    $("#close_suggest_form a").click(function () {
      $(document).trigger('close.facebox');
      return false;
    });

    $("form#new_recommendation").submit(function () {
      form = $(this);
      $.post(this.action + '.js', $(this).serialize(), function () {
        $(document).trigger('close.facebox');
      }, "script");

      return false;
    });
  });

  $('#feedback_complain_false').change(toggle_fields);
  $('#feedback_complain_true').change(toggle_fields);
  toggle_fields();

  $('#search_box #q').autocomplete(search_path, { minChars:3 });

  $("#images li .actions .delete").hide();
  $("#images li .actions .as_avatar").hide();
  $("#images li .actions .delete").parents().find("li").hover(function () {
    $(this).children().find(".delete").show();
    $(this).children().find(".as_avatar").show();
  }, function () {
    $(this).children().find(".delete").hide();
    $(this).children().find(".as_avatar").hide();
  });

  show_hide("#images li .actions .abuse", ".abuse", "li");
  show_hide(".comment .abuse", ".abuse", ".comment");
  show_hide(".comment .delete", ".delete", ".comment");
  show_hide(".comment .edit", ".edit", ".comment");

  $('#new_user_form').submit(function () {
    $.post(this.action + '.js', $(this).serialize());
    return false;
  });

 $().piroBox({
  my_speed: 300, //animation speed
  bg_alpha: 0.5, //background opacity
  slideShow : 'false', // true == slideshow on, false == slideshow off
  slideSpeed : 3, //slideshow
  close_all : '.piro_close' // add class .piro_overlay(with comma)if you want overlay click close piroBox
  });

  if ($('#comments').size() > 0) {
    $.bottomlessPagination({objName:'comentarios', ajaxLoaderPath:'/images/loadingAnimation.gif', results:'#comments .list'});
  }

  $('#theme input').click(function () {
    $('body').attr('class', $(this).val());
  });
  $("#tabs").tabs();
	$("#fb-accordion").accordion();
});
