/* Browser Data */
function BrowserWidth() {
  if (window.innerWidth) {
    return window.innerWidth;
  } else if (document.body && document.body.offsetWidth) {
    return document.body.offsetWidth;
  } else {
    return 0;
  }
}

function BrowserHeight() {
  if (window.innerHeight) {
    return window.innerHeight;
  } else if (document.body && document.body.offsetHeight) {
    return document.body.offsetHeight;
  } else {
    return 0;
  }
}


function createPreloaderImage() {

var pre = new Element('img', {
		id: 'preloader',
                src: '../../tl_files/images/preloader.gif',
		styles: {
			'z-index': '2000',
			position: 'absolute',
			top:	'50%',
			left:	'50%'
		}
		});

pre.inject($('page-background'));

}

/* create img holder invisible*/
function createImage2() {

var img2 = new Element('img', {
            id: 'bgImg2',
            styles: {
                opacity: '0'
            }
            });

img2.inject($('page-background'));
}
createImage2();

function setBackgroundImageSource(srcImg, element)
{
	if(srcImg != "")
	{
		element.src = "../../tl_files/images/bg/" + srcImg;
	}
}

/*draw bg and calculate stretch factors*/ 

function drawbackground() 
{
  if (BrWidth != BrowserWidth() || BrHeight != BrowserHeight())
	redrawBackground();
}


/* größeren stretch Wert holen */
function redrawBackground() 
{
	BrWidth = BrowserWidth();
	BrHeight = BrowserHeight();

	var stretchX = BrWidth / w ;
	var stretchY = BrHeight / h ;
        var stretch = 1;

        

	if(stretchX < stretchY) {
		stretch = stretchY; 
                var marginHorizontal = parseInt((stretch * w - BrWidth)/2) * (-1);
                activeImage.setStyle("margin", "0px");
                activeImage.setStyle("margin-left",marginHorizontal + "px");
        }
        else {
            stretch = stretchX; 
                var marginVertical= parseInt((stretch * h - BrHeight)/2) * (-1);
                activeImage.setStyle("margin", "0px");
                activeImage.setStyle("margin-top",marginVertical+ "px");
        }

    var wid = stretch * w;
    var hid = stretch * h;
    activeImage.setStyle("width", wid + "px");
    activeImage.setStyle("height", hid + "px");
}


/* call once */
// 2 bg images for toggling
bg = $('bgImg');
bg2 = $('bgImg2');

//default options
bg.set('tween', {duration: 'long'});
bg2.set('tween', {duration: 'long'});

/* Überwachung von Internet Explorer initialisieren */
if (!window.Weite && document.body && document.body.offsetWidth)
{
window.onresize = drawbackground;
BrWidth = BrowserWidth();
BrHeight = BrowserHeight();
}
/* Überwachung von Netscape initialisieren */
if (!window.Weite && window.innerWidth)
{
window.onresize = drawbackground;
BrWidth = BrowserWidth();
BrHeight = BrowserHeight();
}

// Defaultgröße der Bilder
var w = 1600;
var h = 1200;

function isImageLoaded(img) {
    // During the onload event, IE correctly identifies any images
    // that weren't downloaded as not complete. Others should too.
    // Gecko-based browsers act like NS4 in that they report this
    // incorrectly: they always return true.
    if (!img.complete) {
        return false;
    }

    // However, they do have two very useful properties: naturalWidth
    // and naturalHeight. These give the true size of the image. If
    // it failed to load, either of these should be zero.
    if (typeof img.naturalWidth != "undefined" && img.naturalWidth == 0) {
        return false;
    }

    // No other way of checking: assume it's ok.
    return true;
}

/* Timer Stuff */
var c=0;
var t;
var timer_is_on=0;
var imgCount;
var lastImgCount;
var loadPic = new Boolean(1);
var activeImage = bg;

//alle 10 seconds
function timedCount()
{
	t=setTimeout("timedCount()",5000);
	if(loadPic)
	{
		loadImage();
		loadPic = 0;
	}
	else {
		switchNextBackground();
		loadPic = 1;
	}
}
function doTimer()
{
if (!timer_is_on)
  {
  timer_is_on=1;
  timedCount();
  }
}

var t2;
var timer_is_on_pre = 0;
// checked ob hg bild geladen dan kein preloader mehr
function doPreloaderTimer()
{
	if(!timer_is_on_pre)
	{
	timer_is_on_pre = 1;
	checkPreloader();
	}	
}

function checkPreloader()
{
	t2 = setTimeout("checkPreloader()", 500);
	if(isImageLoaded($('bgImg')) )
	{
		$('preloader').tween('opacity', 0);
		clearTimeout(t2);
		timer_is_on_pre = 0;
	}
}

function stopTimer()
{
	clearTimer(t);
	timer_is_on=0;
}

function loadImage()
{
	/* preload next image */
	lastImgCount = imgCount;
	
    // no double pics
	do
	{
		imgCount = Math.round( Math.random() * 5 );
	}while(lastImgCount == imgCount);
	
	var img = getImg(imgCount);
    
	if(bg == activeImage) {
		setBackgroundImageSource(img, bg2);
	} else {
		setBackgroundImageSource(img, bg);
	}
}

/* automatic background image swapping*//*     TODO TODO */
function switchNextBackground()
{
	// fade other img in and active one out
	if(bg.getStyle('opacity') == 0)    // not active
    {
		bg2.tween('opacity', '0');
		bg.tween('opacity', '1');
		
		
		activeImage = bg;
	}
	else {
		bg.tween('opacity', '0');
		bg2.tween('opacity', '1');
		
		activeImage = bg2;
	}
	redrawBackground();
}

function getImg(randNr)
{
	var img = "";
		switch(randNr)
		{
			case 0:
				img = "startbild/Pick-Morino_Startbild.jpg";
				break;
			case 1:
				img = "startbild/Alt_Startbild.jpg";
				break;
			case 2:
				img = "startbild/Haanen_Startbild.jpg"
				break;
			case 3:
				img = "startbild/Russ_Startbild.jpg"
				break;
			case 4:
				img = "startbild/Dobrowsky_Startbild.jpg";
				break;
			case 5:
				img = "startbild/Moser_Startbild.jpg"
				break;
			default:
				img = "homeBackground.jpg";
				break;
		}
	return img;
}

function initImages()
{
	//lastImgCount = Math.round(Math.random() * 5);	// 1.bild
	imgCount = Math.round( Math.random() * 5 );
		
	var imgSrc1 = getImg(imgCount);
	//var imgSrc2 = getImg(imgCount);
	setBackgroundImageSource(imgSrc1, bg);
	//setBackgroundImageSource(imgSrc2, bg2);
	activeImage = bg;
	//nonActiveImage = bg2;
	
	redrawBackground();
}

// init bg images & start automation routine
initImages();
createPreloaderImage();
//switchNextBackground();
doTimer();
doPreloaderTimer();
