$(document).ready(function() {
  
  $('.bookmark-lnk').live('click',function(e) {
    e.preventDefault();
    $.getJSON($(this).attr('href'), function(json) {
      
    });
    $(this).toggleClass('bookmarked');
  });

  $('.reply-lnk').live('click',function(e) {
    
    e.preventDefault();
    
    var tweet = $(this).parents('dd:first'); 
    

    if(tweet.length == 0) {
      tweet = $('.feed');
    
      $('.feed form.reply').toggle();
      
         
    } else 
    {
      if(tweet.hasClass('reply')) {
        tweet.find('form.reply').toggle();
        return;
      }
      var replies = tweet.find('.replies');
     
      tweet.find('.retweet-form').hide();
      if(replies.is(":hidden")) {
        tweet.find('form.reply:first').show();
        replies.slideDown('fast');
        
      } else
      {
        tweet.find('form.reply:first').toggle();
       
      }
      
    } 
  });
  
  
  $('form.tweetstyle').keyup(function(e) {
 
    var clength = $(this).find('textarea').val().length;
    $(this).find('.char-count').html(clength);
    // if(clength > 140) {
    //       $('#tweet-form').addClass('invalid');
    //     } else {
    //       $('#tweet-form').removeClass('invalid');
    //     }
  });

  $('.feed dd').live('mouseover',function(e) {
    
    $(this).children('.body').find('.menu').css({'visibility':'visible'});
  });
  $('.feed dd').live('mouseout', function() {
    $(this).children('.body').find('.menu').css({'visibility':'hidden'});
  });
  

  $('#right .avatar').qtip({
    show:'mouseover',
    hide:'mouseout',
    position: {
      corner: {
        target: 'rightTop',
        tooltip: 'leftBottom'
      }
    },
    style: {
      padding: 10,
      fontSize: '14px',
      border: {
       width: 3,
       radius: 7,
       color: '#6699CC'
     }
    }
  })
  $('.help-lnk').qtip({
    //content:$(this).parent('.plan').find('.description').text(),
    show:'mouseover',
    hide:'mouseout',
    position: {
      corner: {
        target: 'rightTop',
        tooltip: 'leftBottom'
      }
    },
    style: {
      padding: 10,
      fontSize: '14px',
      border: {
       width: 3,
       radius: 7,
       color: '#6699CC'
     }
    }
  });
  var auto_update = {}
   if($('#auto-updater').length == 1) {
     
     auto_update = setInterval(autoUpdate, 10000)
   }
  
  $('form.reply').submit(function(e) {
    e.preventDefault();
    $(this).hide();
    $.post(this.action, $(this).serialize(), null, "script");
    // return this
  });
  
  $('.replies-lnk').live('click',function(e) {
    e.preventDefault();
    $(this).parents('dd').find('.replies').slideToggle('fast');
  });
  
  $('.add-tweet-lnk').live('click',function(e) {
    e.preventDefault();
    
    if($("#new-tweet").css('display') == 'none') {
      $('#new-tweet').show();
      $('#tweet_body').focus();
      
    } else
    {
      $('#new-tweet').slideUp('fast');
    }
    
  });
  
  $('.retweet-lnk').live('click',function(e) {
    
    e.preventDefault();
    var form = $(this).parents('dd:first').find('.retweet-form');
    
    if(form.is(':hidden')) {
      form.show();
      $(this).parents('.tweet').find('form.reply').hide();
    } else {
      form.hide();
    }
    
  });
  
  $('.event-details .sub-tabs li a').click(function(e) {
    e.preventDefault();
    $('.sub-tabs li.selected').removeClass('selected');
    $(this).parent().addClass('selected');
    $('#left .content').html("<div style='clear:both;padding:1em 0 0 0'><img src='/images/ajax-loader.gif' /></div>" )
    $.get($(this).attr('href'), null, null, 'script');
  })

  
  
  $('#new-special input').keyup(function(e) {
    $('#right .special .name a').html($('#special_name').val());
    $('#right .special .url').html($('#special_url').val());
  });
  
  $('#special_description').keyup(function(e) {
    $('#right .special .description a').html($('#special_description').val());
  });
  
  $('#image_uploaded_data').change(function(e) {
    e.preventDefault();
    $('#new-special').ajaxSubmit({
        url:"/preview-special",
        iframe:true,
        success:function(resp, form) {
          $('#image_removed').val(0);
          $('#right .special').replaceWith(resp);
          $('#image_id').val($('#right .special img').attr('id'));
        }
    });
  });
  
  $('#image_url').blur(function(e) {
    e.preventDefault();
    $('#new-special').ajaxSubmit({
        url:"/preview-special",
  
        success:function(resp, form) {
          $('#image_removed').val(0);
          $('#right .special').replaceWith(resp);
          $('#image_id').val($('#right .special img').attr('id'));
        }
    });
  });
  
  $('.remove-img-lnk').click(function(e){
    e.preventDefault();
    $('#image_removed').val(1);
    $('#image_url').val('');
    $('#image_uploaded_data').val('');
    $('#image_id').val('');
    $('#right .special img').remove();
  });
  
  $('.upload-style-lnk').click(function(e) {
    e.preventDefault();
    $(this).parent('div').hide();
    $(this).parent('div').find('input').val('');
    $($(this).attr('href')).show();
  })
  
  
  $("#right .tabs a").click(function(e) {
    e.preventDefault();
    var tbd = $($(this).attr('href'));

    tbd.siblings().hide();
    tbd.show();
  });
  
  $('#event_end_date').datepicker({minDate:0});
  $('#event_start_date').datepicker({
    minDate:0,
    onSelect:function(dt,p) {
      $('#event_end_date').datepicker('option','minDate',$.datepicker.parseDate('mm/dd/yy',dt))
    }});
  
  $('#get-featured table td a').live('click',function(e){
    e.preventDefault();
    jQuery.ajaxSetup({
      data: { authenticity_token : AUTH_TOKEN }
    })
    $.post($(this).attr('href'), {}, null, "script");
  });
  
  $('#cart a.cancel').live('click', function(e) {
    e.preventDefault();
    $.post($(this).attr('href'), {}, null, "script");
  }); 
  // setFeatured();
  
  // $('#categories li a').click(function(e) {
  //     e.preventDefault();
  //     $('#categories').selectTab($(this).parent());
  //     $('.tags').hide();
  //     var cat = $($(this).attr('href'));
  //     if(cat.length == 1) {
  //       
  //       cat.fadeIn('fast');
  //     } else {
  //       $.get($(this).attr('href'), {}, null, "script");
  //     }    
  //     // $.get($(this).attr('href'), {}, null, "script");
  //   });
  

  
  $('#bookmarks-only').click(function(e) {
    $.get($(this).attr('href'), {}, null, "script");
  });
  
  $('#promo_type_image').change(function(e) {
    $('#text-form').hide();
    $('#image-form').show();
  });
  
  $('#promo_type_text').change(function(e) {
    $('#image-form').hide();
    $('#text-form').show();
  });
  
  $('.event-details .content .sorting-options a').live('click', function(e) {
    e.preventDefault();
    $('.event-details .content .sorting-options .selected').removeClass('selected');
    $(this).parent().addClass('selected');
    $.get($(this).attr('href'), {}, null, "script"); 
  });
  
  $('.event-details .content .pagination a').live('click', function(e) {
    e.preventDefault();
    $('.event-details .content .pagination .current').removeClass('current');
    $(this).parent().addClass('current');
    $.get($(this).attr('href'), {}, null, "script"); 
  });
  
  $('.wine .content .pagination a').live('click', function(e) {
    e.preventDefault();
    $('.wine .content .pagination .current').removeClass('current');
    $(this).parent().addClass('current');
    $.get($(this).attr('href'), {}, null, "script"); 
  });
  
  setHeadlines();
});

jQuery.ajaxSetup({ 
  'beforeSend': function(xhr) {xhr.setRequestHeader("Accept", "text/javascript")} 
})

$.fn.extend({
  slideToggle:function(speed, callback1, callback2) {
    if($(this).is(':hidden')) {
      $(this).slideDown(speed, callback1);
     
    } else {
      $(this).slideUp(speed);
    }
  },
  
  selectTab:function(el) {
    this.children('.selected').removeClass('selected');
    $(el).addClass('selected');
  }
  


});


function autoUpdate() {

  $.get($('#auto-updater').attr('href'), {}, null, "script");
}

function setHeadlines() {
  var slideshow_delay = 10000;
  if($('#headlines').length == 0) {return false;}
  var timer = setInterval(nextHeadline, slideshow_delay);
  $('#headlines').data('timer', timer);
  
  $('#headlines').data('current',$($('#headlines ul .current a').attr('href')) );
  $('#headlines').data('current').show();
  
  $('#headlines ul li.letter a').click(function(e) {
    e.preventDefault();
    clearInterval($('#headlines').data('timer'));
    var sec = $($(this).attr('href'));
    $('#headlines').data('current').hide();
    $('#headlines').data('current', sec);
    $(this).parents('ul').find('.current').removeClass('current');
    $(this).parents('li').addClass('current');
    sec.fadeIn('fast');
  });
  
  $('.pause-lnk').click(function(e) {
     e.preventDefault();
     $(this).parents('li').addClass('paused');
     clearInterval($('#headlines').data('timer'));
   });
  
  $('.play-lnk').click(function(e) {
    e.preventDefault();
    $(this).parents('li').removeClass('paused');
    var timer = setInterval(nextHeadline, slideshow_delay);
    $('#headlines').data('timer', timer);
  });
}

function nextHeadline() {

  var next = $('#headlines').data('current').next();
  if(next.length == 0) {
    next = $('#headlines dd:first')
  }
  $('#headlines').data('current').hide();
  $('#headlines').data('current', next);
  $('#headlines').data('current').fadeIn('slow');

  var ct = $('#headlines ul').find('.current');
  ct.removeClass('current');
  if(ct.next('.letter').length == 0) {
    $('#headlines ul li:first').addClass('current');
  } else {
    ct.next('.letter').addClass('current');
  }
  
  
  
}
