function unimplemented() {
  alert("NOTICE\n\nThis feature is not implemented yet.");
}

/*** Global behaviors ***/

$(function(){
  // Opens <a href="..." rel="external">External link</a> in a new window.
  $('a[rel=external]').click(function(ev){
    window.open(this.href);ev.preventDefault();
  });
  
  $('ul.listings>li, ul.questions>li, div.question, div.listing, ul.answers>li, ol.comments>li, div.information>span.report, ul.images').each(function(){
    new ReportLink(this);
  });

  if($('div#user-info').length > 0){ 
    new UserForm('div.information form#edit-user-form', 'div#user-info', 'a.edit-user', 'a.cancel');
    new UserForm('div.information form#edit-account-form', 'div#user-info', 'a.edit-account', 'a.cancel-account');
    new UserForm('div.information form#edit-postcode-form', 'div#user-info', 'a.edit-postcode', 'a.cancel-postcode');
  }
  
  // form, textContainer, link, cancel
  $('input.auto-clear').one('focus', function(ev){
    $(this).val("");
  });
  
});

// not used anywhere
// function toggleDropDown(div,visible){
//   if(visible){
//     $(div).find('ul').fadeOut('fast',function(){
//       $(div).addClass('closed');
//     })
//   } else {
//     $(div).removeClass('closed').find('ul').fadeIn('fast')
//   }
// }

// Used to limit characters in a text field.
// Requires the field, HTML element to inject the counter into and the max characters
// will add the class 'over-limit' to the message and the input box
// Suggest using in conjunction with jValidation validates lengthOf.

function CharacterLimiter(input,messageContainer,max){
  this.input = $(input);
  this.messageContainer = $(messageContainer);
  this.max = max;
  this.countCharacters();
  this.messageContainer.html("Characters <span>"+this.count+"</span>/"+this.max);
  this.counter = $(this.messageContainer.find('span'));
  var _this = this;
  this.input.keyup(function(){_this.updateCharacterCount();});
}

CharacterLimiter.prototype.updateCharacterCount = function(ev){
  this.countCharacters();
  this.counter.text(this.count);
  if(this.count>this.max){
    this.input.addClass('over-limit');
    this.messageContainer.addClass('over-limit');
  } else {
    this.input.removeClass('over-limit');
    this.messageContainer.removeClass('over-limit');
  }
}

CharacterLimiter.prototype.countCharacters = function(ev){
  this.count = this.input.val().length;
}

function ReportLink(container){
  this.container = $(container);
  this.link = this.container.find('a.report');
  if(this.link.length > 0){
    var id = this.container[0].id.split('-');
    this.link.colorbox({
      contentAjax:'/report/'+id[0]+'/'+id[1],
      bgOpacity:0.2
    });
    var _this = this;
  }
}

function ReportForm(form){
  this.form = $(form);
  var _this = this;
  this.form.submit(function(){
    var data = _this.form.serialize();
    var action = _this.form[0].action + '.js';
    _this.form.find('div.submit input').attr('disabled','disabled').val('Reporting...');
    $.ajax({
      type:'POST',
      url:action,
      data:data,
      success:function(){
        var p = $('<p style="display:none;" class="reported">Your report was sent to the moderation team</p>');
        _this.form.after(p);
        _this.form.fadeOut('fast',function(){ p.fadeIn('fast'); })
      }
    });
    return false;
  });
}

function UserForm(form, textContainer, link, cancel){
  this.form = $(form);
  this.textContainer = $(textContainer);
  this.link = $(link);
  this.cancel = $(cancel);
  var _this = this;
  this.link.click(function(){
    _this.toggleForm();
    return false;
  });
  this.cancel.click(function(){
    var text = $.trim(_this.textContainer.text());
    _this.toggleForm();
    _this.form.find('textarea').val(text);
    return false;
  })
  this.form.submit(function(){
    _this.submitForm();
    return false;
  });
}
UserForm.prototype.toggleForm = function(){
  var _this = this;
  if(this.form.is(':visible')){
    this.form.fadeOut('fast',function(){
      _this.textContainer.fadeIn('fast');
      _this.link.fadeIn('fast');
    }).find('div.errors').remove();
  } else {
    var linkAndContainer = this.link.add(this.textContainer);
    linkAndContainer.fadeOut('fast',function(){
      _this.form.fadeIn('fast');
    })
  }
}
UserForm.prototype.submitForm = function(){
  var data = this.form.serialize();
  var action = this.form[0].action + '.js';
  var submit = this.form.find('div.submit input');
  submit.attr('disabled','disabled').val('Saving...');
  var _this = this;
  $.ajax({
    type:'POST',
    url:action,
    data:data,
    success:function(result){
      submit.removeAttr('disabled').val("Save");
      result = $.toJSON(result);
      if(result.information || result.information === ""){
        var text = $.trim(result.information);
        if(text == ""){ _this.link.text("Add information about yourself"); }
        else { _this.link.text(result.link); }
        _this.textContainer.html(result.information);
        _this.toggleForm();
      } else {
        var errors = _this.form.find('div.errors');
        if(errors.length == 0){
          _this.form.append('<div class="errors" style="display:none;">Something went wrong. Please try again.</div>').find('div.errors').fadeIn('fast');
        }
      }
    }
  });
}

jQuery.urldecode = function(x) {
  return decodeURIComponent(x).replace(/\+/g, ' ');
}

//User login function
jQuery(function($){
  var user = $.cookie('userInfo')
  if(user){
    user = $.toJSON(user)
    $('#login-bar').html(
      "<a href=\"/listings/new\" class=\"btn\">Post a listing</a>"
      + "<a href=\"/users/" + user.id + "/edit\" class=\"btn\">My profile</a>"
      + "<div class='links'>Hi <a href=\"/users/"+ user.id + "/edit\">"
        + $.urldecode(user.login) + "</a> (<a href=\"/logout\">logout</a>)</div>"
    );
  };
});
