/**
 * JavaScript-Funktionen für das LYNET CMS Bewertungs-Plugin
 * @author ag
 */

function SimpleRating()
{
	var app = this;
	app.elemid = null;
	app.pid = null;
	app.request = null;
	app.rpcurl = 'incl/rating/rating.php';
	app.active = true;

	app.new_request = function()
	{
		if (window.XMLHttpRequest)
			return(new XMLHttpRequest());
		else if (window.ActiveXObject)
			return(new ActiveXObject("Microsoft.XMLHTTP"));
		else
			return(null);
	}

	app.addEvent = function(obj, type, fn)
	{
		if (obj.addEventListener)
		{
			obj.addEventListener(type, fn, false);
		}
		else if (obj.attachEvent)
		{
			obj["e"+type+fn] = fn;
			obj[type+fn] = function() { obj["e"+type+fn]( window.event ); }
			obj.attachEvent("on"+type, obj[type+fn]);
		}
	}

	app.init = function(htmlid, pageid, rpcbaseurl)
	{
		app.htmlid = htmlid;
		app.pid = pageid;
		if (typeof rpcbaseurl != 'undefined') app.rpcurl = rpcbaseurl;

		elem = document.getElementById(app.htmlid);
		if (elem)
		{
			if (elem.className == 'rating')
			{
				app.addEvent(
					elem,
					'mouseout',
					function() {
						if (!app.active) return;
						this.nextSibling.innerHTML = '';
					}
				);

				var children = app.filterChildNodes(elem.childNodes, 'LI');
				for (var k = 0; k < children.length; k++)
				{
					app.addEvent(
						children[k],
						'mouseover',
						function() {
							if (!app.active) return;
							for (var l = 0; l <= children.length; l++)
							{
								children[l].className = app.addClass(children[l].className, 'active');
								if (children[l] == this) break;
							}
							children[l].parentNode.nextSibling.innerHTML = children[l].firstChild.firstChild.nodeValue;
						}
					);
					app.addEvent(
						children[k],
						'mouseout',
						function() {
							if (!app.active) return;
							for (var l = 0; l <= children.length; l++)
							{
								children[l].className = app.removeClass(children[l].className, 'active');
								if (children[l] == this) break;
							}
						}
					);

					app.addEvent(
						children[k],
						'click',
						function() {
							if (!app.active) return;
							app.active = false;
							var num = 0;

							// Index des geklickten Elementes ermitteln und Highlighting entfernen
							for (var l = 0; l < children.length; l++) {
								children[l].className = app.removeClass(children[l].className, 'active');
								if (children[l] == this) num = l+1;
							}

							// Und die Bewertung senden ...
							app.getRequestAsync(

								app.rpcurl + '?opid=' + app.pid + '&rating=' + num,
								function() {
									if (app.request.readyState == 4)
									{
										var response = app.request.responseXML;
										try
										{
											// Sternchen aktualisieren
											var avg = response.getElementsByTagName('result')[0].getAttribute('value');
											var val = Math.round(avg);

											for (var l = 0; l < children.length; l++)
											{
												if (val >= (l +1)) {
													children[l].className = app.addClass(children[l].className, 'current');
													children[l].className = app.removeClass(children[l].className, 'half');
												} else if (avg > l) {
													children[l].className = app.addClass(children[l].className, 'current');
													children[l].className = app.addClass(children[l].className, 'half');
												} else {
													children[l].className = app.removeClass(children[l].className, 'current');
													children[l].className = app.removeClass(children[l].className, 'half');
												}
											}

											// Labels austauschen
											children[0].parentNode.parentNode.className = app.addClass(children[0].parentNode.parentNode.className, 'done');
										} catch (e) {
											alert('Fehler: ' + e);
										}
									}
								}
							);
						}
					);
				}
			}
		}
	}

	app.getRequestAsync = function(url, handler)
	{
		if (app.request != null)
			if (app.request.readyState != 4 && app.request.readyState != 0) app.request.abort();
		app.request = app.new_request();

		if (app.request != null)
		{
			app.request.open('GET', url, true);
			app.request.setRequestHeader('Content-Type', 'text/html; charset=ISO-8859-1');
			app.request.onreadystatechange = handler;
			app.request.send(null);
		}
		return(false);
	}

	app.removeClass = function(old, remove)
	{
		var classes;
		var i;
		var f = null;

		if (!old) old = '';
		classes = old.split(' ');
		for (i = 0; i < classes.length; i++)
		{
			if (classes[i] == remove) f = i;
		}
		if (f != null) classes.splice(f, 1);
		return(classes.join(' '));
	}


	app.addClass = function(old, toadd)
	{
		var classes;
		var i;
		var f = null;

		if (!old) old = '';
		classes = old.split(' ');
		for (i = 0; i < classes.length; i++)
		{
			if (classes[i] == toadd) f = i;
		}
		if (f == null) classes.push(toadd);
		return(classes.join(' '));
	}

	app.filterChildNodes = function(nodes, nodeName)
	{
		var treeNodes = new Array();
		var i;

		if (nodes && nodeName)
		{
			for (i = 0; i < nodes.length; i++)
			{
				if (nodes[i].nodeType == 1 && nodes[i].nodeName == nodeName) treeNodes.push(nodes[i]);
			}
		}

		return(treeNodes);
	}
}

