//------------------------------------------------------------------------------
// Gallery, verze 0.1
// Copyright Hynek Oubrecht <oubrecht@tah.cz> 
//
// 09.10.2009
//------------------------------------------------------------------------------


// ~ Globalni promenne s informacemi o obrazcich      
      var CurrentSlide  = false;
      var GalImageTitle = new Array();
      var GalImagePath  = new Array();
      var imageObj      = new Image;
      var GBox          = false;
      
      
      
 // ~ Vrat velikost okna ~
      function WindowSize(){
          var win = new Object;        
          if(window.innerWidth) {
             win.width  = window.innerWidth;
             win.height = window.innerHeight;
          }
          else {
             win.width  = document.body.clientWidth;
             win.height = document.body.clientHeight;
          }
          return win;
      }
      
      

 // ~ Pripoj na konec BODY kontejner DIV, ve kterem zobrazuj obrazky ~      
	  function CreateGalleryElements() { 	
          var objBody    = document.getElementsByTagName("body").item(0);		
		  var objGallery = document.createElement("div");
		  objGallery.setAttribute("id", "Gallery");
		  objGallery.onclick = function() { GalleryClose(); }
		  objBody.appendChild( objGallery );
		
		  var objGalleryBox = document.createElement("div");
		  objGalleryBox.setAttribute("id", "GalleryBox");
		  objGalleryBox.setAttribute("class", "GalleryBoxClass");
		  objBody.appendChild( objGalleryBox );		
      }



 // ~ Nacti odkazy a tem, kteri patri ke galeri pridej udalost onClick ~
	  function LoadGalleryAnchors() { 	
		  var anchors = document.getElementsByTagName("a"); 
		  var anchor  = false;
		  for (var i=0; i<anchors.length; i++) { 
			   anchor  = anchors[i]; 
			   if (anchor.getAttribute("href") && anchor.getAttribute("rel") == "Gallery") { 
			       CurrentSlide = GalImagePath.length;
			       anchor.Slide = CurrentSlide;
			       GalImagePath[ CurrentSlide ] = anchor.getAttribute("href");
			       GalImageTitle[ CurrentSlide ]= anchor.getAttribute("title");
				   anchor.onclick = function () {
				                                  GotoImage(this.Slide); 
				                                  return false;
				                                }
			   }
		  }	   
       }
	


 // ~ Zobraz volany obrazek ~	
      function GotoImage( Slide ){                            
          CurrentSlide = Slide;
          LoadImage();
          // -> po natazeni zavola DisplayImage
      }

	
	
 // ~ Nacti volany obrazek ~	
	  function LoadImage() {	
	      
	      if( GalImagePath[ CurrentSlide ]) {
			  document.getElementById('Gallery').style.display = 'block';
			  document.getElementById('Gallery').style.height  = document.body.scrollHeight+'px';
			  GBox = document.getElementById('GalleryBox');
			  GBox.setAttribute('class', 'GalleryBoxClass');
			  GBox.style.display = 'block';
			  GBox.innerHTML     = '<div id="Preloader"><img src="/img/preloader.gif"/></div>';
			  		
			  imageObj     = new Image; 		
		      imageObj.src = GalImagePath[ CurrentSlide ];		      
		      if( imageObj.complete ) 
		           DisplayImage();
              else 
                   imageObj.onload = DisplayImage;	     		      		      		      
		  }
		  else  
		     window.alert('Cesta k obrázku ['+CurrentSlide+'] je neplatná, nelze jez zobrazit');
		  
		  return false;
	  }



 // ~ Zobraz volany obrazek ~
 	  function DisplayImage() {	
 	  
		  var winObj       = WindowSize();
		  var topPosition  = 0;
		  var leftPosition = 0;
		  var MoveLeft  = '';
		  var MoveRight = '';	
		  var Title     = '';
		  var IE        = 0;
		  if( document.all ) IE = 0;
 	  
		  if( GalImageTitle[ CurrentSlide ] || GalImagePath.length>1 ) {
			  imageObj.height = imageObj.height+40;
		  }
		  	 		  
		  if( imageObj.width<300 )
		      imageObj.width = 300;					      
		  
		  if( imageObj.width>=winObj.width )
			  leftPosition = 1;
		  else
			  leftPosition = Math.round((winObj.width-imageObj.width)/2);
		  if( imageObj.height>=winObj.height )
			  topPosition  = 1;
		  else
			  topPosition  = Math.round((winObj.height-imageObj.height)/2);		          
		  GBox.style.height= imageObj.height+IE+'px';
		  GBox.style.width = imageObj.width +IE+'px';
		  GBox.style.top   = topPosition-10+document.body.scrollTop +'px';
		  GBox.style.left  = leftPosition-10+'px';
		  		  
		  if( GalImagePath.length>1 ){
			  if( CurrentSlide < GalImagePath.length-1 )
				  MoveRight = '<div id="MoveRight"><img src="/img/move_right.gif" class="mover" onClick="GotoImage('+(CurrentSlide+1)+');" /></div>';
			  else
			      MoveRight = '<div id="MoveRight"><img src="/img/galleryclose.gif" class="mover" onClick="GalleryClose();" /></div>';
			  if( CurrentSlide > 0)
				  MoveLeft = '<div id="MoveLeft"><img src="/img/move_left.gif" class="mover" onClick="GotoImage('+(CurrentSlide-1)+');" /></div>';
			  else
			      MoveLeft = '<div id="MoveLeft"><img src="/img/galleryclose.gif" class="mover" onClick="GalleryClose();" /></div>';				  
		  }				  
		  Title = '<div id="Title">'+GalImageTitle[ CurrentSlide ]+'</div>';		  
		  
		  var HTML = false;			      
		  HTML = '<img src="'+GalImagePath[ CurrentSlide ]+'"/>';
		  HTML +='<div id="GalleryComment">'+MoveLeft+Title+MoveRight+'</div>';		  
		  GBox.innerHTML = HTML;
		  document.getElementById('GalleryComment').style.width = imageObj.width+'px';
		  document.getElementById('Title').style.width = (imageObj.width-80)+'px';
		  document.getElementById('MoveLeft').style.width = '40px';
		  document.getElementById('MoveRight').style.width = '40px';
      }



 // ~ Ukonci prohlizeni ~
      function GalleryClose(){
          document.getElementById('Gallery').style.display = 'none';
          document.getElementById('GalleryBox').style.display = 'none';
      }



 // ~ Incializuj skript po zobrazeni stranky ~
 
      function GalleryInit() {
          CreateGalleryElements();
          LoadGalleryAnchors();        
          //GotoImage(0); 
      }
      
      window.onload = GalleryInit;
       
 // ~ End ~
 
 
    
 
