dojo.require("dojo.parser");
dojo.require("dojo.fx");

function wipeIn(node, duration, onEnd, beforeBegin){
	//onsole.log(node.id+'::wipeIn');
	new dojo.fx.wipeIn({
		node:		node,
		duration:	duration,
		onEnd:		onEnd,
		beforeBegin:beforeBegin
	}).play();
}

function wipeOut(node, duration, onEnd, beforeBegin){
	//onsole.log(node.id+'::wipeOut');
	new dojo.fx.wipeOut({
		node:		node,
		duration:	duration,
		onEnd:		onEnd,
		beforeBegin:beforeBegin
	}).play();
}

function fadeIn(node, duration, onEnd, beforeBegin){
	//onsole.log(node.id+'::fadeIn');
	if( !duration) { duration = 200; }
	new dojo.fadeIn({
		node:		node,
		duration:	duration,
		onEnd:		onEnd,
		beforeBegin:beforeBegin
	}).play();
}


function fadeOut(node, duration, onEnd, beforeBegin){
	//onsole.log(node.id+'::fadeOut');
	if( !duration) { duration = 200; }
	new dojo.fadeOut({
		node:		node,
		duration:	duration,
		onEnd:		onEnd,
		beforeBegin:beforeBegin
	}).play();
}

function slideTo(node, topOffset, leftOffset) {
	dojo.fx.slideTo({
		node:	node,
		top:	topOffset,
		left:	leftOffset,
		duration:	200
	}).play();
	
}


function displayMsg(dialog, msg, status, displayTime)
{
	if(dialog)
	{
		dialog.setContent("<div class='msg "+status+"'>" +msg+ "</div>");
		dialog.show();
		if( !displayTime ) { displayTime = 5; }
		dialog.lifetime = displayTime*1000;
		setTimeout("dijit.byId('"+dialog.id+"').hide()", dialog.lifetime);
	}
	else
	{
		switch(status){
			case 'error'	: status = 'Błąd:'; break;
			case 'ok'		: status = ''; break;
			case 'info'		: status = ''; break;
			default			: status = '';
		}
		alert(status + ': ' + msg);
	}
}

function displayDialogMsg(msg, status, duration)
{
	displayMsg(new dijit.Dialog(), msg, status, duration);
}

function refreshTitlePane( args)
{
	//onsole.log('refreshTitlePane');
	if( args === undefined || args.id === undefined)
	{
		return false;
	}
	
	var tp = dijit.byId( args.id);
	var doRefresh = true;
	
	if(!tp) { return false; }
	
	if( args.href){ tp.setHref(args.href); }
	else{ tp.setHref(tp.href); }
	if( args.title){ tp.setTitle(args.title); }
	
	if( args.content)
	{
		tp.setContent(args.content);
		doRefresh = false;
	}
	
	if( doRefresh){ tp.refresh(); }
}

function inProgressIcon(){
	return "<div class='in-progress'></div>";
}

function showInProgressIcon(layerId)
{
	setInnerHtml( layerId, inProgressIcon());
}

function LineMarginAdjuster(lineNode, containerNode, containerDefaultWidth, destWidth)
{
	this._status = 'paused';
	this._defWidth = containerDefaultWidth;
	this._width = destWidth;
	this._stepForward = 100;
	this._stepBack = 40;
	this._state = 'minimized';	// 'minimized' 'maximized' 'minimizing' 'maximizing'
	this.handleAfter = null;
	this.play = function()
	{
		this._status = 'playing';
		
		var that = this;
		
		var _adjustMargin = function()
		{
			if (that.status() == 'playing') {//}&& lineNode.style.marginLeft >= that._width*-1) {//
				//	jesli jeszcze trzeba sie rozwijac
				//onsole.log("containerNode["+(containerNode.id)+"]width="+containerNode.style.width);
				if (containerNode.clientWidth != that._width) {
					var step = (containerNode.clientWidth < that._width) ? that._stepForward : that._stepBack * -1;
					var tWidth = containerNode.clientWidth + step;

					//	okreslenie stanu, w ktorym znajudje sie element
					if (step<0) {
						that._state = 'minimizing';
					} else {
						that._state = 'maximizing';
					}
					
					// przeskoczyles docelowa szerokosc lub zszedles ponizej docelowej szerokosci
					if ((step>0 && tWidth>that._width) || (step<0 && tWidth<that._width)) {
						tWidth = that._width;
					}
					
					lineNode.style.marginLeft = (tWidth - that._defWidth) + 'px';
					containerNode.style.width = tWidth+'px';
					//onsole.log('_adjustMargin('+lineNode.id+','+containerNode.id+','+lineNode.style.marginLeft+') containerWidth:'+containerNode.style.width);
					setTimeout(_adjustMargin,60);
				} else {
					that._status = 'paused';
					//	okreslenie stanu, w ktorym znajudje sie element
					if (that._state == 'minimizing') {
						that._state = 'minimized';
					} else if (that._state == 'maximizing') {
						that._state = 'maximized';
					}
				}
			} else {
				if (that.handleAfter instanceof Function) {
					that.handleAfter();
				}
				//onsole.log(lineNode.id+' stopped');
			}
		}
		
		if (containerNode.clientWidth == 0) {
			setTimeout(_adjustMargin,250);
		} else {
			_adjustMargin();
		}
	}
	
	this.pause = function()
	{
		this._status = 'paused';
	}
	
	this.status = function()
	{
		return this._status;
	}
}

