var buffer = function(debug) {
	/*
	 * Buffer-Class
	 * v1.0.1 0014
	 * 
	 * Copyright by OSwebstyle.de
	 * If you want to use this class pleas contact us at info@oswebstyle.de for further information.
	 * 
	 * Description
	 * - handles asyncronous and syncronous request
	 * - saves requestet sites
	 * - possibility to force requests
	 * - possibility to forc return-types (plaint text / xml)
	 * - possibility to include pre-loaded content and integrate it into the internal buffer
	 * - integrated debug-functions (writes logs to "console.log(error_string)")
	 * 
	 * Important public-functions
	 * - addData(file_url [string], data [string (txt/xml)], xml [boolean])
	 * 		Saves loaded pages to the internal buffer. You can use this function for pre-loaded content as well.
	 * 
	 * - getData(file_url [string], force_request [boolean], xml [boolean], async [boolean], callback [function (as string)])
	 * 		Requests pages either from the internal buffer (weather it was already loaded) or creats a new http-request. 
	 */
	
	var cache = Array(null);
	var debug = (debug == true ? true : false);
	
	var getAjaxRequest = function(xml) {
		var xmlHttpRequest = false;
		if(window.XMLHttpRequest) {
			try {
				xmlHttpRequest = new XMLHttpRequest();
			}
			catch(e) {
				xmlHttpRequest = false;
			}
		}
		else if(window.ActiveXObject) {
			try {
				xmlHttpRequest = new ActiveXObject("Msxml2.XMLHTTP");
			}
			catch(e) {
				try {
					xmlHttpRequest = new ActiveXObject("Microsoft.XMLHTTP");
				}
				catch(e) {
					xmlHttpRequest = false;
				}
			}
		}
	
		if(xmlHttpRequest.overrideMimeType) {
			xml == true ? xmlHttpRequest.overrideMimeType("text/xml") : xmlHttpRequest.overrideMimeType("text/html");
		}
		
		return xmlHttpRequest;
	}
	
	this.addData = function(file, data, xml) {
		if(debug == true) console.log("buffer.addData('"+file+"','"+unescape(data)+"','"+xml+"');");
		
		cache[file]	= Array(data, (typeof(xml) == "undefined" ? false : true));
	}
	
	this.getData = function(file, force_request, xml, async, callback, xml_cb) {
		if(debug == true) console.log("buffer.getData('"+file+"','"+force_request+"','"+xml+"','"+async+"','"+callback+"');");
		
		if(typeof(cache[file]) != "undefined" && cache[file][1] == (typeof(xml) == "undefined" ? false : xml) && (typeof(force_request) == "undefined" || force_request == false ? false : true) == false) {
			return cache[file][0];
		}
			
		else if((typeof(async) == "undefined" || async == false ? false : true) == false) {
			return this.reqData(file, (typeof(xml) == "undefined" || xml == false ? false : true), false, callback, xml_cb)
		}
		else {
			this.reqData(file, (typeof(xml) == "undefined" || xml == false ? false : true), (typeof(async) == "undefined" || async == false ? false : true), callback, xml_cb);
			return true;
		}
	}
	
	this.reqData = function(file, xml, async, callback, xml_cb) {
		var ajaxRequest = getAjaxRequest(xml);
		var domain		= document.location.protocol + "//" + document.location.host;
		
		//open request
		ajaxRequest.open("GET", domain+"/ajax/"+file, ((typeof(async) == "undefined" || async == true) ? true : false));
		ajaxRequest.setRequestHeader("Method", "POST /ajax/"+file+" HTTP/1.1");
		ajaxRequest.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
		
		ajaxRequest.send(null);
		
		if(typeof(async) == "undefined" || async == true) {
			ajaxRequest.onreadystatechange = function() {
				if (ajaxRequest.readyState == 4) {
					cache[file] = Array((xml == true ? ajaxRequest.responseXML : "("+escape(ajaxRequest.responseText)+")"), xml);
					try { 
						if(xml == true) {
							eval(xml_cb+" = cache[file][0]");
							eval(callback+"()");
						}
						else { eval(callback+"('"+cache[file][0]+"')"); }
					}
					catch(e) {	if(debug == true) console.log(e); }
				}
			}
		}
		else {
			//receving request
			cache[file] = Array((xml == true ? ajaxRequest.responseXML : ajaxRequest.responseText), xml);
			return cache[file][0];
		}

		return true;
	}
	
	this.flush = function(key) {
		if(key == "")	cache = Array(null);
		else			cache[key] = null;
	}
}
var gfx = function() {
	
	this.fades 	 = Array();
	this.smooths = Array();
	
	this.fader	 = null;
	this.smoother 
	
	this.fade = function(obj_id, start, end, asc) {
		this.fades[this.fades.length] = Array(obj_id, start, end, asc);
		
		if(this.fader == null) this.fader = window.setInterval(gfx.intervalHandler, 20);
	}
	
	this.smooth = function(obj_id, new_content) {
		var tmp		  = getObjectById(obj_id).innerHTML;
		var curHeight = getObjectById(obj_id).offsetHeight;
		getObjectById(obj_id).innerHTML += "<div id=\""+obj_id+"_tmp\" style=\"height: 0px; overflow: scroll;\">"+unescape(new_content)+"</div>";
		var newHeight = getObjectById(obj_id+"_tmp").scrollHeight;
		getObjectById(obj_id).innerHTML = tmp;
		
		//debug("curHeight: "+curHeight+" -- newHeight: "+newHeight);
		this.smooths[this.smooths.length] = Array(obj_id, curHeight, newHeight, (newHeight > curHeight ? true : false));
		
		if(this.fader == null) this.fader = window.setInterval(gfx.intervalHandler, 20);
	}
	
	this.fadeHandler = function() {
		var obj;
		var fin = 0;
		
		for(var i = 0;i<gfx.fades.length;++i) {
			
			if(gfx.fades[i] == null) {
				++fin;
				continue;
			}
			
			obj = getObjectById(gfx.fades[i][0]);

			if(gfx.fades[i][3]) {
				//ascending
				if(gfx.fades[i][1] >= gfx.fades[i][2]) {
					gfx.fades[i][1] = gfx.fades[i][2];
					++fin;
				}
				else									gfx.fades[i][1] += 5;
			}
			else {

				if(gfx.fades[i][1] >= gfx.fades[i][2])	gfx.fades[i][1] -= 5;
				else {
					gfx.fades[i][1] = gfx.fades[i][2]
					++fin;
				}
			}
				
			obj.style.opacity = (gfx.fades[i][1] / 100);
			obj.style.MozOpacity = (gfx.fades[i][1] / 100);
			obj.style.KhtmlOpacity = (gfx.fades[i][1] / 100);
			obj.style.filter = "alpha(opacity=" + gfx.fades[i][1] + ")";
			
			if(gfx.fades[i][1] == gfx.fades[i][2]) gfx.fades[i] = null;
			
		}
		
		//debug("fadeHandler:<br />finished: "+fin+"\nmax.:"+gfx.fades.length+"<br />status: "+(fin == gfx.fades.length)+"<br /><br />");
		
		if(fin == gfx.fades.length) return true;
		else						return false;
	}
	
	this.scrollHandler = function() {
		var obj;
		var fin = 0;
		
		for(var i = 0;i<gfx.smooths.length;++i) {
			
			if(gfx.smooths[i] == null) {
				++fin;
				continue;
			}
			
			obj = getObjectById(gfx.smooths[i][0]);
			
			if(gfx.smooths[i][3]) {
				//ascending
				
				//debug("ascending: "+gfx.smooths[i][1]);
				if(gfx.smooths[i][1] >= gfx.smooths[i][2]) {
					gfx.smooths[i][1] = gfx.smooths[i][2];
					++fin;
				}
				else gfx.smooths[i][1] += 15;
			}
			else {
				
				//debug("descending: "+gfx.smooths[i][1]);
				if(gfx.smooths[i][1] >= gfx.smooths[i][2])	gfx.smooths[i][1] -= 15;
				else {
					gfx.smooths[i][1] = gfx.smooths[i][2];
					++fin;
				}

				obj.style.height = gfx.smooths[i][1]+"px";
			}
			
			obj.style.height = gfx.smooths[i][1]+"px";
			
			if(gfx.smooths[i][1] == gfx.smooths[i][2]) gfx.smooths[i] = null;
			
		}
		
		if(fin == gfx.smooths.length)	return true;
		else							return false;
	}
	
	this.running = function(obj_id, event) {
		var i;
		switch(event) {
			case "scroller":
				for(i = 0;i<this.smooths.length;++i) {
					if(this.smooths[i] == null) continue;
					
					if(this.smooths[i][0] == obj_id)	{
						return true;
					}
				}
			break;
			default:	//fader
				for(i = 0;i<this.fades.length;++i) {
					if(this.fades[i] == null) continue;
					
					if(this.fades[i][0] == obj_id)	{
						return true;
					}
				}
			break;
		}
		return false;
	}
	
	this.intervalHandler = function() {
		if(
			gfx.fadeHandler() == true &&
			gfx.scrollHandler() == true
		) {
			gfx.fader = window.clearInterval(gfx.fader);
			gfx.fader = null;
		}
	}
}
var debug = function(string) { console.log(string); }
var getObjectById = function(id) {
	if(document.layers)					return document.layers[id];
	else if(document.all)				return document.all[id];
	else if(document.getElementById)	return document.getElementById(id);
	else 								return null;
}
var news = function() {
	
	this.current_site	= 1;
	this.loading		= false;
	this.spooler		= null;
	this.load_spooler	= null;
	
	this.getSite = function(site) {
		if(this.current_site == site || this.loading == true) return false;
		
		this.loading		= true;
		this.current_site	= site;

		gfx.fade('news_loading', 0, 100, true);
		gfx.fade('news', 100, 0, false);
		
		if(this.load_spooler == null)
			this.load_spooler = window.setInterval("news.load_screener()", 650);
		
		window.location.hash = "#site-"+site;
		
		var content = buffer.getData('news.php?site='+site, false, false, true, 'news.setSite');
		if(typeof content == "boolean") return true;
		
		this.setSite(content);
	}
	this.load_screener = function() {
		if(getObjectById("news_loading").innerHTML == "Wird geladen...") {
			getObjectById("news_loading").innerHTML = "Wird geladen";
		}
		else getObjectById("news_loading").innerHTML+= ".";
	}
	this.setSite = function(content) {
		if(gfx.running('news', 'fading') == true) {
			news.spooler = window.clearInterval(news.spooler);
			news.spooler = window.setInterval('news.setSite("'+content+'")', 500);
			return false;
		}
		else {
			news.spooler = window.clearInterval(news.spooler);
		}

		var json = eval('' + unescape(content) + '');
		var sites = parseInt(json.site_count);
		var new_cont;
		
		if(!json.entry_1 && !json.entry_2) {
			new_cont = "<div class=\"entry_empty\"><h3>Es wurden keine Neuigkeiten gefunden.</h3></div>";
			new_cont+= "<div class=\"entry_empty\">&nbsp;</div>";
		}
		
		if(json.entry_1) {
			new_cont = "<div class=\"entry\"><h3>"+unescape(json.entry_1.title)+"</h3>";
			new_cont+= "<p>"+unescape(json.entry_1.content)+"</p>";
			new_cont+= "<a href=\"/"+unescape(json.entry_1.seo_title)+".html\">";
			new_cont+= "Weiter lesen &raquo;</a></div>";
		}
		
		if(json.entry_2) {
			new_cont+= "<div class=\"entry\"><h3>"+unescape(json.entry_2.title)+"</h3>";
			new_cont+= "<p>"+unescape(json.entry_2.content)+"</p>";
			new_cont+= "<a href=\"/"+unescape(json.entry_2.seo_title)+".html\">";
			new_cont+= "Weiter lesen &raquo;</a></div>";
		}
		if(!json.entry_2)	 {
			new_cont+= "<div class=\"entry_empty\">&nbsp;</div>";
		}
		
		new_cont+= "<div class=\"site\">";
		
		if(this.current_site > 1)
			new_cont+= "	<a href=\"/neuigkeiten.html\" onclick=\"news.getSite("+(this.current_site-1)+");return false;\">&laquo; Vorherige</a>";
		else
			new_cont+= "	<a href=\"/neuigkeiten.html\" onclick=\"return false;\" class=\"disabled\">&laquo; Vorherige</a>";
		
		new_cont+= "	Seite "+this.current_site+" von "+sites+" Seite"+(sites != 1 ? "n" : "");
		
		if(this.current_site+1 <= sites)
			new_cont+= "	<a href=\"/neuigkeiten.html\" onclick=\"news.getSite("+(this.current_site+1)+");return false;\">N&auml;chste &raquo;</a>";
		else
			new_cont+= "	<a href=\"/neuigkeiten.html\" onclick=\"return false;\" class=\"disabled\">N&auml;chste &raquo;</a>";
		
		new_cont+= "</div>";
		
		getObjectById("news").innerHTML = new_cont;
		gfx.fade('news', 0, 100, true);
		gfx.fade('news_loading', 100, 0, false);
		
		this.loading = false;
	}
}
var news	= new news();
var gfx		= new gfx();
var buffer	= new buffer();
