var myPage="home";

var imgBsg1Array = Array("images/bcg_1.jpg", "images/bcg_2.jpg", "images/bcg_3.jpg", "images/bcg_1.jpg", "images/bcg_5.jpg");
var imgBsg2Array = Array("images/bcg_2.jpg", "images/bcg_3.jpg", "images/bcg_1.jpg", "images/bcg_5.jpg", "images/bcg_1.jpg");
var noTbl =0;
/*
 * Original from: http://brainerror.net/scripts/javascript/blendtrans/demo.html
 *
 * Edits by ASC:
 *   - Removed unnecessary OO code that caused MSIE to choke
 *   - Added pause between setting bg image and setting opacity to zero to
 *     prevent MSIE image flash
 *   - Increased opacity check argument to 103 for completely smooth fade
 *
 */

//find next image
function nextImage(o) {
    do o = o.nextSibling;
    while(o && o.tagName != 'IMG');
    return o;
}

//find first image inside an element
function firstChildImage(o) {
    
    o = o.firstChild;
        
    while(o && o.tagName != 'IMG') {
        o = o.nextSibling;
    }
    
    return o;
}

//set the opacity of an element to a specified value
function setOpacity(obj, o) {

    obj.style.opacity = (o / 100);
    obj.style.MozOpacity = (o / 100);
    obj.style.KhtmlOpacity = (o / 100);
    obj.style.filter = 'alpha(opacity=' + o + ')';
}

//make image invisible and set next one as current image
function getNextImage(image) {
	
    if (next = nextImage(image)) {
	image.style.display = 'none';
	image.style.zIndex = 0;

	next.style.display = 'block';
	next.style.zIndex = 100;

    } else {
	//if there is not a next image, get the first image again
	next = firstChildImage(image.parentNode);
    }

    return next;
}

//set default values for parameters and starting image
function blendImages(id, speed, pause, caption) {

    if(speed == null) {
        speed = 30;
    }
    
    if(pause == null) {
        pause = 1000;
    }

    var blend = document.getElementById(id);

//    var image = firstChildImage(blend);
var image = blend;
    startBlending(image, speed, pause, caption);
}

//make image a block-element and set the caption
function startBlending(image, speed, pause, caption) {

    image.style.display = 'block';

    if(caption != null) {
	document.getElementById(caption).innerHTML = image.alt;
    }

    continueFadeImage(image, 0, speed, pause, caption);
}

// ASC: copied from http://www.sean.co.uk/a/webdesign/javascriptdelay.shtm
function pausecomp(millis) {
    var date = new Date();
    var curDate = null;

    do { curDate = new Date(); }
    while(curDate-date < millis);
} 

//set an increased opacity and check if the image is done blending
function continueFadeImage(image, opacity, speed, pause, caption) {

    opacity = opacity + 3;

    if (opacity < 103) {

	setTimeout(function() {fadeImage(image, opacity, speed, pause, caption)}, speed);

    } else {
	//if the image is done, set it to the background and make it transparent
	
	var imageBcg= document.getElementById('tbl_1');
	imageBcg.style.backgroundImage = "url('"+imgBsg1Array[noTbl]+"')";
  	image.style.backgroundImage  = "url('"+imgBsg2Array[noTbl]+"')";
if(noTbl<4){
		noTbl++;
		}else noTbl= 0;
	// ASC: pause 1sec here to prevent MSIE image flash ...
	var paws=pause-1000;
	if (paws < 0 ) {
		paws = 0;
	}
	pausecomp(1000);

	setOpacity(image,0);
	//get the next image and start blending it again
  //	image = getNextImage(image);
setTimeout(function() {startBlending(image, speed, pause, caption)}, paws);		
//set the opacity to a new value and continue the fading
    }
}

function fadeImage(image, opacity, speed, pause, caption) {
    setOpacity(image,opacity);
    continueFadeImage(image, opacity, speed, pause, caption);
}


function gotoPage(page, img){
var oldPage=myPage;
myPage= page;

document.images.numImg1.src='images/mnu1.png';
document.images.numImg2.src='images/mnu2.png';
document.images.numImg3.src='images/mnu3.png';

oldTable=document.getElementById('tbl_'+oldPage);
nevTable=document.getElementById('tbl_'+myPage);

myNum1=document.getElementById('numerki1');
myNum2=document.getElementById('numerki2');
myNum3=document.getElementById('numerki3');

myNum1.style.display= 'none';
myNum2.style.display= 'none';
myNum3.style.display= 'none';

oldTable.style.backgroundColor='transparent';
oldTable.className='normal';

nevTable.style.backgroundColor='#c9cace';
nevTable.className='noclass';

var myDivInnerHTML = document.getElementById('myDiv');
if (page=='home'){
myDivInnerHTML.innerHTML=homeInnerHTML;

}

if (page=='oferta'){
myDivInnerHTML.innerHTML=ofertaInnerHTML;

}

if (page=='cennik'){

myDivInnerHTML.innerHTML=cennikInnerHTML;

}

if (page=='kontakt'){

myDivInnerHTML.innerHTML=kontaktInnerHTML;

}

if (page=='foliastretch'&& img=='img1'){
myDivInnerHTML.innerHTML=foliastretchInnerHTML;
document.images.numImg1.src='images/mnu1_a.png';
myNum1.style.display= 'block';
myNum2.style.display= 'block';
myNum3.style.display= 'block';
}

if (page=='foliastretch'&& img=='img2'){
myDivInnerHTML.innerHTML=foliastretchInnerHTML2;
document.images.numImg2.src='images/mnu2_a.png';
myNum1.style.display= 'block';
myNum2.style.display= 'block';
myNum3.style.display= 'block';
}

if (page=='foliastretch'&& img=='img3'){
myDivInnerHTML.innerHTML=foliastretchInnerHTML3;
document.images.numImg3.src='images/mnu3_a.png';
myNum1.style.display= 'block';
myNum2.style.display= 'block';
myNum3.style.display= 'block';
}
}

function init(){
if(myNavigatorVersion<7 && myNavigatorName=="msie"){
document.getElementById("myDiv").className ="imageInGallery_DESC_MSIE";
}
var myTable=document.getElementById('tbl_'+myPage);
myTable.style.backgroundColor='#c9cace';
myTable.className='noclass';
gotoPage(myPage)
blendImages('tbl_2');
}
