/*
 * Copyright 2008-2009 Palm, Inc. All rights reserved.
 */

function Chrome(controller) {
	this.controller = controller;
	
	// Default orientation is portrait
	this._elementName = undefined;
	this._maxHeight = 0;
	this._currentHeight = 0;
	
	// Set the default orienation
	this._configOrientation('up');
}

Chrome.prototype.setOrientation = function(orientation) {
	
	if ((orientation !== 'up')) {
		// In landscape and down we ALWAYS hide the chrome and force a slide off.
		this._hidePortrait();
	}

	this._configOrientation(orientation);
};

Chrome.prototype._configOrientation = function(orientation) {
	
	if ((orientation !== 'up')) {
		this.show = this._showNoOp;
		this.hide = this._hideNoOp;
	} else {
		// Default is portrait.
		this.show = this._showPortrait;
		this.hide = this._hidePortrait;
	}
	
	this._orientation = orientation;	
};

Chrome.prototype.setup = function(properties) {

	this._elementName = properties.elementName;
	
	var expanded = !!properties.expanded;
	if (this._elementName) {
	
		var element = this.controller.get(this._elementName);
		if (element) {
			this._maxHeight = parseInt(element.getStyle('height'), 10);
			this._currentHeight = expanded ? this._maxHeight : 0; 
			element.setStyle({
				'height': this._currentHeight + 'px'
			});
		}
	}
		
	this._configOrientation(properties.orientation || 'up');
};

Chrome.prototype.cleanup = function() {

};

// Default orientation is portrait mode.
Chrome.prototype.show = Chrome.prototype._showPortrait;
Chrome.prototype.hide = Chrome.prototype._hidePortrait;

Chrome.prototype._showPortrait = function() {
	
	var animator;
	var element = this.controller.get(this._elementName);
	
	// If we are visible then the current spacer height is 
	if (element && (this._currentHeight === 0)) {
		animator = Mojo.Animation.animateStyle(element, 'height', 'linear', {
			from: 0,
			to: this._maxHeight,
			duration: 0.15,
			reverse: false
		});
	
		this._currentHeight = this._maxHeight;
	}
};

Chrome.prototype._hidePortrait = function() {

	var animator;
	var element = this.controller.get(this._elementName);

	if (element && (this._currentHeight > 0)) {
		animator = Mojo.Animation.animateStyle(element, 'height', 'linear', {
			from: 0,
			to: this._maxHeight,
			duration: 0.15,
			reverse: true
		});
	
	
		this._currentHeight = 0;
	}
};

Chrome.prototype._showNoOp = function() {
	// DO NOTHING - We don't show anything in landscape.	
};

Chrome.prototype._hideNoOp = function() {
	// DO NOTHING - We should not be displayed in landscape.
};

Chrome.prototype.isVisible = function() {
	return (this._currentHeight > 0);
};
