/*
 * 
 * @author Przemyslaw Turek
*/
var nOpacity = 100;
var nIntervalId;
var nPic = 0;

var tab_pic = new Array(8);
	tab_pic[0]="zdjecia/slide/pic1.png";
	tab_pic[1]="zdjecia/slide/pic2.png";
	tab_pic[2]="zdjecia/slide/pic3.png";
	tab_pic[3]="zdjecia/slide/pic4.png";
	tab_pic[4]="zdjecia/slide/pic5.png";
	tab_pic[5]="zdjecia/slide/pic6.png";
	tab_pic[6]="zdjecia/slide/pic7.png";
	tab_pic[7]="zdjecia/slide/pic8.png";


var Imgi = new Array(); // Tablica obrazków                           
var Bledy = 0;          // Liczba błędów                              
var preloadTimer;       // Timer używany dalej                        
var preloadOK = '';      // Kod do wykonania po zakończeniu ładowania  
var preloadBlad = '';    // j.w., ale jeśli wystąpi jakiś błąd         
                                                                      
// rozpoczyna ścišganie obrazka                                       
function CloseAll(szName){
if (document.getElementById(szName)) {
	var div = document.getElementById(szName);
	while (div) {
		document.body.removeChild(div);
		div = document.getElementById(szName);		
	}
}	
}




function preloadPrepare()
{
	for (var i=0; i<tab_pic.length; i++) preload(tab_pic[i]);

	preload('img/body-bottom-right.png');
	preload('img/body-bottom.png');
	preload('img/body-line-left.png');
	preload('img/body-line-middle.png');
	preload('img/body-line-right.png');
	preload('img/body-top.png');
	preload('img/body-top1.png');
	preload('img/but-cen.png');
	preload('img/but-dol.png');
	preload('img/but-gal.png');
	preload('img/but-kon.png');
	preload('img/but-lift.png');
	preload('img/but-ofe.png');
	preload('img/but-rez.png');
	preload('img/but-omnie.png');	
	preload('img/gora-wypelnienie.png');
	preload('img/gora.png');
	preload('img/imbg.png');
	preload('img/petla-liscie3.png');
	preload('img/pic-frame.png');
	preload('img/prawy-ramka.png');
	preload('img/witamy.png');
	preload('img/body-lift.png');
	
	preloadStart('AfterLoad()','alert("Wystąpił błąd przy ładowaniu obrazków.\nNie obejrzysz mojej prezentacji.")');  				
	
}


function preload(url) {                                               
   if (document.images) {                                             
      var i = Imgi.length;                                            
      Imgi[i] = new Image();                                          
      Imgi[i].onerror = preloadOnError;                               
      Imgi[i].src = url;                                              
   }                                                                  
}                                                                     
                                                                      
// wywyoływana, jeśli wystąpił jakiś błąd - ustawia flagę Blad        
function preloadOnError() {                                           
   Bledy++;                                                           
}                                                                     
                                                                      
// ustala, co należy zrobić po zakończeniu preloadu, uruchamia pętlę  
function preloadStart(ok, blad) {                                     
   preloadOK = ok;                                                    
   preloadBlad = blad;                                                
   preloadCheck();                                                    
}                                                                     
                                                                      
// sprawdza, czy ładowanie się już zakończyło                         
function preloadCheck() {                                             
   var i=0;                                                           
   var l=Imgi.length;                                                 
   var b = Bledy;                                                     
   
   var j=0,k=0;
   // Trochę zagęszczonego kodu:                                      
   while (i<l && Imgi[i].complete ) 
    { 
      i++;                      
    }
   if (i >= l) {// to już wszystkie (wyjście z pętli na 1. warunku)      
				if (Bledy == 0) 
					eval(preloadOK)
				else 
					eval(preloadBlad)
			}	 
  else // któryś nie był ukończony                                   
      preloadTimer = setTimeout('preloadCheck()',100);                
}                                                                                              
                                                               

function IsIE(nVer)
{
	if (/MSIE (\d+\.\d+);/.test(navigator.userAgent)) {
		if (nVer == null) {
			return true;
		}
		else {
			var ieversion = new Number(RegExp.$1) 
			if ((ieversion >= 8)&&(nVer==8)) return true;
			else if ((ieversion >= 7)&&(nVer==7)) return true;				
			else if ((ieversion >= 6)&&(nVer==6)) return true;
			else return false;
		}
	}
}

function IsFireFox()
{
	return (/Firefox[\/\s](\d+\.\d+)/.test(navigator.userAgent));
}

function IsOpera()
{
	return (/Opera[\/\s](\d+\.\d+)/.test(navigator.userAgent));
}


function browserWidth() {
  var myWidth = 0;
  if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
    myWidth = document.documentElement.clientWidth;
  } else if( document.documentElement && document.documentElement.clientWidth ) {
    //IE 6+ in 'standards compliant mode'
    myWidth = document.documentElement.clientWidth;
  } else if( document.body && document.body.clientWidth ) {
    //IE 4 compatible
    myWidth = document.body.clientWidth;
  }
  return myWidth;
}

function browserHeight() {
  var myHeight = 0;
  if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
    myHeight = document.documentElement.clientHeight;
  } else if( document.documentElement && document.documentElement.clientHeight ) {
    //IE 6+ in 'standards compliant mode'
    myHeight = document.documentElement.clientHeight;
  } else if( document.body && document.body.clientHeight ) {
    //IE 4 compatible
    myHeight = document.body.clientHeight;
  }
  return myHeight;
}


function pause(ms){
	var date = new Date();
	curDate = null;
	do{var curDate = new Date();}
	while( curDate - date < ms);
}

function LeftStr(szText,num)
{	
	if (length(szText)>num) {
		return szText.substr(0,num-1);		
	} else return szText;
}


function SetAlfa(szName, num)
{
	var el = document.getElementById(szName);
	if (el) {	
		el.style.opacity = num/100; //FireFox, CSS3
		el.style.filter = "alpha(opacity="+num+")"; //IE
		//el.style.filter = "progid:DXImageTransform.Microsoft.Alpha(opacity="+num+")";
	}
	
	if ((num <= 0) && (nIntervalId)) {
		window.clearInterval(nIntervalId);
		CloseAll(szName);
	}	
	return num - 5;
}

function ResizeAndClose(szName)
{
	var divheight=101, divwidth=101, divtop, divleft;
	var nheight, nwidth;
	var nhp,nwp;
	var div = document.getElementById(szName);
	if (div) {
			divheight = div.clientHeight;
			divwidth = div.clientWidth;
						
			if (div.style.top) {
			    str = new String;
			    str = div.style.top;
				divtop = parseInt(str.substr(0,str.indexOf("px",0)));		
			}
			else divtop = 0;
						
			if (div.style.left) {
				str = new String;
				str = div.style.left;
				divleft = parseInt(str.substr(0, str.indexOf("px", 0)));
			} else divleft = 0;
				
			nhp = divheight * 0.05;
			if (nhp<1) nhp = 1;
			nwp = divwidth * 0.05;
			if (nwp<1) nwp = 1;
			nheight = divheight - nhp;
			if (nheight < 0) 
				nheight = 0;
			nwidth = divwidth - nwp;
			if (nwidth < 0) 
				nwidth = 0;
			
			div.style.top = divtop + (nhp / 2) + "px";		
			div.style.left = divleft + (nwp / 2) + "px";
			div.style.width = nwidth + "px";
			div.style.height = nheight + "px";
			
	}
	else {
			divwidth = 0;
			divheight = 0;
			CloseAll(szName);
			window.clearInterval(nIntervalId);
		}
}
function MainLoad()
{
	if (!document.getElementById("divloadmain")) 
	{		
		var divall = document.createElement('div');	
		divall.style.top = "0px";
		divall.style.left = "0px";
		divall.style.height = browserHeight()+"px";
		divall.style.width = browserWidth()+"px";
				
		if (IsIE()) {
			divall.className = "divallIE";			
		}
		else 
			divall.className = "divall";				
		
		divall.innerHTML="<div style=\"position:absolute;left:50%; top:50%\"><img src=\"img_lbox/loading.gif\"> Ładowanie strony ... </div>";
				
		divall.setAttribute('id', "divloadmain");			
		document.body.appendChild(divall);		

        preloadPrepare();	  
	} else CloseAll("divloadmain");		
   
}

function AfterLoad(){
    //nIntervalId = window.setInterval('ResizeAndClose("divloadmain")',10);	
	//nIntervalId = window.setInterval('nOpacity = SetAlfa("divloadmain", nOpacity)',20);
	CloseAll("divloadmain");
}

function Show(nId)
{
	for (var i = 1; i < 8; i++) document.getElementById('l' + i).style.visibility = "hidden";
	
	if (document.getElementById('l'+nId))
	{		
		var el = document.getElementById('l'+nId);
		el.style.visibility="visible";		
	} 
}

function Hide(nId){
	if (document.getElementById('l'+nId)) document.getElementById('l' + nId).style.visibility = "hidden";
}

//SlideShow
function slideshow(nId,nSec){
  window.setInterval('nPic = sshow("'+nId+'",nPic)',(nSec*1000));
}

function sshow(nId,nPic)
{
	if (document.getElementById('img1')){
		document.getElementById('img1').style.visibility = "hidden";
	}
	
	if (document.getElementById(nId)) {
		var div = document.getElementById(nId);
		if (IsIE()) {
			div.style.filter = 'progid:DXImageTransform.Microsoft.AlphaImageLoader(src="' + tab_pic[nPic] + '",sizingMethod="crop")';
		} else {
			div.style.backgroundImage = 'url(' + tab_pic[nPic] + ')';
			div.style.backgroundRepeat = 'no-repeat';
		}
	}
	
	if (nPic < tab_pic.length - 1) nPic = nPic + 1; else nPic = 0;
	return nPic;
}

function SetHeight(nStrona){
	var linie=420;
	var bottom=708;
	var body_main=246;
	var body_bottom=700;
	
	switch (nStrona)
	{
		case 1: {
			linie=420;
			bottom=708;
			body_main=246;
			body_bottom=700;			
		} break;
		case 2: {
			linie+=110;
			bottom+=110;
			body_main+=110;
			body_bottom+=110;						
		} break;

		case 3: {
			linie+=50;
			bottom+=50;
			body_main+=50;
			body_bottom+=50;						
		} break;
		
	}
	
	if (document.getElementById('body-main2-left')) document.getElementById('body-main2-left').style.height=body_main+"px";
	if (document.getElementById('body-main2-middle')) document.getElementById('body-main2-middle').style.height=body_main+"px";
	if (document.getElementById('body-main2-right')) document.getElementById('body-main2-right').style.height=body_main+"px";
	if (document.getElementById('body-line-left')) document.getElementById('body-line-left').style.height=linie+"px";
	if (document.getElementById('body-line-middle')) document.getElementById('body-line-middle').style.height=linie+"px";
	if (document.getElementById('body-line-right')) document.getElementById('body-line-right').style.height=linie+"px";
	
	if (document.getElementById('body-bottom')) document.getElementById('body-bottom').style.margin=body_bottom+"px 0px 0px -311px";
	if (document.getElementById('body-bottom-right')) document.getElementById('body-bottom-right').style.margin=body_bottom+"px 0px 0px 297px";
	
	if (document.getElementById('bottom')) document.getElementById('bottom').style.margin=bottom+"px 0px 0px -301px";
}

function SetClass(nId,nOn)
{
	if (document.getElementById('g'+nId)) {
		var el = document.getElementById('g'+nId);		
		el.style.color  = (nOn==1) ? "rgb(228, 104, 13)" : "rgb(30, 77, 0)";
		if (nOn==0) el.blur();
		el.style.cursor = (nOn==1) ? "pointer" : "normal";
	}
}
