var content = null,
  file_list = null,
  lightboxid = 'lbox_id',
  lightboxid_bg = 'lbox_background_id'

function getFotos( element, page ) {

  // id galerie, na niz se ptam
  var g = $(element).attr('href').last( 9 )

  $.post( 'gal.php', { 'g': g, ajax: 'true', 'page': page }, function( data ) {
    if( $('#body_container .content_main').length == 0 )
      $( '#body_container' ).append( data )
    else
      $('#body_container .content_main').replaceWith( data )

    $('a.lightbox').lightBox({
      fixedNavigation:true,
      imageBtnClose:'img/lightbox-btn-close.gif',
      imageBtnPrev:'img/lightbox-btn-prev.gif',
      imageBtnNext:'img/lightbox-btn-next.gif',
      imageLoading:'img/lightbox-ico-loading.gif'
    })
  })

}

function showFotosBox( gallery, currentFoto ) {

  getFotosList(gallery, currentFoto);

}

function getFotosList( gal, cfoto ) {
  fwk.ajax.post( 'gal_foto_list.php', {'gal':gal}, function( data ) {

    lightbox()

    file_list = fwk.parser.obj( data )

    var content = fwk.dom.findByClass( document.getElementById(lightboxid), 'content' ),
        img = document.createElement('img')

    content.appendChild( img )
    var coors = new Array( 800, 600 )
    for( var i = 0; i < file_list.length; i++ )
      if( file_list[i].f == cfoto ) {
        if( window_height() <= file_list[i].h ) {
          with( img ) { setAttribute('src', 'fotky/'+gal+'/full/'+file_list[i].f ); }
          coors = new Array(file_list[i].w,file_list[i].h)
        } else {
          with( img ) { setAttribute('src', 'fotky/'+gal+'/800x600/'+file_list[i].f ); }
        }
        break
      }

    container = fwk.dom.findByClass(document.getElementById(lightboxid),'contentContainer')
    container.style.width = (parseInt(coors[0])+10)+'px'
    container.style.height = (parseInt(coors[1])+10)+'px'
    fwk.event.bind( container, 'click', fwk.dom.stopPropagation )
    setPos()

  })
}

function lightbox() {

  var body = document.getElementsByTagName('body')[0],
      background = document.createElement('div'),
      container = document.createElement('div'),
      content_container = document.createElement('div'),
      content = document.createElement('div')

  body.appendChild( background )
  body.appendChild( container )

  with( background ) { setAttribute('id',lightboxid_bg) }
  with( container ) { setAttribute('id',lightboxid); appendChild(content_container) }
  with( content_container ) { className="contentContainer"; appendChild(content) }
  with( content ) { className="content"; }

  fwk.event.bind( window, 'scroll', setPos )
  fwk.event.bind( container, 'click', lightbox_dispose )
  setPos()

}

function lightbox_dispose() {

  var lbg = document.getElementById(lightboxid_bg),
      lig = document.getElementById(lightboxid)

  lbg.parentNode.removeChild(lbg)
  lig.parentNode.removeChild(lig)

}

function setPos() {
  var offy = 0,
      offx = 0
  if( typeof window.pageYOffset != 'undefined' ) {
    offy = window.pageYOffset
    offx = window.pageXOffset
  } else if( typeof document.documentElement.scrollTop != 'undefined' ) {
    offy = document.documentElement.scrollTop
    offx = document.documentElement.scrollLeft
  }

  if( offy < 0 ) offy = 0
  if( offx < 0 ) offx = 0

  var lboxbg = document.getElementById(lightboxid_bg),
      lbox   = document.getElementById(lightboxid)
  with( lboxbg.style ) { top=offy+'px'; left=offx+'px' }
  with( lbox.style   ) { top=offy+'px'; left=offx+'px' }

  var winH = window_height()

  var contentContainer = fwk.dom.findByClass(lbox,'contentContainer')
  var top_pos = Math.round(((winH-contentContainer.clientHeight)/2))+'px'
  //alert( winH + ' ' + contentContainer.clientHeight + ' ' + top_pos )
  contentContainer.style.marginTop = top_pos
}

function window_height() {
  if( typeof window.innerHeight != 'undefined' )
    return window.innerHeight
  else if( typeof document.body.clientHeight != 'undefined' )
    return document.body.clientHeight
}