﻿// Copyright (c) 2009 Invideon
// Default javascript functionality

function bookmark() {
	if (window.sidebar)
		window.sidebar.addPanel(document.title, window.location.href, "");
	else if (window.external)
		window.external.AddFavorite(window.location.href, document.title);
	else if (window.opera && window.print) {
		var bookmark = document.createElement("a");
		bookmark.setAttribute("rel", "sidebar");
		bookmark.setAttribute("href", window.location.href);
		bookmark.setAttribute("title", document.title);
		bookmark.click();
	}
}

var currentMenuElement = null;
var menuTimeout = null;
function showMenu(sourceElement, menuName, type) {
	if (currentMenuElement) {
		currentMenuElement.style.display = "none";
		currentMenuElement = null;
	}
	if (menuTimeout) {
		clearTimeout(menuTimeout);
		menuTimeout = null;
	}
	
	var menuElement = document.getElementById("menu_" + menuName);
	if (!menuElement) return;

	var position = calculatePosition(sourceElement);
	switch (type) {
		case 1:
			position.left -= 5;
			position.top += 15;
			break;
		case 2:
			position.left += 200;
			position.top -= 1;
			break;
	}
	
	menuElement.style.left = position.left + "px";
	menuElement.style.top = position.top + "px";
	menuElement.style.display = "block";

	currentMenuElement = menuElement;
}

function keepMenu() {
	if (menuTimeout) {
		clearTimeout(menuTimeout);
		menuTimeout = null;
	}
}

function hideMenu() {
	menuTimeout = setTimeout(function () {
		if (currentMenuElement)
			currentMenuElement.style.display = "none";
		currentMenuElement = null;
		menuTimeout = null;
	}, 500);
}

function calculatePosition(element) {
	var offset = {left: element.offsetLeft, top: element.offsetTop};
	for (var parent = element.offsetParent; parent; parent = parent.offsetParent) {
		offset.left += parent.offsetLeft;
		offset.top += parent.offsetTop;
	}
	return offset;
}

var textSizes = ["", "large", "xlarge", "xxlarge"];
function getTextSize() {
	for (var i = 0, n = textSizes.length; i < n; ++i)
		if (document.body.className == textSizes[i])
			return i;
	return 0;
}

function setTextSize(size) {
	document.body.className = textSizes[size];

	var cookieExpireDate = new Date();
	cookieExpireDate.setDate(cookieExpireDate.getDate() + 7); // 1 Week
	document.cookie = "textsize" + "=" + escape(size) + ";expires=" + cookieExpireDate.toGMTString() + ";path=/";
}

function sizeText(size) {
	setTextSize(size);
}

function upsizeText() {
	var size = getTextSize() + 1;
	if (size <= 3)
		setTextSize(size);
}

function downsizeText() {
	var size = getTextSize() - 1;
	if (size >= 0)
		setTextSize(size);
}