var zeljeni_x;
var trenutni_x;
var hitrost = 52;
var max_hitrost = 2;
var min_hitrost = 52;
var counter = 1;
var fadeout = [10];
var slidestep = 0;
var slidepic = 1;
var nextpic = 2;
var cdiv, ndiv;
var interval = 250;
var dex = -170;
var dey = -340;
var popupUp = 0;

function findPos(obj) {
	var curleft = curtop = 0;
	if (obj.offsetParent) {
		curleft = obj.offsetLeft
		curtop = obj.offsetTop
		while (obj = obj.offsetParent) {
			curleft += obj.offsetLeft
			curtop += obj.offsetTop
		}
	}
	return [curleft,curtop];
}

function skrijPriprta(p)
{
	if (popupUp == 1)
	{
		var el = document.getElementById(p);
		el.style.visibility = 'hidden';
		popupUp = 0;
	}
	
}

function pokaziPriprta(p)
{
//	alert(popupUp);
	if (popupUp == 0)
	{
//		alert('show');
		var el = document.getElementById(p);
		el.style.visibility = 'visible';
//		var elo = p.parentNode;
//		var place = findPos(elo);
//		alert(place[0] + ' ' + place[1]);
//		el.style.left = place[0] + "px";
//		el.style.top = (place[1] - 100) + "px";
		popupUp = 1;
	}
}

function normalpointer()
{
	document.body.style.cursor='default';
	document.getElementById('zarnica').style.visibility = 'hidden';
}

function karta(p)
{
	document.body.style.cursor='pointer';
	zar = document.getElementById('zarnica');
	zar.style.visibility = 'visible';
	if (p == 6)
	{
		movemap(-167,-330);
		zar.style.top = '385px';
		zar.style.left = '270px';
	}
	if (p == 5)
	{
		movemap(-370,-120);
		zar.style.top = '192px';
		zar.style.left = '474px';
	}
	if (p == 4)
	{
		movemap(-293,-424);
		zar.style.top = '487px';
		zar.style.left = '394px';
	}
	if (p == 3)
	{
		movemap(-105,-464);
		zar.style.top = '526px';
		zar.style.left = '205px';

	}
	if (p == 2)
	{
		movemap(-181,-385);
		zar.style.top = '449px';
		zar.style.left = '283px';

	}
	if (p == 1)
	{
		movemap(-138,-280);
		zar.style.top = '345px';
		zar.style.left = '237px';
	}

}

function glow(nexttocall,d) {
	if (nexttocall == 0) {
		document.getElementById('glow').className = 'glow0';
	} else {
		document.getElementById('glow').className = 'glow'+nexttocall
	}
	if (nexttocall == 100) {
		d = 1;
		setTimeout('glow('+(nexttocall-10)+','+d+')',400);
		return;
	}
	if (nexttocall == 0) {
		d = 0;
		setTimeout('glow('+(nexttocall+10)+','+d+')',400);
		return;
	}
	
	if(d) {
		setTimeout('glow('+(nexttocall-10)+','+d+')',60);
	} else {
		setTimeout('glow('+(nexttocall+10)+','+d+')',60);
	}
}


function mapmove()
{
	deltax = dex - document.getElementById('map').offsetLeft;
	deltay = dey - document.getElementById('map').offsetTop;
//	document.getElementById('debug').innerHTML = 'desired x = ' + dex + '<br /> desired y = ' + dey + '<br /> dx = ' + deltax + '<br /> dy = ' + deltay;
//	document.getElementById('debug').innerHTML = 'interval = ' + interval;
	if (deltay == 0) {
		if (deltax == 0) {
			interval = 250;
		}
	}

	if (deltax > 0)
	{
		document.getElementById('map').style.left = (document.getElementById('map').offsetLeft + 2) + 'px';
	}
	if (deltax < 0)
	{
		document.getElementById('map').style.left = (document.getElementById('map').offsetLeft - 2) + 'px';
	}
	if (deltay > 0)
	{
		document.getElementById('map').style.top = (document.getElementById('map').offsetTop + 2) + 'px';
	}
	if (deltay < 0)
	{
		document.getElementById('map').style.top = (document.getElementById('map').offsetTop - 2) + 'px';
	}
	if (Math.abs(deltax) < 3) {
		document.getElementById('map').style.left = dex + 'px';
	}
	if (Math.abs(deltay) < 3) {
		document.getElementById('map').style.top = dey + 'px';
	}

	setTimeout(function(){mapmove()},interval);
}
function pojdi(x)
{
	zeljeni_x = x;
}

function pausecomp(millis) 
{
	var date = new Date();
	var curDate = null;

	do { curDate = new Date(); } 
	while(curDate-date < millis);
}
function slide()
{
	
	if (slidestep < 271)
	{
		cdiv.style.left = 0 - slidestep;
		ndiv.style.left = 270 - slidestep;
		setTimeout(function(){slide()},20);
		slidestep = slidestep + 1;
		return;
	}
	if (slidestep == 272)
	{
		slidepic = nextpic;
		nextpic++
		if (nextpic == 20)
		{
			nextpic = 1;
		}
		cdiv.style.left = 0;
		ndiv.style.left = 270;
		document.getElementById('nextpic').src = 'slide/slideshow' + nextpic + '.jpg';
		slidestep = 0;
		setTimeout(function(){slide()},5000);
		return;
	}

	if (slidestep >= 271)
	{
		document.getElementById('currpic').src = 'slide/slideshow' + nextpic + '.jpg';
		setTimeout(function(){slide()},20);
		slidestep = 272;
		return;
	}
}
function dan(e) {
	el = document.getElementById(e)
	document.getElementById('priprto1').style.visibility = 'hidden';
	document.getElementById('priprto2').style.visibility = 'hidden';
	document.getElementById('priprto3').style.visibility = 'hidden';
	document.getElementById('priprto4').style.visibility = 'hidden';
	popupUp = 0;
	document.getElementById('cetrtek').style.display = 'none';
	document.getElementById('petek').style.display = 'none';
	document.getElementById('sobota').style.display = 'none';
	document.getElementById('nedelja').style.display = 'none';
	el.style.display = 'block';

}
function movemap(x,y) {
	// get curr location
	xc = document.getElementById('map').offsetLeft;
	yc = document.getElementById('map').offsetTop;
	dex = x;
	dey = y;
	// priti hocemo v 1 sekundi
	if (Math.abs(xc - dex) >= Math.abs(yc - dey)) {
		interval = Math.floor(500 / Math.abs(xc - dex));
	} else {
		interval = Math.floor(500 / Math.abs(yc - dey));
	}
	if (interval < 20) {
		interval = 20;
	}
}
function gumbi(n) {
	var gumbovi = [["Home","index.php"],["Schedule","program.php"],["Impressions","utrinki.php"],["Gallery","galerija.php"],["About","o_znanstivalu.php"],["Past years","arhiv.php"]];
	document.write('<div id="glava_pasica" onmouseout="pojdi('+ (150*n + 13) +')">');
	for (i = 0; i < gumbovi.length; i++) {
		if (i == n)
		{
			document.write('<div class="knof" id="izbran">'+gumbovi[i][0]+'</div>');
		} else {
			document.write('<div class="knof" onmouseout="fade_out(this,1,'+i+')" onmouseover="fade_in(this,'+i+'); pojdi('+(i*150 + 13)+');" onclick="window.location = \''+ gumbovi[i][1] + '\';">'+gumbovi[i][0]+'</div>');
		}
	}
	document.write('</div>');

}
function initdan()
{
	setTimeout(function(){dan('nedelja')},20);
	setTimeout(function(){dan('sobota')},40);
	setTimeout(function(){dan('petek')},60);
	setTimeout(function(){dan('cetrtek')},80);
}
function init(num)
{
	document.getElementById('glava_glow').style.left = num*150+13;
	trenutni_x = document.getElementById('glava_glow').offsetLeft;
	zeljeni_x = document.getElementById('glava_glow').offsetLeft
	document.getElementById('glava_glow').style.visibility = 'visible';
	premakni_glow();
	setTimeout(slide, 2000);
	cdiv = document.getElementById('currpicdiv');
	ndiv = document.getElementById('nextpicdiv');
}

function premakni_glow()
{
	trenutni_x = document.getElementById('glava_glow').offsetLeft
	dx = Math.abs(zeljeni_x - trenutni_x);
//	document.getElementById('debug').innerHTML = 't_x = ' + trenutni_x + '  z_x = ' + zeljeni_x + '   hitrost = ' + hitrost + '   dx = ' + dx;
	if (trenutni_x != zeljeni_x)
	{
		if (dx < 50)
		{
			if (hitrost != min_hitrost)
			{
				hitrost = hitrost + 5;
			}
		}
		else if (hitrost > max_hitrost)
		{
			hitrost = hitrost - 5;
		}
		else 
		{
			hitrost = hitrost
		}
		if (trenutni_x > zeljeni_x)
		{
			document.getElementById('glava_glow').style.left = trenutni_x - 5;
		}
		else 
		{
			document.getElementById('glava_glow').style.left = trenutni_x + 5 + 'px';
		}

	}
	else 
	{
		hitrost = 52;
	}
	setTimeout(function(){premakni_glow()},hitrost)
}

function fade_out(e,i,n)
{
//	greenhex = (223 + i*2).toString(16);
//	hex = i.toString(16);
//	set_color(e,'#ff'+ greenhex + hex + hex);
//	if (i < 15) {
//		fadeout[n] = setTimeout(function(){fade_out(e,i+1,n)}, 60);
//	}
//	e.style.textDecoration='none';
}

function fade_in(e,n)
{
//	set_color(e,'#ffd801');
//	if (fadeout[n] != undefined)
//	{
//		clearTimeout(fadeout[n]);
//	}
//	e.style.textDecoration='underline';
}

function set_color(el, col)
{
	el.style.color = col;
}
