//aquarius :: a javascript library

//select element
function $() {
	if(arguments.length === 1) {
		var elements = document.getElementById(arguments[0]);
	}
	else {
		var elements = [];
		for(var i = 0; i < arguments.length; i++) {
			elements[i] = document.getElementById(arguments[i]);
		}
	}
	return $object(elements);
}

//add custom functions to elements - would prefer prototype, shit sucks
function $object(elements) {
	elements.listen = $listen;
	elements.submit = $submit;
	elements.opacity = $opacity;
        elements.transform_xy = $transform_xy;
	return elements;
}

//add event listerner(s) to element
function $listen(action, result) {
	if(this instanceof Array) {
		for(var i = 0; i < this.length; i++) {
			$(this[i].id).listen(action, result);
		}
	}
	else {
		if(this.addEventListener) {
			this.addEventListener(action, result, false);
		}
		else if(this.attachEvent) {
			this.attachEvent('on' + action, result);
		}
	}
}

//specialised event listener for enter events on non-form forms
function $submit(result) {
	this.listen('keypress', function(event) {
		if(event.which == 13 || event.keyCode == 13) {
			result();
		}
	});
}

//opacity animation function (super cool)
function $opacity(value, offset) {
	if(this instanceof Array) {
		for(var j = 0; j < this.length; j++) {
			$(this[j].id).opacity(value, offset);
		}
	}
	else {
		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 || 1;
		}
		var element_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++;
				}
				$(element_id).style.opacity = i/10;
				$(element_id).style.filter = 'alpha(opacity=' + i*10 + ')';
				$(element_id).style.zoom = 1;
				if(i == value) {
					clearInterval(timer);
				}
			}, offset);
		}
	}
}

//width/height transformation
function $transform_xy(new_width, new_height, offset) {
    if(this instanceof Array) {
        for(var i = 0; i < this.length; i++) {
            $(this[i].id).transform_xy(new_width, new_height, offset);
        }
    }
    else {
        var cur_width = parseInt(this.offsetWidth);
        var cur_height = parseInt(this.offsetHeight);
        var cur_id = this.id
        var timer = setInterval(function() {
            if(new_width == null) {
                new_width = cur_width;
            }
            else if(new_width > cur_width) {
                cur_width++;
            }
            else if(new_width < cur_width) {
                cur_width--
            }
            if(new_height == null) {
                new_height = cur_height;
            }
            else if(new_height > cur_height) {
                cur_height++;
            }
            else if(new_height < cur_height) {
                cur_height--;
            }
            $(cur_id).style.width = cur_width + 'px';
            $(cur_id).style.height = cur_height + 'px';
            if(cur_width == new_width && cur_height == new_height) {
                clearInterval(timer);
            }
        }, offset);
    }
}

//run ajax
function $ajax(url, post_arr, func) {
	var posts = '';
	for(var i = 0; i < post_arr.length; i++) {
		var post = post_arr[i].split(':', 1);
		var var_length = post[0].length;
		var_length++;
		var value = encodeURIComponent(post_arr[i].slice(var_length));
		posts += post[0] + '=' + value + '&';
	}
	try {
		var ajax_request = new XMLHttpRequest();
	}
	catch(e) {
		return false;
	}
	ajax_request.onreadystatechange = function() {
		if(ajax_request.readyState == 4) {
			func(ajax_request.responseText);
		}
	}
	ajax_request.open('POST', url, true);
	ajax_request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	ajax_request.send(posts);
}
