/*
  Web: Janak bros. spol. s r. o. (www.janakbros.cz)
  Author: Martin Homolka, icontact.cz, http://icontact.cz
*/

//var timeOut;
var pole = new Array();

function Prvek(id,className) {
	this.id=id;
	this.className=className;
	this.timeOut;
}

function isInPole(id) {
	for (var i=0; i<pole.length; i++) {
		if (pole[i].id==id)
			return true;
	}
	return false;
}

function addToPole(id,className) {
	if (!isInPole(id))
		pole.push(new Prvek(id,className));
}

function getPrvek(id) {
	for (var i=0; i<pole.length; i++) {
		if (pole[i].id==id)
			return pole[i];
	}
	return null;
}

function setTimeOutRes(id,res) {
	for (var i=0; i<pole.length; i++) {
		if (pole[i].id==id)
			pole[i].timeOut = res;
	}
}


function onMenuOver(what) {
	addToPole(what,document.getElementById(what+'-span').className);
	if (prvek = getPrvek(what))
		clearTimeout(prvek.timeOut);

	try {
		document.getElementById(what).style.display='block';
		document.getElementById(what+'-span').className='active';
	} catch(e) {
	} 
}

function onMenuOut(what) {
	timeOut = setTimeout("hiddeMenu('"+what+"');",100);
	setTimeOutRes(what,timeOut);
}

function hiddeMenu(what) {
	try {
		document.getElementById(what).style.display='none';
		if (prvek=getPrvek(what))
			document.getElementById(what+'-span').className=prvek.className;
		else
			document.getElementById(what+'-span').className='';
	} catch(e) {
	}
}

function showImg(what) {
  document.getElementById('photo').src=what;
  document.getElementById('photocont').style.visibility='visible';
  try {
  	document.getElementById('balik').style.visibility='hidden';
  } catch(e) {
  } 
}


var sirkaFotky = 640;
var vyskaFotky = 640;
var topInterval;
var neutralFoto;
var fotoPole = new Array();
var aktualni = 0;
var celkem = 0;
var slideInterval;
var slideNextImg;
var slideInProgress = false;

function getScrollXY() {
  var scrOfX = 0, scrOfY = 0;
  if( typeof( window.pageYOffset ) == 'number' ) {
    //Netscape compliant
    scrOfY = window.pageYOffset;
    scrOfX = window.pageXOffset;
  } else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
    //DOM compliant
    scrOfY = document.body.scrollTop;
    scrOfX = document.body.scrollLeft;
  } else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
    //IE6 standards compliant mode
    scrOfY = document.documentElement.scrollTop;
    scrOfX = document.documentElement.scrollLeft;
  }
  return [ scrOfX, scrOfY ];
}

function getMySize() {
  var myWidth = 0, myHeight = 0;
  if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
    myWidth = window.innerWidth;
    myHeight = window.innerHeight;
  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    //IE 6+ in 'standards compliant mode'
    myWidth = document.documentElement.clientWidth;
    myHeight = document.documentElement.clientHeight;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    //IE 4 compatible
    myWidth = document.body.clientWidth;
    myHeight = document.body.clientHeight;
  }
  return [ myWidth , myHeight];
}

//90
function setMaxHeight() {
	mySize = getMySize();
//	alert('myHeight='+mySize[1]+', vyskaFotky='+vyskaFotky);
	if (mySize[1]-50<vyskaFotky)
		vyskaFotky = mySize[1] - 100;	
	
}

function pripravit() {
	celkem  = document.getElementById('celkem').value;
	neutralFotoSrc = document.getElementById('photo').src;
	neutralFoto = new Image();
	neutralFoto.src = neutralFotoSrc+'?w='+sirkaFotky;
	acka = document.getElementsByTagName('a');
	var poradi = 0;
	for (var i=0; i<acka.length; i++) {
		if (acka[i].className=='foto') {
			poradi = fotoPole.push(acka[i].href);
			pom = poradi-1;
//			acka[i].setAttribute('onClick',"showPict("+pom+"); return false;");
		}
	}
	document.getElementById('photo').src = neutralFoto.src;
}



/*

<p id="photocont">
        
        <span>Foto <span id="poradi">1</span>/ (<span class="likea" onclick="hide('photocont')">zavřít</span>)</span><br />

        <img src="{_PATH_storage}" alt="Fotografie č. X" id="photo" onclick="hide('photocont')" /><br />
        <span>
        <a href="javascript:prev();">Předchozí</a>
        <a href="javascript:startSlideshow();" id="slideshow">Slideshow</a>
        <a href="javascript:next();">Další</a>
        </span>
        
*/

function getPoradi(src) {
	var matchPole = src.match("(http.*/).*id=([0-9]+)");
	var odkaz = matchPole[1] + '?id=' + matchPole[2];
	for (var i=0; i<fotoPole.length; i++) {
		if (odkaz == fotoPole[i]) {
			return i;
		}
	}
}

function pripravitStatic() {
	
	var cel = 0;
	
	imgNodes = document.getElementsByTagName('img');
	var poradi = 0;
	for (var i=0; i<imgNodes.length; i++) {
		if (imgNodes.item(i).className == 'rozklik') {
			var item = imgNodes.item(i);
			var matchPole = imgNodes.item(i).src.match("(http.*/).*id=([0-9]+)");
			poradi = fotoPole.push(matchPole[1] + '?id=' + matchPole[2]);
			pom = poradi - 1;
			cel++;
			
			if (item.addEventListener) {
	        	item.addEventListener('click', function(event) {
	        		event.cancelBubble = true;
					event.returnValue = false;
					showPict(getPoradi(this.src));
					
	        	}, false);
	    	} else if (item.attachEvent) {
	        	item.attachEvent('onclick',  function(event) {
	        		event.cancelBubble = true;
					event.returnValue = false;
					
					showPict(getPoradi(event.srcElement.src));

				});
	    	}
		}
	}
	neutralFotoSrc = matchPole[1];
	neutralFoto = new Image();
	neutralFoto.src = neutralFotoSrc+'?w='+sirkaFotky;
	var pNode = document.createElement('div');
	pNode.id = 'photocont';
	pNode.innerHTML = '<p>Foto <span id="poradi">1</span>/'+cel+' (<span class="likea" onclick="hide(\'photocont\')">zavřít</span>)</p>'+
						'<img src="{_PATH_storage}" alt="Fotografie č. X" id="photo" onclick="hide(\'photocont\')" /><br />'+
        				'<p>'+
        				'<a href="javascript:prev();">&laquo; Předchozí</a> '+
        				'<a href="javascript:startSlideshow();" id="slideshow">Slideshow</a> '+
        				'<a href="javascript:next();">Další &raquo;</a> '+
        				'</p>';
	document.getElementById('text').appendChild(pNode);
	document.getElementById('photo').src = neutralFoto.src;
	celkem = cel;
}

function showPict(zdroj,poradi) {
	poradi = zdroj+1;
	setMaxHeight();
	aktualni = zdroj;
	if (topInterval) 
		window.clearInterval(topInterval);
	document.getElementById('photo').src = neutralFoto.src;
	document.getElementById('photo').src=fotoPole[zdroj]+'&w='+sirkaFotky+'&h='+vyskaFotky+'&m=1';
	document.getElementById('photocont').style.visibility='visible';
	try {
		document.getElementById('balik').style.visibility='hidden';
	} catch(e) {
	}
	document.getElementById('poradi').innerHTML=poradi;
	topInterval=window.setInterval("upravPozici();",500);
}

function hide(what) {
  	document.getElementById(what).style.visibility='hidden';
  	try {
	  if (what=='photocont') document.getElementById('balik').style.visibility='visible';
	} catch(e) {
	}
  	window.clearInterval(topInterval);
  	topInterval = null;
  	if (slideInProgress)
  		stopSlideshow();
}

function next() {
	if (celkem!=aktualni+1)
		showPict(aktualni+1,aktualni+2);
}

function prev() {
	if (aktualni!=0)
		showPict(aktualni-1,aktualni);
}

function loadSlideNext() {
	slideNextImg = new Image();
	slideNextImg.src = fotoPole[aktualni]+'&w='+sirkaFotky+'&h='+vyskaFotky+'&m=1';
	
}

function slideNext() {
	if (celkem==aktualni) {
		stopSlideshow();
		return false;
	}
	if (topInterval) {
		window.clearInterval(topInterval);
		topInterval = null;
	}
	aktualni++;
	document.getElementById('photo').src = slideNextImg.src;
	document.getElementById('photocont').style.visibility='visible';
	try {
		document.getElementById('balik').style.visibility='hidden';
	} catch(e) {
	}
	document.getElementById('poradi').innerHTML=aktualni;
	topInterval=window.setInterval("upravPozici();",500);
	loadSlideNext();
}

function startSlideshow() {
	slideInProgress = true;
	aktualni++;
	loadSlideNext();
	slideInterval = window.setInterval("slideNext();",5000);
	document.getElementById('slideshow').innerHTML='Stop';
	document.getElementById('slideshow').href="javascript:stopSlideshow();";
}

function stopSlideshow() {
	slideInProgress = false;
	window.clearInterval(slideInterval);
	document.getElementById('slideshow').innerHTML='Slideshow';
	document.getElementById('slideshow').href="javascript:startSlideshow();";
}


function upravPozici() {
	pozice = getScrollXY();
	if (pozice[1]<200) {
		Ypos = 200;
	} else {
		Ypos = pozice[1];
	}
	document.getElementById('photocont').style.top = Ypos+'px';
//	document.getElementById('poradi').innerHTML = pozice[1];
}

function popwin_ready() {
  document.getElementById('popwin').style.visibility='visible';
}

function popwin_close() {
  document.getElementById('popwin').style.visibility='hidden';
}

function vanocka() {
  if (document.getElementById('vanoce').style.display!='block') {
    document.getElementById('vanoce').style.display='block';
  } else {
    document.getElementById('vanoce').style.display='none';
  }
}
