var div_to_load;
var url_to_load;
var debug = false;
var arVersion = navigator.appVersion.split("MSIE");
var version = parseFloat(arVersion[1]);
var req = getRequestObject();
var flashHistory = new Array();


function preloadImages(strImages){
    if (document.images)
    {
      preload_image_object = new Image();
      arrImages = strImages.split(",");
      for(var i=0; i <arrImages.length - 1 ; i++) {
      	 preload_image_object.src = arrImages[i];
      	 preload_image_object.onload = function(){ window.status = this.src + ' loaded';}
      	 window.status = 'loading ' + preload_image_object.src + "...";
      }
      window.status = 'preloadImages complete';
    }
}    


function getRequestObject() {
	var	thing;
	if (typeof XMLHttpRequest != "undefined") {
		try {
			thing = new XMLHttpRequest();
		} catch (e) {
			if(debug) alert("error from new XMLHttpRequest: " + e)
			thing = false;
		}
	} else {
		try {
			thing = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
				thing = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (E) {
				thing = false;
			}
		}
	}
	return thing;
}

function showElementById(id){
	try{
		var el = document.getElementById(id); 
		if(el) el.style.display = 'block';
	}
	catch(e){window.status = 'error: showElementById ' + id + " " + e.message}	
	
}


function hideElementById(id){
	var el = document.getElementById(id); 
	if(el) el.style.display = 'none';
}

function initSceneObject(){
	//alert('initSceneObject' + getCurrentSceneNumber());
	if(getCurrentSceneNumber() == 0) {
		hideElementById('divMenu');		
		hideElementById('divMenu2');			
		hideElementById('divRunningText');
		hideElementById('divContent');	
		showElementById('divRunningTextFirstPage');		
		preloadImages("flash/i/start.jpg,flash/i/m/menuAboutClubN.gif,flash/i/m/menuAboutClubN_over.png,flash/i/m/menuBarN.gif,flash/i/m/menuBarN_over.png,flash/i/m/menuEnterClubN.gif,flash/i/m/menuEnterClubN_over.png,flash/i/m/menuLevel1N.gif,flash/i/m/menuLevel1N_over.png,flash/i/m/menuLevel2N.gif,flash/i/m/menuLevel2N_over.png,flash/i/m/menuLevel3N.gif,flash/i/m/menuLevel3N_over.png");
	}
	else{
		var el = document.getElementById("body");
		if(el) el.style.backgroundImage = "url('flash/i/bg.jpg')";
		hideElementById('divRunningTextFirstPage');			
		showElementById('divMenu');				
		showElementById('divMenu2');			
		showElementById('divRunningText');
		showElementById('divContent');		
	}
}

function handleReadyStateChange() {
	if (req.readyState == 4) {
		if (div_to_load) {
			if(debug) alert("writing new content to " + div_to_load + "...");
			if (req.status == 200) {	
				document.getElementById(div_to_load).innerHTML = req.responseText;
			} else {
				alert("error loading content: status=" + req.status + " : " + req.statusText + "\n" + url_to_load);
			}
		} else {
			alert("no id in div_to_load");
		}
	}
}

function loadDivContent(divid, url) {
	if(debug) alert("try to load content into div " + divid + " for " + url);
	if (req) {
		if(debug) alert("do it dynamically and stuff");
		if((url.indexOf(".html") > 0) && (url.indexOf("b/") < 0)) url = "b/" + url;
		if((url.indexOf(".html") > 0) && (url.indexOf("flash/") < 0)) url = "flash/" + url;	
		
		// correct Alex Pusser errors in links
		
		if((url.indexOf("level1danceflour") > 0) ) url = url.replace("level1danceflour","level1DanceFlour");
		if((url.indexOf("level2danceflour") > 0) ) url = url.replace("level2danceflour","level2DanceFlour");
		if((url.indexOf("level3danceflour") > 0) ) url = url.replace("level3danceflour","level3DanceFlour");				
	
		div_to_load = divid;
		url_to_load = url;
		req.open("GET", url);
		flashHistory.push(url);
		if(debug) alert(flashHistory);
		req.onreadystatechange = handleReadyStateChange;
		req.send(null);
	} else {
		if(debug) alert("reload browser like in olden days");
		window.location = url + ".htm";
	}
}

function AOver(){window.status = 'load next scene';return(true);}
function AOut() {window.status = '...';}
function AClick() {window.status = 'Loading...' + this.href;return(true);}

function MenuOut(){
			window.status = 'load next movie';
			var re = /(\_over.png)/i;
//			this.style.cursor = 'hand';
			this.src = this.src.replace(re,".gif");
}	

function MenuOver()	{
			window.status = ' ';
			var re = /(\.gif)/i;
			this.src = this.src.replace(re,"_over.png");
}	

function loadPage(target,url){
	if(target == '_blank'){
		window.open(url,null);	
		//document.location.href = url;
	}
	else	
		loadDivContent('divContent',url);	
}


function flashBack(){
	if(flashHistory.length < 2) return;
	if(debug) alert("try to load content into div " + divid + " for " + url);
	if (req) {
		if(debug) alert("do it dynamically and stuff");
		url = flashHistory.pop();
		if((url.indexOf(".html") > 0) && (url.indexOf("b/") < 0)) url = "b/" + url;
		if((url.indexOf(".html") > 0) && (url.indexOf("flash/") < 0)) url = "flash/" + url;		
		div_to_load = 'divContent';
		req.open("GET", url);
		req.onreadystatechange = handleReadyStateChange;
		req.send(null);
	} else {
		if(debug) alert("reload browser like in olden days");
		window.location = url + ".htm";
	}
}


