




function doProgress(progress) {
    //do something with status and progress, i.e. set progressbar width:
  $('#progress').css('width', parseInt(progress*100, 10) +'%');
  $('#progressstatus').html(parseInt(progress*100, 10) + '% - ' + ogg.status);
}





var UploadQueue = function() {
  var that = this;
  this.allFiles = [];
  this.isUploading = false;
  this.$list = $('#filesList');
  this.$progress = $('#progressbar');
  this.len = function() {
    return this.allFiles.length;
  };
  this.getLi = function(no) {
    return this.$list.children('li').eq(no);
  }
  this.init();
}

UploadQueue.prototype.init = function() {
//  $('#progressbar').show();
  $('#progressbar').width(200).height(20);
//  $('#progressbar').css('background-color', '#80ADB0');
  $('#progressbar').html('<div id="progress" style="height:20px;width:0%"></div><div id="progressstatus" style="">uploading</div>');
}

/* param f = file */
UploadQueue.prototype.getData = function(f) {
  return {
    'firefogg': 1,
    'name': f.name,
    'category': $('#files_category').val()
  };
};


UploadQueue.prototype.uploadNext = function() {

};


UploadQueue.prototype.addFile = function(f) {
  for (var i=0; i<this.len(); i++) {
    if (this.allFiles[i].name == f.name) {
      return false;
    }
  }
  this.allFiles.push(f);
  this.addFileToList(f);
//  this.$list.triggerEvent("fileAdded");
  if (!this.isUploading) {
    this.upload(this.len() - 1);
  }
  return this;
};

UploadQueue.prototype.addFileToList = function(f) {
  var that = this;
  var $li = $('<li />').data("file", f).text(f.name).appendTo(that.$list);
};

UploadQueue.prototype.markDone = function(no, data) {
  $('.uploading').removeClass("uploading");
  var $li = this.getLi(no);
  $li.addClass("uploaded");
  fileUploadedCallback($li, data);
  if (this.len() > (no + 1)) {
    this.upload(no + 1);
  } else {
    this.isUploading = false;
    $('#progressbar').hide();
  }
};

UploadQueue.prototype.upload = function(no) {
  var that = this;
  var fil = this.allFiles[no];
//  console.log("uploading", fil);
  var data = this.getData(fil);
  var $li = this.getLi(no);
  this.isUploading = true;
  $('#progressbar').show();
  $li.addClass("uploading");
  ogg = FirefoggUploader(fil, add_url, data, function(ogg) {
    if (ogg.resultUrl) {
      that.markDone(no, ogg);              
    } else {
      $('#progressbar').html(ogg.status);
    }
  }, doProgress);  
};

function fileUploadedCallback(jq, data) {
//  alert("hi");
//  console.log(data);

  var d = JSON.parse(data.responseText);
  var fileId = d.fileId;
  jq.data("fileId", fileId);
  if (d.hasOwnProperty("errors")) {
    var $error = $('<div />').addClass("fileError").text(data.errors[0]).appendTo(jq);
  } else {
    var $formContainer = $('<div />').addClass("fileForm");
//    var $title = $('<div />').addClass("formHelp").text("Add Description").appendTo($formContainer);
//    console.log(data);
    var $titleInput = $('<input />').attr("type", "text").addClass("fileTitle").val(d.title);
//    console.log($titleInput);
    $titleInput.appendTo($formContainer);
    var $descInput = $('<textarea />').attr("placeholder", "Add Description").addClass("fileDesc").appendTo($formContainer);
    if (d.type == 'image') {
      var $thumbnail = $('<img />').addClass("thumbnail").attr("src", d.thumbnail).appendTo($formContainer);
    }
    var $submitBtn = $('<button />').text("Submit").click(function() {
      var $this = $(this);
      $this.attr("disabled", "disabled");
      var $parent = $(this).parents('.uploaded');
      var fileId = $parent.data("fileId");
      var title = $parent.find(".fileTitle").val();
      var description = $parent.find(".fileDesc").val();
      $.post("/files/editFile/", {
        'id': fileId,
        'title': title,
        'description': description
      }, function(response) {
        $this.removeAttr("disabled");
        $this.after("<span class='saved'>Saved</span>");
      },
      "json");
    }).appendTo($formContainer);
    $formContainer.appendTo(jq);
  }  
}
