var Popin = {
  params:{},
  load:function(path, options){
    var options = options || {};
    var style = options.style || "sixeighty";
    var popin = $('popin_'+style);

    $$('.popup_overlay').invoke('show');
    $(popin, popin.down('.popin_loading')).invoke('appear',{duration:0.3});
    
    popin.down('.popin_content').update('');

    new Ajax.Updater(popin.down('.popin_content'), path, {     
      method:'get',
      evalScripts:true,
      parameters: {popin:true},
      
      onComplete:(function(){         
        popin.down('.popin_loading').hide();        

        popin.setStyle({top: (popin.cumulativeScrollOffset().top + 50).toString()+'px'})
        
        if(options.onComplete) options.onComplete();
        
      }).bind(this)
    });
  },
  alert:function(msg){
    $$('.popup_overlay').invoke('show');
    var popin = $('popin_sixeighty');
    popin.down('.popin_content').update(msg);
    popin.down('.popin_loading').hide();
    popin.show();
  },
  show:function(id){
    $$('.popup_overlay').invoke('show');
    Effect.Appear($(id), {duration:0.3});    
  },
  hide:function(el){
    // $$('.popin').invoke('hide');    
    $$('.popup_overlay').invoke('hide');
    
    if($(el))
      this.hide_this(el)
    else 
      $$('.popin').each((function(el){Effect.Fade(el, {duration:.3})}));    
  },
  hide_this:function(el){
    $$('.popup_overlay').invoke('hide');    
    Effect.Fade(el.up('div.popup_container').up('div'), {duration:.3}); 
  },
  loading:function(){
    this.alert("<img src='/images/loading.gif'/> One moment please...");
  },
  size_popin_contents:function(target, source){
    if( !($(target).up('.popin').identify() == 'popin_sixfifty') ){
      var height = $(source).getDimensions().height;

      $(target).setStyle({
        height: height.toString()+'px'
      });          
    }
  },
  resize_popin_contents:function(target, source){
    if( !($(target).up('.popin').identify() == 'popin_sixfifty') ){
      var height = $(source).getDimensions().height;

      new Effect.Morph($(target), {
        style: {
          height: height.toString()+'px'
        },
        afterFinish: (function(){ 
          $(target).setStyle({height:'auto'});         
        }).bind(this)
      })
    }
  }
}