/* A basic frontend user who may use ckEditor for editing
 * Copyright 2003-2011 Johannes Reichardt. All rights reserved.
 * See http://turbopy.com/license for license details.
 * The latest version can be found @ http://turbopy.com/source?file=/tPy/tPj.php
 * @version 0.51 @revision 0 @extension {extensionName} @author johannes.reichardt@gmail.com
 */
if (typeof tPy == 'undefined') {
tPy = {
	/* The mother of all js methods */
	$:function(id) {
		return document.getElementById(id);
	},
	/* Show if hidden and hide if shown and return what we did */
	showHide:function(o, showHide) {
		if (typeof o != 'object') {
			if (!tPy.$(o)) return false;
			o = tPy.$(o);
		}
		if (typeof showHide == 'undefined') {
			if (!o.style.display || o.style.display == 'none' || o.style.display == ' ') var showHide = true;
			else var showHide = false;
		}
		o.style.display = showHide ? 'block' : 'none';
		return showHide;
	},
	/* Wrapper for window.localStorage with a few more options. currently lifeTime is not implemented! */
	storage:function(id, value) {
		if (typeof value == 'undefined') return window.localStorage[id];
		else if (value == null) delete window.localStorage[id];
		else window.localStorage[id] = value;
	},
	/* Templating functions for loading and rendering JS HTML templates */
	template:{
		loaded:[],
		render:function(templateName, content) {
			if (!this.loaded[templateName]) this.loaded[templateName] = this.get(templateName);
			var template = this.loaded[templateName];
			var replaceTemplate = template.text;
			for (var i in template.vars) {
				var replaceValue = eval('content.' + template.vars[i].substring(1, template.vars[i].length - 1));
				if (typeof replaceValue == 'undefined') replaceValue = '';
				replaceTemplate = replaceTemplate.replace(new RegExp(template.vars[i], 'g'), replaceValue);
			}
			return replaceTemplate;
		},
		/* JS HTML templates are stored in script tags like this:
		<script type="tPy/template" id="templateName">contents</script> */
		get:function(name) {
			var template = tPy.$(name).innerHTML;
			var vars = this.uniqueArray(template.match(/{([\$\[ \]a-zA-Z0-9_\.]+)}/g));
			return {'text':template,'vars':vars};
		},
		uniqueArray:function (arr) {
			var uniqueArray = new Array();
			first_loop:
			for (var s = 0; s < arr.length; s++) {
				second_loop:
				for (var t = 0; t < uniqueArray.length; t++ ) if (uniqueArray[t] == arr[s]) continue first_loop;
				uniqueArray[uniqueArray.length] = arr[s];
			}
			return uniqueArray;
		}
	},
	/* Load a single js or css file */
	loadFile:function(path, onload) {
		// Include a .css file
		var addToDom = true;
		if (path.indexOf('.css') !== -1) {
			var s = document.createElement('link');
			s.rel = 'stylesheet';
			s.type = 'text/css';
			s.href = path;
		} else if (path.indexOf('.html') !== -1) {
			this.getFile(path, onload, true);
			var s = {};
			addToDom = false;
			// alert(s.name);
		// Include a .js file
		} else {
			var s = document.createElement('script');
			s.type = 'text/javascript';
			s.src = path;
		}
		s.id = path;
		if (addToDom) {
			if (onload) s.onload = onload;
			document.body.appendChild(s);
		}
	},
	/* Dynamically and asynchronous loading of js and css files
	   Usage: tPy.loadFiles('/path/to/use/|js1|js2|/another/path/|js3|js4|css1.css')
	   Note that directories are assumed to start with / after that all following files WITHOUT extension will be loaded
	   automatically with .js suffix */
	jsIncludes:[],
	loadFiles:function(files) {
		this.jsIncludes = [];
		var jsIncludes = files.split('|');
		var basePath = '/';
		for (var i = 0, file; file = jsIncludes[i]; i++) {
			if (file.charAt(0) == '/') {
				basePath = file;
			} else {
				if (file.split('.').pop() == file) {
					file+= '.js';
					this.jsIncludes[basePath + file] = 0;
				} else this.loadFile(basePath + file);
			}
		}
		/* Load all js includes and attach an onload function that determines if all files have been loaded  */
		for (var f in this.jsIncludes) {
			this.loadFile(f, function(ret) {
				tPy.jsIncludes[this.id] = 1;
				// Check if all jsIncludes are loaded
				for (var f in tPy.jsIncludes) if (tPy.jsIncludes[f] === 0) return;
				tPy.onload();
			});
		}
	},
	onloadFunctions:[],
	onload:function(f) {


		if (tPy.onloadHasRun) {
			if (typeof f == 'function')
				f.call();
		} else {

			if (typeof f == 'function')
				tPy.onloadFunctions.push(f);

			if (tPy.jsIncludes.length);
				for (var f in tPy.jsIncludes) if (tPy.jsIncludes[f] === 0) return;

			/* If so check if the DOM is ready */
			if (document.readyState == 'complete' || document.readyState == 'loaded' || document.readyState == 'interactive' || document.readyState > 3) {

				// document.write('loaded');
				tPy.onloadHasRun = true;
				for (var f in tPy.onloadFunctions) {
					try {
						tPy.onloadFunctions[f].call();
					} catch (e) {
						setTimeout(function(){tPy.onloadFunctions[f].call();}, 50);
					}
				}
			} else if (!tPy.onloadHandlerAttached) {
				tPy.onloadHandlerAttached = true;
				tPy.addEvent('DOMContentLoaded', tPy.onload, false);
			}
		}
	},
	/* Clone an object instead of referencing it */
	clone:function(o) {
		var n = {};for (var i in o) n[i] = o[i];return n;
	},
	/* tPy ajax bridge for calling methods within classes.
	   Usage: tPy.ajax('myPHPClass::method')([arguments,] jsCallback);
	   the method must be ajax enabled within the class with the method tPy_conf
	   See www.turbopy.org/wiki/ajax for more information
	*/
	getFile:function(url, onload, add) {
		var hr = new XMLHttpRequest();
		if (onload)
			hr.callBack = onload;
		if (add)
			hr.add = true;
		hr.onreadystatechange = function() {
			if (this.readyState == 4) {
				if (this.add) {
					var container = document.createElement('div');
					container.style.display = 'none';
					container.innerHTML = this.responseText;
					document.body.appendChild(container);
				}
				if (this.callBack)
					this.callBack.call(this, this.responseText);
			}
		};
		hr.open('GET', url, true);
		hr.setRequestHeader('Content-type', 'charset=UTF-8');
		hr.send();
		return hr;
	},
	ajax:function(classMethod) {
		return function() {
			var hr = new XMLHttpRequest();
			hr.startTime = new Date().getTime();
			hr.originalArgs = arguments;
			var args = Array.prototype.slice.call(arguments);
			var lastArg = args.length ? args.length-1 : 0;
			if (typeof args[lastArg] == 'function') hr.callBack = args.pop();
			args = 'args=' + encodeURIComponent(JSON.stringify(args));
			hr.onreadystatechange = function() {
				if (this.readyState == 4) {

					if (!this.responseText) return;

					try {
						var ret = JSON.parse(this.responseText);
					} catch(e) {
						if (this.responseText == 'tPy ajax token missmatch') {
							tPy.loadFile('/tPy/tPj.php?' + Math.random(1), function() {
								tPy.ajax(classMethod).apply(this, hr.originalArgs);
							});
						} else {
							var f = typeof this.callBack == 'function' ? this.callBack.toString() : this.callBack;
							alert('AJAX FAILURE: ' + this.responseText + "\n" + f);
						}
					}
					if (this.callBack) this.callBack.call(this, ret);
					if (tPy.$('tPy_ajaxTime')) {
						tPy.$('tPy_ajaxTime').innerHTML = new Date().getTime() - this.startTime;
						tPy.$('tPy_ajaxTime').title = (this.callBack ? this.callBack.toString() : '') +
						this.responseText;
					}
				}
			};
			hr.open('POST','/tPy/tPy.php?tPy_classMethod=' + classMethod, true);
			hr.setRequestHeader('X-Requested-With', tPy.id);
			hr.setRequestHeader('Content-type', 'application/x-www-form-urlencoded;charset=UTF-8');
			hr.send(args);
		}
	},
	form:{
		/* Handy function to dynamically add and set values in the form */
		setValue:function(formField, value) {
			var formField = formField.split('.');
			if (tPy.$(formField[1])) {
				tPy.$(formField[1]).value = value;
				return;
			}
			var f = document.createElement('input');
			f.type = 'hidden';
			f.name = formField[1];
			f.id = formField[1];
			f.value = value;
			tPy.$(formField[0]).appendChild(f);
		},
		ajaxSubmit:function(form, target, callback) {
			var values = tPy.form.getValues(form);
			tPy.ajax(target)(values, function(r) {
				if (r && r.error) {
					alert(r.error);
				} else if (typeof callback != 'undefined') callback.call(this, r);
			});
			return false;
        	},
		getValues:function(form) {
			var values = {};
			if (typeof form == 'string')
				form = document.forms[form] ? document.forms[form] : tPy.$(form);
			for (var e in form.elements) {
				if (form.elements[e].name) {
					var value = this.getValue(form.elements[e]);
					if (typeof value != 'undefined' && value != '')
						values[form.elements[e].name] = value;
				}
			}
			return values;
		},
		getValue:function(element, retMode) {
			if (typeof element == 'string') {
				var fF = element.split('.');
				element = document.forms[fF[0]].elements[fF[1]];
			}
			if (element.length) {
				var selected = (retMode == 'all') ? {} : [];
				for (var i in element) {
					var isSelected = (element[i].checked || element[i].selected) ? true : false;
					if (retMode == 'all')
						selected[element[i].name] = isSelected;
					else if (retMode == 'onlyUnselected' && !isSelected && element[i].value)
						selected.push(element[i].value);
					else if ((!retMode || retMode == 'onlySelected') && isSelected)
						selected.push(element[i].value);
				}
				return selected;
			}
			return element.value;
		}
	},
	addEvent:function( obj, type, fn ) {
	  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]);
	  } else
		obj.addEventListener( type, fn, false );
	},
	removeEvent:function( obj, type, fn ) {
	  if (obj.detachEvent) {
		obj.detachEvent( 'on'+type, obj[type+fn] );
		obj[type+fn] = null;
	  } else
		obj.removeEventListener( type, fn, false );
	}
};
}
tPy.id = '
Notice: Undefined index: HTTP_REFERER in /home/www/neocube.de/tPy/tPj.php on line 311
64e6eb2156017a181d16d6376453d933';

