
function scrollPos() {
	var left, top;

	if (window.pageYOffset) { top = window.pageYOffset; }
	else if (document.documentElement && document.documentElement.scrollTop) { top = document.documentElement.scrollTop; }
	else if (document.body) { top = document.body.scrollTop; }
	
	if (window.pageXOffset) { left = window.pageXOffset; }
	else if (document.documentElement && document.documentElement.scrollLeft) { left = document.documentElement.scrollLeft; }
	else if (document.body) { left = document.body.scrollLeft; }
	
	return {
		left: left,
		top: top,
		toString: function() {
			return 'left: ' + left + ' top:' + top;
		}
	};
}

function windowSize() {
	var width, height;
	
	if (window.innerWidth) { width = window.innerWidth - 26; }
	else if (document.documentElement && document.documentElement.clientWidth) { width = document.documentElement.clientWidth - 20; }
	else if (document.body) { width = document.body.offsetWidth; }	
	
	if (window.innerHeight) { height = window.innerHeight - 26; }
	else if (document.documentElement && document.documentElement.clientHeight) { height = document.documentElement.clientHeight - 20; }
	else if (document.body) { height = document.body.clientHeight; }	
	
	return {
		width: width,
		height: height,
		toString: function() {
			return 'width: ' + width + ' height:' + height;
		}
	};
}

function windowSize() {
	var width, height;
	
	if (window.innerWidth) { width = window.innerWidth - 26; }
	else if (document.documentElement && document.documentElement.clientWidth) { width = document.documentElement.clientWidth - 20; }
	else if (document.body) { width = document.body.offsetWidth; }	
	
	if (window.innerHeight) { height = window.innerHeight - 26; }
	else if (document.documentElement && document.documentElement.clientHeight) { height = document.documentElement.clientHeight - 20; }
	else if (document.body) { height = document.body.clientHeight; }	
	
	return {
		width: width,
		height: height,
		toString: function() {
			return 'width: ' + width + ' height:' + height;
		}
	};
}

function moveTopFloating() {
	var top_floating = $('#top_floating');
	
	var parent = top_floating.parent();
	var parent_offset = parent.offset();
	
	var parent_top = parent_offset.top;
	var parent_bottom = parent_offset.top + parent.height();
	
	// height 1500, top_floating display setting. 
	if (parseInt(document.getElementById("content").clientHeight) > 1200) {
		top_floating.show();
	} else {
		top_floating.hide();
	}
	
	var sPos = scrollPos();
	var wSize = windowSize();
	
	// top floating element posistion
	var floating_top = (sPos.top + wSize.height - top_floating.height());
	var topDiff = (parent.attr('id') == 'spec_sec') ? parent_top : 0;
	
	if (floating_top >= parent_bottom - top_floating.height() - 3) {
		var top = parent_bottom - top_floating.height() - 3;
		top -= topDiff;
		top_floating.css({top: top + 'px'});
	} else if (floating_top <= parent_top + 3) {
		var top = parent_top + 3;
		top -= topDiff;
		top_floating.css({top: top + 'px'});
	} else {
		var top = floating_top;
		top -= topDiff;
		top_floating.css({top: top + 'px'});
	}
	top_floating.css({left: (parent.width() - top_floating.width() - 3) + 'px'});
}

var oldOnLoad = window.onload;
function topInit() {
	if (typeof oldOnLoad == "function")
		oldOnLoad();
	if (parseInt(document.getElementById("content").clientHeight) > 1200) {
		moveTopFloating(); 
		window.onscroll = moveTopFloating;
		window.onresize = moveTopFloating;
	}else{
		var top_floating = $('#top_floating');
		top_floating.hide();
	}
}
