/* Script created by Andreas Brauchli
 *
 * please support open sourced software
 */

function showPlayers() {
	document.getElementById('musicplayer').style.visibility = 'visible';
	document.getElementById('videoplayer').style.visibility = 'visible';
}

function hidePlayers() {
	document.getElementById('musicplayer').style.visibility = 'hidden';
	document.getElementById('videoplayer').style.visibility = 'hidden';
}

// class ResizeElement {

	function ResizeElement(element) {
		if (element == null){
			alert('caller: '+ResizeElement.caller.toString());
			return;
		}
		this._element = element;
		this._orig_rect[0] = element.offsetLeft;
		this._orig_rect[1] = element.offsetTop;
		this._orig_rect[2] = element.offsetWidth + element.offsetLeft;
		this._orig_rect[3] = element.offsetHeight + element.offsetTop;
	}

	ResizeElement.prototype._element = null;
	ResizeElement.prototype._orig_rect = new Array(); //array x1,y1,x2,y2
	ResizeElement.prototype._fullsized = false;
	ResizeElement.prototype._instance_id = 0;

	ResizeElement.prototype._ppf = 50; // resize by N pixels per frame
	ResizeElement.prototype._fps = 25; // frames per sec
	ResizeElement.prototype._target_rect = new Array(); //array x1,y1,x2,y2

	ResizeElement.prototype.getElement = function() {
		return this._element;
	}

	ResizeElement.prototype.sizeDown = function() {
		if (!this._fullsized)
			return;
		this._cur_rect = null;
		this._target_rect = this._orig_rect;
		this.animateStep();
		/*
			this._element.style.width = this._orig_width + 'px';
			this._element.style.height = this._orig_height + 'px';
			this._element.style.left = this._orig_left + 'px';
			this._element.style.top = this._orig_top + 'px';
			this._element.style.zIndex = 0;
		*/
		this._fullsized = false;
		this.unHideElements();
	}

	ResizeElement.prototype.sizeUp = function() {
		if (this._fullsized)
			return;
		this._cur_rect = null;
		this._target_rect = new Array(0,0,650,650);
		this._element.style.zIndex = "20";
		hidePlayers();
		this.animateStep();

		/*
			this._element.style.width = "650px"; //this._element.parentNode.style.width;
			this._element.style.height = "650px"; //this._element.parentNode.style.height;
			this._element.style.left = 0;
			this._element.style.top = 0;
			this._element.style.zIndex = 1;
		*/
		this._fullsized = true;
		this.unHideElements();
	}

	ResizeElement.prototype.resize = function() {
		if (this._fullsized)
			this.sizeDown();
		else
			this.sizeUp();
	}

	ResizeElement.prototype._cur_rect; //array x1,y1,x2,y2
	ResizeElement.prototype._target_v;

	ResizeElement.prototype.animateStep = function() {
		if (this._cur_rect == null) {
			var x1 = this._element.offsetLeft;
			var y1 = this._element.offsetTop;
			var x2 = this._element.offsetWidth + x1;
			var y2 = this._element.offsetHeight + y1;
			this._cur_rect = new Array(x1, y1, x2, y2);

			var vx1 = (x1 > this._target_rect[0]) ? -1 : 1;
			var vy1 = (y1 > this._target_rect[1]) ? -1 : 1;
			var vx2 = (x2 > this._target_rect[2]) ? -1 : 1;
			var vy2 = (y2 > this._target_rect[3]) ? -1 : 1;
			this._target_v = new Array(vx1, vy1, vx2, vy2);
		}

		var done = 0;
		for (var i=0; i<4; i++) {
			this._cur_rect[i] += Math.round(this._ppf * this._target_v[i]);
			var a = this._cur_rect[i];
			var b = this._target_rect[i];
			if (this._target_v[i] > 0 ? a >= b : a <= b ) {
				this._cur_rect[i] = this._target_rect[i];
				this._target_v[i] = 0;
			}
			if (this._cur_rect[i] == this._target_rect[i])
				done++;
		}

		this._element.style.left = this._cur_rect[0] + "px";
		this._element.style.top = this._cur_rect[1] + "px";
		this._element.style.width = (this._cur_rect[2] - this._cur_rect[0]) + "px";
		this._element.style.height = (this._cur_rect[3] - this._cur_rect[1]) + "px";

		if (done < 4) {
			var method = this;
			window.setTimeout(function() { method.animateStep(); }, Math.round(1000/this._fps));
		}
		else if (!this._fullsized) {
			this._element.style.zIndex = "0";
			showPlayers();
		}
	}

	ResizeElement.prototype.unHideElements = function() {
		var divs = this._element.getElementsByTagName("div");
		for (var i=0; i<divs.length; i++) {
			if (divs[i].className == "maximized")
				divs[i].className = "minimized";
			else if (divs[i].className == "minimized")
				divs[i].className = "maximized";
		}
	}

// } End class ResizeElement

var cur_re = null;

function resize(element) {
	if (element == null || element.parentNode == null)
		return;
	var re;

	if (cur_re != null) {
		if (cur_re.getElement() != element.parentNode) {
			cur_re.sizeDown();
			cur_re = null;
		} else {
			re = cur_re;
		}
	}
	if (re == null)
		re = new ResizeElement(element.parentNode);
	re.resize();
	cur_re = re;
}


