/* Runs when the DOM is loaded. */
$(document).ready(function(){
  bindEvents();
 // loadGoogleMapsScript();
  setPlaceholders();
  setStars();
});

// Bind event handlers.
function bindEvents() {

  /* Link to toggle comment form on product page. */
  $('#comment-toggle').bind('focus', function(){
    $(this).attr('rows', 4);
    $('#full-form').addClass('visible');
    return false;
  });

  /* Link to toggle search autocomplete popout. */
  $('#product_name').bind('click', function(){
    $('#product_autocomplete').toggle();
    return false;
  });

  /* Link to toggle search autocomplete popout. */
  $('#search_term').bind('focus', function(){
    $('#search_autocomplete').toggle();
    return false;
  });

  /* Link to toggle login form. */
  $('#user.loggedout a#login_link').bind('click', function(){
    $(this).parent().toggleClass('foldin')
    $('#login').toggle();
    return false;
  });

  /* Link to toggle foldout. */
  $('#article a.foldout').bind('click', function(){
    $(this).toggleClass('foldin')
    $('#foldout_articles').toggle();
    return false;
  });

  /* Bind events for recipe search. */
  $('#recipes_search ul.tags, #recipes_categories ul.tags').not('.radio').find('li').bind('click', function(){
    $(this).toggleClass('active');
    return false;
  });
  $('#recipes_search ul.tags.radio li, #recipes_categories ul.tags.radio li').bind('click', function(){
    $(this).addClass('active').siblings().removeClass('active');
    return false;
  });

  /* Bind events to remove tags. */
  $('ul.tags.remove a[class!="add"], ul.tags.remove label').append('<span class="close"></span>').bind('click', function(){
    $(this).closest('li').remove();
    return false;
  });

  /* Make the "add" link open a lightbox. */
  $('ul.tags.remove a.add').bind('click', function(){
    $('#recipes_categories').beansbox();
    return false;
  });

  /* Make the "share" link open a popup. */
  $('.secnav li.share a').bind('click', function(){
    $(this).addClass('active');
    $('#social_sharing').show();
    return false;
  });

  /* "Close" link in social sharing popup. */
  $('#social_sharing a.close').bind('click', function(){
    $('.secnav li.share a').removeClass('active');
    $('#social_sharing').hide();
    return false;
  });

  /* Like > thank you message - popup and fadeOut within 3seconds */
  $('.secnav li.like a').click(function(){
    $(this).next('#like_feedback').show().delay(3000).hide(0);
    return false;
  });

  /* Mein Tegut: "Favoriten" Popup */
  $('.activities li a.favorites').click(function(){
    $('#favorites').show();

    $('#favorites a.close').click(function(){
       $('#favorites').hide();
       return false;
     });
    return false;
  });

  /* Mein Tegut: "Comments" Popup*/
  $('.activities li a.comments').click(function(){
    $('#comments').show();

    $('#comments a.close').click(function(){
       $('#comments').hide();
       return false;
     });
    return false;
  });

  /* Mein Tegut: "more markets" Popup*/
  $('.markets li.more a').click(function(){
    $('#more_markets').show();

    $('#more_markets a.close').click(function(){
       $('#more_markets').hide();
       return false;
     });
    return false;
  });

}

function setStars() {
  var stars = $('div.popup ul.stars');
  if (!stars.length) return false;

  stars.find('a').each(function(){
    var ul = $(this).closest('ul.stars');
    $(this).mouseover(function(){
      ul.removeClass('stars_1');
      ul.removeClass('stars_2');
      ul.removeClass('stars_3');
      ul.removeClass('stars_4');
      ul.removeClass('stars_5');
      ul.addClass('stars_' + $(this).attr('data-stars'));
      $('#stars_description').html($(this).attr('data-description'));
    }).mouseout(function(){
      ul.removeClass('stars_1');
      ul.removeClass('stars_2');
      ul.removeClass('stars_3');
      ul.removeClass('stars_4');
      ul.removeClass('stars_5');
      $('#stars_description').html('');
    });
  });
}

// Mimic the placeholder behaviour using JS.
function setPlaceholders() {
  $('input[placeholder]').each(function(){
    var placeholder_text = $(this).attr('placeholder');
    $(this).attr('placeholder', false);

    if($(this).val() == '') {
      $(this).val(placeholder_text).addClass('placeholder');
    }

    $(this).focus(function() {
      if ($(this).val() == placeholder_text) {
        $(this).val('').removeClass('placeholder');
      }
    });

    $(this).blur(function() {
      if ($(this).val() == '') {
        $(this).val(placeholder_text).addClass('placeholder');
      }
    });
  });
}

//// Add Google Map.
//function initialize() {
//  if (!$('#map').length) return false;
//
//  var latLng = new google.maps.LatLng(48.5911, 9.2223);
//  var options = {
//    zoom: 15,
//    center: latLng,
//    zoomControl: true,
//    streetViewControl: false,
//    mapTypeControl: false,
//    mapTypeId: google.maps.MapTypeId.ROADMAP
//  }
//  var map = new google.maps.Map(document.getElementById('map'), options);
//}
//
//// Loads Google Maps script.
//function loadGoogleMapsScript() {
//  var script = document.createElement('script');
//  script.type = 'text/javascript';
//  script.src = 'http://maps.google.com/maps/api/js?sensor=false&callback=initialize';
//  document.body.appendChild(script);
//}
