var pageTitle = document.title;

function getCurrentPic() {
  var currID = false;
  if (document.location.hash != '') {
    // backwards compatibility :(
    if (document.location.hash.search("p") != -1) {
      currID = document.location.hash.split("#p")[1];
    //} else {
    //  currID = document.location.hash;
    }
    if (typeof(arrGetCurrentPos(_g, currID)) == "undefined") {
      currID = firstPic;
    }
  }
  return currID;
}


function loadPic(picId) {
  // ja kaut kas ne tā, tad aiznes uz pirmo biwzhu
  if (typeof(arrGetCurrentPos(_g, picId)) == "undefined") {
    picId = firstPic;
    gotoPic(picId);
  }
  
  // aizvāc mazo tambneiliņu
  $('#thmb').remove();

  var picture = $('#gallery-picture-img');
  var pictureStatus = $('#gallery-picture-status');
  var pictureDummy = $('#gallery-dummy');

  showCurrentPosition(picId);

  var picPrevClass = picture.className;

  var arrPicFull = arrGetValue(_g, picId);
  var pictureUrlFull = picRootFull + '/' + picId + ".jpg";
  var pictureUrlPreload = picRootPreload + '/' + picId + ".jpg";

  pictureStatus.show();
  pictureDummy.append("<img src='" + pictureUrlFull + "' id='dummy-pic'/>");
  
  picture.attr({src: pictureUrlPreload, width: arrPicFull[0], height: arrPicFull[1]});

  //fitPictureToScreen();

  picture.addClass("picture-loading");

  $('#gallery-picture-summary').html(arrPicFull[5]);

  //$('title').html(pageTitle + " » " + arrPicFull[4]);
  document.title = pageTitle + " » " + arrPicFull[4];

  // kad pilnais attēls ielādējies, to attēlo preload'ētā attēla vietā
  // un paslēpj 
  $('#dummy-pic').load(function() { 
    picture.attr({src: pictureUrlFull});
    pictureDummy.empty();
    picture.removeClass("picture-loading");
    pictureStatus.hide();

    $.ajax({
      type: "GET",
      url: galleryRoot + "/ajax/?gpi=" + picId,
      data: "",
      success: function(msg) {
        $('#gallery-picture-info-container').html(msg);
        if (readCookie("gallery-picture-info") == 0) {
          $('#gallery-picture-info').hide();
        } else {
          $('#gallery-picture-info').show();
        }
        if (readCookie("metainfo-content-exif") == 0) {
          $('#metainfo-content-exif').hide();
        } else {
          $('#metainfo-content-exif').show();
        }
      }
    });
  });
  
  //setTimeout("showThmbToFullsize()", 2000);
  
  // all done, now can preload next one
  var nextPic = "";
  var tmp = arrGetNextKey(_g, picId);
  if (typeof(tmp) != "undefined") {
    nextPic = tmp;
    var preloadPicUrlFull = picRootFull + '/' + nextPic + ".jpg";
    $('#gallery').append('<div id="gallery-preload" style="display: none;"></div>');
    $('#gallery-preload').append("<img src='" + preloadPicUrlFull + "' id='gallery-preload-img'/>");
    // preloaded, now remove it
    $('#gallery-preload-img').load(function() {
      $('#gallery-preload').remove();
    });
  }
  
}

function gotoNext(e) {
  var tmp = arrGetNextKey(_g, lastShown);
  if (typeof(tmp) != "undefined") {
    gotoPic(tmp);
    lastShown = tmp;
    //this.blur();
  }
  return false;
}
function gotoPrev(e) {
  var tmp = arrGetPrevKey(_g, lastShown);
  if (typeof(tmp) != "undefined") {
    gotoPic(tmp);
    lastShown = tmp;
    //this.blur();
  }
  return false;
}

function gotoPic(picId) {
	document.location.hash = "p" + picId;
	loadPic(picId);
}

function showCurrentPosition(picId) {
  $('#gallery-navi-position').text((arrGetCurrentPos(_g, picId)+1) + "/" + arrCount(_g));
}

// funkcija, kas ielādē iepriekšējo vai nākamo attēlu,
// ja nospiež bultiņas pa kreisi vai labi uz klaviatūras
function checkKeycode(e) {
  if (typeof(_g) == "undefined")
    return true;

  var keycode;
  if (window.event) {
    keycode = window.event.keyCode;
  } else if (e) {
    keycode = e.which;
  }

  if (keycode == 37) {
    gotoPrev();
  }
  if (keycode == 39) {
    gotoNext();
  }

}


// funkcija, kas ielādē pareizo attēlu,
// ja izmanto pārlūka back un forward pogas
function manageHistory() {
  if (lastShown != getCurrentPic()) {
    lastShown = getCurrentPic();
    gotoPic(lastShown);
  }
}


function initGallery() {
  if (typeof(_g) == "undefined")
    return false;

  // $('#gallery').append('<div id="gallery-picture-info-container"></div>');
  // $('#gallery').append('<div id="gallery-thumbs"></div>');

  $('#gallery').append('<div id="gallery-picture"></div>');
  $('#gallery-picture').append('<div id="gallery-navi"><a href="#" id="gallery-navi-prev">«« REV</a>&nbsp;<span id="gallery-navi-position"></span>&nbsp;<a href="#" id="gallery-navi-next">FFW »»</a></div>');  
  $('#gallery-picture').append('<img id="gallery-picture-img" src="#" alt="Nākamais"/>');
  $('#gallery-picture').append('<div id="gallery-picture-summary"></div>');
  $('#gallery').append('<div id="gallery-dummy" style="display: none;"></div>');
  $('#gallery').append('<div id="gallery-picture-status">Loading..</div>');

  $('#gallery-navi-next').bind("click", gotoNext);
  $('#gallery-navi-prev').bind("click", gotoPrev);
  $('#gallery-picture-img').bind("click", gotoNext);

  loadPic(startingID);
  
  setInterval("manageHistory()", 500);
  //setInterval("fitPictureToScreen()", 1000);
}


window.onload = initGallery;
document.onkeydown = checkKeycode;
//window.onresize = fitPictureToScreen;
setInterval("keepfooteronbottomofthepage()", 100);
