imageLoader = new Image();
imageLoader.src = "images/image_load.gif";
var transitionsDone = new Array();
var imagesLoaded = new Array();
var galerie = new Array();
var shiftPixels = -118;

function checkMail(lang, url) {
	if (document.getElementById) {
		mailInput = document.getElementById('email').value;
		lightStatus = document.getElementById('light-status').value;
		re = /^[\w-]+(\.[\w-]+)*@([\w-]+\.)+[a-zA-Z]{2,7}$/;
		if (mailInput != '' && re.test(mailInput)) {
			var check = false;
			switch (lang) {
				case 'cz' :
				check = confirm('Přejete si dostávat program klubu a aktuální informace' +"\n" + 'na e-mail: ' + mailInput.toLowerCase() + '?');
				break;
				
				case 'en' :
				check = confirm('Would you like to receive club programme and news' + "\n" + 'on e-mail: ' + mailInput.toLowerCase() + '?');
				break;
			}
			if (check) window.location = "cfg/mail_insert.php?mail=" + mailInput.toLowerCase() + "&url=" + url + "&light=" + lightStatus;
		} else {
			switch (lang) {
				case 'cz' :
				alert('Zadejte prosím platnou e-mailovou adresu.');
				break;

				case 'en' :
				alert('Please enter a valid e-mail address.');
				break;
			}		
		}
	}
}

function screenHeight() {
var x,y;
var test1 = document.body.scrollHeight;
var test2 = document.body.offsetHeight
if (test1 > test2) // all but Explorer Mac
{
	x = document.body.scrollWidth;
	y = document.body.scrollHeight;
}
else // Explorer Mac;
     //would also work in Explorer 6 Strict, Mozilla and Safari
{
	x = document.body.offsetWidth;
	y = document.body.offsetHeight;
}
return y;
}

function closeGallery() {
	if (remover = document.getElementById('gallery-screen')) remover.parentNode.removeChild(remover); 
}

function displayImage(pictureToLoad) {
	var path = "images/uploaded/galerie" + galerie[pictureToLoad]['imgID'] + ".jpg";
	imagesLoaded[pictureToLoad]=true;
	document.getElementById('pictureToLoad').src = path;
	if (caption = document.getElementById('gallery-caption')) { if  (galerie[pictureToLoad] != undefined) caption.innerHTML = galerie[pictureToLoad]['caption']; }		
}

function loadImage(pictureToLoad) {
	var path = "images/uploaded/galerie" + galerie[pictureToLoad]['imgID'] + ".jpg";
	if (el = document.getElementById('pictureToLoad')) {
		if (imagesLoaded[pictureToLoad] == undefined) {
		el.src = imageLoader.src;
		newPic = new Image();
		newPic.onload = function() { setTimeout("displayImage("+pictureToLoad+")",600); }
		newPic.src = path;
		} else { 
		el.src = path; 
		if (caption = document.getElementById('gallery-caption')) caption.innerHTML = galerie[pictureToLoad]['caption']; 
		}
	}
}

function Transition(curve, milliseconds, callback) {
    this.curve_ = curve;
    this.milliseconds_ = milliseconds;
    this.callback_ = callback;
    this.start_ = new Date().getTime();
    var me = this;
    this.runCallback_ = function() {
        me.run();
    };
}

Transition.prototype.run = function() {
    if (!this.hasNext()) return; 
    this.callback_(this.next());
    setTimeout(this.runCallback_, 10);
}

Transition.prototype.hasNext = function() {
    if (this.done_) return this.finished();
    var now = new Date().getTime();
    if ((now - this.start_) > this.milliseconds_) {
        this.done_ = true;
        this.oneLeft_ = true;
    }
    return true;
}

Transition.prototype.next = function() {
    this.oneLeft_ = false;
    var now = new Date().getTime();
    var percentage = Math.min(1, (now - this.start_) / this.milliseconds_);
    return this.curve_(percentage);
}

Transition.prototype.finished = function() {
	var elOffset = this._count-1;
	transitionsDone.push(elOffset);
	if (transitionsDone.length == 9) { 
		transitionsDone = []; 
		//alert('done');
		if (parEl = document.getElementById('thumbs-container')) {	
		totalEls = parEl.getElementsByTagName('a');
		i = 0;
		while (i < totalEls.length) {
			id = parseInt(totalEls[i].id.substring(5));
			switch (this._dir) {
			case "forward" : 	newId = id+(1*this._steps); 
								if (id == 8 && this._steps==2) { 
								newId = 1; 
								si=this._ni-3; 
								frag = Math.floor(si/total);
								if (Math.floor(si/total)==si/total) si=total;
								else si = si - (total*frag);								
								totalEls[i].childNodes[0].src = "images/uploaded/galerie" + galerie[si-1]['imgID'] + "t.jpg";
								totalEls[i].childNodes[0].alt = galerie[si-1]['caption'];
								}
								if (id == 7 && this._steps==2) { 
								newId = 0; 
								si=this._ni-4; 
								frag = Math.floor(si/total);
								if (Math.floor(si/total)==si/total) si=total;
								else si = si - (total*frag);								
								totalEls[i].childNodes[0].src = "images/uploaded/galerie" + galerie[si-1]['imgID'] + "t.jpg";
								totalEls[i].childNodes[0].alt = galerie[si-1]['caption'];
								}
								if (id == 8 && this._steps==1) { 
								newId = 0; 
								si=this._ni-4; 
								frag = Math.floor(si/total);
								if (Math.floor(si/total)==si/total) si=total;
								else si = si - (total*frag);								
								totalEls[i].childNodes[0].src = "images/uploaded/galerie" + galerie[si-1]['imgID'] + "t.jpg";
								totalEls[i].childNodes[0].alt = galerie[si-1]['caption'];
								}
								break;
			case "backward": 	newId = id-(1*this._steps); 
								if (id == 0 && this._steps==2) {
								newId = 7; 
								si=this._ni+3; 
								frag = Math.floor(si/total);
								if (Math.floor(si/total)==si/total) si=total;
								else si = si - (total*frag);								
								totalEls[i].childNodes[0].src = "images/uploaded/galerie" + galerie[si-1]['imgID'] + "t.jpg";
								totalEls[i].childNodes[0].alt = galerie[si-1]['caption'];
								}
								if (id == 1 && this._steps==2) { 
								newId = 8; 
								si=this._ni+4; 
								frag = Math.floor(si/total);
								if (Math.floor(si/total)==si/total) si=total;
								else si = si - (total*frag);								
								totalEls[i].childNodes[0].src = "images/uploaded/galerie" + galerie[si-1]['imgID'] + "t.jpg";
								totalEls[i].childNodes[0].alt = galerie[si-1]['caption'];
								}
								if (id == 0 && this._steps==1) { 
								newId = 8; 
								si=this._ni+4; 
								frag = Math.floor(si/total);
								if (Math.floor(si/total)==si/total) si=total;
								else si = si - (total*frag);								
								totalEls[i].childNodes[0].src = "images/uploaded/galerie" + galerie[si-1]['imgID'] + "t.jpg";
								totalEls[i].childNodes[0].alt = galerie[si-1]['caption'];
								}
								break;
			}
			totalEls[i].id = 'thumb' + newId;
			totalEls[i].style.left = '';
			if (newId != 0 && newId != 8 && newId != 4) totalEls[i].href = "javascript:galleryGo(" + (newId-4) +");";
			if (newId == 4) totalEls[i].className = 'thumb selected';
			i++;
		}
		}
	loadImage(this._ni-1);
	if (arr = document.getElementById('galleryleft')) { arr.href="javascript:galleryGo(-1);"; }
	if (arr = document.getElementById('galleryright')) { arr.href="javascript:galleryGo(1);"; }	
	}
	return this.oneLeft_;
}

function SineCurve(percentage) {
    return (1 - Math.cos(percentage * Math.PI)) / 2;
}

function LinearCurve(percentage) {
    return percentage;
}

function galleryGo(gg) {
	if (arr = document.getElementById('galleryleft')) { arr.href="#"; }
	if (arr = document.getElementById('galleryright')) { arr.href="#"; }
	var steps = Math.abs(gg);
	var shiftPosition = steps*shiftPixels;
	if (steps != gg) shiftPosition = -shiftPosition;
	var newIndex = currentIndex + gg;
	if (newIndex > total) 	newIndex = newIndex - total;
	if (newIndex == 0) 		newIndex = total;
	if (newIndex < 0) 		newIndex = (total+1) + gg;
	currentIndex = newIndex;
	for (i=-4; i <= 4; i++) {
	id = i + 4;
		if (el = document.getElementById('thumb'+id)) {
		el.className = 'thumb';
		el._trans = function(elIndex) {
		var transition = new Transition(SineCurve, 200, function(percentage) {
			if (Math.abs(shiftPosition) == shiftPosition) {
			xOrig = (-shiftPixels * elIndex) - (2/steps)*shiftPosition;
			document.getElementById('thumb'+elIndex).style.left = xOrig + Math.round(percentage * shiftPosition) + "px"; 
			} else {
			xOrig = (-shiftPixels * elIndex) + (2/steps)*shiftPosition;
			document.getElementById('thumb'+elIndex).style.left = xOrig - Math.round(percentage * -shiftPosition) + "px"; 
			}
			});
		transition._count = id;
		transition._ni = newIndex;
		transition._steps = steps;
		if (Math.abs(shiftPosition) == shiftPosition) transition._dir = "forward";
		else transition._dir = "backward"; 
		transition.run();
		}
		el.href="#";
		el._trans(id);
		
		}
	}

}

function makeRequest(url,lang) {
var httpRequest;
if (window.XMLHttpRequest) { // Mozilla, Safari, ...
	httpRequest = new XMLHttpRequest();
	if (httpRequest.overrideMimeType) {
	httpRequest.overrideMimeType('text/xml');
    // See note below about this line
    }
   } 
   else if (window.ActiveXObject) { // IE
   try {
   httpRequest = new ActiveXObject("Msxml2.XMLHTTP");
   } 
   catch (e) {
         try {
              httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
              } 
   catch (e) {}
   }
   }
   if (!httpRequest) {
   alert('Chyba při čtení XML.');
   return false;
   }
httpRequest.onreadystatechange = function() { alertContents(httpRequest,lang); };
httpRequest.open('GET', url, true);
httpRequest.send('');
}

function alertContents(httpRequest,cl) {
	if (httpRequest.readyState == 4) {
    if (httpRequest.status == 200) {
		var xmldoc = httpRequest.responseXML;
		var xmlitems = xmldoc.getElementsByTagName('photo');
		total = xmlitems.length;

		for (i=0; i<total; i++) {
		galerie[i] = new Array('imgId', 'caption');
		galerie[i]['imgID'] = xmlitems[i].getAttribute('imgID');
		galerie[i]['caption'] = xmlitems[i].getAttribute('caption'+cl);
		}
		
		if (document.createElement) { //existuje fce. createElement
		if (!document.getElementById('gallery-screen')) { //existuje gallery-screen
		galleryScreenNode = document.createElement('div');
		galleryScreenNode.id = "gallery-screen";
		galleryScreenNode.style.height = screenHeight() + 'px';		
		galleryNode = document.createElement('div');
		galleryNode.id = "gallery";
		galleryImageDiv = document.createElement('div');
		galleryImageDiv.id = "image-main";
		galleryThumbs = document.createElement('div');
		galleryThumbs.id = "thumbs-container";
		galleryCaption = document.createElement('div');
		galleryCaption.id = "gallery-caption";
		galleryNav = document.createElement('div');
		galleryNav.id = "gallery-nav";
		galleryNav.innerHTML = '<a href="javascript:closeGallery();" id="closegallery"><span>x</span></a><a href="javascript:galleryGo(-1);" id="galleryleft"><img src="images/arr_left.gif" alt="" /></a><a href="javascript:galleryGo(1);" id="galleryright"><img src="images/arr_right.gif" alt="" /></a>';
		imgToLoad = document.createElement('img');
		imgToLoad.id = "pictureToLoad";
		imgToLoad.width = 600;
		imgToLoad.height = 400;
		galleryImageDiv.appendChild(imgToLoad);
		galleryNode.appendChild(galleryImageDiv);
		galleryNode.appendChild(galleryThumbs);
		galleryNode.appendChild(galleryCaption);	
		galleryNode.appendChild(galleryNav);	
		galleryScreenNode.appendChild(galleryNode);	
		}
		document.body.appendChild(galleryScreenNode);
		}
		
		loadImage(currentIndex-1);
		
		if (document.createElement) { //existuje fce. createElement
			parElement = document.getElementById('thumbs-container');
			for (i=0; i<9; i++) {
			thumb = document.createElement('a');
			if (i!=4) thumb.className = "thumb"; else thumb.className = "thumb selected";
			thumb.id = "thumb" + i;
			if (i!=0 && i!=1 && i!=4 && i!=7 && i!=8) thumb.href = "javascript:galleryGo("+(i-4)+")";
			tempIndex = (currentIndex-1)+i-4;
			frag = Math.floor(tempIndex/total);
			if (Math.floor(tempIndex/total)==tempIndex/total) tempIndex=total;
			else tempIndex = tempIndex - (total*frag);
			if (tempIndex<0) tempIndex = Math.abs(total+tempIndex);
			if (tempIndex>=total) tempIndex = tempIndex-total;
			if (tempIndex==total) tempIndex = 0;
			var path = "images/uploaded/galerie" + galerie[tempIndex]['imgID'] + "t.jpg";
			thumbIMG = document.createElement('img');
			thumbIMG.src = path;
			thumbIMG.width = 100;
			thumbIMG.height = 66;
			thumbIMG.alt = galerie[tempIndex]['caption'];
			thumb.appendChild(thumbIMG);
			parElement.appendChild(thumb);
			}
		}		
		
    } else {
    alert('There was a problem with the request.');
    }
	}
}

function openGallery(frame,cl) {
	currentIndex = frame || 1;
	cl = cl.toUpperCase() || 'CZ';
	makeRequest("scripts/gallery.php",cl);
}

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 pointerTrans(target, pos, targetPos, direction) {
	var step = 20;
	if (el = document.getElementById('pointer')) {
	if (pos < targetPos-step && direction=='up') {
			if (targetPos == pointerEdge-100)	newPos = pos+step*2;
			else newPos = pos+step;
			el.style.left = newPos+'px';
			timeout = setTimeout("pointerTrans('" + target + "',"+ newPos +","+ targetPos +",'"+ direction +"')",15);

	} else if (pos > targetPos+step && direction=='down') {
			if (targetPos == pointerEdge-100)	newPos = pos-step*2;
			else newPos = pos-step;
			el.style.left = newPos+'px';
			timeout = setTimeout("pointerTrans('" + target + "',"+ newPos +","+ targetPos +",'"+ direction +"')",15);
	} else {
		el.style.left = targetPos + 'px';
		if (targetPos == pointerEdge-100) el.style.visibility = "hidden";
		}
	}
}

function movePointer(linkID,state) {
	var screenOffset = 51;
	if (p = document.getElementById('pointer')) {
	plength = p.offsetWidth;
	pcoords = findPos(p);
	if (p.style.visibility != "visible") p.style.visibility = "visible";
	if (m = document.getElementById('menu'))  {
	if (l = document.getElementById(linkID)) {
			var coords  = findPos(l);
			var xlength = l.offsetWidth;
			//var movePos = coords[0] - screenOffset + Math.round(xlength/2) - Math.round(plength/2);
			if (typeof timeout != "undefined") clearTimeout(timeout);
			if (state == "in" && typeof wait != "undefined") clearTimeout(wait);
			if (pcoords[0] - screenOffset < coords[0] - screenOffset + Math.round(xlength/2) - Math.round(plength/2))
			//wait = setTimeout("pointerTrans('" + linkID +"',"+ parseInt(pcoords[0] - screenOffset)+","+ parseInt(coords[0] - screenOffset + Math.round(xlength/2) - Math.round(plength/2))+",'up')", 200);
			pointerTrans(linkID, pcoords[0] - screenOffset, coords[0] - screenOffset + Math.round(xlength/2) - Math.round(plength/2), 'up');
			else if (state == "in") pointerTrans(linkID, pcoords[0] - screenOffset, coords[0] - screenOffset + Math.round(xlength/2) - Math.round(plength/2), 'down');
			else {
				if (pointerPosition < pcoords[0] - screenOffset)
				wait = setTimeout("pointerTrans('" + linkID +"',"+ parseInt(pcoords[0] - screenOffset)+","+ pointerPosition +",'down')", 700);
				else 
				wait = setTimeout("pointerTrans('" + linkID +"',"+ parseInt(pcoords[0] - screenOffset)+","+ pointerPosition +",'up')", 700);
			}
			//else wait = setTimeout("pointerTrans('" + linkID +"',"+ parseInt(pcoords[0] - screenOffset)+","+ parseInt(coords[0] - screenOffset + Math.round(xlength/2) - Math.round(plength/2))+",'down')", 700);
			//pointerTrans(linkID, pcoords[0] - screenOffset, coords[0] - screenOffset + Math.round(xlength/2) - Math.round(plength/2), 'down');
			//p.style.left =  movePos + "px";
	}
	}
	}
}

function flipSwitch() {
		if (lightswitch = document.getElementById('switch')) {
				for( var x = 0; x < lightswitch.attributes.length; x++ ) {
				if( lightswitch.attributes[x].nodeName.toLowerCase() == 'title' ) {
						if (lightswitch.attributes[x].nodeValue == switchon) lightswitch.attributes[x].nodeValue = switchoff;
						else lightswitch.attributes[x].nodeValue = switchon;
				}
			}
		}

		images = new Array("switch","heading","calendar","intro","logo","lang","menu-klub","dot0","menu-cafebar","dot1","menu-koncerty","dot2","menu-napoje","dot3","menu-galerie","dot4","menu-rezervace","dot5","menu-odkazy");

		for (i=0; i < images.length; i++) {
		if (el = document.getElementById(images[i])) {
		if (el.className != "dark") el.className = "dark";
		else el.className = "";
		}
		}
		
		links = new Array("menu-cafebar","menu-klub","menu-koncerty","menu-napoje","menu-rezervace","menu-odkazy","logo","lang-cz","lang-en");

		for (i=0; i < links.length; i++) {

		if (el = document.getElementById(links[i])) {
			oldlink = el.href;
			if (oldlink.indexOf("light=off") == -1) newlink = el.href+"?light=off";
			else newlink = el.href.substring(0,el.href.length-10);
			el.href = newlink;
		}
		}
		
		lightStatus = document.getElementById('light-status').value;
		lightStatus == 'true' ? document.getElementById('light-status').value = 'false' : document.getElementById('light-status').value = 'true';
}

function insertFlashMC() {
	html = "<object";
	movie = "";
	args = arguments;
	
	for (var i=0; i < args.length; i=i+2) {
		var currArg = args[i].toLowerCase();	
		switch (currArg) {
			case "width" : html += ' width="' + args[i+1] + '"'; break;
			case "height" : html += ' height="' + args[i+1] + '"'; break;
			case "data" : html += ' data="' + args[i+1] + '"'; movie = args[i+1]; break;
		}
	}
	html += ' type="application/x-shockwave-flash">'+"\n";
	html += '<param name="movie" value="' + movie + '" />'+"\n";
	html += '<param name="quality" value="high" />'+"\n";
	html += '<param name="wmode" value="transparent" />'+"\n";
	html += '<param name="menu" value="false" />'+"\n";
	html += "</object>\n";
	document.writeln(html);
}