var thumbimagew = 112;
var thumbimageh = 100;
var fsimagew = 615;
var fsimageh = 420;
var rootfolder = 'http://www.megales.si/';

function refreshfsimagecontrols() {
imageindex = parseInt(document.getElementById('fsimage').getAttribute('imageindex'));
imagecount = parseInt(document.getElementById('thumbsdisplay').getAttribute('imagecount'));
if (imageindex == imagecount) {
document.getElementById('fsimagecontrols_nextbtn').style.display = 'none';
} else {
document.getElementById('fsimagecontrols_nextbtn').style.display = 'inline';
}
if (imageindex == 1) {
document.getElementById('fsimagecontrols_prevbtn').style.display = 'none';
} else {
document.getElementById('fsimagecontrols_prevbtn').style.display = 'inline';
}
}

function OpenFSimage(obj) {
if (document.getElementById('fsimage') != null) {
document.getElementById('fsimageopis').innerHTML = obj.getAttribute('opis');
document.getElementById('fsimage').onload = function() {this.className='fsimage'; document.getElementById('fsimagediv').style.display = 'block'; refreshfsimagecontrols();};
document.getElementById('fsimage').setAttribute('imageindex', obj.getAttribute('imageindex'));
document.getElementById('fsimage').src = rootfolder + 'subpages/fsimage.php?imgwidth=' + fsimagew + '&imgheight=' + fsimageh + '&imgfn=' + obj.getAttribute('imgfn');
PreloadImage(parseInt(obj.getAttribute('imageindex')) + 1);
PreloadImage(parseInt(obj.getAttribute('imageindex')) - 1);
refreshfsimagecontrols();
}
}

function CloseFSimage() {
if (document.getElementById('fsimagediv') != null) {
document.getElementById('fsimagediv').style.display = 'none';
}
}

function PreloadImage(imageindex) {
if (document.getElementById('thumbsdisplay') != null) {
imagecount = parseInt(document.getElementById('thumbsdisplay').getAttribute('imagecount'));
if ((imageindex > 0) && (imageindex <= imagecount)) {
if (document.getElementById('imgpreloader_' + imageindex) == null) {
   arrayofthumbdivelements = document.getElementById('thumbsdisplay').getElementsByTagName('a');
   thumbdivobj = null;
   for (i = 0; i < arrayofthumbdivelements.length; i++) {
      if (parseInt(arrayofthumbdivelements[i].getAttribute('imageindex')) == imageindex) {
          thumbdivobj = arrayofthumbdivelements[i];
      }
   }
   if (thumbdivobj != null) {
   imgfn = rootfolder + 'subpages/fsimage.php?imgwidth=' + fsimagew + '&imgheight=' + fsimageh + '&imgfn=' + thumbdivobj.getAttribute('imgfn');
   var imgobj = document.createElement('img');
   imgobj.setAttribute('id', 'imgpreloader_' + imageindex);
   imgobj.setAttribute('imageindex', imageindex);
   imgobj.style.display = 'none';
   imgobj.onload = function() {PreloadImage(parseInt(this.getAttribute('imageindex'))+1); this.parentNode.removeChild(this);}
   document.body.appendChild(imgobj);
   imgobj.src = imgfn;
   }
}
}
}
}

function OpenFSimagefromindex(obj,incindex) {
if (document.getElementById('fsimage') != null) {
imageindex = parseInt(document.getElementById('fsimage').getAttribute('imageindex'));
imageindex = imageindex + incindex;
imagecount = parseInt(document.getElementById('thumbsdisplay').getAttribute('imagecount'));
if ((imageindex > 0) && (imageindex <= imagecount)) {
arrayofthumbdivelements = document.getElementById('thumbsdisplay').getElementsByTagName('a');
thumbdivobj = null;
for (i = 0; i < arrayofthumbdivelements.length; i++) {
    if (parseInt(arrayofthumbdivelements[i].getAttribute('imageindex')) == imageindex) {
        thumbdivobj = arrayofthumbdivelements[i];
    }
}
if (thumbdivobj != null) {
document.getElementById('fsimageopis').innerHTML = thumbdivobj.getAttribute('opis');
document.getElementById('fsimageloader').style.visibility = 'visible';
document.getElementById('fsimage').className = 'loading';
document.getElementById('fsimage').setAttribute('imageindex', imageindex);
document.getElementById('fsimage').onload = function() {document.getElementById('fsimageloader').style.visibility = 'hidden'; this.className='fsimage';};
document.getElementById('fsimage').src = rootfolder + 'subpages/fsimage.php?imgwidth=' + fsimagew + '&imgheight=' + fsimageh + '&imgfn=' + thumbdivobj.getAttribute('imgfn');
PreloadImage(imageindex + 1);
PreloadImage(imageindex - 1);
}
}
refreshfsimagecontrols();
}
}

function ThumbLoaded(obj)
{
obj.style.display='inline';
obj.parentNode.getElementsByTagName('img')[0].style.display='none';
}

function OpenGallery(obj, galleryid)
{
activebuttonobj = null;
if (document.getElementById('gallerybuttons') != null) {
arrayofgallerybuttons = document.getElementById('gallerybuttons').getElementsByTagName('a');
for (i = 0; i < arrayofgallerybuttons.length; i++) {
    if (arrayofgallerybuttons[i].className == 'gallerybuttonactive') {
        activebuttonobj = arrayofgallerybuttons[i];
    }
  if ((obj == null) && (galleryid == parseInt(arrayofgallerybuttons[i].getAttribute('galleryid')))) {
      obj = arrayofgallerybuttons[i];
  }
}
}
if (activebuttonobj != null) {
   activebuttonobj.className = '';
}
if ((obj != null) && (obj != 'REFRESHFROMUPLOAD')) {
obj.className = 'gallerybuttonactive';
}
if (obj == 'REFRESHFROMUPLOAD') {
document.getElementById('thumbsdisplay').innerHTML = '<img src="design/indicator_waitanim.gif" style="position: absolute; top: 50%; left: 50%; width: 78px; height: 78px; margin-left: -39px; margin-top: -39px;" />';
document.getElementById('thumbsdisplay').innerHTML = makeRequestSynch(rootfolder + 'subpages/lsgallery.php', '?action=getgallerythumbs&itemid=' + galleryid);
AddImageForm();
} else {
document.getElementById('thumbsdisplay').innerHTML = '<img src="design/indicator_waitanim.gif" style="position: absolute; top: 50%; left: 50%; width: 78px; height: 78px; margin-left: -39px; margin-top: -39px;" />';
document.getElementById('thumbsdisplay').innerHTML = makeRequestSynch(rootfolder + 'subpages/lsgallery.php', '?action=getgallerythumbs&itemid=' + galleryid);
}
document.getElementById('thumbsdisplay').setAttribute('galleryid', galleryid);
InitGallery();
}

function InitGallery()
{
CloseFSimage();
arrayofthumbdivelements = document.getElementById('thumbsdisplay').getElementsByTagName('a');
imageindex = 1;
for (i = 0; i < arrayofthumbdivelements.length; i++) {
        if (arrayofthumbdivelements[i].getAttribute('imgfn') != null) {
        imgfn = rootfolder + 'subpages/fsimage.php?imgwidth=' + thumbimagew + '&imgheight=' + thumbimageh + '&imgfn=' + arrayofthumbdivelements[i].getAttribute('imgfn') + '&isthumb=1&cancreatethumb=1';
        arrayofthumbdivelements[i].getElementsByTagName('img')[1].src = imgfn;
        arrayofthumbdivelements[i].getElementsByTagName('span')[0].innerHTML = arrayofthumbdivelements[i].getAttribute('opis');
        imageindex = imageindex + 1;
		}
}
document.getElementById('thumbsdisplay').setAttribute('imagecount', imageindex-1);
PreloadImage(1);
}

function GetRequeryVal()
{
var d = new Date();
return d.getTime();
}

function EditImageComment(simgfn, comment)
{
var newcomment = prompt('Opis:', comment);
if ((newcomment != null) && (newcomment != comment)) {
   result = makeRequestSynch(rootfolder + 'subpages/lsgallery.php', '?action=editimagecomment&requeryval=' + GetRequeryVal() + '!POST?imgfn=' + simgfn + '&newcomment=' + newcomment);
   resultarr = result.split(';');
   if (resultarr[0] != 'OK') {
     if (resultarr[1] == '') {
   alert(resultarr[0]);
   } else {
     alert(resultarr[1]);
   }
   return false;
   } else {
     arrayofthumbdivelements = document.getElementById('thumbsdisplay').getElementsByTagName('a');
     thumbdivobj = null;
     for (i = 0; i < arrayofthumbdivelements.length; i++) {
       if (parseInt(arrayofthumbdivelements[i].getAttribute('imageindex')) == imageindex) {
          thumbdivobj = arrayofthumbdivelements[i];
       }
     }
     if (thumbdivobj != null) {
      thumbdivobj.setAttribute('opis', newcomment);
    thumbdivobj.getElementsByTagName('span')[0].innerHTML = thumbdivobj.getAttribute('opis');
        document.getElementById('fsimageopis').innerHTML = thumbdivobj.getAttribute('opis');
   }
   }
}
}

function DeleteImage(imgfn)
{
if (confirm("Izbrisi sliko?")) {
   if (confirm("Zelite resnicno izbrisati sliko?")) {
      result = makeRequestSynch(rootfolder + 'subpages/lsgallery.php', '?action=deleteimage&requeryval=' + GetRequeryVal() + '!POST?imgfn=' + imgfn);
    resultarr = result.split(';');
    if (resultarr[0] != 'OK') {
       if (resultarr[1] == '') {
     alert(resultarr[0]);
     } else {
       alert(resultarr[1]);
     }
     return false;
    } else {
     galleryid = document.getElementById('thumbsdisplay').getAttribute('galleryid');
     OpenGallery(null, galleryid);
     alert('Slika je bila izbrisana!');
    }
   }
}
}

function AddImageForm(nopth)
{
galleryid = document.getElementById('thumbsdisplay').getAttribute('galleryid');
if (window.XMLHttpRequest) {
nopth = true;
}
if (nopth == true) {
document.getElementById('uploaderdiv').innerHTML = makeRequestSynch(rootfolder + 'lsgallery.php', '?action=addimageform&galleryid=' + galleryid);
} else {
document.getElementById('uploaderdiv').innerHTML = makeRequestSynch(rootfolder + 'subpages/lsgallery.php', '?action=addimageform&galleryid=' + galleryid);
}
}

function AddEditGallery()
{
CloseFSimage();
actionstringpost = '';
formel = document.getElementById('addeditgalleryform');
tmpformelements = formel.elements;
for (iformel = 0; iformel < tmpformelements.length; iformel++) {
    if ((tmpformelements[iformel].type == 'text') || (tmpformelements[iformel].type == 'hidden')) {
    formelname = tmpformelements[iformel].name;
    formelvalue = tmpformelements[iformel].value;
    }
  actionstringpost = actionstringpost + formelname + "=" + formelvalue + "&";
}
result = makeRequestSynch(rootfolder + 'subpages/lsgallery.php', '?action=addeditsave' + '&requeryval=' + GetRequeryVal() + '!POST?' + actionstringpost);
resultarr = result.split(';');
if (resultarr[0] != 'OK') {
  alert(resultarr[1]);
  return false;
} else {
  lang = ParseGetParamFromUrl(window.location,'lang');
  galleryid = parseInt(resultarr[1]);
  window.location = "index.php?subpage=3&galleryid=" + galleryid + "&lang=" + lang;
}
}

function DeleteGallery()
{
CloseFSimage();
var promptres = prompt('Ce zelite izbrisati celotno galerijo vpisite IZBRISI:', '');
if (promptres == 'IZBRISI') {
   if (confirm("Izbrisana bo celotna galerija in vse slike v galeriji! Ali zelite nadaljevati?")) {
      galleryid = document.getElementById('thumbsdisplay').getAttribute('galleryid');
	  result = makeRequestSynch(rootfolder + 'subpages/lsgallery.php', '?action=deletegallery&requeryval=' + GetRequeryVal() + '&itemid=' + galleryid);
    resultarr = result.split(';');
    if (resultarr[0] != 'OK') {
       if (resultarr[1] == '') {
     alert(resultarr[0]);
     } else {
       alert(resultarr[1]);
     }
     return false;
    } else {
	 lang = ParseGetParamFromUrl(window.location,'lang');
     alert('Galerija je bila izbrisana!');
	 window.location = "index.php?subpage=3" + "&lang=" + lang;
    }
   }
}
}