var indexPrvniho = 0;  // Index prvniho
var indexPicAktiv = 0;  // Index velkeho obrazku

var imagePath = "/application/layouts/images/u-galerie";  // Cesta k obrazkum prostredi

var tmp;  // Pomocna promenna

var pictures = new Array();  // Pole pro obrazky


/**
 * Nacteni obrazku
 * 
 * @param img  Cesta k obrazku
 * @param sekce  Sekce zarazeni obrazku
 * @param popisek  Popisek obrazku
 */
function nactiImage(img, sekce, popisek)
{
  pictures[pictures.length] = new Object();
  
  pictures[pictures.length - 1]["dataPic"] = img;  // URL obrazku
  pictures[pictures.length - 1]["sekce"] = sekce;  // URL obrazku
  pictures[pictures.length - 1]["popisText"] = popisek;  // Popisek obrazku
}

/**
 * Sestaveni nahledu
 */
function vytvorNahledy()
{
  for( i = 0; i < nahleduCelkem; i++ )
  {
    li = document.createElement("li");  // List item
    
    link = document.createElement("a");  // Link
    link.href = "javascript:zobrazVelkyObr(" + (i + 1) + ")";
    
    obr = document.createElement("img");  // Image
    obr.border = "0";
    obr.id = "thumb" + (i + 1);
    obr.style.border = "2px solid " + borderColorDeactive;
    obr.setAttribute('class', 'thumb-image');
    obr.setAttribute('height', '60px');

    link.appendChild(obr);
    li.appendChild(link);
    
    document.getElementById("thumbs").appendChild(li);
  }
}

/**
 * Zarovnani nahledu
 */
function vyrovnej()
{
  for( i = 0; i < nahleduCelkem; i++ )
  {
    tmp = document.getElementById('thumb' + (i + 1));
  }
  
  // Zarovnani navigace nahledu
  document.getElementById('prev').style.verticalAlign = (thumbHeight - document.getElementById('prev').height) / 2 + "px";
  document.getElementById('next').style.verticalAlign = (thumbHeight - document.getElementById('next').height) / 2 + "px";
}


/**
 * Obnoveni nahledu
 */
function obnovNahled()
{
  // Navigace
  if( indexPrvniho > 0 )
    document.getElementById('pgPrev').src = imagePath + "/pgPrev.gif";
  else
    document.getElementById('pgPrev').src = imagePath + "/pgFirst.gif";
  if( indexPrvniho + nahleduCelkem < obrazkuCelkem )
    document.getElementById('pgNext').src = imagePath + "/pgNext.gif";
  else
    document.getElementById('pgNext').src = imagePath + "/pgLast.gif";
  
  if( indexPicAktiv == 0 )
    document.getElementById('prev').src = imagePath + "/first.gif";
  else
    document.getElementById('prev').src = imagePath + "/prev.gif";

  if( indexPicAktiv + 1 >= obrazkuCelkem )
    document.getElementById('next').src = imagePath + "/last.gif";
  else
    document.getElementById('next').src = imagePath + "/next.gif";
  
  if( !document.getElementById('thumbPopis').firstChild )
    document.getElementById('thumbPopis').appendChild(document.createTextNode(""));
  
  thumbStr = thumbString.replace(/%prvni%/i, (indexPrvniho + 1));
  thumbStr = thumbStr.replace(/%posledni%/i, (indexPrvniho + nahleduCelkem));
  thumbStr = thumbStr.replace(/%celkem%/i, obrazkuCelkem);
  document.getElementById('thumbPopis').innerHTML = thumbStr;
  
  
  for( i = 0; i < nahleduCelkem; i++ )
  {
    document.getElementById('thumb' + (i + 1)).src = "/foto/small/s_" + pictures[indexPrvniho + i]["dataPic"] + ".jpg";  // URL
    //document.getElementById('thumb' + (i + 1)).title = pictures[indexPrvniho + i]["popisText"];  // Title
  }
  
  ramecekNahledu();
}


/**
 * Predchozi nahled
 */
function prev()
{
  if( indexPicAktiv > 0 )
  {
    /*// Nahledy jsou vpravo od aktivniho obrazku
    if( indexPicAktiv < indexPrvniho )
    {
      indexPicAktiv = --indexPrvniho;
    }
    // Nahledy jsou vlevo od aktivniho obrazku
    else if( indexPicAktiv > nahleduCelkem + indexPrvniho )
    {
      indexPrvniho = indexPicAktiv - nahleduCelkem;
    }
    */
    if( indexPicAktiv == indexPrvniho )
    {
      indexPrvniho--;
    }
    
    indexPicAktiv--;
    
    obnovVelkyObrazek();
  }
  else if( indexPicAktiv == 0 )
  {
    indexPrvniho = 0;
  }
  
  obnovNahled();
}

/**
 * Nasledujici nahled
 */
function next()
{
  if( indexPicAktiv + 1 < obrazkuCelkem )
  {
    /*// Nahledy jsou vpravo od aktivniho obrazku
    if(indexPicAktiv + 1 < indexPrvniho )
    {
      indexPrvniho = indexPicAktiv + 1;
    }
    // Nahledy jsou vlevo od aktivniho obrazku
    else if( indexPicAktiv + 1 - indexPrvniho > nahleduCelkem )
    {
      indexPrvniho = indexPicAktiv + 1 - nahleduCelkem;
    }
    */
    if( indexPicAktiv + 1 - indexPrvniho == nahleduCelkem )
    {
      indexPrvniho++;
    }
    
    indexPicAktiv++;
    
    obnovVelkyObrazek();
  }
  else if( indexPicAktiv + 1 == obrazkuCelkem )
  {
    indexPrvniho = indexPicAktiv + 1 - nahleduCelkem;
  }
  
  obnovNahled();
}


/**
 * Predchozi strana nahledu
 */
function pgPrev()
{
  indexPicAktiv -= nahleduCelkem - 1;
  if( indexPicAktiv < 0 )
    indexPicAktiv = 0;
  
  indexPrvniho = indexPicAktiv;
  
  obnovNahled();
  
  obnovVelkyObrazek();
}

/**
 * Nasledujici strana nahledu
 */
function pgNext()
{
  indexPicAktiv += nahleduCelkem - 1;
  if( indexPicAktiv > obrazkuCelkem - 1 )
    indexPicAktiv = obrazkuCelkem - 1;
  
  if( indexPrvniho+" x "+(indexPicAktiv - nahleduCelkem + 1) )
    indexPrvniho = indexPicAktiv - nahleduCelkem + 1;
  
  obnovNahled();
  
  obnovVelkyObrazek();
}





/**
 * Zobrazeni obrazku
 * 
 * @param i  Index
 */
function zobrazVelkyObr(i)
{
  indexPicAktiv = indexPrvniho + i - 1;
  
  ramecekNahledu();
  
  obnovVelkyObrazek();
}

/* Obnova obrazku */
function obnovVelkyObrazek()
{
  document.getElementById('pic').src = "/foto/thumb/t_" + pictures[indexPicAktiv]["dataPic"] + ".jpg";  // URL, 480
  //document.getElementById('pic').title = pictures[indexPicAktiv]["popisText"];  // Title
  document.getElementById('picBoxA').title = pictures[indexPicAktiv]["popisText"];
  document.getElementById('picText').innerHTML = pictures[indexPicAktiv]["popisText"];
  
  tmp = document.getElementById('pic');  // Obrazek
  
  if( typeof(pictures[indexPicAktiv]["popisText"]) == 'undefined' )
  {
    str = popisObrazku.replace(/%popisText%/i, pictures[indexPicAktiv]["popisText"]);
    document.getElementById('picBoxA').title = str;
  }
  
  document.getElementById('picBoxA').href = "/foto/" + pictures[indexPicAktiv]["dataPic"] + ".jpg?keepThis=true&TB_iframe=true&height=640&width=480";
  
  /* Navigace */
  /*if( indexPicAktiv > 0 )
    document.getElementById('prev').src = imagePath + "/prev.gif";
  else
    document.getElementById('prev').src = imagePath + "/first.gif";

  if( obrazkuCelkem > indexPicAktiv + 1 )
    document.getElementById('next').src = imagePath + "/next.gif";
  else
    document.getElementById('next').src = imagePath + "/last.gif";
  */
  document.getElementById('file').value = pictures[indexPicAktiv]["dataPic"];
}


/* Zobrazeni prvniho obrazku pri otevreni galerie */
function zobrazPrvni()
{
  if( indexPicAktiv + 1 <= obrazkuCelkem )
  {
    indexPicAktiv;
    
    obnovVelkyObrazek();
  }
}


/* Vytvoreni ramecku kolem thumb obrazku */
function ramecekNahledu()
{
  for( i = indexPrvniho; i < nahleduCelkem + indexPrvniho; i++ )
  {
    if( (i + 1 - indexPrvniho) > 0 )
      document.getElementById('thumb' + (i + 1 - indexPrvniho)).style.borderColor = borderColorDeactive;
  }
  
  document.getElementById('thumb' + (indexPicAktiv + 1 - indexPrvniho)).style.borderColor = borderColorActive;
}

