﻿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 currentMenuButtonElement = null;
var menuTimeout = null;
function showMenu(sourceElement, menuName, type) {
	if (currentMenuElement)
		currentMenuElement.style.display = "none";
	currentMenuElement = null;
	if (currentMenuButtonElement)
		currentMenuButtonElement.className = "navigation-item";
	currentMenuButtonElement = null;

	if (menuTimeout) {
		clearTimeout(menuTimeout);
		menuTimeout = null;
	}
	
	var menuElement = document.getElementById("menu_" + menuName);
	var menuButtonElement = document.getElementById("menu_button_" + menuName);
	if (!menuElement || !menuButtonElement) return;

	var position = calculatePosition(sourceElement);
	switch (type) {
		case 1:
			position.left -= 0;
			position.top += menuButtonElement.offsetHeight - 1;
			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";
	menuButtonElement.className = "navigation-item navigation-item-expanded";

	currentMenuElement = menuElement;
	currentMenuButtonElement = menuButtonElement;
}

function keepMenu() {
	if (menuTimeout) {
		clearTimeout(menuTimeout);
		menuTimeout = null;
	}
}

function hideMenu() {
	menuTimeout = setTimeout(function () {
		if (currentMenuElement)
			currentMenuElement.style.display = "none";
		currentMenuElement = null;
		if (currentMenuButtonElement)
			currentMenuButtonElement.className = "navigation-item";
		currentMenuButtonElement = 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);
}

var banners = 0;
var bannerIndex = 0;
var bannerTime = 1;
function changeBanner() {
	var element1Index = bannerIndex;
	var element2Index = bannerIndex + 1;
	if (element2Index == banners)
		element2Index = 0;
	var element1 = document.getElementById("banner" + element1Index);
	var element2 = document.getElementById("banner" + element2Index);
	changeOpacity(element1, 1);
	changeOpacity(element2, 1);
	element1.style.zIndex = 1;
	element2.style.zIndex = 0;
	element2.style.display = "";
	bannerTime = 1;

	var interval = setInterval(function() {
		bannerTime -= 0.05;
		if (bannerTime <= 0) {
			clearInterval(interval);
			if (++bannerIndex == banners)
				bannerIndex = 0;
			element1.style.display = "none";
		} else
			changeOpacity(element1, bannerTime);
	}, 50);
}

function changeOpacity(element, opacity) {
	if (typeof element.style.opacity == "undefined") // Check for legacy browsers
		element.style.filter = "progid:DXImageTransform.Microsoft.Alpha(opacity=" + (opacity * 100) + ")";
	else
		element.style.opacity = opacity;
}
