var alignNavigaceRight = false;
var draggingEnabled = true;

function onBar()
{
var pano = document.getElementById ("panorama");
pano.style.overflow = "auto";
}

function offBar()
{
var pano = document.getElementById ("panorama");
pano.style.overflow = "hidden";
}

function getDragParent(el) {return el;
	var oldEl=el;
	while (el) {
		el=el.parentNode;
		if (el.id=="panorama" || el.nodeName.toUpperCase()=='BODY') {
			return oldEl;
		}
		oldEl=el;
	}
}

var lastX, lastLeft, offsetX, offsetY, offsetWidth, minLeft, maxLeft, draggingThing, draggingParent, sideDrag, dragTimer;

function dragBySide()
{
if (!draggingThing || (sideDrag == 0)) return;
var newLeft = lastLeft + sideDrag * 4;
if (newLeft > maxLeft) newLeft = maxLeft;
if (newLeft < minLeft) newLeft = minLeft;
draggingThing.style.left = newLeft +'px';
lastLeft = newLeft;
return true;
}

function startDrag(e) {
        if (draggingThing != null) return;
	draggingThing = getDragParent(e.srcElement || e.target);
	draggingParent = draggingThing.parentNode.parentNode;
        offsetWidth = draggingThing.width;
	minLeft = -(offsetWidth - draggingParent.offsetWidth) / 2;
	maxLeft = -minLeft;
	if (minLeft > maxLeft) 
		{
		draggingThing = null;
		return;
		}
	lastX = e.clientX;
	offsetX = e.clientX - draggingThing.offsetLeft;
	offsetY = e.clientY - draggingThing.offsetTop;
	//alert (draggingThing.offsetLeft);
        offsetWidth = document.getElementById ("koko").width;
	//alert (offsetWidth);
	//alert (draggingThing.parentNode.offsetWidth);
	draggingThing.onmousemove=moveDrag;
	if (draggingThing.style.left)
		lastLeft = parseInt (draggingThing.style.left);
	else
		lastLeft = 0;
	//draggingThing.onmousewheel=wheelDrag;
	//document.body.onmousemove=moveDrag;
	//draggingThing.onmouseout= endDrag;
	//document.body.onmouseup=endDrag;
	//document.onselectstart=nullFunc;
	sideDrag = 0;
	dragTimer = window.setInterval ('dragBySide()', 50);
}

function nullFunc(e) {
	return false;
}


function moveDrag(e) {
	if (!draggingEnabled) return false;
	e=e || event;
	if (draggingThing) {
		//draggingThing.style.top=(e.clientY-offsetY)+'px';
		if (e.clientX < draggingParent.offsetLeft) 
			{
			sideDrag = -1;
			return true;
			};
		if (e.clientX > (draggingParent.offsetLeft + draggingParent.offsetWidth)) 
			{
			sideDrag = 1;
			return true;
			};
		sideDrag = 0;
		var newLeft = lastLeft + (e.clientX - lastX);
		lastX = e.clientX;
		if (newLeft > maxLeft) newLeft = maxLeft;
		if (newLeft < minLeft) newLeft = minLeft;
		draggingThing.style.left = newLeft +'px';
		lastLeft = newLeft;
		return true;
	}
}

function endDrag(e) {
	//draggingThing.onmouseover = function ("startDrag(event)");
	draggingThing=null;
	document.body.onmousemove=null;
	document.body.onmouseend=null;
	document.onselectstart=null;
	window.clearInterval (dragTimer);
}


function getWheelDelta (event)
	{
	return 10;
	var delta = 0;
        if (!event) /* For IE. */ event = window.event;
        if (event.wheelDelta) 
		{ /* IE/Opera. */
                delta = event.wheelDelta/120;
                /** In Opera 9, delta differs in sign as compared to IE.
                 */
                if (window.opera) delta = -delta;
        	} 
	else if (event.detail) { /** Mozilla case. */
                /** In Mozilla, sign of delta is different than in IE.
                 * Also, delta is multiple of 3.
                 */
                delta = -event.detail/3;

	return delta;
        }}

function wheelDrag(e) {
	if (!draggingThing) return false; 
		lastX += getWheelDelta (e);
	        var newLeft = draggingThing.offsetLeft + lastX;
		if (newLeft > 1) newLeft = 1;
		if (newLeft < minLeft) newLeft = minLeft;
		draggingThing.style.left= newLeft +'px';
		return true;
}

var lastWindowHeight = 0;
var lastWindowWidth = 0;

function setPanoramaItemsStyles (titulStyle, panoramaStyle, navigaceStyle)
{
var titul = document.getElementById ('titul');
var panorama = document.getElementById ('panorama');
var navigace = document.getElementById ('navigace');

if (titul) titul.style.top = titulStyle;
if (panorama) panorama.style.top = panoramaStyle;
if (navigace) navigace.style.top = navigaceStyle;

document.cookie = "panorama_titul_style_top" + "=" + escape(titulStyle) + "; expires=Fri, 31 Dec 2099 23:59:59 GMT;";
document.cookie = "panorama_panorama_style_top" + "=" + escape(panoramaStyle) + "; expires=Fri, 31 Dec 2099 23:59:59 GMT;";
document.cookie = "panorama_navigace_style_top" + "=" + escape(navigaceStyle) + "; expires=Fri, 31 Dec 2099 23:59:59 GMT;";
}

function resizePanorama ()
{
var windowHeight = document.body.offsetHeight;
var windowWidth = document.body.offsetWidth;

if ((windowHeight == lastWindowHeight) && (windowWidth == lastWindowWidth)) return;
lastWindowHeight = windowHeight;
lastWindowWidth = windowWidth;

var titul = document.getElementById ('titul');
var panorama = document.getElementById ('panorama');
var navigace = document.getElementById ('navigace');

panorama.style.height = "auto";

var navigaceHeight = navigace ? navigace.offsetHeight : 0;
var panoramaHeight = panorama ? panorama.offsetHeight : 0;
var titulHeight = titul ? titul.offsetHeight : 0;

var restHeight = windowHeight - (navigaceHeight + panoramaHeight);

if (navigace && alignNavigaceRight)
	{
	var img = document.getElementById ('koko');
	var panoramaRight = (windowWidth - img.offsetWidth) / 2;
        if (panoramaRight < 0)
		{
		panoramaRight = 10;
		}
	else
		{
		if (restHeight <= 0) panoramaRight +=  10;
		};
	var navigaceRightStyle = panoramaRight + "px";
	navigace.style.right = navigaceRightStyle;
	document.cookie = "panorama_navigace_style_right" + "=" + escape(navigaceRightStyle) + "; expires=Fri, 31 Dec 2099 23:59:59 GMT;";
	};

if (restHeight >= 0)
	{
	var spaceHeight = Math.min (navigaceHeight / 4, restHeight / 3);
	var frameHeight = (restHeight - spaceHeight) / 2;
	setPanoramaItemsStyles ((frameHeight + spaceHeight) + "px",
				frameHeight + "px",
				(windowHeight - navigaceHeight - frameHeight) + "px");
	return;
	};
restHeight = windowHeight - panoramaHeight;
if (restHeight >= 0)
	{
	var spaceHeight = navigaceHeight / 4;
	var frameHeight = restHeight  / 2;
	setPanoramaItemsStyles ((frameHeight + spaceHeight) + "px",
				frameHeight + "px",
				(windowHeight - navigaceHeight - frameHeight - spaceHeight) + "px");
	return;
	};
var spaceHeight = navigaceHeight / 4;
var frameHeight = restHeight  / 2;
setPanoramaItemsStyles (spaceHeight + "px",
			frameHeight + "px",
			(windowHeight - navigaceHeight - spaceHeight) + "px");

}

window.onresize = window.onload = resizePanorama;

function showTitulMenu (titul, showIt)
{
return true;
if (showIt)
	{
	titul.style.background = "black";
	titul.style.color = "white";
	}
else
	{
	titul.style.background = null;
	titul.style.color = null;
	};
}

function mouseOverTitul()
{
var titul = document.getElementById ('titul');
titul.onmouseout = mouseOutTitul;
showTitulMenu (titul, true);
}

function mouseOutTitul()
{
var titul = document.getElementById ('titul');
showTitulMenu (titul, false);
}

// Get the value of the cookie with the specified name.
function getCookie(sName)
{
  // cookies are separated by semicolons
  var aCookie = document.cookie.split("; ");
  for (var i=0; i < aCookie.length; i++)
  {
    // a name/value pair (a crumb) is separated by an equal sign
    var aCrumb = aCookie[i].split("=");
    if (sName == aCrumb[0]) 
      return unescape(aCrumb[1]);
  }

  // a cookie with the requested name does not exist
  return null;
}

function loadPositionsFromCookie()
{
var panoramaStyleTop = getCookie ("panorama_panorama_style_top");
var navigaceStyleTop = getCookie ("panorama_navigace_style_top");
var navigaceStyleRight = alignNavigaceRight ? getCookie ("panorama_navigace_style_right") : false;
var titulStyleTop = getCookie ("panorama_titul_style_top");
if (panoramaStyleTop || navigaceStyleTop || titulStyleTop)
	{
	document.write ('<style type="text/css">\r\n');
	if (panoramaStyleTop)
		{
		document.write ('.panorama { top: ');
		document.write (panoramaStyleTop);
		document.write ('; }\r\n');
		};
	if (navigaceStyleTop || navigaceStyleRight)
		{
		document.write ('.popis { ');
		if (navigaceStyleTop)
			{
			document.write ('top: ');
			document.write (navigaceStyleTop);
			document.write ('; ');
			};
		if (navigaceStyleRight)
			{
			document.write ('right: ');
			document.write (navigaceStyleRight);
			document.write ('; ');
			};
		document.write ('}\r\n');
		};
	if (titulStyleTop)
		{
		document.write ('.dayTitle { top: ');
		document.write (titulStyleTop);
		document.write ('; }\r\n');
		};
	document.write ('</style>\r\n');
	};
}

