function showSpecialPage() {
  var obj = ge('specialpage');
  
  if(obj.style.display != 'block')
    obj.style.display = 'block';
  else
    obj.style.display = 'none';
}

function showSpecialOpening() {
  var obj = ge('imgSpecChr');
  if(!obj)
    return false;
    
  if(obj.style.display != 'block')
    obj.style.display = 'block';
  else
    obj.style.display = 'none';
}

window.onload = function() {
  StartInit();
  galleryRun();
}

var directionDisplay;
var directionsService;
var map;
var map_end;


var mapContainer = false;
var mapIn = false;
var mapShowed = false;
function MapInitialize(address, callBack) {
  directionsService = new google.maps.DirectionsService();
	
  if(google && address) {
    if(!mapContainer) {
      mapContainer = document.createElement('div');
      mapContainer.id = 'googleMap';
      
      
      mapIn = document.createElement('div');
      mapIn.style.width = '100%';
      mapIn.style.height = '100%';
      
       
      mapContainer.appendChild(mapIn);
      document.body.appendChild(mapContainer);
    }
    
    if(mapShowed) {
      callBack.call(this);
      return false;
    }
    
    mapShowed = true
        
    var geocoder = new google.maps.Geocoder();
    
    geocoder.geocode({ 'address': address}, function(results, status) {
      
     if (status == google.maps.GeocoderStatus.OK) {
       var myOptions = {
          zoom: 16,
          center: results[0].geometry.location,
          mapTypeId: google.maps.MapTypeId.ROADMAP
        };
        
       map = new google.maps.Map(mapIn, myOptions);
       map.setCenter(results[0].geometry.location);
       var marker = new google.maps.Marker({
         map: map, 
         position: results[0].geometry.location
       }); 
       
       // create the tooltip and its text
       var infoWindow = new google.maps.InfoWindow();
       var html='<form id="najitCestu" onsubmit="return callRoute(this);">Vaše adresa:<br><input type="text" name="inputAdresa"><br><input type="submit" value="Najít cestu k nám"><br><span id="celkem_km">&nbsp;</span></form>';

       infoWindow.setContent(html);
       infoWindow.open(map, marker);
       
       map_end = address;
       
       directionsDisplay = new google.maps.DirectionsRenderer();
       directionsDisplay.setMap(map);
        
       google.maps.event.addListener(directionsDisplay, 'directions_changed', function() {
         computeTotalDistance(directionsDisplay.directions);
       });
        
       callBack.call(this);
       
     }else {
       hide('googleMap');
     }});
  }
}


function callRoute(that) {
  var start = that.inputAdresa.value;
  
  var request = {
    origin: start, 
    destination: map_end,
    travelMode: google.maps.DirectionsTravelMode.DRIVING
  };
  directionsService.route(request, function(response, status) {
    if (status == google.maps.DirectionsStatus.OK) {
      directionsDisplay.setDirections(response);
    }
    else {
      ge('celkem_km').innerHTML = '<strong style="color:red;">Adresa nenalezena</strong>';
    }
  });
  return false;
}


function computeTotalDistance(result) {
  var total = 0;
  var myroute = result.routes[0];
  for (i = 0; i < myroute.legs.length; i++) {
    total += myroute.legs[i].distance.value;
  }
  total = total / 1000.
  document.getElementById("celkem_km").innerHTML = "Celkem: <strong>" + total + " km</strong>";
}


function showMap(address) {
  
  MapInitialize(address, function() {
	show('black');
	show('googleMap')
	  
    var left = (document.body.offsetWidth - 800) / 2;
    mapContainer.style.left = left + 'px';
    mapContainer.style.top = getCenter(600) + 'px';

    
    ge('black').onclick = function() {
      hide('googleMap');
      hide('black');
      this.onclick = function(){}
    }
  }); 
}


function galleryRun() {
  var links = ge('InAll').getElementsByTagName('a');
  var gallery = [];

  for(var i=0; i<links.length; i++) {
  	if(links[i].getAttribute('rel') == "gallery") {
  	  gallery.push(links[i]);
  	}
  }
  
  for(var i=0; i<gallery.length; i++) {
  	gallery[i].onclick = showGallery;
  }
  
  function showGallery() {
    show('black');
 
    var img = document.createElement('img');
    img.src = this.href;
    img.style.border = '2px solid black';
    img.style.position = 'absolute';
    img.style.cursor = 'pointer';
    img.style.zIndex = '550';
    img.onload = galleryImageLoad;
    img.onclick = function() {
      hide('black');
      document.body.removeChild(img);    
    };
    
    document.body.appendChild(img);
    
    ge('black').onclick = function() {
      hide('black');
      document.body.removeChild(img);
      return false;
    }
   
    
    return false;
  }
  
  function galleryImageLoad() {
    var left = (document.body.offsetWidth - this.offsetWidth) / 2;
    this.style.left = left + 'px';
    this.style.top = getCenter(this.offsetHeight) + 'px';
   
  }
}

function getCenter(height) {
  var size = getPageSize();
  return (size[3] - height) / 2 +  getScrollXY()[1];
}


function show(id,data){

  var obj  = document.getElementById(id);
  if(obj){
    obj.style.display = "block";
    if(data) obj.innerHTML = data;
  }
}  

function getScrollXY() {
  var scrOfX = 0, scrOfY = 0;
  if( typeof( window.pageYOffset ) == 'number' ) {
    //Netscape compliant
    scrOfY = window.pageYOffset;
    scrOfX = window.pageXOffset;
  } else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
    //DOM compliant
    scrOfY = document.body.scrollTop;
    scrOfX = document.body.scrollLeft;
  } else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
    //IE6 standards compliant mode
    scrOfY = document.documentElement.scrollTop;
    scrOfX = document.documentElement.scrollLeft;
  }
  return [ scrOfX, scrOfY ];
}



function getPageSize(){
   var xScroll, yScroll;
    
    if (window.innerHeight && window.scrollMaxY) {  
      xScroll = document.body.scrollWidth;
      yScroll = window.innerHeight + window.scrollMaxY;
    } else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
      xScroll = document.body.scrollWidth;
      yScroll = document.body.scrollHeight;
    } else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
      xScroll = document.body.offsetWidth;
      yScroll = document.body.offsetHeight;
    }
    
    var windowWidth, windowHeight;
    if (self.innerHeight) { // all except Explorer
      windowWidth = self.innerWidth;
      windowHeight = self.innerHeight;
    } else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
      windowWidth = document.documentElement.clientWidth;
      windowHeight = document.documentElement.clientHeight;
    } else if (document.body) { // other Explorers
      windowWidth = document.body.clientWidth;
      windowHeight = document.body.clientHeight;
    } 
    
    // for small pages with total height less then height of the viewport
    if(yScroll < windowHeight){
      pageHeight = windowHeight;
    } else { 
      pageHeight = yScroll;
    }
  
    // for small pages with total width less then width of the viewport
    if(xScroll < windowWidth){  
      pageWidth = windowWidth;
    } else {
      pageWidth = xScroll;
    }
  
  
    arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight);
    return arrayPageSize;
  }


function blackShow(e) {
  ge('black').style.top = document.body.scrollTop + 'px'; 
}


function hide(id){
  var obj  = document.getElementById(id);
  if(obj){
    obj.style.display = "none";
  }
}  
