/* _libr.js 2.0 */

//element handlers
function $(input) {
	var element = document.getElementById(input);
	element.event = $event;
	element.transform = $transform;
	element.opacity = $opacity;
	element.previous_node = $previous_node;
	element.next_node = $next_node;
	return element;
}

function $event(action, result) {
	if(this.addEventListener) {
		this.addEventListener(action, result, false);
	}
	else if(this.attachEvent) {
		return this.attachEvent('on'+action, result);
	}
}
function $transform(property, change, offset) {
	var value;
	if(this.currentStyle) {
		value = this.currentStyle[property];
	}
	else if (document.defaultView && document.defaultView.getComputedStyle) {
		value = document.defaultView.getComputedStyle(this, '')[property];
	}
	value = value.match(/\w[0-9]/);
	try { value = parseInt(value[0]); } catch(e) { value = this.offsetHeight; }
	var id = this.id;
	var lt_id = Math.random()*1000;
	var timer = setInterval(function() {
		if(_libr_transform == 0) {
			_libr_transform = lt_id;
		}
		if(_libr_transform == lt_id) {
			if(value > change) {
				value--;
			}
			if(value < change) {
				value++;
			}
			$(id).style[property] = value + 'px';
			if(value == change) {
				clearInterval(timer);
				_libr_transform = 0;
			}
		}
	}, offset);
}
var _libr_transform = 0;

function $opacity(value, offset) {
	if(/*@cc_on!@*/false) {
		var i = this.style.filter.match(/(alpha\(opacity=)(.*)(\))/);
		if(i instanceof Array) {
			i = i[2]/10;
		}
		else {
			i = 10;
		}
	}
	else {
		var i = this.style.opacity*10;
	}
	var id = this.id;
	if(offset == null) {
		this.style.opacity = value/10;
		this.style.filter = 'alpha(opacity=' + value*10 + ')';
		this.style.zoom = 1;
	}
	else {
		var timer = setInterval(function() {
			if(i > value) {
				i--;
			}
			if(i < value) {
				i++;
			}
			$(id).style.opacity = i/10;
			$(id).style.filter = 'alpha(opacity=' + i*10 +')';
			$(id).style.zoom = 1;
			if(i == value) {
				clearInterval(timer);
			}
		}, offset);
	}
}
function $previous_node() {
	var node = this.previousSibling;
	while(node.nodeType == 3) {
		node = node.previousSibling;
	}
	return node;
}
function $next_node() {
	var node = this.nextSibling;
	while(node.nodeType == 3) {
		node = node.nextSibling;
	}
	return node;
}

//php-style array
function $array() {
	var arr = new Array()
	for(var i = 0; i < arguments.length; i++) {
		arr[i] = arguments[i];
	}
	return arr;
}

//ajax
function $fetch(url, arr, response) {
	var parameters = '';
	for(var i = 0; i < arr.length; i++) {
		var parameter = arr[i].split(':', 1);
		var j = parameter[0].length;
		j++;
		var value = encodeURIComponent(arr[i].slice(j));
		parameters += parameter[0] + '=' + value + '&';
	}
	try {
		var ajax_request = new XMLHttpRequest();
	}
	catch(e) {
		alert('Sorry, your browser doesn\'t support AJAX.');
		return false;
	}
	ajax_request.onreadystatechange = function() {
		if(ajax_request.readyState == 4) {
			response(ajax_request.responseText);
		}
	}
	ajax_request.open('POST', url, true);
	ajax_request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	ajax_request.send(parameters); 
}

//random number between (inclusive)
function $random(from, to) {
	return Math.floor(Math.random() * (to - from + 1) + from);
}
