/* 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, templateText) {
			if (!this.loaded[templateName] || templateText)
				this.loaded[templateName] = templateText ? this.get(templateName, templateText) : this.get(templateName);
			var template = this.loaded[templateName];
			var replaceTemplate = template.text;
			for (var i in template.vars) {
				var placeHolder = template.vars[i];
				var valueName = placeHolder.replace('}', '').replace('{', '').replace('[', '.').replace(']', '');
				var valueParts = valueName.split('.');
				// We replace the placeholder [] in order to create a valid regular expression later
				if (valueParts.length > 1)
					placeHolder = placeHolder.replace('[', '\\[').replace(']', '\\]');
				try {
					switch(valueParts.length) {
						case 1: var replaceValue = content[valueParts[0]];break;
						case 2: var replaceValue = content[valueParts[0]][valueParts[1]];break;
						case 3: var replaceValue = content[valueParts[0]][valueParts[1]][valueParts[2]];break
					}
				} catch (e) {}
				if (!replaceValue) var replaceValue = '';
				replaceTemplate = replaceTemplate.replace(new RegExp(placeHolder, '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, template) {
			if (!template)
				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
		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) {
			var s = document.createElement('iframe');
			s.style.display = 'none';
			s.src = path;
		} else {
			var s = document.createElement('script');
			s.type = 'text/javascript';
			s.src = path;
		}
		s.id = path;
		if (onload) {
			tPy.addEvent(s, 'load', onload);
			s.onreadystatechange = 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 */
	includes:{},
	loadFiles:function(files) {
		var includes = files.split('|');
		var basePath = '';
		for (var i = 0, file; file = includes[i]; i++) {
			// Check if we have a path here, if so set the basepath to this
			if (file.charAt(0) == '/') {
				if (file.charAt(file.length-1) !== '/') {
					this.includes[file] = 0;
					this.loadFile(file);
				} else
					basePath = file;
			// Check if we have a .html file here
			} else if (file.indexOf('.html') !== -1) {
				this.includes[basePath + file] = 0;
			} else if (file.split('.').pop() == file) {
				file+= '.js';
				this.includes[basePath + file] = 0;
			} else this.loadFile(basePath + file);
		}
		
		// setTimeout("tPyA.debug(tPy.includes)", 500);
		// Load all js includes and attach an onload function that determines if all files have been loaded
		for (var path in this.includes) {
			this.loadFile(path, function() {
				if (!tPy.includes[this.id] && (!this.readyState || this.readyState === 'loaded' || this.readyState === 'complete')) {
					this.onload = this.onreadystatechange = null;
					tPy.includes[this.id] = 1;
					tPy.runOnloadFunctions();		
				}
			});
		 }
	},
	onloadFunctions:[],
	runOnloadFunctions:function() {
		if (tPy.onloadHasRun) return;
		// Loop through all file includes - if any of them is not loaded return
		for (var i in tPy.includes) {
			if (tPy.includes[i] === 0) {
				tPy.onloadTimeout = setTimeout("tPy.runOnloadFunctions()", 0);
				return;
			}
		}
		// Check if the document is ready
		if (!document.readyState || document.readyState === 'complete' || document.readyState === 'interactive' || document.readyState === 'loaded') {
		} else {
			tPy.onloadTimeout = setTimeout("tPy.runOnloadFunctions()", 0);
			return;
		}
		if (tPy.onloadTimeout) clearTimeout(tPy.onloadTimeout);
		// All checks passed, let´s run the onload functions
		for (var f in tPy.onloadFunctions)
				tPy.onloadFunctions[f].call(window, this);
		// Set the flat so we dont add functions to the queue anymore
		tPy.onloadHasRun = true;
	},
	onload:function(fn) {
		tPy.onloadHasRun ? fn.call(window, this) : tPy.onloadFunctions.push(fn);
	},
	/* 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
	*/
	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') {
				alert(form);
				form = document.forms[form] ? document.forms[form] : tPy.$(form);
			}
			for (var e in form.elements) {
				try {
					if (typeof form.elements[e] != 'undefined' && form.elements[e].name) {
						var value = this.getValue(form.elements[e]);
						if (typeof value != 'undefined' && value != '')
							values[form.elements[e].name] = value;
					}
				} catch (e) {}
			}
			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/tPy_master/v1.0/tPy/tPj.php on line 305
767aeee5f830a775e7f07958c4e4a973';

