/*
Copyright (c) 2009 Marcello Mamino

Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
of the Software, and to permit persons to whom the Software is furnished to do
so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/

var Dom_utils = function(){with(Utils){

	var getElementsByClassName = function(root_obj, tag_name, class_name)
	{
		return filter(function(e) {return e.className == class_name;},
				root_obj.getElementsByTagName(tag_name));
	}
	
	var get_ancestors = function(obj)
	{
		if(obj) {
			var x = get_ancestors(obj.offsetParent);
			x.push(obj);
			return x;
		} else return new Array();
	}
	
	var get_offset = function(obj)
	{
		var l = obj.offsetLeft?obj.offsetLeft:0;
		var t = obj.offsetTop?obj.offsetTop:0;
		return [l,t];
	}
	
	var remove_border = function(obj)
	{
		with(obj.style) {
			border = "none";
			padding = "0px";
			margin = "0px";
		}
		return obj;
	}
	
	var absolute_zero = function(obj)
	{
		with(obj.style) {
			position = "absolute";
			top = "0px";
			left = "0px";
		}
		return obj;
	}
	
	var set_position = function(obj,pos)
	{
		with(obj.style) {
			left = Math.floor(pos[0])+"px";
			top = Math.floor(pos[1])+"px";
		}
	}
	
	var is_in_viewport = function(e)
	{
		// f_scrollTop() and f_clientHeight() are in size_and_scroll.js
		var p = get_position(e);
		var top = f_scrollTop();
		var bottom = top + f_clientHeight();
		return (top <= p[1]) && (p[1]+e.offsetHeight <= bottom);
	}
	
	var get_position = function(obj)
	{
		return reduce(vector_plus,map(get_offset,get_ancestors(obj)));
	}
	
	var make_invisible = function(obj)
	{
		obj.style.visibility = "hidden";
		return obj;
	}
	
	var make_visible = function(obj)
	{
		obj.style.visibility = "";
		return obj;
	}

	var filter_text = function(f,obj)
	{
		//if(obj.nodeType == obj.TEXT_NODE)
		if(obj.nodeType == 3) // Because we love IE
			obj.nodeValue = f(obj.nodeValue);
		else
			map(function(x){filter_text(f,x);},obj.childNodes);
	}
	
	var replace_text = function(obj,search,replace)
	{
		filter_text(function(x){return x.replace(search,replace);},obj);
	}

	return {
		getElementsByClassName:	getElementsByClassName,
		get_offset:		get_offset,
		remove_border:		remove_border,
		absolute_zero:		absolute_zero,
		set_position:		set_position,
		is_in_viewport:		is_in_viewport,
		get_position:		get_position,
		make_invisible:		make_invisible,
		make_visible:		make_visible,
		replace_text:		replace_text
	}

}}();
