/* MAP RESULTS
------------------------------------------------------------------ */
function mapResultsInit(marker_list) {
  jQuery('#map_results').map_results(marker_list);
  jQuery(document).bind('map:paneOpened', onPaneOpened);
  jQuery('.place_list a.locate').bind('click', onPaneOpen);
}

// Pane Opened
function onPaneOpened(e, marker) {
  jQuery('.place_list a.locate').removeClass('on');
  jQuery('#marker_'+marker.id).addClass('on');
}

// Pane Open
function onPaneOpen(e) {
  e.preventDefault();
  var map_offset = jQuery('.map').offset().top;
  var win_scroll = jQuery(window).scrollTop();
  if (win_scroll > map_offset) {
    jQuery('html,body').animate({scrollTop: map_offset}, 200);
  }
  marker_index = jQuery(e.target).attr('id').replace('marker_', '');
  jQuery(document).trigger('map:paneOpen', [marker_index])
}

// Pane Close
function paneClose() {
  jQuery(document).trigger('map:paneClose');
}


/* BUSINESS
------------------------------------------------------------------ */
var Business = {
  init: function() {
    this.setDescription();
  },
  
  setDescription: function() {
    show_height = 99;
    speed = 200;
    desc = jQuery('body.business_detail .content .description');
    
    if (desc.height() > show_height+50) {
      desc.orig_height = desc.height();
      desc.css({'overflow': 'hidden', 'height': show_height+'px'});
      desc.after('<p class="more_description"><a href="#">More description</a></p>');

      jQuery('.more_description a').bind('click', function(e) {
        e.preventDefault();
        if (desc.orig_height > desc.height()) {
          desc.animate({'height': desc.orig_height+'px'}, speed);
          jQuery(this).html('Less description');
        }
        else {
          desc.animate({'height': show_height+'px'}, speed);
          jQuery(this).html('More description');
        }
      });
    }
  }
};


/* HOURS
------------------------------------------------------------------ */
var Hours = {
  init: function() {
    jQuery('.hours > ul > li > a').bind('click', this.click);
    jQuery('.hours_disabled > ul > li > a').unbind('click', this.click);
  },

  click: function(e) {
    e.preventDefault();
    target = jQuery(this);
    target_ul = target.parent().find('ul');
    if (target_ul.css('display') == 'none') {
      target_ul.slideDown(300);
      target.parent().addClass('on');
    }
    else {
      target_ul.slideUp(300);
      target.parent().removeClass('on');
    }
  }
};


/* AUTH
------------------------------------------------------------------ */
var Auth = {
  login: function() {
    if (!jQuery('#xhr_login').length) {
      jQuery('#body .content').append('<div id="xhr_login"></div>');
      var div = jQuery('#xhr_login');
      div.hide().load('/accounts/login/ .login_form', function() {
        div.find('.login_form').append('<input type="hidden" name="next" value="'+window.location+'">')
        div.fadeIn();
      });
    }
    return false;
  }
};


/* RATING
------------------------------------------------------------------ */
var Rating = {
  init: function() {
    jQuery('.rate_form button').bind('click', this.click);
    this.form_stars();
  },

  click: function(e) {
    e.preventDefault();
    var button = jQuery(this);
    var form = button.parents('form.rate_form');
    var value = button.attr('value');
    var buttons = form.find('li button');

    if (form.hasClass('is_anonymous')) {
      Auth.login();
    }

    form.find('li').removeClass('user_on').removeClass('on');
    for (var i=0; i<buttons.length; i++) {
      jQuery(buttons[i]).val(i+1);
    }

    serialized = form.serialize()+'&value='+value;
    jQuery.post(form.attr('action'), serialized);

    if (value != 0) {
      button.val(0);
      button.parent().addClass('user_on');
      button.parent().prevAll('li').addClass('user_on');
    }
  },
  
  form_stars: function() {
    li = jQuery('.review_form ul.stars li');
    li.find(' input').hide();
    li.append('<span></span>');
    li.find('span').click(function(e) {
      target = jQuery(this);
      input = target.siblings('input');
      input.attr('checked', 'checked');

      li.removeClass('user_on');
      target_li = target.parent();
      target_li.addClass('user_on');
      target_li.prevAll().addClass('user_on');
    });
  }
};


/* REVIEW
------------------------------------------------------------------ */
var Review = {
  init: function() {
    jQuery('.review_list .comment_form, .review_list .login_form, .review_list .signup_form').hide();
    jQuery('.review_list .reply').click(function(e) {
      e.preventDefault();
      link = jQuery(this);
      div = link.parent().parent().parent();
      div.find('.comment_form, .login_form, .signup_form').slideToggle(200);
    })
  }
};


/* TABS
------------------------------------------------------------------ */
jQuery.fn.tabs = function() {
  return this.each(function() {
    var div = jQuery(this);
    var tabs = div.find('> ul li');
    var content = div.find('> div');

    content.hide();

    // Make tabs clickable
    tabs.find('a').bind('click', function(e) {
      e.preventDefault();
      activate(jQuery(this).attr('href'));
    });

    // If the URL has a hash try to turn its tab on,
    // otherwise turn the first tab on.
    if (window.location.hash) {
      activate(window.location.hash);
    }
    else {
      activate_first();
    }

    function activate_first() {
      first_tab = tabs.find('a:first').attr('href');
      activate(first_tab);
    }

    // Logic to activate a tab
    function activate(link) {
      var tab = div.find('[href="'+link+'"]').parent();
      if (tab.length > 0) {
        tabs.removeClass('on');
        tab.addClass('on');

        content.hide()
        div.find(link).show();
      }
      else {
        activate_first();
      }
    }
  });
}


/* VOTE
------------------------------------------------------------------ */
var Vote = {
  count: null,

  init: function() {
    jQuery('.vote_form .up, .vote_form .down').bind('click', this.click);
  },

  click: function(e) {
    e.preventDefault();
    target = jQuery(this);
    p = target.parent();
    form = p.parent();
    value = target.attr('value');

    Vote.count = target.parent().find('.count');

    if (form.hasClass('is_anonymous')) {
      window.location = '/accounts/login/?next='+window.location.pathname
    }

    switch(value) {
      case '1':
        p.removeClass('voted_down').addClass('voted_up');
        p.find('.up').attr('value', '');
        p.find('.down').attr('value', 0);
        break;
      case '-1':
        p.removeClass('voted_up').addClass('voted_down');
        p.find('.up').attr('value', 0);
        p.find('.down').attr('value', '');
        break;
      case '0':
        p.removeClass('voted_up').removeClass('voted_down');
        p.find('.up').attr('value', 1);
        p.find('.down').attr('value', -1);
        break;
    }

    if (value) {
      serialized = form.serialize()+'&value='+value;
      jQuery.post(form.attr('action'), serialized, Vote.post, 'json');
    }
  },

  post: function(response) {
    score = response.score
    if (response.score > 0) { score = '+'+score }
    Vote.count.text(score);
  }
};


/* INIT
------------------------------------------------------------------ */
jQuery(function() {
  Rating.init();
  Review.init();
  Business.init();
  Hours.init();
  Vote.init();

  jQuery('.select').uiSelect();
  jQuery('.tab_widget').tabs();
});