//load.js

var LoadPane = new Class({

	initialize: function(el,options){
		options = options || {};
		this.cnt = $(el);
		if (options.url)
			this.setUrl(options.url);
	},

	setUrl: function(url) {
		this.url = url;
		this.reload();
	},

	reload: function() {
		if (this.url) {
			var conn = new Ajax(this.url,{method:'get',update:this.cnt,onComplete:this.update.bind(this)});
			conn.request();
		}
	},

	update: function() {
	}
});

// scroll.js

var ScrollPane = new Class({

	initialize: function(el,options){
		options = options || {};
		var scrollWidth = options.scrollWidth || '16px';
		this.pane = $(el);
		var cnt = this.pane.getFirst();
		this.pane.removeChild(cnt);
		this.pane.setHTML('<div class="scrollViewport" style="overflow:hidden;height:100%;width:0;"><div class="scrollContent" style="top:0;left:0;position:relative;"></div></div><div class="scrollBar" style="left:0;height:100%;"><table class="scrollBar" cellspacing="0" cellpadding="0" style="width:100%;height:100%;"><tr><td class="scrollBarButtonUp" /></tr><tr><td class="scrollBarDrag" style="vertical-align:top"><div class="scrollHandle" style="position:relative;top:0;left:0;width:100%;font-size:10px" /></td></tr><tr><td class="scrollBarButtonDn" /></tr></table></div>');
		var viewport = this.pane.getElement('.scrollViewport');
		this.content = this.pane.getElement('.scrollContent');
		this.content.appendChild(cnt);
		if (options.height) {
			viewport.setStyle('height',options.height);
			this.pane.setStyle('height',options.height);
		}
		if (options.width)
			this.pane.setStyle('width',options.width);
		var bar = this.pane.getElement('div.scrollBar').setStyle('width',scrollWidth);
		this.pane.getElement('.scrollBarDrag').onclick = this.page.bind(this);
		this.handle = this.pane.getElement('.scrollHandle').setStyle('height',scrollWidth);
		this.handle.onmousedown = this.start.bind(this);
		var	btnUp = this.pane.getElement('.scrollBarButtonUp').setStyle('height',scrollWidth);
		btnUp.onmousedown = this.scrollUpStart.bind(this);
		btnUp.onmouseup = this.scrollEnd.bind(this);
		btnUp.onmouseout = this.scrollEnd.bind(this);
		var	btnDn = this.pane.getElement('.scrollBarButtonDn').setStyle('height',scrollWidth);
		btnDn.onmousedown = this.scrollDnStart.bind(this);
		btnDn.onmouseup = this.scrollEnd.bind(this);
		btnDn.onmouseout = this.scrollEnd.bind(this);
		this.yMax = bar.clientHeight-3*this.handle.offsetHeight;
		this.barTop = this.pane.getElement('.scrollBarDrag').getTop();
		if (options.url)
			this.setUrl(options.url);
		else
			this.update();
	},

	update: function() {
		this.cntRange = this.content.offsetHeight-this.pane.clientHeight;
		this.cntPage = this.pane.clientHeight*this.yMax/this.cntRange;
		if (this.cntRange <= 0) {
			this.pane.getElement('div.scrollBar').setStyle('display','none');
			this.pane.getElement('div.scrollViewport').setStyle('width',this.pane.clientWidth+'px');
		}
		else {
			var bar = this.pane.getElement('div.scrollBar').setStyle('display','block');
			this.pane.getElement('div.scrollViewport').setStyle('width',(this.pane.clientWidth-bar.offsetWidth-2)+'px');
		}
		this.scrollTo(0);
	},

	start: function(evt){
		evt = evt || window.event;
		this.offsetY = evt.clientY+this.getScrollTop()-this.barTop-this.handle.getStyle('top').toInt();
		document.onmousemove = this.drag.bind(this);
		document.onmouseup = this.end.bind(this);
		document.body.onselectstart = function () {return false;};
		return false;
	},

	drag: function(evt){
		evt = evt || window.event;
		if (!evt.button && !evt.which) {
			this.end();
			return false;
		}
		var y = evt.clientY+this.getScrollTop()-this.barTop-this.offsetY;
		this.scrollTo(y);
	},

	scrollTo: function(y) {
		if (y < 0)
			y = 0;
		else if (y > this.yMax)
			y = this.yMax;
		this.handle.setStyle('top',y+'px');
		this.content.setStyle('top',-y*this.cntRange/this.yMax+'px');
	},

	end: function(){
		document.onmousemove = null;
		document.onmouseup = null;
		document.body.onselectstart = null;
	},

	scrollUpStart: function(evt) {
		this.lineUp();
		this.timer = this.lineUp.delay(300,this);
	},

	lineUp: function() {
		var	y = this.handle.getStyle('top').toInt();
		if (y > 0)
			y -= 3;
		this.scrollTo(y);
		if (this.timer)
			this.timer = this.lineUp.delay(15,this);
	},

	scrollDnStart: function(evt) {
		this.lineDn();
		this.timer = this.lineDn.delay(300,this);
	},

	lineDn: function(evt) {
		var	y = this.handle.getStyle('top').toInt();
		if (y < this.yMax)
			y += 3;
		this.scrollTo(y);
		if (this.timer)
			this.timer = this.lineDn.delay(15,this);
	},

	scrollEnd: function(evt) {
		clearTimeout(this.timer);
		this.timer = null;
	},

	getScrollTop: function() {
		return document.body.scrollTop || window.pageYOffset || 0
	},

	page: function(evt) {
		evt = evt || window.event;
		var y = evt.clientY+this.getScrollTop()-this.barTop;
		var	current = this.handle.getStyle('top').toInt();
		if (y < current)
			this.scrollTo(current-this.cntPage);
		else if (y > current+this.handle.clientHeight)
			this.scrollTo(current+this.cntPage);
	},

	setUrl: function(url) {
		this.url = url;
		this.reload();
	},

	reload: function() {
		if (this.url) {
			var conn = new Ajax(this.url,{method:'get',update:this.content,onComplete:this.update.bind(this)});
			conn.request();
		}
	}
});

// remote.js

var Remote = new Class({

	initialize: function(url,options) {
		this.url = url;
		if (options && options.onComplete) {
			this.onCompleteUsr = options.onComplete;
		}
	},

	call: function(args) {
		if (this.url) {
			var argsUrl = '';
			for (i in args) {
				var type = $type(args[i]);
				if (type == 'string' || type == 'number') {
					if (argsUrl != '')
						argsUrl += '&';
					argsUrl += i + '=' + encodeURIComponent(args[i]);
				}
			}
			var conn = new Ajax(this.url+'?'+argsUrl,{method:'get',onComplete:this.remoteComplete.bind(this)});
			conn.request();
		}
	},

	getElement: function(name) {
		if (this.result == null)
			return '';
		var	el = this.result.getElementsByTagName(name)[0];
		if (el && el.firstChild) {
			return el.firstChild.nodeValue;
		}
		return '';
	},

	remoteComplete: function(resultText,resultXML) {
		this.result = resultXML;
		var resultType = this.getElement('ResultType');
		if (resultType == 'Error') {
			var msg = this.getElement('Message');
			if (msg) {
				msg = msg.replace(/\<br\>/ig,'\n');
			}
			else {
				msg = 'Error';
			}
			alert(msg);
		}
		else if (resultType == 'Redirect') {
			document.location = this.getElement('Message');
		}
		else if (this.onCompleteUsr) {
			this.onCompleteUsr.pass(this).delay(10);
		}
	}

});

/**
 * Event handler when mouse cursor is over a toolbar button
 *
 * @access public
 * @return void
 */
function buttonOvr(el)
{
	$(el).addClass('TButtonHvr');
}

/**
 * Event handler when mouse cursor leaves a button
 *
 * @access public
 * @return void
 */
function buttonOut(el)
{
	$(el).removeClass('TButtonHvr');
}

