var PB_FADE_UP = 0;
var PB_FADE_DOWN = 1;
function PB_fader(el)
{
	this.fadeItem = el;
	this.timeout = null;
	this.opacity = 0;
	this.dir = PB_FADE_UP;
	this.step = 8;
	this.startOpacity = 0;
	this.stopOpacity = 100;
	this.speed = 1;

	this.fade = ctxFunction(this, PB_fade);
	this.OnFadeComplete = null;
}
PB_fader.prototype.oncomplete = PB_fader_oncomplete;
PB_fader.prototype.start = PB_fade_start;
PB_fader.prototype.stop = PB_fade_stop;

function PB_fader_oncomplete()
{
	this.timeout = null;

	if(this.OnFadeComplete && typeof(this.OnFadeComplete) == "function")
		this.OnFadeComplete();

	//this.fadeItem = null;
}

function PB_fade_stop()
{
	if(this.timeout)
	{
		window.clearTimeout(this.timeout);
		this.timeout = null;
	}
}

function PB_fade_start(start, stop, step)
{
	if(this.dir == PB_FADE_UP)
	{
		this.step = 8;
		this.opactiy = 0;
		this.startOpacity = 0;
		this.stopOpacity = 100;
	}
	else if(this.dir == PB_FADE_DOWN)
	{
		this.step = -12;
		this.opacity = 100;
		this.startOpacity = 100;
		this.stopOpacity = 0
	}

	this.fade();
}

function PB_fade()
{
	if(!this.fadeItem) return;

	this.opacity += this.step;
	var opacity = this.opacity;
	if(this.dir==PB_FADE_UP && opacity > this.stopOpacity) opacity = this.stopOpacity;
	if(this.dir==PB_FADE_DOWN && opacity < this.stopOpacity) opacity = this.stopOpacity;

	this.fadeItem.style.opacity = (opacity/100);
	this.fadeItem.style.filter = "progid:DXImageTransform.Microsoft.Alpha(opacity=" + opacity +")";
	if((this.dir==PB_FADE_UP && opacity < this.stopOpacity) ||
		(this.dir==PB_FADE_DOWN && opacity > this.stopOpacity))
		this.timeout = window.setTimeout(this.fade, 1);
	else
		this.oncomplete();
}

var PB_SIZE_UP = 0;
var PB_SIZE_DOWN = 1;
function PB_sizer(el)
{
	this.el = el;
	this.h = 0;
	this.startSize = 0;
	this.stopSize = 0;
	this.sizeToContainer = null;
	this.timeout = null;
	this.dir = null;
	this.step = 3;
	
	this.size = ctxFunction(this, PB_resize);
	this.OnResizeComplete = null;
}

PB_sizer.prototype.stop =
function()
{
	if(this.timeout)
	{
		window.clearTimeout(this.timeout);
		this.timeout = null;
	}
}

PB_sizer.prototype.start =
function()
{
	this.el.style.height = this.startSize + "px";
	if(this.sizeToContainer)
		this.stopSize = this.sizeToContainer.offsetHeight;
	if(this.startSize >= this.stopSize)
	{
		this.dir = PB_SIZE_DOWN;
	}
	else
	{
		this.dir = PB_SIZE_UP;
	}
	this.h = this.startSize;
	this.size();
}

PB_resize =
function()
{
	if(!this.el) return;

	this.h += this.getStep();
	if(this.sizeToContainer && this.stopSize != this.sizeToContainer.offsetHeight)
		this.stopSize = this.sizeToContainer.offsetHeight;

	if(this.dir==PB_SIZE_UP && this.h > this.stopSize) this.dir=PB_SIZE_DOWN; //this.h = this.stopSize;
	if(this.dir==PB_SIZE_DOWN && this.h < this.stopSize) this.dir=PB_SIZE_UP; //this.h = this.stopSize;

	this.el.style.height = this.h + "px";
	if((this.dir==PB_SIZE_UP && this.h < this.stopSize) ||
		(this.dir==PB_SIZE_DOWN && this.h > this.stopSize))
	{
		this.timeout = window.setTimeout(this.size, 1);
	}
	else
	{
		this.oncomplete();
	}

}

PB_sizer.prototype.oncomplete =
function()
{
	this.timeout = null;

	if(this.OnResizeComplete && typeof(this.OnResizeComplete) == "function")
		this.OnResizeComplete();

	//this.fadeItem = null;
}

PB_sizer.prototype.getStep =
function()
{
	var diff = Math.abs(this.h - this.stopSize);
	if(diff > 50) this.step = 20;
	if(diff > 30) this.step = 10;
	else if(diff > 10) this.step = 5;
	else if(diff > 5) this.step = 3;
	else if(diff > 0) this.step = 1;

	if(this.dir == PB_SIZE_DOWN)
		this.step = this.step * -1;

	return this.step;
}


function popup()
{
	this.div = null;
	this.classname = null;

	this.checkclose = ctxFunction(this, popup_checkClose);
	this.setupclose = ctxFunction(this, popup_setupClose);
	this.close = ctxFunction(this, popup_close);
}

popup.prototype.create =
function(x, y)
{
	var div = document.createElement("DIV");
	this.div = div;
	div.style.top = y + "px";
	div.style.left = x + "px";
	div.className = "popup";
	document.body.appendChild(div);

	var closebtn = document.createElement("DIV");
	closebtn.style.position = "absolute";
	closebtn.style.top = "3px";
	closebtn.style.right = "3px";
	closebtn.style.border = "1px dashed #000000";
	closebtn.style.cursor = "pointer";
	closebtn.style.height = "15px";
	closebtn.style.width = "15px";
	closebtn.style.textAlign = "center";
	closebtn.style.fontSize = "8pt";
	closebtn.style.fontWeight = "bold";
	closebtn.innerHTML = "X";
	AttachDOMEvent(closebtn, "click", this.close);
	div.appendChild(closebtn);

	AttachDOMEvent(document.body, "mouseup", this.setupclose);
}

function popup_setupClose(e)
{
	DetachDOMEvent(document.body, "mouseup", this.setupclose);
	AttachDOMEvent(document.body, "click", this.checkclose);
}

function popup_checkClose(e)
{
	var target = e.target || e.srcElement;
	if(isChildElement(target, this.div)) return true;

	this.close();
}

function popup_close()
{
	document.body.removeChild(this.div);
	this.div = null;
	DetachDOMEvent(document.body, "click", this.checkclose);
}

function isChildElement(el, prt)
{
	var element = el;
	while(element.parentNode)
	{
		if(element == prt) return true;
		element = element.parentNode;
	}

	return false;
}

