/*global jQuery*/
/*jslint browser: true*/
(function ($) {

	var errors = false;

	$.viewportSize = function () {
		var viewportwidth, viewportheight;
		if (typeof(window.innerWidth) !== 'undefined') {
			viewportwidth = window.innerWidth;
			viewportheight = window.innerHeight;
		} else if (typeof(document.documentElement) !== 'undefined' && 
					typeof(document.documentElement.clientWidth) !== 'undefined' &&
					document.documentElement.clientWidth !== 0) {
			viewportwidth = document.documentElement.clientWidth;
			viewportheight = document.documentElement.clientHeight;
		} else {
			viewportwidth = document.getElementsByTagName('body')[0].clientWidth;
			viewportheight = document.getElementsByTagName('body')[0].clientHeight;
		}
		return {"height" : viewportheight, "width" : viewportwidth};
	};
	
	$.fn.viewportPosition = function () {
		if (!this[0]) {
			return;
		}
		try {
			var pos = this.offset(),
				win = $(window),
				top = pos.top - win.scrollTop(),
				left = pos.left - win.scrollLeft();
			return {
				top: top,
				left: left,
				bottom: top + this.height(),
				right: left + this.width()
			};
		} catch (e) {
			if (errors) {
				console.error(e);
			}
			return {};
		}
	};
	
	$.fn.viewportOffset = function (vp, vp_pos) {
		if (!this[0]) {
			return;
		}
		try {
			if (!vp) {
				vp = $.viewportSize();
			}
			if (!vp_pos) {
				vp_pos = this.viewportPosition();
			}
			vp_pos.right = vp.width - vp_pos.right;
			vp_pos.bottom = vp.height - vp_pos.bottom;
			return vp_pos;
		} catch (e) {
			if (errors) {
				console.error(e);
			}
			return {};
		}
	};

	$.fn.fourPointPosition = function () {
		if (!this[0]) {
			return;
		}
		var pos = this.position();
		pos.right = pos.left + this.width();
		pos.bottom = pos.top + this.height();
		return pos;
	};
	
	$.fn.getVisib = function () {
		if (!this[0]) {
			return;
		}
		var obj = this[0],
			value = obj.style.visibility;
		if (!value) {
			if (document.defaultView && typeof (document.defaultView.getComputedStyle) === "function") { // Gecko, W3C
				//if (!Calendar.is_khtml)
				value = document.defaultView.getComputedStyle(obj, "").getPropertyValue("visibility");
				//else
				//	value = '';
			} else if (obj.currentStyle) { // IE
				value = obj.currentStyle.visibility;
			} else {
				value = '';
			}
		}
		return value;
	};

	$(document).ready(function () {
		var menus = $("#themeMainNav > li");
		menus.hover(
			function () {
				var menu = $("ul", this), right;
				if ($.browser.msie) {
					menus.removeClass("sfhover");
					$(this).addClass("sfhover");
				}
				right = menu.viewportOffset().right - 15;
				if (right < 0) {
					menu.css("left", right + $(this).position().left);
				}
				menu.bgiframe();
			},
			function () {
				var menu = $("ul", this);
				menu.css("left", "");
				$(this).removeClass("sfhover");
			}
		);
	});

	$(document).ready(function () {
		var menus = $("#themeWcmLeftNav > li.flyout");
		menus.hover(
			function () {
				if ($.browser.msie) {
					menus.removeClass("over");
					$(this).addClass("over"); 
				}
				var menu = $("ul", this), vp_size, height, vp_offset, bottom;
				if (menu.size() > 0) {
					vp_size = $.viewportSize();
					height = menu.height();
					if (vp_size.height > height) {
						vp_offset = menu.viewportOffset(vp_size);
						bottom = vp_offset.bottom - 15;
						if (bottom < 0) {
							menu.css("top", 
								(vp_size.height + $(window).scrollTop()) - 
								height);
						}
					} else {
						menu.css("top", $(window).scrollTop() + 20);
						menu.css("overflow", "scroll");
						menu.css("height", vp_size.height);
					}
					menu.bgiframe();
				}
			},
			function () {
				var menu = $("ul", this);
				menu.css("top", "auto");
				menu.css("overflow", "auto");
				menu.css("height", "auto");
				$(this).removeClass("over");
			}
		);
	});

}(jQuery));