$(function() {
  /**
   * Entries slider
   */
  if($("#slider").length > 0){
      $("#slider ul").cycle({
        prev: '#prevBtn',
        next: '#nextBtn',
        speed:  'slow', 
        timeout: 7000, 
        pause: 1,
        fx: 'scrollHorz'
      });
  }
  
  /**
    * Limit the characters in the entry field
    */

    $('#boohoo').bind('keyup', function(e) {
        var chars = $(this).val(),
        count = chars.length;
        max_chars = 190;

        // too many words?
        if (count > max_chars) {
            var fixed = chars.substring(0, max_chars);
            $(this).val(fixed);
            alert('You can only enter ' + max_chars + ' characters'); 
        } else {
            // update word count
            //$(this).siblings('p.counter').children('span').text(max_words - count);
        }
    });
    
    // simulate checkbox
    $('.checkbox span').click(function(e){
      $(this).removeClass('error').toggleClass('checked');
      
      if($(this).hasClass('checked')){
        $('#terms').attr('checked','checked');
      } else {
        $('#terms').removeAttr('checked');
      }
    });
    
    // get rid of error classes when a field gains focus
    $('.error, .error2').focus(function(e){
      $(this).removeClass('error error2');
    });
    
    $('.terms').click(function(e){
      e.preventDefault();
      window.open($(this).attr('href'), 'Terms', 'width=600,height=800,scrollbars=1');
    });
  
    $('.share').click(function(e){
      e.preventDefault();
      window.open($(this).attr('href'), 'Share', 'width=580,height=300');
    });
    
    // write out the letter when the preview button is clicked
    $('.previewit span').click(function(){
      if($('#boohoo').text()!= "Enter your break up message here")
        writeLetter($('#boohoo').val());
    })
    
    // submit the form from the remote button
    $('#writeit').click(function(){
      $('#breakup form').submit();
    });
    
    // IE handlers
    $('input, textarea').placeholder();
});

function writeLetter(theletter){
  // write out the fancy script, replacing nl with br. Replace double nl with singles first
  var scribbles = "";
  var br = '<br />';
  scribbles = (theletter + '').replace(/(\r\n\r\n|\n\r\n\r|\r\r|\n\n)/g, '\n');
  scribbles = scribbles.replace(/([^>\r\n]?)(\r\n|\n\r|\r|\n)/g, '$1'+ br +'$2');

  if(scribbles.length > 0) {
    $('#dearjohn span').type({ content: scribbles, deviation: 1});
  } else {
    $('#dearjohn span').empty();
  }
}

(function() {
    var e = document.createElement('script'); e.async = true;
    e.src = document.location.protocol +
    '//connect.facebook.net/en_US/all.js';
    document.getElementById('fb-root').appendChild(e);
}()); 

/*! http://mths.be/placeholder v1.8.5 by @mathias */
(function(g,a,$){var f='placeholder' in a.createElement('input'),b='placeholder' in a.createElement('textarea');if(f&&b){$.fn.placeholder=function(){return this};$.fn.placeholder.input=$.fn.placeholder.textarea=true}else{$.fn.placeholder=function(){return this.filter((f?'textarea':':input')+'[placeholder]').bind('focus.placeholder',c).bind('blur.placeholder',e).trigger('blur.placeholder').end()};$.fn.placeholder.input=f;$.fn.placeholder.textarea=b;$(function(){$('form').bind('submit.placeholder',function(){var h=$('.placeholder',this).each(c);setTimeout(function(){h.each(e)},10)})});$(g).bind('unload.placeholder',function(){$('.placeholder').val('')})}function d(i){var h={},j=/^jQuery\d+$/;$.each(i.attributes,function(l,k){if(k.specified&&!j.test(k.name)){h[k.name]=k.value}});return h}function c(){var h=$(this);if(h.val()===h.attr('placeholder')&&h.hasClass('placeholder')){if(h.data('placeholder-password')){h.hide().next().show().focus().attr('id',h.removeAttr('id').data('placeholder-id'))}else{h.val('').removeClass('placeholder')}}}function e(){var l,k=$(this),h=k,j=this.id;if(k.val()===''){if(k.is(':password')){if(!k.data('placeholder-textinput')){try{l=k.clone().attr({type:'text'})}catch(i){l=$('<input>').attr($.extend(d(this),{type:'text'}))}l.removeAttr('name').data('placeholder-password',true).data('placeholder-id',j).bind('focus.placeholder',c);k.data('placeholder-textinput',l).data('placeholder-id',j).before(l)}k=k.removeAttr('id').hide().prev().attr('id',j).show()}k.addClass('placeholder').val(k.attr('placeholder'))}else{k.removeClass('placeholder')}}}(this,document,jQuery));
