
/* TurnTo23.com scripts */
// Begin national js scripts


/** 
 * @fileoverview IB SmoothResizer: Animates resizing of a block-level element with 
 *	"soft" deceleration.  2 states are available: small
 *	and large.  It spits out three different class names
 *	for display of content - smallClass, expandingClass, and 
 *	largeClass. Copyright 2006, Internet Broadcasting Inc.
 *	example usage: 
 *	var resizeThing = new SmoothResizer(myelement,40,40,400,400,20,"small","expanding","large");
 *	document.getElementById("controlSwitch").onclick = resizeThing.go();
 * 
 * @author breisinger / Internet Broadcasting, Inc.
 */

/**
 * Construct a new IbsSlider
 * @class This is the object that contains copies of all of the functions and 
 * 	variables it needs to work. 
 * @constructor
 * @param {String} element Id of element to resize
 * @param {Integer} minW Small size width
 * @param {Integer} minH Small size height
 * @param {Integer} maxW Large size width
 * @param {Integer} maxH Large size height
 * @param {Integer} speed Speed of resizing animation (between 15 and 50 is recommended)
 * @param {String} smallClass Class name of @element in small state
 * @param {String} expandingClass Class name of @element while expanding/contracting
 * @param {String} largeClass Class name of @element in enlarged state
 * @param {FunctionObject} onEnlarge Function Object to fire after enlarging
 * @return A new instance of IB SmoothResizer
 */
function SmoothResizer(element,minW,minH,maxW,maxH,speed,smallClass,expandingClass,largeClass,onEnlarge,onShrink) {
	this.element = element;
	this.minWidth = minW;
	this.minHeight = minH;
	this.speed = speed;
	this.maxWidth = maxW;
	this.maxHeight = maxH;
	this.onEnlarge = onEnlarge ? onEnlarge : false;
	this.onShrink = onShrink ? onShrink : false;
	///////////////////////////////////
	///////////////////////////////////
	var self = this;
	this.smallClass = smallClass;
	this.largeClass = largeClass;
	this.expandingClass = expandingClass;
	this.expanded = false;
	this.timer;
	this.curSpeed = this.speed;
	this.currentWidth = this.minWidth;
	this.currentHeight = this.minHeight;
	this.wToHRatio = (this.maxHeight - this.minHeight) / (this.maxWidth - this.minWidth);	
	this.currentHeightFloat = this.currentHeight; //floating point representation of height
	///////////////////////////////////
	///////////////////////////////////
	this.updateSize = function(w,h) {
		this.element.style.height = h + "px";
		this.element.style.width = w + "px";
	}
	this.go = function() {
		self.element.className = self.expandingClass;
		if (parseInt(self.element.style.width) == self.maxWidth) {
			self.shrink();
		} else {
			self.grow();
		}
	}
	this.calcVariableSpeed = function(step) {
		var newSpeed = this.curSpeed;
		var percentComplete = 0.0;
		percentComplete = (this.currentWidth - this.minWidth)/(this.maxWidth - this.minWidth);
		//flip percent if shrinking
		if(step == "shrink") {
			percentComplete = 1 - percentComplete;
		}
		percentComplete = parseInt(100 * percentComplete);
		if (percentComplete > 60) {
			if(percentComplete > 80) {
				if (percentComplete > 90) {
					newSpeed = 3;
				} else {
					newSpeed = 5;
				}
			} else {
				newSpeed = 7;
			}
		} else {
			newSpeed = speed;
		}
		return newSpeed;
		
	}
	
	//step = string, "grow" or "shrink"
	this.calculateHeight = function(step) {
		var newHeight = this.currentHeightFloat;
		if(step == "grow") {
			this.currentHeightFloat += this.wToHRatio * this.curSpeed;
			newHeight = Math.round(this.currentHeightFloat);
			if(newHeight >= this.maxHeight) {
				newHeight = this.maxHeight;
			}
		} else if (step =="shrink") {
			this.currentHeightFloat -= this.wToHRatio * this.curSpeed;
			newHeight = Math.round(this.currentHeightFloat);
			if(newHeight <= this.minHeight) {
				newHeight = this.minHeight;
			}
		}
		return newHeight;
	}
	
	//step = string, "grow" or "shrink"
	this.calculateWidth = function(step) {
		var newWidth = this.currentWidth;
		if(step == "grow") {
			newWidth = this.currentWidth + this.curSpeed;
			if(newWidth > this.maxWidth) {
				return this.maxWidth;
			}
		} else if (step == "shrink") {
			newWidth = this.currentWidth - this.curSpeed;
			if(newWidth < this.minWidth) {
				return this.minWidth;
			}
		}
		return newWidth;
	}
	this.grow = function() {
		self.timer = window.setInterval(function() {
			self.curSpeed = self.calcVariableSpeed("grow");
			self.currentWidth = self.calculateWidth("grow");
			self.currentHeight = self.calculateHeight("grow");
			//Finished enlarging
			if(self.currentWidth == self.maxWidth && self.currentHeight == self.maxHeight) {
				self.updateSize(self.currentWidth,self.currentHeight);
				window.clearInterval(self.timer);
				self.expanded = true;
				self.element.className = self.largeClass;
				if(self.onEnlarge) {
					self.onEnlarge();
				}
			}
			self.updateSize(self.currentWidth,self.currentHeight);
		},20);
	}
	this.shrink = function() {
		self.timer = window.setInterval(function() {
			self.curSpeed = self.calcVariableSpeed("shrink");
			self.currentWidth = self.calculateWidth("shrink");
			self.currentHeight = self.calculateHeight("shrink");
			//finished shrinking
			if (self.currentWidth == self.minWidth && self.currentHeight == self.minHeight) {	
				self.updateSize(self.currentWidth,self.currentHeight);
				window.clearInterval(self.timer);
				self.expanded = false;
				self.element.className = self.smallClass;
				if(self.onShrink) {
					self.onShrink();
				}
			}
			self.updateSize(self.currentWidth,self.currentHeight);
		},20);
	}
}


// End national_js scripts

