// JavaScript Document
function FolderPanel(panel,homePage,textFunc,galleryObj){
  this.currentPage=homePage;
  this.homePage=homePage;
  this.textFunc=textFunc;
  this.panelObj=$('#'+panel);
  tmp = new Image();
  tmp.src = 'images/panel_load.gif';
  this.loading = $(tmp).css({'position':'absolute','top':'180px','left':'68px'});
  this.panelObj.append(this.loading);
  this.activePage;
  this.panelObj.hide();
  this.galleryObj = galleryObj;
  this.clickFunc = null;
  this.waitCount = 0;
  this.waitInterval = null;
  this.textSpacing = 35;
}
FolderPanel.prototype.setClickFunc=function(clickFunc){
  this.clickFunc = clickFunc;
}
FolderPanel.prototype.showPanel=function(page){
  if(page==null || page==this.currentPage) return false;
  if(!this.showing){
    if(page != this.homePage){
      this.loading.show(); 
      var obj = this;
      this.waitCount=0;
      $.getJSON('gallery/folder.php',{cat:page},function(dataSrc){
        var list = $(document.createElement("span"));
        var link,item,bg,folderName;
        var j = 0;
        
        if($(dataSrc).size() > 0){
          for(var folderName in dataSrc){
            if(dataSrc[folderName] > 0){
              link = $(document.createElement("a")).attr({id:page+'_'+folderName,href:encodeURI("/ns/"+page+"_"+folderName+".html")}).append("<div class='foldertext'><span style='color:#7d3c1e'>"+folderName.substr(0,1)+"</span>"+folderName.substring(1)+"</div>");
              link.hover(function(){$(this).prev(".rollover").stop(true, true).fadeIn(100);},function(){$(this).prev().stop(true, true).fadeOut(300);}).click(
                function(event){
                  if(obj.clickFunc != null)
                    obj.clickFunc($(this).attr('id'));
                  return false;
              });
              bg  = $(document.createElement("div")).addClass("rollover").addClass("folderhl");
              item = $(document.createElement("div")).addClass("folder").css("top",j+"px").append(bg).append(link);
              list.append(item);
              obj.galleryObj.preloadGallery(page+'_'+folderName,dataSrc[folderName]);
              j+=obj.textSpacing;
            }
          }
          var pH = parseInt(obj.panelObj.css('height'),10);
          var nH = j+40; 
          obj.loading.hide(); 
          obj.loading.css('top',nH/2-10+'px');
          obj.panelObj.animate({'height':nH+'px'},Math.abs(pH-nH)*2,'swing',function(){obj.textFunc(list)});
        }
      });
      if(this.currentPage != this.homePage){
        this.currentPage=page;
        return true;
      }
    }
    this.activePage=page;
    this.showing = true;
    var obj=this;
    this.panelObj.toggle('slide',{direction:'left'},300,function(){
      obj.currentPage=page;
      obj.showing=false;
    });
  }else if(page != this.activePage) return false;
  return true;
}
