//BASIC AJAX INIT
var tidin = new Array (); 
var tidout = new Array (); 
var timeout = 0;
var timein = 0;
var menu_in = "busy";
var tmptxt = "";

function myAIAS (location, handler ) {

	var httphandler = false;
	//initialize HTTP obj
    /*@cc_on @*/
   	/*@if (@_jscript_version >= 5)
    // JScript gives us Conditional compilation, we can cope with old IE versions.
   	// and security blocked creation of the objects.
    try {
   	 httphandler = new ActiveXObject("Msxml2.XMLHTTP");
    } catch (e) {
   	  try {
      	httphandler = new ActiveXObject("Microsoft.XMLHTTP");
     	} catch (E) {
         httphandler = false;
     	}
   	}
   	@end @*/
   
   	if (!httphandler && typeof XMLHttpRequest!='undefined') {
       	httphandler = new XMLHttpRequest();
   	}
   	
   	httphandler.open('GET', location, true);
    httphandler.onreadystatechange= function () {
   		
   
   		if ( httphandler.readyState == 4 ) {
   			//alert ( "done loading" );
   			handler ( httphandler);
   		}
   	}
   	
	httphandler.setRequestHeader("Cache-Control", "no-cache");
	httphandler.send (null);

}

function dataHandler (httphandler) {
	
	xml = httphandler.responseXML;
	//only one item will be returned 
	d = document;
	
	
	
	if ( xml.getElementsByTagName("topimg")[0].firstChild.nodeValue.length ){
		d.getElementById("body").style.backgroundImage = "url(resize_bg.php?fn=uploads/"+xml.getElementsByTagName("topimg")[0].firstChild.nodeValue+")";
		d.getElementById("topimg").src="uploads/"+xml.getElementsByTagName("topimg")[0].firstChild.nodeValue;
	}
	
	d.getElementById("content").innerHTML = xml.getElementsByTagName("body")[0].firstChild.nodeValue;
	d.getElementById("orange_title").innerHTML = xml.getElementsByTagName("flash_title")[0].firstChild.nodeValue;
	//document.getElementById("flashtitle").setVariable ( "htext", xml.getElementsByTagName("txt_title")[0].firstChild.nodeValue);
	//document.getElementById("orange").innerHTML = xml.getElementsByTagName("menu_title")[0].firstChild.nodeValue;
	
	
	//set flash orangetitle
	flashobject = window.document.flashtitle;
	flashobject.SetVariable ( "/:htext", xml.getElementsByTagName("flash_title")[0].firstChild.nodeValue );
	flashobject.Play ();
}


function newsHandler ( httphandler ) {

	var xml = httphandler.responseXML;
	var d = document;
	var root = xml.getElementsByTagName ( "items" )[0];
	var htmlBuffer = "";
	
	items = root.getElementsByTagName ( "item" );
	
	d.getElementById("orange_title").innerHTML = "Nieuws";
	
	for ( i = 0; i < items.length; i ++ ){
		
		if ( i == 0 && xml.getElementsByTagName("news_img")[0].firstChild.nodeValue.length ) {
			
			d.getElementById("body").style.backgroundImage = "url(resize_bg.php?fn=uploads/"+xml.getElementsByTagName("news_img")[0].firstChild.nodeValue+")";
			d.getElementById("topimg").src="return_image.php?x=444&y=222&fn=uploads/"+xml.getElementsByTagName("news_img")[0].firstChild.nodeValue;
		}
		
		htmlBuffer += "<div class=\"newstitle\">"+items[i].getElementsByTagName("ts")[0].firstChild.nodeValue+" | "+items[i].getElementsByTagName("txt_title")[0].firstChild.nodeValue+"</div>";
		htmlBuffer += items[i].getElementsByTagName("body_short")[0].firstChild.nodeValue+"<br /><br />";
		htmlBuffer += "<a class=\"orangelink\" href=\"javascript://\" onclick=\"menuclick('nd',"+ items[i].getElementsByTagName("news_id")[0].firstChild.nodeValue+");return false;\">lees meer <img src=\"img/triangle.gif\" /></a>";
		htmlBuffer += "<div class=\"newsspacer\">&nbsp;</div>";
	}	
	
	if ( items.length == 3 ) 
		htmlBuffer += "<a class=\"orangelink\" href=\"javascript://\" onclick=\"menuclick('nl',0);return false;\"><img src=\"img/triangle_left.gif\" /> ouder nieuws</a>";
	
	//pager
	d.getElementById( "content" ).innerHTML = htmlBuffer;
	
	//set flash orangetitle
	flashobject = window.document.flashtitle;
	flashobject.SetVariable ( "/:htext", "Nieuws" );
	flashobject.Play ();
}

function newsDataHandler ( httphandler ) {

	var xml = httphandler.responseXML;
	var d = document;
	var root = xml.getElementsByTagName ( "items" )[0];
	var htmlBuffer = "";
	
	items = root.getElementsByTagName ( "item" );
	
	d.getElementById("orange_title").innerHTML = "Nieuws";
		
	if ( xml.getElementsByTagName("news_img")[0].firstChild.nodeValue.length ) {
			
		d.getElementById("body").style.backgroundImage = "url(resize_bg.php?fn=uploads/"+xml.getElementsByTagName("news_img")[0].firstChild.nodeValue+")";
		d.getElementById("topimg").src="return_image.php?x=444&y=222&fn=uploads/"+xml.getElementsByTagName("news_img")[0].firstChild.nodeValue;
	}	
		
	htmlBuffer += "<div class=\"newstitle\">"+items[0].getElementsByTagName("ts")[0].firstChild.nodeValue+" | "+items[0].getElementsByTagName("txt_title")[0].firstChild.nodeValue+"</div>";
	htmlBuffer += items[0].getElementsByTagName("body")[0].firstChild.nodeValue+"<br /><br />";
	htmlBuffer += "<a class=\"orangelink\" href=\"javascript://\" onclick=\"menuclick('nl',0);return false;\"><img src=\"img/triangle_left.gif\" /> terug</a>";
	
	
	
	//pager
	d.getElementById( "content" ).innerHTML = htmlBuffer;
	
	//set flash orangetitle
	flashobject = window.document.flashtitle;
	flashobject.SetVariable ( "/:htext", "Nieuws" );
	flashobject.Play ();
}

function submenuHandler ( httphandler ) {
	
	tmptxt = httphandler.responseText;
	
	timein = 0;
	tidin.push ( setInterval ( "submenuIn()", 1 ) );
}

//animate
function clearIntervals (intervals) {
	
	for ( var i = 0; i < intervals.length; i ++ ) {
		clearInterval ( intervals [i] );
	}
	
	intervals = new Array ();
}

function submenuIn () {
	
	if ( menu_in == "done" ) {
		timein ++;
		
		//cast to int with | 0 
		var intval = easing.bounceOut(timein,0,222,30)|0;
		document.getElementById("innersubmenu").style.width = intval+"px";
	
		if ( timein >= 30 || intval >= 222 ) {
			
			document.getElementById("innersubmenu").style.width = "222";
			clearIntervals ( tidin );
			menu_in == "busy";
			document.getElementById("innersubmenu").innerHTML = tmptxt;
		}
	}
}


function submenuOut (id) {
	
	menu_in = "busy";
	timeout ++;
	
	//cast to int with | 0 
	var intval = easing.easeIn(timeout,222,-222,10)|0;
	document.getElementById("innersubmenu").style.width = intval+"px";
	
	if ( timeout >= 10 || intval <= 0 ) {
			
			document.getElementById("innersubmenu").style.width = "0";
			clearIntervals ( tidout );
			menu_in = "done";
	}
}

function make_active ( menuitem, type ) {
	
	var items = document.getElementsByName ( type+"menuitem" );

	for ( var i = 0; i < items.length; i++ ) {
		items[i].className = type+"menuitem";
	}
	
	menuitem.className = type+"menuitem_selected";
}

function menuclick ( type, id, a ) {
	
	if ( !type.match('^n.*$') ) A = new myAIAS ( "ajax/default.php?"+type+"="+id, dataHandler );
	if ( type == "s" ) {
		make_active ( a, "sub" );
	}
	
	else if ( type == "m" ) { //if main menu animate sub
		
		make_active ( a, "main" );
		
		timeout = 0;
		tidout.push ( setInterval ( "submenuOut("+id+")", 1 ));
		
		document.getElementById("innersubmenu").innerHTML = "";
		B = new myAIAS ( "ajax/submenu.php?i="+id, submenuHandler ); 

	}
	
	else if ( type == "n" ) { //news first three
		
		make_active ( a, "main" );
		
		document.getElementById("innersubmenu").innerHTML = "";
		
		C = new myAIAS ( "ajax/news.php?limit=3&i="+id, newsHandler );
	}
	
	else if ( type == "nl" ) { //news full list
		
		C = new myAIAS ( "ajax/news.php?i="+id, newsHandler );
	}

	else if ( type == "nd" ) { //news detail
		
		C = new myAIAS ( "ajax/news.php?i="+id, newsDataHandler );
	}
	
}

