var fade_timer = 0;
var shoutdata = 0;
var shout_started = 0;
var shoutcheckzor = 1;
window.onload = function() {
	setTimeout('scrollUser(true)', 1);
	shoutdata = setTimeout('get_shoutbox_data()', 50);
	readyShow = true;
	
	var ins = document.getElementsByTagName('input');
	for (var count_ins = 0; count_ins < ins.length; count_ins++) {
		if (ins[count_ins].getAttribute('type') == 'checkbox') {
			if (ins[count_ins].checked == true) {
				total_pics++;
			}
		}
	}
	if (document.getElementById('fotonum')) {
		document.getElementById('fotonum').innerHTML = total_pics;
	}
	if (document.getElementById('wieisonline_content')) {
		updateWieIsOnline();	
	}
}

function scrollUser(first) {
	var top_div = parseInt(document.getElementById('topDiv').style.top.replace('px', ''));
	var bottom_div = parseInt(document.getElementById('bottomDiv').style.top.replace('px', ''));	
	
	if (first == true) {
		if (top_div > 0) {
			if (bottom_div <= -50) {	
				document.getElementById('bottomDiv').style.filter = 'Alpha(opacity=' + 90 / fade_timer + ')';
				document.getElementById('bottomDiv').style.opacity = 0.9 / fade_timer;		
				document.getElementById('bottomDiv').style.top = (bottom_div - 5) + 'px';
				bottom_div = parseInt(document.getElementById('bottomDiv').style.top.replace('px', ''));
			}			
						
			document.getElementById('topDiv').style.top = (top_div - 5) + 'px';						
			top_div = parseInt(document.getElementById('topDiv').style.top.replace('px', ''));
			
			setTimeout('scrollUser(true)', 25);
		}
		else {
			div_num = 2;
			get_user_data();
			fade_timer = 0;
			document.getElementById('bottomDiv').style.filter = 'Alpha(opacity=100)';
			document.getElementById('bottomDiv').style.opacity = 1;
			setTimeout('scrollUser(false)', 2000);
		}		
	} 
	else {
		if (bottom_div == -100) {	
		
			document.getElementById('bottomDiv').style.top = '0px';
			bottom_div = parseInt(document.getElementById('bottomDiv').style.top.replace('px', ''));
		}
		if (bottom_div > -50) {
			document.getElementById('topDiv').style.filter = 'Alpha(opacity=' + 90 / fade_timer + ')';
			document.getElementById('topDiv').style.opacity = 0.9 / fade_timer;
			document.getElementById('topDiv').style.top = (top_div - 5) + 'px';			
			document.getElementById('bottomDiv').style.top = (bottom_div - 5) + 'px';
			top_div = parseInt(document.getElementById('topDiv').style.top.replace('px', ''));
			bottom_div = parseInt(document.getElementById('bottomDiv').style.top.replace('px', ''));
			
			setTimeout('scrollUser(false)', 25);
		}
		else {			
			div_num = 1;
			get_user_data();
			fade_timer = 0;
			document.getElementById('topDiv').style.filter = 'Alpha(opacity=100)';
			document.getElementById('topDiv').style.opacity = 1;
			document.getElementById('topDiv').style.top = '50px';
			setTimeout('scrollUser(true)', 2000);
		}
	}	
	fade_timer = fade_timer + 1;
}

function createRequestObject() {
	var xmlHttp;
	try {
		xmlHttp=new XMLHttpRequest();
		return xmlHttp;
	}
	catch (e) {
		try {
			xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
			return xmlHttp;
		}
		catch (e) {
			try {
				xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
				return xmlHttp;
			}
			catch (e) {
				alert("Uw browser ondersteunt geen AJAX");
				return false;
			}
		}
	}
}

var http = createRequestObject();
var httpinfobox = createRequestObject();
var http_wio = createRequestObject();
var http_wiotop = createRequestObject();

var div_num = 2;

function updateWIOTop() {
	http_wiotop.open('get', '/ajax/get_wio_top.php');
	http_wiotop.onreadystatechange = handleWIOTOPResponse;
	http_wiotop.send(null);
}
function handleWIOTOPResponse() {
	if (http_wiotop.readyState == 4 && http_wiotop.status == 200) {
		var response_wiotop = http_wiotop.responseText;
		
		if (response_wiotop != false) {
			document.getElementById('nav_left_memberinfo').innerHTML = response_wiotop;	
		}
	}
}

function updateWieIsOnline() {
	http_wio.open('get', '/ajax/get_wie_is_online.php');	
	http_wio.onreadystatechange = handleWIOResponse;	
	http_wio.send(null);	
}
function handleWIOResponse() {
	if(http_wio.readyState == 4 && http_wio.status == 200) {
		var response_wio = http_wio.responseText;
		
		if (response_wio != false) {
			document.getElementById('wieisonline_content').innerHTML = response_wio;
		}
		setTimeout('updateWieIsOnline()', 30000);
		setTimeout('updateWIOTop()', 30000);
	}
}

function get_user_data() {	
	http.open('get', '/ajax/get_user_data.php');
	http.onreadystatechange = handleResponse;
	http.send(null);
}

function changeLink(link_code) {
	document.getElementById('cont_link').innerHTML = link_code;
}

function handleResponse() {
	if(http.readyState == 4 && http.status == 200) {
		var response = http.responseText;
		var broken_response = response.split('|');

		if (response == false) {
			document.getElementById('cont_text_' + div_num).innerHTML = 'Kon geen data ophalen';			
		}
		else {			
			document.getElementById('cont_text_' + div_num).innerHTML = broken_response[2];
			document.getElementById('cont_img_' + div_num).innerHTML = broken_response[0];
			setTimeout("changeLink('" + broken_response[1] + "')", 2000);
		}
	} 
	else {
		document.getElementById('cont_text_' + div_num).innerHTML = 'Bezig met data ophalen';
	}
}





/* SHOUTBOX CODE */
function generate_shoutlist() { // returns an array with all shoutbox shout div ids
	var doc = document.getElementsByTagName('div');
	var shoutlist = new Array();
	var j = 0;
	
	for (var i = 0; i < doc.length; i++) {
		if (document.getElementById(doc[i].id).parentNode.id == "shoutcontent") {	
		   shoutlist[j] = doc[i].id;
		   j++;
		}	   
	}
	
	return shoutlist;	
}		

var currentshout = 0;
var shoutlist = new Array();

function scrollshoutbox() {	
	var shouts = shoutlist;
	
	if (currentshout == shouts.length) {
		currentshout = 0;
		document.getElementById("shoutbox").scrollTop = 0;
	}
	document.getElementById("shoutbox").scrollTop = document.getElementById("shoutbox").scrollTop + document.getElementById(shouts[currentshout]).offsetHeight;	
	currentshout++;	
}

var scrolltemp = 0;
var scrollnummsg = 21;
var pause_num = 0;

function shoutboxanimation() {
	pause_num = 0;
	scrolltemp++;
	if (scrolltemp < scrollnummsg) {
		scrollshoutbox();
		scrolltimeout = setTimeout("shoutboxanimation()",5000);
	} else {
		document.getElementById("shoutbox").scrollTop = 0;
		scrolltemp = 0;
		currentshout = 0;
		clearTimeout(scrolltimeout);
		shout_started = 0;
		scrolltimeout = setTimeout("get_shoutbox_data()",5000);		
	}	
}
function shoutboxanimation_reset() {
	document.getElementById('animationcontrol').innerHTML = "<a href=\"#\" onclick=\"shoutboxanimationstop()\"><img src=\"/images/control_pause_blue.gif\" border=\"0\" width=\"16\" height=\"16\" alt=\"pauze\" /></a>";
	shoutcheckzor = 1;
	document.getElementById("shoutbox").scrollTop = 0;
	pause_num = 0;
	scrolltemp = 0;
	currentshout = 0;
	clearTimeout(scrolltimeout);
	shout_started = 0;
	get_shoutbox_data();
	//scrolltimeout = setTimeout("shoutboxanimation()",2000);		
}
function shoutboxanimationstop() {
	pause_num = 1;
	clearTimeout(scrolltimeout);
	document.getElementById('animationcontrol').innerHTML = "<a href=\"#\" onclick=\"shoutboxanimation_reset()\"><img src=\"/images/control_play_blue.gif\" border=\"0\" width=\"16\" height=\"16\" alt=\"start\" /></a>";
	shoutcheckzor = 0;
}

var httpshoutbox = createRequestObject();
function get_shoutbox_data() {
	if (shout_started == 0 || pause_num == 1) {
		var randcode = Math.floor(Math.random()*999999);
		httpshoutbox.open('get', '/ajax/shoutbox.php?'+randcode);
		httpshoutbox.onreadystatechange = handleshoutboxResponse;
		httpshoutbox.send(null);
		shout_started = 1;
		shoutdata = setTimeout('get_shoutbox_data()', 20000);
	}
}
function handleshoutboxResponse() {
	if(httpshoutbox.readyState == 4 && httpshoutbox.status == 200) {
		var httpshoutboxresponse = httpshoutbox.responseText;

		if (httpshoutboxresponse == false) {
			document.getElementById('shoutcontent').innerHTML = 'Kon geen data ophalen';
			//alert('fout');
		}
		else {
			
			var broken_divs_response = httpshoutboxresponse.split('|');
			
			//document.getElementById('shoutcontent').innerHTML = broken_response[2];
			document.getElementById('shoutcontent').innerHTML = broken_divs_response[0];

			var broken_div_ids_response = broken_divs_response[1].split(';');
			
			for(shoutdivs = 0; shoutdivs < broken_div_ids_response.length; shoutdivs++) { 
   				shoutlist[shoutdivs] = broken_div_ids_response[shoutdivs];
			}		
			
			if (pause_num == 0) {			
				scrolltimeout = setTimeout("shoutboxanimation()",5000);
			}
		}
	} 
	else {
		//document.getElementById('shoutcontent').innerHTML = 'Bezig met data ophalen';
	}
}

var httpshoutreply = createRequestObject();
function send_shoutbox_data(sho_content) {
	httpshoutreply.open('get', '/ajax/shoutbox_reply.php?i='+sho_content);
	httpshoutreply.onreadystatechange = handleshoutreplyResponse;
	httpshoutreply.send(null);
}
function handleshoutreplyResponse() {
	if(httpshoutreply.readyState == 4 && httpshoutreply.status == 200) {
		var httpshoutreplyresponse = httpshoutreply.responseText;
		var fix_response = httpshoutreplyresponse.split('|');

		if (httpshoutreplyresponse == false) {
			//document.getElementById('shoutcontent').innerHTML = 'Kon geen data ophalen';
			//alert('fout');
		}
		else {			
			//document.getElementById('shoutcontent').innerHTML = broken_response[2];
			//document.getElementById('shoutcontent').innerHTML = httpshoutreplyresponse;
			//alert('kaboem');
			clearTimeout(shoutdata);
			shoutdata = setTimeout('get_shoutbox_data()', 50);
			//document.getElementById('sho_inhoud').value = "";
			document.getElementById('sho_inhoud').focus();
			if (fix_response[0] == 2) {
				alert('Er kan slechts om de vijf seconden een shout geplaatst worden...\nNog '+fix_response[1]+' wachten voor er weer geshout mag worden.');	
			}
			if (fix_response[0] == 1) {

				document.getElementById('sho_inhoud').value = "";

				if (shoutcheckzor) {
					//alert("AAN");	
					shoutboxanimation_reset();
				} else {
					//alert("UIT");	
					document.getElementById("shoutbox").scrollTop = 0;
				}

				
				
			
				
			}
			
		}
	} 
	else {
		//document.getElementById('shoutcontent').innerHTML = 'Bezig met data ophalen';
	}
}
