BAREFOOT.PopupPanel = function(el, userConfig) {
   if( arguments.length > 0 ) {
      BAREFOOT.PopupPanel.superclass.constructor.call(this, el, userConfig);
   }
}

YAHOO.extend( BAREFOOT.PopupPanel, YAHOO.widget.Panel );


BAREFOOT.PopupPanel.FADE_TIME   = 0.3;
BAREFOOT.PopupPanel.FADE_TYPE   = YAHOO.util.Easing.easeOut;

BAREFOOT.PopupPanel.MIN_WIDTH   = '177px';
BAREFOOT.PopupPanel.MIN_HEIGHT  = '107px';

BAREFOOT.PopupPanel.MAX_WIDTH   = '696px';
BAREFOOT.PopupPanel.MAX_HEIGHT  = '437px';

BAREFOOT.PopupPanel.MAX_POSITION = [200,100];
BAREFOOT.PopupPanel.StaggerLast = BAREFOOT.PopupPanel.MAX_POSITION;
BAREFOOT.PopupPanel.StaggerSize = [20,20];

BAREFOOT.PopupPanel.prototype.open = function(){
   var anim = new YAHOO.util.Anim(this.id, {opacity: {to: 0}}, BAREFOOT.PopupPanel.FADE_TIME, BAREFOOT.PopupPanel.FADE_TYPE);
   anim.onComplete.subscribe( function( type, args, obj ) {
      obj.oldx = obj.cfg.getProperty('x');
      obj.oldy = obj.cfg.getProperty('y');

      obj.cfg.setProperty('close',  true);
      obj.cfg.setProperty('width',  BAREFOOT.PopupPanel.MAX_WIDTH);
      obj.cfg.setProperty('height', BAREFOOT.PopupPanel.MAX_HEIGHT);

      YAHOO.util.Dom.setXY(obj.element, BAREFOOT.PopupPanel.StaggerLast);
      BAREFOOT.PopupPanel.StaggerLast[0] += BAREFOOT.PopupPanel.StaggerSize[0];
      BAREFOOT.PopupPanel.StaggerLast[1] += BAREFOOT.PopupPanel.StaggerSize[1];

      var min = YAHOO.util.Dom.getElementsByClassName('min','div',obj.element);
      YAHOO.util.Dom.setStyle(min,'display','none');

      var max = YAHOO.util.Dom.getElementsByClassName('max','div',obj.element);
      YAHOO.util.Dom.setStyle(max,'display','block');

      document.getElementById(obj.id +'_max').style.display = 'none';

      var fadeIn = new YAHOO.util.Anim(obj.id, {opacity: {to: 1}}, BAREFOOT.PopupPanel.FADE_TIME, BAREFOOT.PopupPanel.FADE_TYPE);
      fadeIn.onComplete.subscribe( function( type, args, obj ) {
         obj.cfg.setProperty('underlay',  'shadow');
      }, obj );
      fadeIn.animate();
      //document.getElementById(obj.id + "_c").style.zIndex = 200;

      pageTracker._trackPageview("/maximize/" + obj.id );
   }, this );
   anim.animate();
};

BAREFOOT.PopupPanel.prototype.init = function( el, userConfig ) {
   // set initial values for a minimized portfolio box
   userConfig.close  = false;
   userConfig.width  = BAREFOOT.PopupPanel.MIN_WIDTH;
   userConfig.height = BAREFOOT.PopupPanel.MIN_HEIGHT;
   userConfig.constraintoviewport = true;

   BAREFOOT.PopupPanel.superclass.init.call(this, el,userConfig);
   this.beforeInitEvent.fire(YAHOO.widget.PopupPanel);

   this.oldx   = 0;
   this.oldy   = 0;


   var anim = new YAHOO.util.Anim(this.id, {opacity: {to: 0}}, BAREFOOT.PopupPanel.FADE_TIME, BAREFOOT.PopupPanel.FADE_TYPE);
   anim.onComplete.subscribe( function( type, args, obj ) {
      obj.oldx = obj.cfg.getProperty('x');
      obj.oldy = obj.cfg.getProperty('y');

      obj.cfg.setProperty('close',  true);
      obj.cfg.setProperty('width',  BAREFOOT.PopupPanel.MAX_WIDTH);
      obj.cfg.setProperty('height', BAREFOOT.PopupPanel.MAX_HEIGHT);

      YAHOO.util.Dom.setXY(obj.element, BAREFOOT.PopupPanel.StaggerLast);
      BAREFOOT.PopupPanel.StaggerLast[0] += BAREFOOT.PopupPanel.StaggerSize[0];
      BAREFOOT.PopupPanel.StaggerLast[1] += BAREFOOT.PopupPanel.StaggerSize[1];

      var min = YAHOO.util.Dom.getElementsByClassName('min','div',obj.element);
      YAHOO.util.Dom.setStyle(min,'display','none');

      var max = YAHOO.util.Dom.getElementsByClassName('max','div',obj.element);
      YAHOO.util.Dom.setStyle(max,'display','block');

      document.getElementById(obj.id +'_max').style.display = 'none';

      var fadeIn = new YAHOO.util.Anim(obj.id, {opacity: {to: 1}}, BAREFOOT.PopupPanel.FADE_TIME, BAREFOOT.PopupPanel.FADE_TYPE);
      fadeIn.onComplete.subscribe( function( type, args, obj ) {
         obj.cfg.setProperty('underlay',  'shadow');
      }, obj );
      fadeIn.animate();
      pageTracker._trackPageview("/maximize/" + obj.id );
   }, this );
   
   YAHOO.util.Event.addListener(this.id +'_max', 'click', function(e, obj) { obj.cfg.setProperty('underlay','none'); }, this, true );
   YAHOO.util.Event.addListener(this.id +'_max', 'click', anim.animate, anim, true);
   YAHOO.util.Event.addListener(this.id +'_thumb', 'click', function(e, obj) { obj.cfg.setProperty('underlay','none'); }, this, true );
   YAHOO.util.Event.addListener(this.id +'_thumb', 'click', anim.animate, anim, true);
   

   /* Close event... minimize the window */
   this.hideEvent.subscribe( function() {
      this.show();
      this.cfg.setProperty('underlay',  'none');

      var anim = new YAHOO.util.Anim( this.id, {opacity: {to: 0}}, BAREFOOT.PopupPanel.FADE_TIME, BAREFOOT.PopupPanel.FADE_TYPE );
      anim.onComplete.subscribe( function( type, args, obj ) {
         obj.cfg.setProperty('close',  false);
         obj.cfg.setProperty('width',  BAREFOOT.PopupPanel.MIN_WIDTH);
         obj.cfg.setProperty('height', BAREFOOT.PopupPanel.MIN_HEIGHT);
         /*
         obj.cfg.setProperty('x',     obj.oldx);
         obj.cfg.setProperty('y',     obj.oldy);
         */
         YAHOO.util.Dom.setXY(obj.element, [obj.oldx, obj.oldy]);
         BAREFOOT.PopupPanel.StaggerLast[0] -= BAREFOOT.PopupPanel.StaggerSize[0];
         BAREFOOT.PopupPanel.StaggerLast[1] -= BAREFOOT.PopupPanel.StaggerSize[1];

         var min = YAHOO.util.Dom.getElementsByClassName('min','div',obj.element);
         YAHOO.util.Dom.setStyle(min,'display','block');

         var max = YAHOO.util.Dom.getElementsByClassName('max','div',obj.element);
         YAHOO.util.Dom.setStyle(max,'display','none');

         document.getElementById(obj.id +'_max').style.display = 'block';

         var fadeIn = new YAHOO.util.Anim(obj.id, {opacity: {to: 1}}, BAREFOOT.PopupPanel.FADE_TIME, BAREFOOT.PopupPanel.FADE_TYPE);
         fadeIn.onComplete.subscribe( function( type, args, obj ) {
            obj.cfg.setProperty('underlay',  'shadow');
         }, obj );
         fadeIn.animate();
      }, this );
      anim.animate();
   });
}
