﻿var PhotoListingController =Class.create();



 PhotoListingController.prototype ={
initialize:function()
 {
   this.url =categoryController.url;
  // this.listing = new PhotoListing("listingContainer",photoData);
  // this.listing.onImageClicked = this.showLightBox.bindAsEventListener(this);
   this.photoForm =new PhotoUploadForm("uploadFormEditor");
   this.createLightBox();
  
   categoryController.onChangeCategory= this.onChangeCategory.bindAsEventListener(this);
   if(isAdmin)
   {
   this.btnAddPhoto = $("btnAddPhoto");
   this.btnAddPhoto.onclick = this.showUploadForm.bindAsEventListener(this);
   }
   window.photoListingController = this;
  
 },
 createLightBox:function()
 { 
  var me = this;
   Event.observe(window,"load",function(){
   me.lightBox = new LightBox("bigImages");  }); 
 },
 showLightBox:function(img)
 {
    var src = img.getAttribute("bigImageUrl");
    this.lightBox.open(src);
 },
 
 showUploadForm:function()
 {
    this.photoForm.open();
 },
 
  onChangeCategory:function()
    {
   
        
         if( categoryController.categoryId =="")
         {
           if(!isAdmin) return;
             $("disableAddPhoto").show();
              this.btnAddPhoto.disabled = true;                      
              
         }  else
         {
         
           this.refreshListing();
        
            if(this.listing)
            this.listing.clear();  
            if(!isAdmin) return;
            $("disableAddPhoto").hide();
            this.btnAddPhoto.disabled = false;   
         }    
    },
    
    refreshListing:function()
    {
           var pars = "getByCategoryId=true&categoryId="+ categoryController.categoryId ;      
           send(pars,this.url,this.fillListing.bindAsEventListener(this));
    },
    
   fillListing:function(r)
   {
        if(r.responseText =="") return;
      var json = eval(eval("("+r.responseText+")"));
      this.listing = new PhotoListing("listingContainer",json);
         this.listing.onImageClicked = this.showLightBox.bindAsEventListener(this);
         
         this.listing.onDeleteClick = this.deleteImage.bindAsEventListener(this);
      
   },
   deleteImage:function(id)
   {
 
         var pars = "deletePhoto=true&photoId="+ id ;      
         send(pars,this.url);
   }
 
}
