
// JavaScript Document

var userAgent = navigator.userAgent.toLowerCase() ;
var is_opera  = (userAgent.indexOf('opera') != -1) ;
var is_saf    = ((userAgent.indexOf('safari') != -1) || (navigator.vendor == "Apple Computer, Inc.")) ;
var is_webtv  = (userAgent.indexOf('webtv') != -1) ;
var is_ie     = ((userAgent.indexOf('msie') != -1) && (!is_opera) && (!is_saf) && (!is_webtv)) ;

function get_style(id, param) {
	
	var obj = document.getElementById(id) ;	
	
	if (! obj)				return false ;
	
	if (param == "opacity")	return get_opacity(id) ;
	
	return obj.style[param] ;
	
}

function set_style(id, param, value) {
	
	var obj = document.getElementById(id) ;	
	
	if (! obj)				return false ;
	
	if (param == "opacity")	return set_opacity(id, value) ;
	
	obj.style[param] = value ;
	
	return true ;
	
}

function get_opacity(id) { // return [0.0, 1.0]

	var obj = document.getElementById(id) ;
	var value = 1.0 ;
	
	if (is_ie) {
		var regexp = /alpha\(opacity=([0-9]*)\)/ ;
		if (regexp.exec(obj.style.filter))
			value = RegExp.$1 / 100 ;
	} else if (obj.style.opacity != "undefined") {
		
		if (obj.style.opacity != "")
			value = obj.style.opacity ;
	}
	
	return value ;

}

function set_opacity(id, value) { // value [0.0, 1.0]

	var obj = document.getElementById(id) ;
	
	if (value == 0.0 && obj.style.display != "none")
		obj.style.display = "none" ;
	else if (value > 0.0 && obj.style.display != "block")
		obj.style.display = "block" ;
		
	if (is_ie)
		obj.style.filter = value >= 1.0 ? "" : "alpha(opacity=" + (value * 100) + ")" ;
	else if (obj.style.opacity != "undefined")
		obj.style.opacity = value ;
	
	return true ;
	
}

function _fade(id, value, end, offset, remove) {
	
	var obj = document.getElementById(id) ;
	if (! obj) return ;
	
	if (offset == 0.0)
		return ;
	
	if ((offset > 0.0 && value > end) || (offset < 0.0 && value < end))
		value = end ;
		
	set_opacity(id, value) ;
	
	if (value == end) {
		if (remove && obj.parentNode)
			obj.parentNode.removeChild(obj) ;
		return ;
	}
		
	value = parseFloat(value) + parseFloat(offset) ;
	
	obj.fade_timer_id = setTimeout("_fade('" + id + "', " + value + ", " + end + ", " + offset + ", " + remove + ")", 10) ;
	
}

function fade(id, offset, remove) {
	
	//alert(remove) ;
	
	remove = (remove == true) ;
	
	var obj = document.getElementById(id) ;
	if (! obj) return ;
	
	if (obj.fade_timer_id)
		clearTimeout(obj.fade_timer_id) ;
	
	var value	= get_opacity(id) ;
	var end		= offset > 0.0 ? 1.0 : 0.0 ;
	
	_fade(id, value, end, offset, remove) ;
	
}

function _move(id, from_x, from_y, to_x, to_y, offset) {
	
	var obj = document.getElementById(id) ;
	if (! obj) return ;
	
	var curr_x = get_style(id, "left") ;
	var curr_y = get_style(id, "top") ;
	
	curr_x = parseInt(curr_x.substr(0, curr_x.search(/px/i))) ;
	curr_y = parseInt(curr_y.substr(0, curr_y.search(/px/i))) ;
	
	if (curr_x == to_x && curr_y == to_y)
		return ;
		
	var offset_x = Math.ceil(Math.sin(Math.PI * Math.abs((curr_x + (from_x < to_x ? 1 : -1) - from_x) / (to_x - from_x))) * offset) ;
	var offset_y = Math.ceil(Math.sin(Math.PI * Math.abs((curr_y + (from_y < to_y ? 1 : -1) - from_y) / (to_y - from_y))) * offset) ;
	
	if (curr_x < to_x && curr_x + offset_x < to_x) {
		set_style(id, "left", (curr_x + offset_x) + "px") ;
	} else if (curr_x > to_x && curr_x - offset_x > to_x) {
		set_style(id, "left", (curr_x - offset_x) + "px") ;
	} else {
		set_style(id, "left", to_x + "px") ;
	}
	
	if (curr_y < to_y && curr_y + offset_y < to_y) {
		set_style(id, "top", (curr_y + offset_y) + "px") ;
	} else if (curr_y > to_y && curr_y - offset_y > to_y) {
		set_style(id, "top", (curr_y - offset_y) + "px") ;
	} else {
		set_style(id, "top", to_y + "px") ;
	}
	
	obj.move_timer_id = setTimeout("_move('" + id + "', " + from_x + ", " + from_y + ", " + to_x + ", " + to_y + ", " + offset + ")", 10) ;
	
}

function move(id, to_x, to_y, offset) {
	
	var obj = document.getElementById(id) ;
	if (! obj) return ;
	
	if (obj.move_timer_id)
		clearTimeout(obj.move_timer_id) ;
	
	var from_x = get_style(id, "left") ;
	var from_y = get_style(id, "top") ;
	
	from_x = parseInt(from_x.substr(0, from_x.search(/px/i))) ;
	from_y = parseInt(from_y.substr(0, from_y.search(/px/i))) ;
	
	_move(id, from_x, from_y, to_x, to_y, offset) ;
	
}

function _resize(id, from_w, from_h, to_w, to_h, offset_w, offset_h) {
	
	var obj = document.getElementById(id) ;
	if (! obj) return ;
	
	var curr_w = get_style(id, "width") ;
	var curr_h = get_style(id, "height") ;
	
	curr_w = parseInt(curr_w.substr(0, curr_w.search(/px/i))) ;
	curr_h = parseInt(curr_h.substr(0, curr_h.search(/px/i))) ;
	
	if (curr_w == to_w && curr_h == to_h)
		return ;
		
	var _offset_w = Math.ceil(Math.sin(Math.PI * Math.abs((curr_w + (from_w < to_w ? 1 : -1) - from_w) / (to_w - from_w))) * offset_w) ;
	var _offset_h = Math.ceil(Math.sin(Math.PI * Math.abs((curr_h + (from_h < to_h ? 1 : -1) - from_h) / (to_h - from_h))) * offset_h) ;
	
	//alert(offset_w) ;
	
	if (curr_w < to_w && curr_w + _offset_w < to_w) {
		set_style(id, "width", (curr_w + _offset_w) + "px") ;
	} else if (curr_w > to_w && curr_w - _offset_w > to_w) {
		set_style(id, "width", (curr_w - _offset_w) + "px") ;
	} else {
		set_style(id, "width", to_w + "px") ;
	}
	
	if (curr_h < to_h && curr_h + _offset_h < to_h) {
		set_style(id, "height", (curr_h + _offset_h) + "px") ;
	} else if (curr_h > to_h && curr_h - _offset_h > to_h) {
		set_style(id, "height", (curr_h - _offset_h) + "px") ;
	} else {
		set_style(id, "height", to_h + "px") ;
	}
	
	obj.move_timer_id = setTimeout("_resize('" + id + "', " + from_w + ", " + from_h + ", " + to_w + ", " + to_h + ", " + offset_w + ", " + offset_h + ")", 10) ;
	
}

function resize(id, to_w, to_h, offset_w, offset_h) {
	
	var obj = document.getElementById(id) ;
	if (! obj) return ;
	
	if (obj.move_timer_id)
		clearTimeout(obj.move_timer_id) ;
	
	var from_w = get_style(id, "width") ;
	var from_h = get_style(id, "height") ;
	
	from_w = parseInt(from_w.substr(0, from_w.search(/px/i))) ;
	from_h = parseInt(from_h.substr(0, from_h.search(/px/i))) ;
	
	_resize(id, from_w, from_h, to_w, to_h, offset_w, offset_h) ;
	
}

