var W3CDOM = (document.createElement && document.getElementsByTagName);
window.onload = init;

function init()
{
	       if (!W3CDOM) return;
	       new_window_link();	
          if(document.getElementById('addfav')){
          var addfav = document.getElementById('addfav');
	        addfav.onclick = addfavix;
          } 
          if(document.getElementById('quest')){
          var quest = document.getElementById('quest');
          quest.number = 'quest';
	        quest.onclick = workImage;
          }              
}

function addfavix(){
 
 if (window.confirm('Chcete vložit stránku mezi oblíbené ?')) {
  external.AddFavorite('http://www.jiri-cepelak.cz','Jiri-Cepelak.cz')
  };
  return false;
}

       var path_to_icon 		= 'http://www.jiri-cepelak.cz/newimages/icon.gif';
			 var link_alt_text 		= 'Otevřít odkaz v novém okně';
			 var link_title_text 	= 'Otevřít odkaz v novém okně';

			function new_window_link() {
			  
			if (!document.getElementById || !document.createTextNode || !document.domain || document.getElementById('cepa') || !document.getElementById('newbox')) return;
				
        var text = document.getElementById('newbox');
        var linx = text.getElementsByTagName('a');
       
				for (var i = 0; i < linx.length; i++) {
        
					// just for off-site links
					
			    if(linx[i].protocol == 'http:'){

					if (linx[i].href.split('/')[2].replace(/www\./, '') != document.domain.replace(/www\./, '')
            && !linx[i].getAttribute('target')
						&& !linx[i].parentNode.id.match(/^copy/)) {
 
						// create new elements
						var nwl = document.createElement('a');
						var nwl_image = document.createElement('img');
						var space = document.createTextNode(' ');

						// setup image attributes
						nwl_image.setAttribute('src', path_to_icon);
						nwl_image.setAttribute('alt', link_alt_text);
						nwl_image.setAttribute('title', link_title_text);

						// set link attributes
						nwl.setAttribute('href', linx[i].getAttribute('href'));
						nwl.setAttribute('target', '_blank');
						nwl.setAttribute('title', link_title_text);
						nwl.className = 'new_window_link';

						// append new elements
						nwl.appendChild(nwl_image);
						linx[i].parentNode.insertBefore(space, linx[i].nextSibling);
						linx[i].parentNode.insertBefore(nwl, linx[i].nextSibling.nextSibling);
					}
				}
			
				}
			}
  
function workImage(){
 
    var stringfrom = this.number;      
    if(stringfrom == 'quest'){
    var imagew = 400;
    var imageh = 350;    
    var imagehnew = 370; 
    }
    if(document.getElementById('printgalerie')){ 
    var overlayscroll = (document.documentElement.scrollTop);
    }    
    var monitor = alertSize()-40;
    if(monitor > imageh){
    var mtop = -(imageh/2);
    var vyska = imageh;
    }
    else{
    var mtop = -(monitor/2)
    var vyska = monitor;
    }
    mtop = mtop + overlayscroll - 15 ;
    if(document.getElementById('printgalerie')){ document.getElementById('printgalerie').style.height = vyska+'px'; }
    newheight = vyska-20;
    var newidth = imagew/(imageh/newheight);   
 
    var mleft = newidth/2;
    if(document.getElementById('printgalerie')){ document.getElementById('printgalerie').style.width = newidth+'px'; }    
    if(document.getElementById('printgalerie')){ document.getElementById('printgalerie').style.marginTop = mtop+'px'; }
    if(document.getElementById('printgalerie')){ document.getElementById('printgalerie').style.marginLeft = '-'+mleft+'px'; }

        
        if(stringfrom == 'quest'){
        url='../newpages/question.php'; 
        } 
 
        if (window.ActiveXObject)
        {
          httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
        }
        else
        {
          httpRequest = new XMLHttpRequest();
        }
        httpRequest.open("GET", url, true);
        httpRequest.onreadystatechange= function () {processRequest(); } ;
        httpRequest.send(null);
    return false;
} 

function processRequest()
{
  if (httpRequest.readyState == 4)
  {
    if(httpRequest.status == 200)
    {
      buildGalery();
      var semka = document.getElementById("printgalerie");
      semka.innerHTML = httpRequest.responseText;
    }
    else
    {
      alert("Chyba pri nacitani stanky"+ httpRequest.status +":"+ httpRequest.statusText);
    }
  }
}
function buildGalery(){
   //var overlayheight = (document.body.clientHeight);
   var overlayheight = (document.getElementById('main').clientHeight + 30);
   if(document.getElementById('overlay')){ document.getElementById('overlay').style.display = 'block'; }
   if(document.getElementById('overlay')){ document.getElementById('overlay').style.height = overlayheight + 'px'; }
   if(document.getElementById('printgalerie')){ document.getElementById('printgalerie').style.display = 'block'; }
}
function killImage(){
   if(document.getElementById('overlay')){ document.getElementById('overlay').style.display = 'none'; }
   if(document.getElementById('printgalerie')){ document.getElementById('printgalerie').style.display = 'none'; }
}
function alertSize() {
  var myWidth = 0, myHeight = 0;
  if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
    myWidth = window.innerWidth;
    myHeight = window.innerHeight;
  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    //IE 6+ in 'standards compliant mode'
    myWidth = document.documentElement.clientWidth;
    myHeight = document.documentElement.clientHeight;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    //IE 4 compatible
    myWidth = document.body.clientWidth;
    myHeight = document.body.clientHeight;
  }
    return myHeight;
}
