/**
 * Footer
 */
var Footer = Class.create({
  /**
   * Init Footer
   */	
  initialize: function() {
	this.options = Object.extend({
	  elemContent : null,
	  bOpen	      : false
	}, arguments[0] || { });		
	
	Footer.INIT_STATE = 0;
	Footer.SHOW_STATE = 1;
	Footer.HIDE_STATE = 2;

	Footer.VIEW_MAX = 0;
	Footer.VIEW_MIN = 1;
	
	this.nState = Footer.INIT_STATE;
	this.nView = Footer.VIEW_MIN;
	
	this.bTransition = false;
	this.bPanelOpen = false;
	this.panelEffect = null;
	
    this.contentBtn = new EggFade({elemContent:this.options.elemContent.down('.btn')});
    
    this.elemPanel = this.options.elemContent.down('.panel');
		
    this.elemTogglePanelBtn = this.options.elemContent.down('.toggleview');
    if (this.elemTogglePanelBtn) {
      this.elemTogglePanelBtn.observe('click', this.clickTogglePanelBtn.bind(this));    	
    }
    
	Event.observe(window, 'resize', this.repaint.bind(this));
	this.repaint();
  },

  restore: function() {	 
	this.showBtn.bind(this).delay(1);
	if (this.nView == Footer.VIEW_MAX) {
	  // restore view
	  this.maximizePanel.bind(this).delay(2);
	}
  },
  
  show: function() {	 
	this.options.elemContent.style.visibility = "visible";
	  
    this.showBtn.bind(this).delay(1);
	if (this.options.bOpen) {
	  // restore view
	  this.maximizePanel.bind(this).delay(2);
	}
  },

  showBtn: function() {	 
	this.contentBtn.show();	
  },
  
  hide: function() {
	this.nState = Footer.HIDE_STATE;
    this.minimizePanel();
	if (this.nView == Footer.VIEW_MAX) {	
	  this.hideBtn.bind(this).delay(1);
	}
	else {
	  this.hideBtn();
	}
  },
  
  hideBtn: function() {
	this.contentBtn.hide();		  
  },
  
  clickTogglePanelBtn: function(evt) {
	if (this.bTransition) return;

	var bOpen = false;
	this.nState = Footer.INIT_STATE;
	this.bTransition = true;	  
	  
    if (this.bPanelOpen) {
      this.nView = Footer.VIEW_MIN;
      this.minimizePanel();
    }
    else {
      bOpen = true;
      this.nView = Footer.VIEW_MAX;
      this.maximizePanel();
    }
    // fire event
	this.options.elemContent.fire("footer:toggle", { bOpen: bOpen});    
  },

  minimizePanel: function() {
    this.bPanelOpen = false;
    this.panelEffect = new Effect.Morph(this.elemPanel, {duration: 1, style: {top: 0 + 'px', height: 26 + 'px'}, afterFinish: this.panelTransitionComplete.bind(this)});	
  },

  maximizePanel: function() {
    this.bPanelOpen = true;
    this.panelEffect = new Effect.Morph(this.elemPanel, {duration: 1, style: {top: -60 + 'px', height: 86 + 'px'}, afterFinish: this.panelTransitionComplete.bind(this)});	
  },

  panelTransitionComplete: function(evt) {
	this.bTransition = false;
  },
  
  repaint: function(evt) {
    if (Prototype.Browser.MobileSafari) {
   	  var viewDim = document.viewport.getDimensions();
	      	
  	  // viewDim.height not reliable on ios
  	  var D = document
  	  var nHeight = Math.max(
  	        Math.max(D.body.scrollHeight, D.documentElement.scrollHeight),
  	        Math.max(D.body.offsetHeight, D.documentElement.offsetHeight),
  	        Math.max(D.body.clientHeight, D.documentElement.clientHeight)
  	    );
	      	  
	  if (nHeight > viewDim.height) {
	    this.options.elemContent.setStyle({'position': 'relative'});    		  
	  }
	  else {
		this.options.elemContent.setStyle({'position': 'fixed'});    		  		  
	  }
	}
  }  
  
});
