document.observe('dom:loaded',function(){
  Dialuogo = new dialuogo;
 
});

Event.observe(window, 'load', function() {
   if($('mainContent')) {
    
    var scrollbar = new Control.ScrollBar('mainContent','scrollbar_track');
  }
});

var dialuogo = {};
dialuogo = Class.create();
dialuogo.prototype = {
     initialize: function(){ 
        instance = this;
        this.popupWidth = 600;
        this.popupHeight = 400;
      
        this.curTargetEvent = null;
        this.initTargets();
        //Shadowbox.init({overlayColor: '#CAACCA',overlayOpacity:'0.1',animate: false});
    },
    initTargets: function() {
        instance = this;
        $$('.target').each(function(item){
          item.observe("mouseover", function(event){    
               instance.openTarget(item);
          });
          item.observe("mouseout", function(event){
               instance.closeTarget(item);
          });
        });
        this.initLinks(true);  
   
    
    },
    initLinks: function(withCasa) {
          instance = this;
          selector = 'a[rel=dialuogo]';
          if (!withCasa) selector = '#popup a[rel=dialuogo]'; 
          $$(selector).each(function(item){
          item.observe("click", function(event){    
                $('casa').fade();
                $('popup').fade();
                $('waiting').appear();
                return true; // No Ajax
                link = item.readAttribute('href');
                new Ajax.Updater('popup', link, {
                    parameters: {'type':99},
                    evalScripts: true,
                    onComplete: function() { 
                      
                      var scrollbar = new Control.ScrollBar('mainContent','scrollbar_track');
                      $('waiting').fade();
                      $('popup').appear({afterFinish: function(){scrollbar.recalculateLayout();$('popup').show();}});
                      $('showMainMenu').observe('click',function(event){instance.showMain();event.stop();return false;});
                      instance.initLinks(false);  
                      
                    },
                    onFailure: function(response) {
                                
                    }
                });
              
               event.stop();
               return false;
          });
         
        });
    } ,
    openTarget: function(item) {
         if(this.curTargetEvent) this.curTargetEvent.cancel();
         if(item.hasClassName('window')) {
           zwerg = item.getElementsBySelector('.window-zwerg-holder')[0];
           this.curTargetEvent = new Effect.Appear(zwerg);
           item.removeClassName('window-closed');
           item.addClassName('window-open');
         } else if(item.hasClassName('rabbit')) {
            item.removeClassName('rabbit-closed');
            item.addClassName('rabbit-open');
         } else if(item.hasClassName('news')) {   
            cTop = (item.positionedOffset().top-25)+'px;';
            if(!item.readAttribute('top')) item.writeAttribute('top',item.positionedOffset().top+'px');
            item.setStyle({height:'50px',top: item.readAttribute('top')});
            this.curTargetEvent = new Effect.Morph(item,{style:'height: 76px;top:'+cTop,'duration':'0.5'});
         }  else if(item.hasClassName('door')) {   
              if(item.hasClassName('door-closed') || item.hasClassName('door-open')) {
                item.removeClassName('door-closed');
                item.addClassName('door-open');
              } else {
                item.removeClassName('door-small-closed');
                item.addClassName('door-small-open');
              }
         }
    },
    closeTarget: function(item) {
         if(this.curTargetEvent) this.curTargetEvent.cancel();
         if(item.hasClassName('window')) {
           zwerg = item.getElementsBySelector('.window-zwerg-holder')[0];
           zwerg.hide();
           item.removeClassName('window-open');
           item.addClassName('window-closed');
          } else if(item.hasClassName('rabbit')) {
            item.removeClassName('rabbit-open');
            item.addClassName('rabbit-closed');
         }  else if(item.hasClassName('news')) {
            cTop = item.readAttribute('top');
            
            item.setStyle({height:'50px',top: cTop});
           
         }  else if(item.hasClassName('door')) {
             if(item.hasClassName('door-closed') || item.hasClassName('door-open')) {
              item.removeClassName('door-open');
              item.addClassName('door-closed');
             } else {
              item.removeClassName('door-small-open');
              item.addClassName('door-small-closed');
             }
         }
         
    } ,
    _getLinkInfo: function(html) {
         
    var container = document.createElement("p");
    container.innerHTML = html;
    var anchors = container.getElementsByTagName("a");
    return anchors[0].href;
    }  ,
    _getLinkText: function(html) {
         
    var container = document.createElement("p");
    container.innerHTML = html;
    var anchors = container.getElementsByTagName("a");
    return anchors[0].innerHTML;
    } ,
    showMain: function() {
        $('popup').fade();
        $('casa').appear();
    }
    
}



