userAgent = window.navigator.userAgent;
browserVers = parseInt(userAgent.charAt(userAgent.indexOf("/")+1),10);

var isPreloaded = false;

function initialize(){
	
	soundFrame = document.getElementById("isound");
	//preloadImages();
	//if(screen.width > 1024 && screen.height > 768 && window.outerWidth != 1000 && window.outerHeight != 830){
		//window.resizeTo(1000,900);
	//}
	//document.getElementById("isound").innerHTNML = document.getElementById("isound").firstChild;
	//alert(window.isound.document.body.childNodes[1].getAttributeNode("autostart").nodeValue);
	//alert(window.navigator.systemLanguage && !window.navigator.language);
}

function newImage(arg) {
	if (document.images) {
		img = new Image();
		img.src = arg;
		return img;
	}
}

function findElement(n,ly) {
	if (browserVers < 4){
		return document[n];
	}
	var curDoc = ly ? ly.document : document;
	var elem = curDoc[n];
	
	// Netscape 4.x
	if (!elem) {
		for (var i=0;i<curDoc.layers.length;i++) {
			elem = findElement(n,curDoc.layers[i]);
			if (elem) return elem;
		}
	}
	return elem;
}

// odd parameter: name of <img> tag 
// even parameter: path to source image

function animate() {
	if (document.images && (isPreloaded == true)) {
		var img;
		for (var i=0; i<animate.arguments.length; i+=2) {
			img = null;
			if (document.layers) {
				img = findElement(animate.arguments[i],0);
			}
			else {
				img = document.images[animate.arguments[i]];
			}
			if (img) {
				img.src = animate.arguments[i+1];
			}
		}
	}
}

function changeImages(imgId, src){
	document.getElementById(imgId).src = src;
}

function changePNGImages(id, imgSrc){
	document.getElementById(id).style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src=\"" + imgSrc + "\", sizingMethod='scale');";
}

function preloadImages() {
	if (document.images) {
		about_read__over = newImage("images/about_read_-over.png");
		inside_over = newImage("images/inside-over.png");
		sound_on_over = newImage("images/sound_on-over.png");
		sound_off_over = newImage("images/sound_off-over.png");
		outside_over = newImage("images/outside-over.png");
		menu_outside_over = newImage("images/menu_outside-over.png");
		menu_inside_over = newImage("images/menu_inside-over.png");
		footer_gap_over = newImage("images/footer_gap_over.png");
		footer_credits_over = newImage("images/footer_credits-over.png");
		preloadFlag = true;
	}
}

function playSound() {
	
	if ( !soundFrame ) {
		soundFrame = document.createElement("iframe");
		soundFrame.setAttribute("id", "isound");
		soundFrame.src = "snd.html";
		soundFrame.width = 0;
		soundFrame.height = 0;
		soundFrame.style.visibility = "hidden";
	}
	document.body.appendChild(soundFrame);
	document.cookie="snd=false; path=/;";
}

function stopSound() {
	if ( soundFrame ){
		document.body.removeChild(soundFrame);
	}
	document.cookie="snd=true; path=/;";
}

function toggleSound(){
	if(extractCookie("snd") == "true"){
		stopSound();
	}
	else{
		if(extractCookie("snd") == "false"){
			playSound();
		}
	}
}

function readCookie(c){
	if(document.cookie){
		snd = extractCookie(c);
	}
	else{
		document.cookie = "snd=true; path=/;";
		snd = "true";
	}
	return snd;
}

function extractCookie(c){
	cName = c + "=";
	var cArray = document.cookie.split(';');
	for(var i = 0; i < cArray.length; i++){
		var cook = cArray[i];
		while(cook.charAt(0) == ' '){
			cook = cook.substring(1, cName.length);
		}
		if(cook.indexOf(cName) == 0){
			return cook.substring(cook.length + 1, cName.length);
		}
	}
	return null;
}

function showAbout(){
	document.getElementById("aboutBox").style.visibility = "visible";
	//document.getElementById("aboutText").style.overflow = "auto";
}

function hideAbout(){
	document.getElementById("aboutBox").style.visibility = "hidden";
	//document.getElementById("aboutText").style.overflow = "visible";
}
