///////////////////////
// Todo: support za opero
///////////////////////
// config
var anchor_speed = 10;
var anchor_smooth = 10;
var anchor_stop_tolerance = 1;
///////////////////////
var anchor_timer = new Object();
///////////////////////

function anchor_initialize()
	{
	var arr_elem = new Array();
	var tag_name = "a";
	
	if (tag_name == "*" && document.all)
		{arr_elem = document.all;}
	else
		{arr_elem = document.getElementsByTagName(tag_name);}
	
	var href_value = "";
	
	//fukin IE fix
	var curr_link = location.href;
	var hash_pos = curr_link.indexOf("#");
	if (hash_pos >= 0)
		{curr_link = curr_link.substr(0,curr_link.indexOf("#"));}
	
	for (var i = 0;i < arr_elem.length;i++)
		{href_value = arr_elem[i].getAttribute("href");
		
		if (href_value) //fukin IE fix
			{href_value = href_value.replace(curr_link,"");}
		
		if (!isOpera && href_value && href_value.substr(0,1) == "#")
			{arr_elem[i].onclick = function() {anchor_scroll(this); return false;};}
		}
	}

function anchor_scroll(link_obj)
	{
	var anchor_value = link_obj.getAttribute("href");
	anchor_value = anchor_value.substr(anchor_value.indexOf("#") + 1);
	
	var dest_pos = 0;
	if (anchor_value && document.getElementById(anchor_value))
		{
		//if (!isIE) //fukin IE fix
			//{window.location.hash = anchor_value;}
		
		var anchor_obj = document.getElementById(anchor_value);
		
		do {dest_pos += anchor_obj.offsetTop;}
		while (anchor_obj = anchor_obj.offsetParent)

		//page height
		var page_height = 0;
		if (window.innerHeight && window.scrollMaxY) {page_height = window.innerHeight + window.scrollMaxY;} 
		else if (document.body.scrollHeight > document.body.offsetHeight) {page_height = document.body.scrollHeight;} 
		else {page_height = document.body.offsetHeight;}
		
		//viewport height
		var viewport_height = 0;
 		if (typeof window.innerWidth != 'undefined') 
			{viewport_height = window.innerHeight;}
		else if (typeof document.documentElement != 'undefined'
     && typeof document.documentElement.clientWidth !=
     'undefined' && document.documentElement.clientWidth != 0)
 			{viewport_height = document.documentElement.clientHeight;}
 		else
			{viewport_height = document.getElementsByTagName('body')[0].clientHeight;}

		if (dest_pos > page_height - viewport_height) {dest_pos = page_height - viewport_height;}
		
		anchor_scroll_loop(dest_pos);
		}
	else if (anchor_value == "top")
		{anchor_scroll_loop(0);}
	}

function getDocHeight() {
    var D = document;
    return Math.max(
        Math.max(D.body.scrollHeight, D.documentElement.scrollHeight),
        Math.max(D.body.offsetHeight, D.documentElement.offsetHeight),
        Math.max(D.body.clientHeight, D.documentElement.clientHeight)
    );
}

function anchor_scroll_loop(dest_pos)
	{
	var step = (dest_pos - Scroll_Y) / anchor_smooth;
	if (Math.abs(step) < 1) {step = Math.ceil(step);}
	
	var new_pos = Scroll_Y + step;
	
	clearTimeout(anchor_timer);
	//window.status = "new pos: " + new_pos + " dest pos: " + dest_pos;
	
	if (new_pos >= dest_pos - anchor_stop_tolerance && new_pos <= dest_pos + anchor_stop_tolerance)
		{new_pos = dest_pos;
		
		window.scrollTo(Scroll_X, new_pos);
		Scroll_Y = new_pos;
		}
	else
		{window.scrollTo(Scroll_X, new_pos);
		Scroll_Y = new_pos;
		
		anchor_timer = setTimeout("anchor_scroll_loop(" + dest_pos + ");",anchor_speed);
		}
	
	window.status = Scroll_Y;
	}
