// Galerie, v. 2, testovano na FF2, Opera 9.5, IE6, Safari 3 Beta -- (c) Ondrej 080215 :-)
// Pouziti:
// do body.onload zavolat photos.init( "soubor.dat", "barva_pozadi", "barva_ramecku" )
// a potom pro zobrazeni fotky photos.show( cislo_fotky ) .
//
// soubor.dat je soubor, kde je jeden nazev souboru s fotkou na radek.
// detaily viz popisky u jednotlivych metod


// objekt galerie
var photos = new function Photos()
{
    //
    // promenne
    // 


    // private -- div s fotkami
    this.photoDiv = null;
    // private -- primy odkaz na fotku
    this.photoImage = null;
    // private -- primy odkaz "nahravam" vypln
    this.photoLoader = null;
    // private -- cislo zobrazovane fotky
    this.photoCurrent = 0;
    // private -- horni souradnice pred zobrazenim okna (aby se k ni dalo vratit)
    this.documentScreenTop = 0;

    // vsechny fotky
    this.photoList = new Array();


    //
    // funkce
    //

    // private
    // nahraje data z textoveho souboru
    this.loadPhotoList = function( url )
    {
	var ajaxObject = ( window.XMLHttpRequest 
		? new XMLHttpRequest : (window.ActiveXObject ? new ActiveXObject("Microsoft.XMLHTTP") : false));

	
	if (!ajaxObject) 
	    return null;

	ajaxObject.open( "GET", url, false );
	ajaxObject.send( null );

	if ( ajaxObject.status != 200 )
	    return null;
	
	return ajaxObject.responseText;
    };

    // private
    // rozparsuje seznam fotek, vytahne si nazvy souboru
    this.parsePhotoList = function( listText )
    {
	var l = 0, i = 0;
	var c = 0;

	while( (i = listText.indexOf( "\n", l ) ) != -1 )
	{
	    if ( l != i && listText.charAt(l) != '#' )
		this.photoList[c++] = listText.substring( l, i );
	    l = i + 1;
	}
	return ( c != 0 );
    }

    // private
    // po nahrani fotky -- jeji zobrazeni a prenastaveni velikosti
    this.loaded = function()
    {
	this.photoLoader.style.display = "none";
	this.photoImage.style.display = "";
	
	var propsDiv = this.photoDiv.offsetWidth / this.photoDiv.offsetHeight;
	var propsImg = this.photoImage.width / this.photoImage.height;

      	if ( window.opera )
	    photoImage.focus(); // kvuli opere - vynuti prekresleni

	if ( propsDiv > propsImg ) // div je "sirsi" nez obrazek -- fit to height
	{
	    propsImg = ( this.photoDiv.offsetHeight / this.photoImage.height ) * this.photoImage.width;
	    this.photoImage.style.width = Math.floor( propsImg ) + 'px';
	    this.photoImage.style.height = this.photoDiv.offsetHeight + 'px';
	    this.photoImage.style.marginLeft = Math.floor( ( this.photoDiv.offsetWidth - propsImg ) / 2 ) + 'px';
	    this.photoImage.style.marginTop = '0';
	}
	else  // div je "vyssi" nez obrazek -- fit to width
	{
	    propsImg = ( this.photoDiv.offsetWidth / this.photoImage.width ) * this.photoImage.height;
	    this.photoImage.style.width = this.photoDiv.offsetWidth + 'px';
	    this.photoImage.style.height = Math.floor( propsImg ) + 'px';
	    this.photoImage.style.marginLeft = '0';
	    this.photoImage.style.marginTop = Math.floor( ( this.photoDiv.offsetHeight - propsImg ) / 2 ) + 'px';
	}
    }

    // private
    // zjisteni y-pozice aktualniho viewportu (viz Quirksmode.org)
    this.getScreenTop = function()
    {
	if (self.pageYOffset) // all except Explorer
	{
	    return self.pageYOffset;
	}
	else if (document.documentElement && document.documentElement.scrollTop) // Explorer 6 Strict
	{
	    return document.documentElement.scrollTop;
	}
	else if (document.body) // all other Explorers
	{
	    return document.body.scrollTop;
	}
    }


    // public
    // inicializace -- dostava jmeno textoveho souboru se seznamem fotek, 
    // barvu pozadi (color1) a oramovani (color2)
    this.init = function( url, color1, color2 )
    {
	var tmp_list;

	// vyrobi div pro fotky
	this.photoDiv = document.createElement( 'DIV' );
	this.photoDiv.id = "photoDiv";
	this.photoDiv.style.position = "absolute";
	this.photoDiv.style.width = "95%"; 
	this.photoDiv.style.height = "95%"; 
	this.photoDiv.style.left = "2%"; 
	this.photoDiv.style.top = "2%"; 
	this.photoDiv.style.zIndex = "10";
	this.photoDiv.style.background = color1;
	this.photoDiv.style.overflow = "hidden";
	this.photoDiv.style.border = "5px solid " + color2;
	this.photoDiv.style.display = "none";
	this.photoDiv.innerHTML = '<div id="photoControls" style="position:absolute; right:2.5%; top:0; z-index:15;">'
	    + '<button id="photoBackward" onclick="photos.backward();" type="button">&lt;</button>'
	    + '<button id="photoForward" onclick="photos.forward();" type="button">&gt;</button>'
	    + '<button id="photoClose" onclick="photos.close();" type="button">X</button></div>'
	    + '<img id="photoImage" src="" alt="fotka" onload="photos.loaded()" /><span id="photoLoader" ></span>';

	// vlozi do dokumentu
	document.body.appendChild( this.photoDiv );

	// nastavi promenne
	this.photoLoader = document.getElementById( "photoLoader" );
	this.photoImage = document.getElementById( "photoImage" );
	this.photoImage.style.display = "none";

	// nahraje data	
	if ( ( tmp_list = this.loadPhotoList( url ) ) == null )
	    return false;
	return this.parsePhotoList( tmp_list );

    }

    // public 
    // reinicializuje na jiny seznam fotek
    this.reinit = function( url )
    {
	this.photoList = new Array();

	if ( ( tmp_list = this.loadPhotoList( url ) ) == null )
	    return false;
	return this.parsePhotoList( tmp_list );
    }


    // public
    // zobrazi a nahraje fotku se zadanym cislem
    this.show = function( num )
    {
	// nutna otrava, skrolovani tak, aby bylo okno videt
	if ( this.photoDiv.style.display != "" )
	{
	    this.documentScreenTop = this.getScreenTop();
	    window.scrollTo( 0, 0 );
	}

	// zobrazi div a loader
	this.photoDiv.style.display = "";
	this.photoLoader.style.display = "";
	this.photoImage.style.display = "none";
	this.photoImage.style.width = "";
	this.photoImage.style.height = "";
	this.photoImage.style.marginTop = "";
	this.photoImage.style.marginLeft = "";
	
	// spusti nahravani fotky
	this.photoLoader.innerHTML = "Nahrávám " + this.photoList[num] + "...";
    	this.photoImage.src = this.photoList[num];
	this.photoCurrent = num;
    }

    // public
    // zobrazi a nahraje fotku o jedno mensi
    this.backward = function()
    {
	if ( this.photoCurrent == 0 )
	    return;
	this.show( this.photoCurrent - 1 );
    }

    // public
    // zobrazi a nahraje fotku o jedno vetsi
    this.forward = function()
    {
	if ( this.photoCurrent >= this.photoList.length - 1 )
	    return;
	this.show( this.photoCurrent + 1 );
    }

    // public
    // "zavre" okynko
    this.close = function()
    {
	this.photoDiv.style.display = "none";
	this.photoCurrent = 0;

	window.scrollTo( 0, this.documentScreenTop );
    }

}


// aktualni seznam fotek
var photo_dir = null;
// bylo uz photos inicializovane?
var photos_inited = false;

// zobrazi fotku, pripadne inicializuje
function show_photo(url, num)
{
    if (photo_dir == null || photo_dir != url)
    {
	if (!photos_inited)
	{
	    photos.init(url, 'black', 'white');
	    photos_inited = true;
	}
	else 
	    photos.reinit(url);

	photo_dir = url;
    }
    photos.show(num);
}


