/*
 * Copyright (c) 2006 AWS Convergence Technologies, Inc. 
 * All rights reserved.
 * Visit http://www.aws.com
 *
 * WBDrawer.js
 *
 * This file contains the WBDrawer reusable class. This class takes care of animating
 * a drawer element using the Apple Animator class. Note, that an Apple Stretcher object
 * can not be used because the size of our drawer needs to be variable (not just open and close.)
 * if you just need open and close functionality, use the Apple Stretcher class.
 */
function WBDrawer(drawer_element,duration)
{	
	this.element = drawer_element;
	
	this.beginHeight = parseInt(document.defaultView.getComputedStyle(this.element, "").getPropertyValue("height"));
	this.originalHeight = this.beginHeight;
	this.positionHeightFrom = this.beginHeight;
	this.positionHeightTo;
	
	this.beginWidth = parseInt(document.defaultView.getComputedStyle(this.element, "").getPropertyValue("width"));
	this.positionWidthFrom = this.beginWidth;
	this.positionWidthTo;	

	this.windowResizeOffsetX = window.innerWidth;
	this.windowResizeOffsetY = window.innerHeight;
	this.originalXOffset = this.windowResizeOffsetX; // store the originals for dynamically changing the drawer later
	this.originalYOffset = this.windowResizeOffsetY; // store the originals for dynamically changing the drawer later
	
	this.duration = duration;
	this.onFinished;
	
	var self = this;
	this.stretchAnimator = null;
	this.nextFrame = function (animation, now, first, done) {
		if(self.positionWidthFrom < self.positionWidthTo)
		{
			self.element.style.width = parseInt(self.positionWidthFrom + (self.positionWidthTo-self.positionWidthFrom+self.beginWidth)*now) + "px";
		}
		else
		{
			self.element.style.width = parseInt(self.beginWidth + self.positionWidthTo + (self.positionWidthFrom-self.positionWidthTo-self.beginWidth)*(1-now)) + "px";
		}
		
		if(self.positionHeightFrom < self.positionHeightTo)
		{
			self.element.style.height = parseInt(self.positionHeightFrom + (self.positionHeightTo-self.positionHeightFrom+self.beginHeight)*now) + "px";
		}
		else
		{
			self.element.style.height = parseInt(self.beginHeight + self.positionHeightTo + (self.positionHeightFrom-self.positionHeightTo-self.beginHeight)*(1-now)) + "px";
		}
	}
	
	this.doneStretching = function () {
		self.positionHeightFrom = self.positionHeightTo + self.beginHeight;
		self.positionWidthFrom = self.positionWidthTo + self.beginWidth;
		if (window.widget) {
			window.resizeTo(self.positionWidthTo + self.windowResizeOffsetX, self.positionHeightTo + self.windowResizeOffsetY);
		}
		delete self.stretchAnimator;
		if (self.onFinished) {
			self.onFinished();
		}
	}
}

// Do not call this directly, only from an initialized WBDrawer object.
// Used to change the height of the WBDrawer object.
WBDrawer.prototype.changeSize = function (width,height,callBack)
{
	this.positionHeightTo = height;
	this.positionWidthTo = width;
	if(callBack){
		this.onFinished = callBack;
	}
	if (this.stretchAnimator) {
		this.stretchAnimator.stop();
		var handler = this.onFinished;
		this.onFinished = null;
		this.doneStretching();
		this.onFinished = handler;
	}
	if (window.widget && ((this.positionHeightFrom - this.beginHeight) < this.positionHeightTo)) {
		window.resizeTo(window.innerWidth, this.positionHeightTo + this.windowResizeOffsetY);
	}
	if (window.widget && ((this.positionWidthFrom - this.beginWidth) < this.positionWidthTo)) {
		window.resizeTo(this.positionWidthTo + this.windowResizeOffsetX,window.innerHeight);
	}
	this.stretchAnimator = new AppleAnimator(this.duration, 13,.01,1, this.nextFrame);
	this.stretchAnimator.oncomplete = this.doneStretching;
	this.stretchAnimator.start();
}

// Do not call this directly, only from an initialized WBDrawer object.
// Changes the height back to the original starting point.
WBDrawer.prototype.close = function (callback)
{
	this.changeSize(0,0,callback);
}

// Do not call this directly, only from an initialized WBDrawer object.
// Changes onlythe height of the drawer.
WBDrawer.prototype.changeHeight = function (height,callback)
{
	this.changeSize(this.positionWidthFrom-this.beginWidth,height,callback);
}

// Do not call this directly, only from an initialized WBDrawer object.
// This tells us if the object is stretched or not.
WBDrawer.prototype.isNotStretched = function() {
	if(this.stretchAnimator == null){
		return ((this.positionHeightFrom == this.beginHeight) && (this.positionWidthFrom == this.beginWidth));
	}
	else { return false; }
}