var JQ = jQuery.noConflict();

JQ(document).ready(function() {

  Shadowbox.init({
    skipSetup: true
  });
  
  JQ('#download').hide().fadeIn(1200);
  
  Shadowbox.setup("a.report-frame", {
      height:     220,
      width:      360,
      title: 'Zgłoś naruszenie zasad regualminu',
      overlayOpacity: 0.9
    });

  Shadowbox.setup("a.secure-frame", {
      height:     140,
      width:      360,
      title: 'Zabezpiecz hasłem',
      overlayOpacity: 0.9,
      onClose: function(){ window.location.reload(); }
    });
    
  Shadowbox.setup("a.resecure-frame", {
      height:     220,
      width:      360,
      title: 'Zmień hasło',
      overlayOpacity: 0.9,
      onClose: function(){ window.location.reload(); }
    });
    
  Shadowbox.setup("a.unsecure-frame", {
      height:     120,
      width:      360,
      title: 'Usuń hasło',
      overlayOpacity: 0.9,
      onClose: function(){ window.location.reload(); }
    });

  JQ(".confirm").each(function() {
    JQ(this).click(function() {
      return confirm('Czy na pewno?');
    });
  });
  
  JQ(".adult_switch_on").each(function() {
    JQ(this).click(function() {
      return confirm('Czy na pewno chcesz wyłączyć filtr treści dla dorosłych?');
    });
  });

  JQ("#addfolder").each(function() {
    JQ(this).click(function() {
      JQ(".status").css("display", "block");
      JQ(".addingfolder").css("display", "inline");
    });
  });
  
  JQ(".share").each(function() {
    JQ(this).click(function() {
      JQ(".status").css("display", "block");
      JQ(".sharing").css("display", "inline");
    });
  });
  
  JQ(".unshare").each(function() {
    JQ(this).click(function() {
      JQ(".status").css("display", "block");
      JQ(".unsharing").css("display", "inline");
    });
  });
  
  JQ(".show_link a").click(function() {
    Shadowbox.open({
      content : '',
      player : 'html',
      title : JQ(this).attr("href"),
      width : 900,
      height : 1,
      overlayOpacity: 0.9
    });
    return false;
  });

  JQ("#download").each(function() {
    JQ(this).click(function() {
      var downloads = parseInt(JQ("#download_counter").text());
      downloads += 1;
      JQ("#download_counter").html(downloads);
    });
  });

  var renamet;
  var renames;
  var renamep;
  var renamef = function() {
    var filename = renames.html();
    renamep = renames.parent();
    renamep.html("<input type='text' value='"+filename+"' />");
    renamep.find("input").focus().focusout(savename).keypress(function(event) {
      if(event.keyCode == 13)
        savename();
    });
  }
  var savename = function() {
    JQ(".status").css("display", "block");
    JQ(".renaming").css("display", "inline");
    location.href = location.href + '?rename=' + renamep.parent().attr('id').substring(5) + '&newname=' + renamep.find("input").attr("value");
  }

  var dropFcn = function(ev, ui) {
    var from = JQ(ui.draggable).attr('id').substring(5);
    var to = JQ(this).attr('id').substring(5);
    JQ(".status").css("display", "block");
    JQ(".moving").css("display", "inline");
    location.href = location.href + '?from=' + from + '&to=' + to;
  }

  JQ("#table-view").each(function() {
    JQ(this).find("tr.file, tr.folder").draggable({
      helper:'clone',
      cursor:'move',
      opacity: 0.7,
      scroll:true,
      cursorAt:{left:0},
      start:function(event,ui) {
        clearTimeout(renamet);
      }
    });
    JQ(this).find("tr.folder").droppable({
      accept: "tr.file, tr.folder",
      activeClass: 'droppable-active',
      hoverClass: 'droppable-hover',
      drop: dropFcn
    });
    JQ(this).find("tr").each(function() { JQ(this).find("td:eq(1) a").mousedown(function() {
      renames = JQ(this);
      renamet = setTimeout(renamef, 1000);
      JQ(this).mouseup(function() {
        clearTimeout(renamet);
      });
    })});
  });

   JQ("#icon-view").each(function() {
    JQ(this).find("li.file, li.folder").draggable({
      helper:'clone',
      cursor:'move',
      opacity: 0.7,
      scroll:true,
      start:function(event,ui) {
        clearTimeout(renamet);
      }
    });
    JQ(this).find("li.folder").droppable({
      accept: "li.file, li.folder",
      activeClass: 'droppable-active',
      hoverClass: 'droppable-hover',
      drop: dropFcn
    });
    JQ(this).find("li").each(function() { JQ(this).find("span a").mousedown(function() {
      renames = JQ(this);
      renamet = setTimeout(renamef, 1000);
      JQ(this).mouseup(function() {
        clearTimeout(renamet);
      });
    })});
  });

  /* WRZUCARKA WRAPPER */  
  JQ("body").append('<div id="wrzucarka-wrapper"><div id="wrzucarka"></div></div>');
  JQ("#wrzucarka").load("wrzucarka.html", function() {
    JQ(this).append('<div id="close-wrzucarka">X</div>');
    JQ("#close-wrzucarka").click(function() { wrzucarka.cancelAll(); JQ("#wrzucarka-wrapper").fadeOut('slow'); });
    var folder_rel = JQ(".upload_button").attr("rel");
    var folder_arr = /folder\[([0-9]+),(.+)\]/.exec(folder_rel);
    if(folder_arr && folder_arr[2] != null) {
      JQ("#folder").val(folder_arr[1]);
      JQ("#wrzucarka_title span").html(folder_arr[2]);
    }
  });
  JQ(".upload_button").click(function() { JQ("#wrzucarka-wrapper").fadeIn('slow'); });
  
  /* USAGE */
  JQ(".whitebox .usage, #top p.usage span.usage").each(function() {
    var usage = JQ(this).html();
    JQ(this).html('<span class="progress"></span><span class="percent">'+usage+'</span>');
    JQ(this).find(".progress").css("width", usage);
  });
  JQ("#top p.usage span.usage .progress").fadeTo(0, 0.7);
  
  /* SCREENSHOTS */
  Shadowbox.setup(".screenshot");
  JQ(".screenshot").each(function() {
    var title = JQ(this).attr("title");
    JQ(this).prepend('<span>'+title+'</span>');
  });
  
  /* SLIDESHOW */
  JQ("#slideshow").each(function() {
    var photos = JQ(this).attr('rel').split(';');
    for(var i in photos) {
      photos[i] = {
        player : 'img',
        content : photos[i]
      };
    }
    JQ(this).click(function() {
      Shadowbox.open(photos);
    });
  });
  
  /* PREVIEW */
  JQ(".preview").click(function() {
    Shadowbox.open({
      player : 'img',
      title : 'Podgląd pliku: '+ JQ(this).attr("title"),
      content : JQ(this).attr("href"),
      overlayOpacity: 0.9
    });
    return false;
  });
  
  var url = String(window.location);
  var index = url.indexOf("?");
  var data = url.substr(index+1);
  var splitted = data.split("=");
  q = splitted[0];

  if(q == 'preview') {
    JQ(window).load(function() {
      Shadowbox.open({
        player : 'img',
        title : 'Podgląd pliku: '+ JQ('.autopreview').attr("title"),
        content : JQ('.autopreview').attr("href"),
        overlayOpacity: 0.9
      });
    });
  }
});



/* ajax load public files */
function reloadList(){
  function updateTimers(){
          var dest = String(window.location);
          JQ.ajax({
                    method: 'get',
                    url : dest+'?showtemplate=false',
                    dataType : 'text',
                    success: function (text) { 
                      JQ('.box-wide-content').html(text);
                    }
                 });
  }

  function refreshPublicFilesList(){
      var ts = Math.round((new Date()).getTime() / 1000);
      JQ.ajax({
                    method: 'get',
                    url : 'public/files.html?showtemplate=false&ts='+ts,
                    dataType : 'text',
                    success: function (text) { 
                      JQ('#pf').prepend(text);
                      JQ('.hidden').fadeIn('slow');
                    }
                 });
       updateTimers();
  }
  var newFilesInterval = setInterval(refreshPublicFilesList, 10000);
}

