$(function () {
  var w = $(window);
  var winWidth = w.width();

  $.ajaxSetup({
    global: true,
    beforeSend: function () {
      $('<div id="loader"></div>').css({
        position: 'absolute',
        top: 0,
        left: 0,
        width: winWidth,
        height: $(document).height(),
        background: '#000',
        opacity: 0.6,
        'z-index': 40
      }).appendTo('body');

      $('<div class="message"><h1>Please wait...</h1></div>')
        .appendTo('#loader')
        .css({
          position: 'absolute',
          top: 200,
          left: winWidth/2-200
        })
    },
    complete: function(){
      $('#loader').remove()
    }
  })
});

var submitting = false;

function showError(obj, selector){
  var error = ''
  $(obj).each(function(){ error += this; });
  if(error != '')
    $(selector).html(error).css('color', 'red').show();
}

function showLogin(){
  location.href=login_url;
  return false;
}

function publishThumb(){
  var $this = $(this);
  var status = $('#publish_status');

  $.ajax({
    url : $(this).attr('action'),
    type : 'POST',
    data : $(this).serializeArray(),
    dataType : 'json',
    timeout : 3000,
    beforeSend : function(){
      status.html('sending...').show();
    },
    success : function(obj){
      if(obj.success){
        status.css('color', 'green').html('published!');
        setTimeout(hidePublishForm, 1600);
      }else if(obj.error){
        status.css('color', 'red').html(obj.error);
      }else if(obj.errors.title){
        showError(obj.errors.title, '#publish_title_status');
        status.empty().hide();
      }else if(obj.errors.description){
        showError(obj.errors.description, '#publish_desc_status');
        status.empty().hide();
      }
    },
    error : function(){
      status.css('color', 'red').html('An Error Occured!');
    }
  });
  return false;
}
function hidePublishForm(){
  $('#publish_form').parent('div').hide('fast');
  $('#publish_status').empty().hide();
  $('#publish_title_status').empty().hide();
  $('#publish_desc_status').empty().hide();
  $('#id_title').val('');
  $('#id_description').val('');
  $('#id_thumb_key').val('');
}

var del = new Object();
del = {
  conf: function(e, type, key){
    if (window.confirm('Sure you want to delete this ' + type + '? There is NO undo!')) {
      del.destroy(e, type, key);
    }
    return false;
  },
  destroy: function(e, type, key){
    var obj = $(e.parentNode);
    var container = $('#' + type + '_' + key);
    $.ajax({
      type : 'POST',
      url : '/' + type + '/' + key + '/delete/',
      data : {},
      dataType : 'json',
      timeout : 3200,
      beforeSend : function(){ obj.html('deleting...'); },
      success : function(res){
        message = "";
        if(res.error) {alert('error occured.');}
        else if(res.success){
          obj.hide().css('color', 'green').html('deleted').fadeIn(200);
          if(container.length != 0){
            setTimeout(function(){ container.hide(200); }, 1000 * 1);
          }else if(res.redirect){
            window.location = res.redirect;
          }else{window.location = '/';}
        }else{alert('error occured.');}
      }
    });
  }
}

