jQuery.cookie = function(name, value, options) {
    if (typeof value != 'undefined') { // name and value given, set cookie
        options = options || {};
        if (value === null) {
            value = '';
            options.expires = -1;
        }
        var expires = '';
        if (options.expires && (typeof options.expires == 'number' || options.expires.toUTCString)) {
            var date;
            if (typeof options.expires == 'number') {
                date = new Date();
                date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000));
            } else {
                date = options.expires;
            }
            expires = '; expires=' + date.toUTCString(); // use expires attribute, max-age is not supported by IE
        }
        // CAUTION: Needed to parenthesize options.path and options.domain
        // in the following expressions, otherwise they evaluate to undefined
        // in the packed version for some reason...
        var path = options.path ? '; path=' + (options.path) : '';
        var domain = options.domain ? '; domain=' + (options.domain) : '';
        var secure = options.secure ? '; secure' : '';
        document.cookie = [name, '=', encodeURIComponent(value), expires, path, domain, secure].join('');
    } else { // only name given, get cookie
        var cookieValue = null;
        if (document.cookie && document.cookie != '') {
            var cookies = document.cookie.split(';');
            for (var i = 0; i < cookies.length; i++) {
                var cookie = jQuery.trim(cookies[i]);
                // Does this cookie string begin with the name we want?
                if (cookie.substring(0, name.length + 1) == (name + '=')) {
                    cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
                    break;
                }
            }
        }
        return cookieValue;
    }
};



// Puts a watermark. Also remembers the value with a cookie
function text_field_label(text_field, label, store_value) {
  text_field.each(function(i){
          if ($.cookie($(this).attr('name')) == null) {
            $(this).val(label);
          } else {
            $(this).val($.cookie($(this).attr('name')));
          }
          
          swapValues[$(this).attr('id')] = $(this).val();
          $(this).focus(function(){
                  if ($(this).val() == swapValues[$(this).attr('id')]) {
                          $(this).val("");
                  }
          }).blur(function(){
                  if ($.trim($(this).val()) == "") {
                          $(this).val(swapValues[$(this).attr('id')]);
                  } else if (store_value == true) {
                    $.cookie($(this).attr('name'), $.trim($(this).val()), { expires: 999 });
                  }
          });
  });
}

function scroll_to_anchor(anchor) {
	var full_url = this.href;

	var target_offset = $("#"+anchor).offset();
	var target_top = target_offset.top;

	$('html, body').animate({scrollTop:target_top}, 500);
};

function some_fields_missing() {
  regexp = new RegExp(/^([^\s]+)((?:[-a-z0-9]\.)[a-z]{2,})$/i)
  
  return $('#comment_author').val() == 'Your Name (required)' ||
  $('#comment_author_email').val() == 'Your Email (required)' ||
  jQuery.trim($('#comment_author_url').val()) == '' ||
  jQuery.trim($('#comment_author_email').val()) == '' ||
  jQuery.trim($('#comment_content').val()) == '' ||
  !($('#comment_author_email').val().match(regexp))
}

function reply_to_comment(comment_id, author) {
  $('#comment_parent_id').val(comment_id);
  $('#new-comment h3').html('Reply to ' + author)
  $('#cancel_comment').show()
  scroll_to_anchor('new-comment');
}

function cancel_reply_to_comment() {
  $('#cancel_comment').show()
  $('#comment_body').val('');
  $('#comment_parent_id').val('');
  $('#new-comment h3').html('Leave a Reply')
  $('#cancel_comment').hide()
  scroll_to_anchor('new-comment');
}


$(function() {
  
  // label watermark
  swapValues = [];
  text_field_label($('#mce-EMAIL'), 'Your Email', false)
  text_field_label($('#mce-FNAME'), 'Your Name', false)
  
  text_field_label($('#comment_author'), 'Your Name (required)', true)
  text_field_label($('#comment_author_email'), 'Your Email (required)', true)
  text_field_label($('#comment_author_url'), 'Your URL (optional)', true)
  
  $('#new_comment').submit(function(e) {
    if (some_fields_missing()) {
      alert('Please fill all the required fields. Thanks');
      return false;
    }
  })
  
  $('#popular-posts').click(function(e) {
    e.preventDefault();
    $(this).removeClass('inactive');
    $('#recent-posts').addClass('inactive');
    $('#recent-posts-list').hide();
    $('#popular-posts-list').show();
  })
  
  $('#recent-posts').click(function(e) {
    e.preventDefault();
    $(this).removeClass('inactive');
    $('#popular-posts').addClass('inactive');
    $('#popular-posts-list').hide();
    $('#recent-posts-list').show();
  })
  
});