// ______    comments are for pussies            __________                //
// ___  /_________      ________________________ __  /___(_)__   ______    //
// __  __ \_  ___/_______  ___/_  ___/  _ \  __ `/  __/_  /__ | / /  _ \   //
// _  /_/ /  /   _/_____/ /__ _  /   /  __/ /_/ // /_ _  / __ |/ //  __/   //
// /_.___//_/           \___/ /_/    \___/\__,_/ \__/ /_/  _____/ \___/    //
//                                     (C)2009 oliver@br-creative.com      //

BRCLICK = false;
EMPTY = true;
window.returnTo = "";
FadeState = new Object();
brbtext = new Object();
brbtext["BRwho"] = new Array("juggling<br>ideas");
brbtext["BRgraf"] = new Array("drawing<br>lines");
brbtext["BRphoto"] = new Array("catching<br>light");
brbtext["BRweb"] = new Array("pushing<br>bytes");
brbcolor = new Array("#933","#955","#a77","#b88","#b99","#caa","#dbb","#dcc","#edd","#fee","#fff");

function brinit(){
	for(var bstr in brbtext){
		for(var c=0;c<10;c++) {
			var parts = brbtext[bstr][0].split('<br>');
			brbtext[bstr].push(shuffle(parts[0])+"<br>"+shuffle(parts[1]));
		}
		brbtext[bstr].reverse();
		document.getElementById(bstr).innerHTML = brbtext[bstr][0];
	}
	if(navigator.appName == "Microsoft Internet Explorer")
		document.getElementById('contentbg').style.filter = 'alpha(opacity = 0)';
	setscreen();
	drawtheline();
	setTimeout("welcome();",4000);
}
function welcome(){
	if(document.getElementById('content').innerHTML.length < 100) 
		showcontent('brwelcome.html');
}

function shuffle(theString){
	var arr = theString.split('');
	arr.sort(function(){return Math.random()<0.5?-1:1});
	return arr.join('')
}

function drawtheline() {
	if(typeof(size) == "undefined") size = 1;
	size++;
	document.getElementById("line").style.width = size + "%";	
	if(size == 30) blendin("BRwho");
	if(size == 50) blendin("BRphoto");
	if(size == 70) blendin("BRgraf");
	if(size == 90) blendin("BRweb");
	if(size < 100)
		setTimeout("drawtheline()", 25);
}

function setscreen() {
	var screenx = 800;
	var screeny = 600;

	if(window.innerWidth){
		screenx = window.innerWidth;
		screeny = window.innerHeight;
	}
	if(document.body.clientWidth && !window.innerWidth) {
		screenx = document.body.clientWidth;
		screeny = document.body.clientHeight;
	}
	if(document.documentElement.clientWidth && !window.innerWidth) {
		screenx = document.documentElement.clientWidth;
		screeny = document.documentElement.clientHeight;
	}
	var startx  = (screenx<1000) ? 0 : (screenx - 1000)/2;
	var starty  = (screeny< 760) ? 0 : (screeny - 760)/2;
	
	document.getElementById("wrapper").style.top = starty+"px";
	document.getElementById("wrapper").style.left = startx+"px";
}

function switchbutton(eid, text, image) {
	var tick = 22;
	if(image.length) {  
		for(var cnt in brbtext[eid]) setTimeout('document.getElementById("'+eid+'").innerHTML = brbtext["'+eid+'"]['+cnt+'];document.getElementById("'+eid+'").style.color = brbcolor['+cnt+']',cnt*tick);
	} else {
		var tmx = brbtext[eid].length * tick;
		for(var cnt=brbtext[eid].length-1;cnt>=0;cnt--) setTimeout('document.getElementById("'+eid+'").innerHTML = brbtext["'+eid+'"]['+cnt+'];document.getElementById("'+eid+'").style.color = brbcolor['+cnt+']',tmx-(cnt*tick));
	}
	showteaser(image);
}

function showteaser(image) {
	var element = document.getElementById("teaser");
	if(image.length) {  
		var fromtop  = 200+(200*Math.random()) + "px";
		var fromleft =  50+(450*Math.random()) + "px";
		element.style.top = fromtop;
		element.style.left = fromleft;
		element.style.backgroundImage = "url(images/" + image + ")";
//		fadeout('content');
		blendin('teaser');
	} else {
		fadeout('teaser');
		if(!BRCLICK && !EMPTY) {
//			blendin('content');
		}
		BRCLICK = false;
	}
}

function startgallery(theGallery) {
	GALLERY="";
	var headID = document.getElementsByTagName("head")[0];         
	var newScript = document.createElement('script');
	newScript.id = 'galjs';
	newScript.type = 'text/javascript';
	newScript.src = '/galleries/galjs.php?gallery='+theGallery;
	if(document.getElementById("galjs"))
		headID.replaceChild(newScript, document.getElementById("galjs"));
	else
		headID.appendChild(newScript);
	
	var cssNode = document.createElement('link');
	cssNode.type = 'text/css';
	cssNode.rel = 'stylesheet';
	cssNode.id = 'galcss';
	cssNode.href = '/styles/gallery.css';
	cssNode.media = 'screen';
	if(document.getElementById("galcss"))
		headID.replaceChild(cssNode, document.getElementById("galcss"));
	else
		headID.appendChild(cssNode);

	wfgodot('galleryloaded', theGallery);
	showcontent('galleries/index.html')
}

function wfgodot(id, load){
	if(window.GALLERY && window.GALLERY==load && document.getElementById(id)) {
		galinit();
		return;
	}
	setTimeout("wfgodot('"+id+"', '"+load+"')", 60);
}

function showcontent(theurl) {
	if(theurl.indexOf(":")>0) {
		window.open(theurl,"_blank");
		return;
	}
	if(theurl == 'galleries/index.html')
		document.getElementById("contentbg").style.backgroundColor="#333";
	else
		document.getElementById("contentbg").style.backgroundColor="#ccc";
	
	if(document.getElementById("content").style.opacity && document.getElementById("content").style.opacity >0) {
	fadeout("content");
	setTimeout('document.getElementById("content").innerHTML = getpage("'+theurl+'")',750);
	setTimeout('blendin("content")',750);
  } else {
  	document.getElementById("content").innerHTML = getpage(theurl);
  	blendin("content");
	}
	if(document.getElementById("brreturn"))
		window.returnTo = document.getElementById("brreturn").getAttribute('name');
	BRCLICK = true;
	EMPTY = false;
	document.getElementById("teaser").style.display = "none";
}

function getpage(theurl){
	xmlhttp=null;
	if (window.XMLHttpRequest)
	  xmlhttp=new XMLHttpRequest();
	else if (window.ActiveXObject)
	  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
	if (xmlhttp!=null) {
	  xmlhttp.open("GET",theurl,false);
	  xmlhttp.send(null);
	  return xmlhttp.responseText;
    } else {
	  alert(lang_XMLHTTP_error);
    }
}

function setopa(eid, opacity) {
	var element = document.getElementById(eid);
	element.style.opacity = opacity/100;
	element.style.filter = 'alpha(opacity = ' + opacity + ')';
	if(eid=="content"){
		cbg = document.getElementById("contentbg");
		bgopacity = opacity/3;
		cbg.style.opacity = bgopacity/100;
		cbg.style.filter = 'alpha(opacity = ' + bgopacity + ')';
	}
}

function blendin(eid) {
	FadeState[eid] = 1;
	var element = document.getElementById(eid);
	var opacity = (element.style.opacity) ? (element.style.opacity*100) : 0;
	setopa(eid, opacity);
	element.style.display = "block";
	setTimeout("doblend('"+eid+"',"+opacity+")",50);
}

function doblend(eid,opacity) {
	if(FadeState[eid] != 1) return;
	setopa(eid, opacity);
	opacity+=10;
	if(opacity<=100)
		setTimeout("doblend('"+eid+"',"+opacity+")",5);
	else
		FadeState[eid] = 0;
}

function fadeout(eid) {
	FadeState[eid] = -1;
	var element = document.getElementById(eid);
	var opacity = (element.style.opacity) ? (element.style.opacity*100) : 100;
	dofade(eid,opacity);
}

function dofade(eid,opacity) {
	if(FadeState[eid] != -1) return;
	setopa(eid, opacity);
	opacity-=10;
	if(opacity>0)
		setTimeout("dofade('"+eid+"',"+opacity+")",5);
	else {
		document.getElementById(eid).style.display = "none";
		FadeState[eid] = 0;
	}
}

function reset() {
	EMPTY = true;
	fadeout('content');
}
	
function debug(string) {
	if(!document.getElementById('debug')) return;
	document.getElementById('debug').innerHTML = string;
}
	
window.onresize = setscreen;

