/*
 * 0 = picture ID
 * 1 = preview picture path
 * 2 = large picture path
 * 3 = picture name
 * 4 = picture resolution
 * 5 = picture description
 */
var albumpictures = new Array();
var currentimage = -1;
var slideshowtimeout;
var emptyimage = '';

function insertWrappedText(text, node) {
  while (node.childNodes.length > 0) {
    node.removeChild(node.childNodes[0]);
  }
  
  var lines = text.split('<br />');
  for(var i=0; i<lines.length; i++) {
    var newnode = document.createTextNode(lines[i]);
    node.appendChild(newnode);
    
    var breaknode = document.createElement('br');
    node.appendChild(breaknode);
  }
}

function imageLoaded() {
  var image              = document.getElementById('previewpicture');
  var loadingpanel       = document.getElementById('pictureloading');

  if (image && loadingpanel) {
    image.style.display = 'block';
    loadingpanel.style.display = 'none';
  }
}

function updatePreviewImage() {
  var image              = document.getElementById('previewpicture');
  var picturename        = document.getElementById('picturename');
  var picturesize        = document.getElementById('picturesize');
  var pictureresolutuion = document.getElementById('pictureresolution');
  var picturedescription = document.getElementById('picturedescription');
  var picturecount       = document.getElementById('picturecount');
  var picturenumber      = document.getElementById('picturenumber');


  if (image) {
    if (currentimage == -1) {
      image.src   = emptyimage;
      image.alt   = '';
      image.title = '';
    } else {
		if (zoom==1) {
	      image.src   = 'image.php?id='+albumpictures[currentimage][0];
		} else {
	      image.src   = 'image.php?id='+albumpictures[currentimage][0]+'&width=300&height=400';
		}
      image.alt   = albumpictures[currentimage][1];
      image.title = albumpictures[currentimage][1];
    }
  }

  if (picturename) {
    if (currentimage == -1) {
      insertWrappedText('', picturename);
    } else {
      insertWrappedText(albumpictures[currentimage][1], picturename);
    }
  }

  if (picturesize) {
    if (currentimage == -1) {
      insertWrappedText('0 KB', picturesize);
    } else {
      insertWrappedText(albumpictures[currentimage][4] + ' kB', picturesize);
    }
  }

  if (pictureresolutuion) {
    if (currentimage == -1) {
      insertWrappedText('', pictureresolutuion);
    } else {
      insertWrappedText(albumpictures[currentimage][2], pictureresolutuion);
    }
  }

  if (picturedescription) {
    if (currentimage == -1) {
      insertWrappedText('', picturedescription);
    } else {
      insertWrappedText(albumpictures[currentimage][3], picturedescription);
    }
  }

  if (picturecount) {
    if (picturecount.childNodes.length == 0) {
      var textnode = document.createTextNode('0');
      picturecount.appendChild(textnode);
    }
    
    picturecount.firstChild.data = albumpictures.length;
  }

  if (picturenumber) {
    if (picturenumber.childNodes.length == 0) {
      var textnode = document.createTextNode('0');
      picturenumber.appendChild(textnode);
    }
    
    picturenumber.firstChild.data = currentimage + 1;
  }
  
  update_left_menu();
}

function stepFirst() {
  if (albumpictures.length == 0) {
    currentimage = -1;
  } else {
    currentimage = 0;
  }

  updatePreviewImage();
}

function stepPrior() {
  if (albumpictures.length == 0) {
    currentimage = -1;
  } else {
    if (currentimage == 0) {
      currentimage = albumpictures.length-1;
    } else {
      currentimage--;
    }
  }

  updatePreviewImage();
}

function stepNext() {
  if (albumpictures.length == 0) {
    currentimage = -1;
  } else {
    if (currentimage == (albumpictures.length-1)) {
      currentimage = 0;
    } else {
      currentimage++;
    }
  }

  updatePreviewImage();
}

function stepLast() {
  if (albumpictures.length == 0) {
    currentimage = -1;
  } else {
    currentimage = albumpictures.length-1;
  }

  updatePreviewImage();
}

function getSlideTime() {
  var control = document.getElementById('slidetime');
  if (control) {
    var value = parseInt(control.value);
    if (value < 1) {
      value = 1;
    } else if (value > 60) {
      value = 60;
    }
  } else {
    value = 3;
  }
  
  return value;
}

function startSlideShow() {
  var leftsubcaption = document.getElementById('leftsubcaption');
  if (leftsubcaption) {
    if (leftsubcaption.childNodes.length == 0) {
      var textnode = document.createTextNode('');
      leftsubcaption.appendChild(textnode);
    }
    
    leftsubcaption.firstChild.data = 'diavetítés';
  }

  
  var stime = getSlideTime();
  slideshowtimeout = setTimeout('stepSlideShow()', stime * 10);
  slideshowtimeout = setTimeout('startSlideShow()', stime * 3000);
}

function stopSlideShow() {
  clearTimeout(slideshowtimeout);

  var leftsubcaption = document.getElementById('leftsubcaption');
  if (leftsubcaption) {
    if (leftsubcaption.childNodes.length == 0) {
      var textnode = document.createTextNode('');
      leftsubcaption.appendChild(textnode);
    }
    
    leftsubcaption.firstChild.data = 'előnézet';
  }
}

function stepSlideShow() {
  stepNext();
  var stime = getSlideTime();
  slideshowtimeout = setTimeout('stepSlideShow()', stime * 1000);
}

function zoomImageByID(pictureid) {
  var zoomwindow = window.open('zoom.php?id='+pictureid, 'zoomimagewindow');
  if (!zoomwindow) {
    alert('Böngészőjében tiltva van a felugró ablakok megjelenése!\n\nAmennyiben Internet Explorer böngészőt használ, klikkeljen az ablak tetején megjelenő sárga információs sávra.');
  } else {
    zoomwindow.focus();
  }
}

function zoomImage() {
  if (currentimage >= 0) {
    zoomImageByID(albumpictures[currentimage][0]);
  }
}

function denotation(pictureid, userid) {
  var denotwindow = window.open('denotation.php?id='+pictureid, '','width=370,height=250,scrollbars=no,resizable=no,toolbar=no,location=no,directories=no,status=no,menubar=no');
  if (!denotwindow) {
    alert('Böngészőjében tiltva van a felugró ablakok megjelenése!\n\nAmennyiben Internet Explorer böngészőt használ, klikkeljen az ablak tetején megjelenő sárga információs sávra.');
  } else {
    denotwindow.focus();
  }
}
function sendPostcard() {
  if (currentimage >= 0) {
    window.location.href='index.php?mod=postcard&id=' + albumpictures[currentimage][0];
  }
}

function addCurrentImageToCart(producttype) {
  if (currentimage >= 0) {
    addToCart(albumpictures[currentimage][0], false, producttype);
  }
}

