// ----------------------------------------------------------
// ----------------------------------------------------------
//                Java-Script www.DerSDX.de
// ----------------------------------------------------------
// ----------------------------------------------------------
var SiteBaseName = "DerSDX";
var SiteNewName;

var BackgroundImage;

var DivDerSDX;
var	DivWebSites;
var DivGraphics;
var DivPrivateStuff;

var ChangeContentTimer;

// -------------------- Für den PreLoad ---------------------
var ElementArray = new Array();
ElementArray[0] = "DerSDX";
ElementArray[1] = "WebSites";
ElementArray[2] = "Graphics";
ElementArray[3] = "PrivateStuff";

var images = new Array();
var imageBackground;
var PreloadCount;
// ----------------------------------------------------------

// -------------------------- Init --------------------------
function initPage()
{
	DebugOut("Page Init Start");
	
	ConstructionImage = new Image();
	with(ConstructionImage){
		src = "Images/Construction.png";
		style.position	= "fixed";
		style.bottom	= "20px";
		style.right		= "20px";
		style.zIndex	= "9998";
	}
	SneakPeekImage = new Image();
	with(SneakPeekImage){
		src = "Images/SneakPeek.png";
		style.position	= "fixed";
		style.top		= "20px";
		style.left		= "20px";
		style.zIndex	= "9998";
	}
	document.body.appendChild(SneakPeekImage);
	document.body.appendChild(ConstructionImage);

	BackgroundImage = new Image();
	BackgroundImage.src = "Images/SiteBack.jpg";
	BackgroundImage.id = "BackgroundImage";
	setOpac(BackgroundImage, 0);
	
	TmpBackground = document.createElement("div");
	TmpBackground.id = "Background";
	TmpBackground.style.zIndex = "0";
	TmpBackground.appendChild(BackgroundImage);
	document.body.appendChild(TmpBackground);
	
	document.title = SiteBaseName + "." + "Initialize";
	
	PreloadCount = 0;
	PreloadImages();
	
	DebugOut("Page Init OK");
}

function initElements(){
	DebugOut("initElements()");
	
	DivWebSites 	= new RCLE("WebSites",-250,260,10);
	DivGraphics 	= new RCLE("Graphics",-250,260,10);
	DivPrivateStuff = new RCLE("PrivateStuff",-250,260,10);
	DivDerSDX   	= new RCLE("DerSDX",-350,235,20);

	DivDerSDX.Element.onmousedown   	= SiteSelect;
	DivWebSites.Element.onmousedown 	= SiteSelect;
	DivGraphics.Element.onmousedown 	= SiteSelect;
	DivPrivateStuff.Element.onmousedown = SiteSelect;

	window.setTimeout("fadeIn(BackgroundImage, 50, 0.05, null);", 200);
	window.setTimeout("SiteSelect();",1000);
}
// ----------------------------------------------------------

// -------------------- Image Preload -----------------------
function PreloadImages()
{
	imageBackground = new Image();
	imageBackground.src = "Images/SiteBack.jpg";
	for (i=0; i < ElementArray.length; i++) {
		images[i*3] = new Image();
		images[i*3 + 1] = new Image();
		images[i*3 + 2] = new Image();
		images[i*3].src = "Images/" + ElementArray[i] + ".png";
		images[i*3 + 1].src = "Images/" + ElementArray[i] + "_shadow.png";
		images[i*3 + 2].src = "Images/" + ElementArray[i] + "_highlight.png";
	}
	window.setTimeout("checkPreloads()",100);
}

function checkPreloads(){
	DebugOut("checkPreloads()");
	var OK = true;
	
	if(!isImageOk(imageBackground)){ OK = false; }
	for (i=0; i < ElementArray.length; i++) {
		if(!isImageOk(images[i*3])){ OK = false; }
		if(!isImageOk(images[i*3 + 1])){ OK = false; }
		if(!isImageOk(images[i*3 + 2])){ OK = false; }
	}
	
	if(OK){
		initElements();
	} else {
		window.setTimeout("checkPreloads()",100);
	}
}

function isImageOk(img) {

    if (!img.complete) {
        return false;
    }

    if (typeof img.naturalWidth != "undefined" && img.naturalWidth == 0) {
        return false;
    }

    return true;
}
// ----------------------------------------------------------

// ---------------------- setTitel --------------------------
function setTitle(text){
	SiteNewName = SiteBaseName + "." + text;
	if(SiteNewName != document.title){
		window.setTimeout("unbuildSiteName();",50);
	}
}

function unbuildSiteName(){
	if(document.title.length == SiteBaseName.length){
		window.setTimeout("buildSiteName();",50);
	} else {
		document.title = document.title.slice(0,document.title.length-1);
		window.setTimeout("unbuildSiteName();",50);
	}
}

function buildSiteName(){
	if(document.title.length < SiteNewName.length){
		document.title = SiteNewName.slice(0,document.title.length+1);
		window.setTimeout("buildSiteName();",50);
	}
}
// ----------------------------------------------------------

// --------------------- Diverses ---------------------------
function Test(){
	DebugOut("Test OK!");
}

function GetRandom( min, max ) {
	if( min > max ) {
		return( -1 );
	}
	if( min == max ) {
		return( min );
		}
	return( min + parseInt( Math.random() * ( max-min+1 ) ) );
}

function fadeIn(div, time, stepsize, endaction){
	div.style.visibility = "visible";
	opac = div.style.opacity;
	if(opac < 1){
		opac = parseFloat(opac) + stepsize;
		setOpac(div, opac);
		window.setTimeout("fadeIn(document.getElementById('" + div.id + "'), " + time + ", " + stepsize + ", " + endaction + ");", time);
	} else {
		setOpac(div, 1.0);
		if(endaction != null){
			eval(endaction);
		}
	}
}

function fadeOut(div, time, stepsize, endaction){
	opac = div.style.opacity;
	if(opac > 0){
		opac = parseFloat(opac) - stepsize;
		setOpac(div, opac);
		window.setTimeout("fadeOut(document.getElementById('" + div.id + "'), " + time + ", " + stepsize + ", " + endaction + ");", time);
	} else {
		setOpac(div, 0.0);
		div.style.visibility = "hidden";
		if(endaction != null){
			eval(endaction);
		}
	}
}

function setOpac(div, opac)
{
	var object = div.style;
	object.filter  = "alpha(opacity=" + (Math.floor(opac * 100)) + ")";
	object.opacity = opac;
}
// ----------------------------------------------------------

// ------------------ Inhalte nachladen ---------------------
var postLoadContentAction = null;
function postLoadContent(destinationDiv, strURL, endaction)
{
	postLoadContentAction = endaction;

	if (window.XMLHttpRequest) {
		oXML_Content = new XMLHttpRequest();
	} else {
		oXML_Content = new ActiveXObject("Microsoft.XMLHTTP");
	}
	
	oXML_Content.open("GET", strURL ,true);
	oXML_Content.onreadystatechange = function()
	{
		if (oXML_Content.readyState == 4) {
			var resp = oXML_Content.responseText;
			if (resp == '') {
				destinationDiv.innerHTML = '<div>Kein Inhalt!</div>';
				return;
			}
			destinationDiv.innerHTML = resp;
			if(postLoadContentAction != null){
				eval(postLoadContentAction);
				postLoadContentAction = null;
			}
			oXML_Content = null;
		}
	}
	oXML_Content.send(null);
}
// ----------------------------------------------------------
