var cenaZbozi = 0;

function toggleVisibility(objId) {
  var obj = document.getElementById(objId);
  if(obj) {
    obj.style.visibility = obj.style.visibility=='visible' ? 'hidden' : (obj.style.visibility=='hidden' ? 'visible' : 'visible');
    obj.style.display = obj.style.display=='block' ? 'none' : (obj.style.display=='none' ? 'block' : 'block');
  }
}

/* Modern JavaScript implementations has DOM functions getElementById and getElementsByTagName.
   However there is no function which return list of elements by the given CSS class name.
   This provides function getElementsByClass which do this. */
function getElementsByClass(searchClass, domNode, tagName) {
	if (domNode == null) domNode = document;
	if (tagName == null) tagName = '*';
	var el = new Array();
	var tags = domNode.getElementsByTagName(tagName);
	var tcl = " "+searchClass+" ";
	for(i=0,j=0; i<tags.length; i++) {
		var test = " " + tags[i].className + " ";
		if (test.indexOf(tcl) != -1)
			el[j++] = tags[i];
	}
	return el;
}

function button_onmouseover() {
	this.style.cursor='pointer';
}

function button_onmouseout() {
	this.style.cursor='auto';
}


function musicBackButton_onclick() {
	goToURL('index.php?s=music');
}

function addMusicListener() {
	/* Add listeners for Back button(s) events */
	var backButtons = getElementsByClass('buttonBack');
	for(var i=0; i<backButtons.length; i++) {
		var backButton = backButtons[i];
		backButton.onclick = musicBackButton_onclick;
		backButton.onmouseover = button_onmouseover;
		backButton.onmouseout = button_onmouseout;
	}
}


function galleryBackButton_onclick() {
	goToURL('index.php?s=pictures');
}

function addGalleryListener() {
	/* Transform every image in the gallery into lightbox image */
	var images = document.getElementsByTagName('img');
	for(var i=0; i<images.length; i++) {
		var img = images[i];
		if(img.src.indexOf('/foto/') != -1) {
			var par = img.parentNode;
			var elA = document.createElement('a');
			var elIMG = document.createElement('img');
			
			var lastSlashPos = img.src.lastIndexOf('/');
			var imgFileName = img.src.substr(lastSlashPos+1);
			var path = img.src.substr(0,lastSlashPos);
			lastSlashPos = path.lastIndexOf('/');
			path = path.substr(0,lastSlashPos+1);
			
			elIMG = img.cloneNode(false);
			elA.setAttribute('rel', 'lightbox[gallery]');
			elA.setAttribute('href', path+imgFileName);
			elA.appendChild(elIMG);
			par.insertBefore(elA, img);			
			par.removeChild(img);
		}
	}
	
	/* Add listeners for Back button(s) events */
	var backButtons = getElementsByClass('buttonBack');
	for(var i=0; i<backButtons.length; i++) {
		var backButton = backButtons[i];
		backButton.onclick = galleryBackButton_onclick;
		backButton.onmouseover = button_onmouseover;
		backButton.onmouseout = button_onmouseout;
	}
}


function recentPerformances_onclick() {
  toggleVisibility('recentPerformances_table');
}

function addRecentPerformancesListener() {
  /* Add listeners for Recent Performances button events */
  var obj = document.getElementById('recentPerformances');
  if(obj) {
	obj.onclick = recentPerformances_onclick;
	obj.onmouseover = button_onmouseover;
	obj.onmouseout = button_onmouseout;
  }
}


function popImage(image, title) {
  var w = image.width;
  var h = image.height;
  var src = new String(image.src);
  var newSrc = src.replace(/thumb\//,"");
  var nH = 480;
  var nW = Math.round(nH*(w/h));
  var xPos = (window.screen.availWidth - nW)/2;
  var yPos = (window.screen.availHeight - nH)/2;
  
  var newWindow = window.open('','imageWindow','toolbar=no,location=no,status=no,menubar=no,scrollbars=no,resizable=no,width='+nW+',height='+nH+',left='+xPos+',top='+yPos+',screenX='+xPos+',screenY='+yPos);
  if(!newWindow) {
    //popups disabled
    //alert('Prosím povolte Javascript pop-up okna.');
  } else {
    var buffer = '<html>\n';
	buffer += '<head>\n';
	if(title!=null && title!='') buffer += '<title>'+title+'</title>\n';
	buffer += '</head>\n';
	buffer += '<body style="margin:0px; padding:0px; background-color:#ffffff;" onclick="window.close()">\n';
	buffer += '<div align="center">\n';
	//buffer += '<img src="'+newSrc+'" width="'+nW+'" height="'+nH+'" border="0">\n';
	buffer += '<img src="'+newSrc+'" border="0">\n';
	buffer += '</div>\n';
	buffer += '</body>\n';
	buffer += '</html>\n';
    newWindow.document.write(buffer);
	newWindow.focus();
  }
}

function goToURL(URL) {
  eval("window.location='"+URL+"'");
}

function setPrice(newPrice) {
  cenaZbozi = newPrice;
}

function initPrices(lang) {
  var form = document.getElementById('form_shop01');
  if(!form) return false;
  
  if(lang=='CZ') {
    form.poslani[0].setAttribute('cena', '0');  //Osobne
    form.poslani[1].setAttribute('cena', '29'); //Posta - CR - obycejne
    form.poslani[2].setAttribute('cena', '39'); //Posta - CR - doporucene
    form.platba[0].setAttribute('cena', '0');   //Hotove pri predani
    form.platba[1].setAttribute('cena', '3');   //Bankovnim prevodem
    form.platba[2].setAttribute('cena', '20');  //Dobirkou
  } else if(lang=='EN') {
    form.poslani[0].setAttribute('cena', '0'); //Osobne
    form.poslani[1].setAttribute('cena', '2'); //Posta - CR - obycejne
    form.poslani[2].setAttribute('cena', '3'); //Posta - CR - doporucene
    form.platba[0].setAttribute('cena', '0');  //Hotove pri predani
    form.platba[1].setAttribute('cena', '1');  //Bankovnim prevodem
    form.platba[2].setAttribute('cena', '2');  //Dobirkou
  }
}

function reCalculate(str_currency) {
  var form = document.getElementById('form_shop01');
  if(!form) return false;

  if(form.poslani[0].checked) { //osobne
    form.platba[0].disabled=false; // hotove
    form.platba[1].disabled=false; //prevod
    form.platba[2].disabled=true;  //dobirka
    if(form.platba[0].checked) form.platba[0].checked=true;
  } else if(form.poslani[1].checked) { //obycejne cz
    form.platba[0].disabled=true;   
    form.platba[1].disabled=false;
    form.platba[2].disabled=true;
    if(form.platba[0].checked||form.platba[2].checked) form.platba[1].checked=true;
  } else if(form.poslani[2].checked) {  // doporucene cz
    form.platba[0].disabled=true;
    form.platba[1].disabled=false;
    form.platba[2].disabled=false;
    if(form.platba[0].checked) form.platba[2].checked=true;
  }
  
  var ii = 0;
  var poslani = 0;
  var platba = 0;
  
  for(ii=0; ii<form.poslani.length; ii++) {
    if(form.poslani[ii].checked) {
      poslani = parseInt(form.poslani[ii].getAttribute('cena'));
	  break;
	}
  }
  for(ii=0; ii<form.platba.length; ii++) {
    if(form.platba[ii].checked) {
	  platba = parseInt(form.platba[ii].getAttribute('cena'));
	  break;
	}
  }

  document.getElementById('postovne').innerHTML = (poslani+platba) + (str_currency!=null && str_currency!='' ? ' '+str_currency : '');
  document.getElementById('celkem').innerHTML= (poslani+platba+cenaZbozi) + (str_currency!=null && str_currency!='' ? ' '+str_currency : '');
}

