﻿// Ovládání prezentace vytvořené pomocí inCMS
// (C)2007 Špika David, Inexes

// Inicializační fce po načtení stránky
//addEvent(window,'onload','pageResize();');
addEventx(window, 'onresize', 'pageResize();');
addEventx(window, 'onload', 'menuEffect.init(getObj(\'menuHL\'));');

function pageResize() {
	scrollTo(52, 0);
}


// Fce na inicializaci hlavního menu (ovládané přes hover) - nahradí chybějící podporu css pseudotřídy li:hover pro IE6 + zruší kursor ruky při href="javascript:void(0)" + přidá šipku (class) u položek obsahujících podpoložky
// parametry: ID nebo menu (menu = <ul>)
function mainMenuInit(obj) {
	if (getObj(obj)) {
		var menu = getObj(obj);
		var childItems = menu.getElementsByTagName('li');
		for (var i = 0; i < childItems.length; i++) {
			if (browser.IE6) {
				addEventx(childItems[i], 'onmouseover', 'o.className += \' hover\';');
				addEventx(childItems[i], 'onmouseout', 'o.className = o.className.replace(\' hover\',\'\');');
			}
			if (childItems[i].getElementsByTagName('ul').length > 0) {
				childItems[i].className += ' isParent';
			}
		}
		var links = menu.getElementsByTagName('a');
		for (var i = 0; i < links.length; i++) {
			if (links[i].href == 'javascript:void(0);') setObjStyle(links[i], 'cursor', 'default');
		}
	}
}

var menuEffect = {
	timer: null,
	content: null,
	position: 0,
	direction: 1
}

menuEffect.init = function(linkSpan) {
	menuEffect.timer = window.setInterval('menuEffect.run();', 60);
	menuEffect.linkSpan = linkSpan;
	menuEffect.content = (linkSpan != null ? linkSpan.innerHTML : null);
}

menuEffect.run = function() {
	var text = menuEffect.content;
	if (text != null) {
		menuEffect.linkSpan.innerHTML = text.substring(0, menuEffect.position) + '<span>' + text.substring(menuEffect.position, menuEffect.position + 1) + '</span>' +
			text.substring(menuEffect.position + 1, text.length);
		if (menuEffect.position == text.length) menuEffect.direction = 0;
		if (menuEffect.position == 0) menuEffect.direction = 1;
		if (menuEffect.direction == 1) menuEffect.position++;
		if (menuEffect.direction == 0) menuEffect.position--;
	}
}

// Fce uloží do cookies zadanou hodnotu
function setCookie(name, value, path, domain, secure) {
	expires = new Date();
	expires.setTime(expires.getTime() + (24 * 60 * 60 * 1000 * 31));
	document.cookie = escape(name) + "=" + escape(value) +
	((expires) ? "; expires=" + expires.toGMTString() : "") +
	((path) ? "; path=" + path : "") +
	((domain) ? "; domain=" + domain : "") +
	((secure) ? "; secure" : "");
}

// Fce vrátí z cookies hodnotu podle jména
function getCookie(name) {
	var cookieList = document.cookie.split("; ");
	for (var i = 0; i < cookieList.length; i++) {
		var cookie = cookieList[i].split("=");
		if (unescape(cookie[0]) == name) {
			return unescape(cookie[1]);
			break;
		}
	}
	return null;
}

// Fce smaze cookie podle zadaných hodnot
function deleteCookie(name, path, domain) {
	if (getCookie(name)) {
		document.cookie = name + "=" + ((path) ? ";path=" + path : "") + ((domain) ? ";domain=" + domain : "") + ";expires=Thu, 01-Jan-1970 00:00:01 GMT";
	}
}


// Fce na inicializaci submenu (ovládané přes onClick) - zabalí všechny položky a rozbalí určenou položku (včetně všech položek v cestě) + přidá šipku (class) u položek obsahujících podpoložky
// parametry: cesta k položce, která se má rozbalit (Array)
function subMenuInit(path) {
	var menuID = 'menuList-' + path[0];
	if (getObj(menuID)) {
		var menu = getObj(menuID);
		var childUL = menu.getElementsByTagName('ul');
		for (var i = 0; i < childUL.length; i++) {
			displayObj(childUL[i], false);
		}
		for (var i = 1; i < path.length; i++) {
			menuID += '-' + path[i];
			displayObj(menuID, true);
		}
		var childLI = menu.getElementsByTagName('li');
		for (var i = 0; i < childLI.length; i++) {

			if (browser.IE6) {
				addEventx(childLI[i], 'onmouseover', 'o.className += \' hover\';');
				addEventx(childLI[i], 'onmouseout', 'o.className = o.className.replace(\' hover\',\'\');');
			}
			if (childLI[i].getElementsByTagName('ul').length > 0) {
				childLI[i].className += ' isParent';
			}
		}
	}
}

// Fce na rozbalení určené položky submenu (ovládané přes onClick)
// Pokud položka, na kterou bylo kliknuto, někam odkazuje - Fce vrací true a žádné rozbalení se neprovede
// pokud neodkazuje - Dojde k rozbalení a fce vrací false
// parametry: položka na kterou bylo kliknuto; ID seznamu UL, který se má rozbalit
function expandMenuList(item, menuListID) {
	if (item.href != 'javascript:void(0);') return true;
	var path = menuListID.split('-');
	for (var i = 0; i < path.length - 1; i++) path[i] = path[i + 1];
	path.length--;
	item.blur();
	subMenuInit(path);
	return false;
}

// Fce vrátí cenu jako upravený text
// parametry: cena jako číslo
function priceToString(price) {
	priceInt = Math.floor(price);
	priceFract = price.toString().indexOf('.') == -1 ? '0' : 100 * ('0.' + price.toString().split('.')[1]);
	return priceInt + ',' + (priceFract == '0' ? '&ndash;' : (priceFract < 10) ? ('0' + priceFract) : priceFract);
}

// Fce vrátí zaokrouhlenou cenu na zadaný počet haléřů
// parametry: cena jako číslo, desetinná část na kterou zaokrouhlit (0.01 až 1), způsob zaokrouhlení (0 = matematicky | 1 = dolů)
function roundPrice(price, fract, mode) {
	priceInt = Math.floor(price);
	priceFract = price.toString().indexOf('.') == -1 ? '0' : '0.' + price.toString().split('.')[1]; // desetinná část ceny
	if (mode == 0) {
		for (var i = 0; i < 2 / fract; i++) {
			var downLimit = Math.round(1000 * i * fract / 2) / 1000;
			var upLimit = Math.round(1000 * (i + 1) * fract / 2) / 1000;
			if (downLimit <= priceFract && priceFract < upLimit) {
				if (i % 2 == 0) return priceInt + downLimit;
				else return priceInt + upLimit;
			}
		}
	}
	if (mode == 1) {
		for (var i = 0; i < 1 / fract; i++) {
			var downLimit = Math.round(1000 * i * fract) / 1000;
			var upLimit = Math.round(1000 * (i + 1) * fract) / 1000;
			if (downLimit <= priceFract && priceFract < upLimit) return priceInt + downLimit;
		}
	}
	return null;
}

// Fce na validaci data
// parametry: formulář. pole (ID nebo prvek)
function validateDate(obj) {
	var o = getObj(obj);
	var date = o.value;
	var dateField = date.split('.');
	var testDate = new Date();
	var ok = true;
	if (dateField.length == 3 && dateField[2].length == 4) {
		testDate.setFullYear(dateField[2]);
		testDate.setMonth(dateField[1] - 1);
		testDate.setDate(dateField[0]);
		if (testDate.getFullYear() != dateField[2] || testDate.getMonth() != dateField[1] - 1 || testDate.getDate() != dateField[0])
			ok = false;
	}
	else ok = date != '' ? false : true;
	if (!ok) {
		if (o.className.indexOf('date-error') == -1) o.className += ' date-error';
		alert((o.title != '' ? '"' + o.title + '"' : 'Datum') + ' není ve správném formátu.');
		o.value = '';
	}
	else o.className = o.className.replace('date-error', '');
}

// Fce pro zobrazeni bubliny pri vyberu bloku v arealu
function viewBuble(text, top, left) {
	var buble = getObj('infoBuble');

	buble.style.top = top + "px";
	buble.style.left = left + "px";
	buble.innerHTML = text;
	buble.style.display = "block";
}

// Fce pro zobrazeni bubliny pri vyberu bloku v arealu
function hideBuble() {
	var buble = getObj('infoBuble');
	buble.style.display = "none";
	buble.innerHTML = '';
}

// fce pro ovladani mapy obchodniho centra
function mapOver(mapID, text, top, left) {
	var buble = getObj('bubleBox');

	getObj('arealImg').src = 'Design/Areal/Areal-' + mapID + '.jpg';
	buble.style.top = top + "px";
	buble.style.left = left + "px";
	buble.innerHTML = text;
	buble.style.display = "block";
}
function mapOut() {
	var buble = getObj('bubleBox');

	getObj('arealImg').src = 'Design/Areal/Areal-0.jpg';
	buble.style.display = "none";
}

// udalost na zmenu dropdownu
function changeSeason(obj) {
	setCookie('WebSkin', obj.options[obj.selectedIndex].value);
	deleteCookie('ExpandedMenuItemID');
	location.href = location.href; //'./Page-P2.html';
}

// zmena stylu webu dle obdobi ci uzivatelske volby
$(document).ready(function() {
	var Seasons = { 'winter': 0, 'summer': 1 };
	var selectedStyle = getCookie('WebSkin');
	if (selectedStyle == null) { // pokud neni ulozen styl, nastavi se dle aktualniho obdobi
		var actualMonth = (new Date()).getMonth() + 1;
		if ((actualMonth > 3) && (actualMonth < 10)) { setCookie('WebSkin', Seasons.summer); }
		else { setCookie('WebSkin', Seasons.winter); }
	}
});

