//Needs UtilityService.js and scriptaculous
var EBookMessageBox=Class.create(PanelHandler,
{
   initialize:function($super,containerId,parentHandler)
   {
       $super(containerId,parentHandler);
       this.vBUTTON_BUY_SELECTOR        ="ebookMessageBoxButtonBuy";
       this.vBUTTON_CLOSE_SELECTOR      ="ebookMessageBoxButtonClose";
       this.vSECTION_EBOOK_INFO_SELECTOR="ebookMessageBoxInfo";
       
       this.relatedProductId=null;
       this.initEvents();
   },

   getEButtonClose:function()
   {return UtilityService.prototype.getFirstMatching(this.getEContainer(),"class",this.vBUTTON_CLOSE_SELECTOR);},

   getEButtonBuy:function()
   {return UtilityService.prototype.getFirstMatching(this.getEContainer(),"class",this.vBUTTON_BUY_SELECTOR);},

   getEEBookInfo:function()
   {return UtilityService.prototype.getFirstMatching(this.getEContainer(),"class",this.vSECTION_EBOOK_INFO_SELECTOR);},

   setRelatedProductId:function(productId)
   {
       this.relatedProductId=productId;
   },
   
   initEvents:function()
   {
       this.getEButtonClose().observe("click",
         function()
         {
             this.deactivateBox();
         }.bind(this)
       );
           
       this.getEButtonBuy().observe("click",
         function()
         {
             this.getEButtonBuy().hide();
             if(this.relatedProductId!=null)
             {
                 this.addToShoppingCart();
             }
             this.deactivateBox();
         }.bind(this)
       );    
   },

   loadEBookInfo:function()
   {
       var url=this.getBaseUrl()+"/shoppingcart?method=showEBookInfo&productId="+this.relatedProductId;

       new Ajax.Request(url,
       {
           method:'post',
           onSuccess:function(response)
           {
               var ebookInfoE=UtilityService.prototype.getElementFromResponse(response, "class", this.vSECTION_EBOOK_INFO_SELECTOR);
               if(ebookInfoE!=null)
               {
                   this.getEEBookInfo().update(ebookInfoE.innerHTML);
               }
               else
               {//CHECK:delete this
                   this.getEEBookInfo().update("error");
               }
           }.bind(this),
           onLoading: function(){}.bind(this),
           onLoaded: function(){}.bind(this)
       });
   },

   addToShoppingCart:function()
   {
       ajaxManager('load_page',this.getBaseUrl()+'/shoppingcart?method=addItem&productId='+this.relatedProductId+'&s', 'shoppingCartContent', 'shoppingCart');
       //window.scrollBy(0,-document.body.scrollTop);
       Effect.ScrollTo($("shoppingCartContent"));
       return false;
   },

   activateBox:function()
   {
       this.getEButtonBuy().show();
       this.getEButtonClose().show();
       
       var top=this.getContextDocument().viewport.getScrollOffsets().top+100;
       var left=200;

       this.loadEBookInfo();       

       if(!this.getEContainer().visible())
       {
          this.getEContainer().setStyle({"top":top,"left":left,width:"300px",height:"300px"});
          Effect.Appear(this.getEContainer());
       }
       else
       {
           
          new Effect.Move(this.getEContainer(),
           { x: left,
             y: top,
             mode: 'absolute',
             afterFinish:function(){new Effect.Highlight(this.getEContainer(),{queue: { position: 'end', scope: 'queueForEBookMessageBoxHighlight' }});}.bind(this),
             queue: { position: 'end', scope: 'queueForEBookMessageBoxMovement' }
           });
           
       }
   },

   deactivateBox:function()
   {
       this.relatedProductId=null;
       this.getEButtonBuy().hide();
       this.getEButtonClose().hide();
       Effect.Puff(this.getEContainer(),{afterFinish:function(){this.getEContainer().hide();}.bind(this)});
   }
}
);