/*
 *******************************************
 *******************************************
 *
 * Takeover Module
 * Nicholas Ortenzio 
 *
 * Requires Prototype and Scriptaculous
 * 
 * Last Modified 11/04/2009
 *******************************************
 *******************************************
 */

var Takeover = Class.create({

	/*
	 *************************
	 * CONSTRUCTOR
	 *************************
	 */

	initialize : function(base) {
		this.base = base;
		
		this.setOptions();
		this.setDomProperties();
		this.setEvents();
		this.setInitialState();
	},

	/*
	 *************************
	 * INITIALIZATION METHODS
	 *************************
	 */

	setOptions : function() {
		this.options = {
			minSelector : '',
			maxSelector : '',
			closeSelector : '',
			openSelector : '',
			openEvent : 'click',
			showOnLoad : false,
			showOnLoadDuration : 20, // Seconds
			showOnLoadRepeatDelay : 2 // Minutes
		};

		for (var i in this.options) { 
      this.options[i] = this.base.readAttribute(i) || this.options[i] 
    };
		
		this.options.showOnLoad = (this.options.showOnLoad == 'true');
		this.options.showOnLoadRepeatDelay = parseInt(this.options.showOnLoadRepeatDelay, 10);
		this.options.showOnLoadDuration = parseInt(this.options.showOnLoadDuration, 10);
		this.options.effectDuration = parseFloat(this.options.effectDuration, 10);
	},

	setDomProperties : function() {
		this.dom = {};

		this.dom.min = this.base.select(this.options.minSelector)[0];
		this.dom.max = this.base.select(this.options.maxSelector)[0];
		this.dom.close = this.base.select(this.options.closeSelector)[0];
		this.dom.open = this.base.select(this.options.openSelector)[0];

    this.closedHeight = this.dom.min.getHeight();
    this.openHeight = this.dom.max.getHeight();
    
    this.base.setStyle({height:this.closedHeight+'px'});
	},

	setEvents : function() {
		this.dom.open.observe(this.options.openEvent, this.onToggleVisible.bind(this));
		this.dom.close.observe('click', this.onToggleVisible.bind(this));
	},

	setInitialState : function() {
		this.isAnimating = false;
    this.isOpen=false;

    if ((!this.options.showOnLoad) || (GetCookie("hasSeenHomepageTakeover"))) { 
      return;
    }

    this.onToggleVisible();
    this.setTakeoverCookie();
    this.timeout = setTimeout(function(){
      if(this.isOpen) { this.onToggleVisible(); }
    }.bind(this), this.options.showOnLoadDuration*1000);
	},

	setTakeoverCookie : function() {
		var expires = new Date();
    expires.setMinutes(expires.getMinutes()+this.options.showOnLoadRepeatDelay);
    SetCookie ("hasSeenHomepageTakeover", "1", expires);
	},

	/*
	 *************************
	 * EVENT METHODS
	 *************************
	 */

  beforeStart : function() {
    this.isAnimating = true;
    if (!this.isOpen) {
      this.dom.min.hide();
    }    
  },
  
  afterFinish : function() { 
    if (this.isOpen) {
      this.dom.min.show();
    }
    this.isAnimating = false; 
    this.isOpen=!this.isOpen;
  },

	onToggleVisible : function() {
		if (this.isAnimating) return;

    if (this.timeout) { 
      clearTimeout(this.timeout);
      this.timeout = null; 
    }

    var h = 'height:' + ((this.isOpen) ? this.closedHeight : this.openHeight ) + 'px;';
 
    new Effect.Morph(this.base, {
      style : h,
      duration : .5,
      queue : 'end',
      beforeStart : this.beforeStart.bind(this),
      afterFinish : this.afterFinish.bind(this)
    })
	}

});

NBAUtil.Events.addDomLoadedHandler(function() { $$('.takeover').each(function(n) { new Takeover(n); }); });
