﻿var typonine = {
	
	_container:			null,
	_holder:			'typonine_holder',
	_image_holder:		'typonine_image_holder',
	_form_holder:		'typonine_form_holder',
	_default_text:		'Type text here... test fonts, test features.',
	_config:			null,
	_active_layouts:	[],
	_active_styles:		[],
	_script:			'http://lettersetter.net/cgi-bin/ls?foundry=typonine',
	
	init: function(el, layout) {
		if (typonine._string(el) == '') {
			return;
		}
		if (!typonine._undefined(layout)) {
			for (var i = 0; i < typonine._config.layout_count; i++) {
				typonine._active_layouts.push(layout);
			}
		}
		typonine._container = el;
		typonine._add_event(window, 'onload', typonine.check_container);
	},
	
	check_container: function() {
		var _container = typonine._container = document.getElementById(typonine._container);
		if (_container == null) {
			return;
		}
		if (!typonine._number(_container.childNodes.length)) {
			return;
		}
		if (_container.childNodes.length > 0) {
			var cns = [];
			for (var i = 0; i < _container.childNodes.length; i++) {
				var cn = _container.childNodes[i];
				if (cn.nodeType == 3) {
					var ccn = cn.cloneNode(true);
					var s = document.createElement('span');
						s.appendChild(ccn);
					cns.push(s);
				} else {
					cns.push(cn);
				}
			}
			_container.innerHTML = '';
			for (var i = 0; i < cns.length; i++) {
				var cn = _container.appendChild(cns[i]);
				if (typonine._object(cn.style)) {
					cn.style.display = 'none';
				}
			}
		}
		var _holder = typonine._holder = document.createElement('div');
			_holder.setAttribute('id', 'typonine_holder');
		_container.insertBefore(_holder, _container.firstChild);
		
		typonine.set_application();
	},
	
	set_application: function() {
		
		var txt = typonine._image_holder;
		var image_holder = typonine._image_holder = document.createElement('div');
			image_holder.setAttribute('id', txt);
		typonine._holder.appendChild(image_holder);
		
		var txt = typonine._form_holder;
		var form_holder = typonine._form_holder = document.createElement('div');
			form_holder.setAttribute('id', txt);
		typonine._holder.appendChild(form_holder);
		
		typonine._images = [];
		typonine._forms = [];
		typonine._sizes = [];
		typonine._queries = [];
		if (typonine._active_layouts.length == 0) {
			typonine._active_layouts = typonine._config.layout_default_fonts;
		}
		typonine._active_styles = typonine._config.layout_default_styles;
		
		for (var i = 0; i < typonine._config.layout_count; i++) {
			typonine.update_application(i);
		}
		
	},
	
	update_application: function(i) {
		// check i
		if (typonine._number(i) === false || i > typonine._config.layout_count - 1) {
			return;
		}
		
		// query array
		if (!typonine._object(typonine._queries[i])) {
			typonine._queries.push({});
		}
		typonine._queries[i]['features'] = {};
		typonine._queries[i]['style'] = undefined;
		
		// check active layouts
		if (typonine._number(typonine._active_layouts[i]) === false || typonine._active_layouts[i] > typonine._config.layouts.length - 1) {
			typonine._active_layouts[i] = 0;
		}
		if (typonine._undefined(typonine._sizes[i])) {
			typonine._sizes[i] = typonine._config.layouts[typonine._active_layouts[i]].size;
		}
		var active_layout = typonine._active_layouts[i];
		typonine._queries[i]['font'] = typonine._config.layouts[typonine._active_layouts[i]].id; //--
		
		// check active styles
		var tmp_style_valid = true;
		if (typonine._undefined(typonine._active_styles[i])) {
			tmp_style_valid = false;
		} else {
			var tmp_obj = [];
			for (var j in typonine._config.feature_groups.layout_styles) {
				tmp_obj.push(j);
			}
			if (typonine._active_styles[i].indexOf('|') != -1) {
				var tmp_style = typonine._active_styles[i].split('|');
			} else {
				var tmp_style = [typonine._active_styles[i]];
			}
			if (typonine._string(typonine._active_styles[i]) == '') {
				tmp_style_valid = false;
			}
			for (var j = 0; j < tmp_style.length; j++) {
				if (String('|' + tmp_obj.join('|') + '|').indexOf('|' + tmp_style[j] + '|') == -1) {
					tmp_style_valid = false;
				}
			}
		}
		if (!tmp_style_valid) {
			var primary_styles = '|' + typonine._config.layout_primary_style.join('|') + '|';
			for (var j in typonine._config.layouts[typonine._active_layouts[i]].styles) {
				//if (j == typonine._config.layout_primary_style) {
				if (primary_styles.indexOf('|' + j + '|') != -1) {
					typonine._active_styles[i] = j;
					tmp_style_valid = true;
					break;
				}
			}
			if (!tmp_style_valid) {
				for (var j in typonine._config.layouts[typonine._active_layouts[i]].styles) {
					typonine._active_styles[i] = j;
					break;
				}
			}
		}
		var active_style = typonine._active_styles[i];
		typonine._queries[i]['style'] = active_style; //--
		
		// check features
		var features_obj = typonine._config.layouts[active_layout].features;
		var features = [];
		var feature_groups = {};
		
		for (var j in features_obj) {
			features.push(j);
		}
		
		for (var j in typonine._config.feature_groups) {
			feature_groups[j] = {};
			for (var k in typonine._config.feature_groups[j]) {
				if (String('_' + features.join('_') + '_').indexOf(String('_' + k + '_')) != -1) {
					feature_groups[j][k] = typonine._config.feature_groups[j][k];
				}
			}
		}
		
		// last child class
		var last_child_class = i == typonine._config.layout_count - 1 ? ' last_child' : '';
		
		// image div
		if (typonine._object(typonine._images[i])) {
			var image = typonine._images[i];
		} else {
			var image = typonine.get_div('image_' + i, 'image' + last_child_class);
			typonine._images.push(image);
			typonine._image_holder.appendChild(image);
		}
		
		// form div
		if (typonine._object(typonine._forms[i])) {
			var form = typonine._forms[i];
				form.innerHTML = '';
		} else {
			var form = typonine.get_div('form_' + i, 'form' + last_child_class);
			typonine._forms.push(form);
			typonine._form_holder.appendChild(form);
		}
		
		// form - selector div
		var selector = typonine.get_div('', 'selector' + last_child_class);
		form.appendChild(selector);
		
		// form - selector - text input
		var tmp_text = typonine._queries[i]['text'];
		tmp_text = typonine._undefined(tmp_text) ? typonine._default_text : tmp_text;
		var text_input = typonine.get_input_text('', 'text_input_' + i, tmp_text, typonine.update_input_text);
		selector.appendChild(text_input);
		typonine._queries[i]['text'] = text_input.value; //--
		
		// form - selector - font name dropdown
		var font_name = typonine.get_select('', 'font_name_' + i, typonine.update_select);
		for (var j = 0; j < typonine._config.layouts.length; j++) {
			var tmp_obj = typonine._config.layouts[j];
			var tmp_selected = active_layout == j ? true : false;
			var tmp_el = typonine.get_option(tmp_obj.name, tmp_obj.id, tmp_selected);
			font_name.appendChild(tmp_el);
		}
		selector.appendChild(font_name);
		
		// form - selector - font style dropdown
		var font_style = typonine.get_select('', 'font_style_' + i, typonine.update_select);
		for (var j in typonine._config.layouts[active_layout].styles) {
			var name = '';
			var id = '';
			
			if (j.indexOf('|') != -1) {
				var tmp_id = j.split('|');
			} else {
				var tmp_id = [j];
			}
			for (var k = 0; k < tmp_id.length; k++) {
				var tmp_name = typonine._config.feature_groups.layout_styles[tmp_id[k]];
				if (typonine._undefined(tmp_name)) {
					continue;
				}
				name += (name == '' ? '' : ' ') + tmp_name;
				id += (id == '' ? '' : '_') + tmp_id[k];
			}
			var tmp_selected = active_style == j ? true : false;
			var tmp_el = typonine.get_option(name, id, tmp_selected);
			font_style.appendChild(tmp_el);
		}
		if (font_style.childNodes.length > 0) {
			selector.appendChild(font_style);
		}
		
		// form - textbox div
		var textbox = typonine.get_div('', 'textbox' + last_child_class);
		
		// form - textbox - opentype fieldset
		var opentype_fs = typonine.get_fieldset('', '' + last_child_class, 'OpenType');
		var opentype_fs_div = typonine.get_div('', 'fieldset_div');
		
		// form - textbox - opentype fieldset - checkboxes
		for (var j in feature_groups.opentype_checkboxes) {
			var disabled = false;
			if (j == 'kern') {
				disabled = true;
			}
			var tmp_el = typonine.get_checkbox('', 'opentype_checkbox_' + i + '_' + j.split('|').join('_'), j, typonine._config.layouts[active_layout].features[j], disabled, typonine.update_checkbox);
			var tmp_el_label = typonine.get_label('opentype_checkbox_' + i + '_' + j.split('|').join('_'), typonine._config.feature_groups.opentype_checkboxes[j], tmp_el, true);
			opentype_fs_div.appendChild(tmp_el_label);
			if (j.indexOf('|') != -1) {
				var tmp_j = j.split('|');
				for (var k = 0; k < tmp_j.length; k++) {
					if (!typonine._undefined(typonine._queries[i]['features'][tmp_j[k]]) || typonine._undefined(typonine._config.feature_groups.opentype_checkboxes[tmp_j[k]])) {
						if (typonine._queries[i]['features'][tmp_j[k]] || typonine._config.layouts[active_layout].features[k]) {
							typonine._queries[i]['features'][tmp_j[k]] = 1;
						} else {
							typonine._queries[i]['features'][tmp_j[k]] = 0;
						}
					} else {
						typonine._queries[i]['features'][tmp_j[k]] = typonine._config.layouts[active_layout].features[k]; //--
					}
				}
			} else {
				typonine._queries[i]['features'][j] = typonine._config.layouts[active_layout].features[j]; //--
			}
		}
		if (opentype_fs_div.childNodes.length > 1) {
			opentype_fs.appendChild(opentype_fs_div);
			textbox.appendChild(opentype_fs);
		}
		
		// form - textbox - numeral fieldset
		var numeral_fs = typonine.get_fieldset('', '' + last_child_class, 'Extras');
		var numeral_fs_div = typonine.get_div('', 'fieldset_div');
		
		// form - textbox - numeral fieldset - dropdown
		var numeral_dd = typonine.get_select('', 'numeral_dropdown_' + i, typonine.update_select);
		var tmp_el = typonine.get_option('Numerals', '');
			numeral_dd.appendChild(tmp_el);
		for (var j in feature_groups.numeral_dropdown) {
			var tmp_selected = typonine._config.layouts[active_layout].features[j];
			var tmp_el = typonine.get_option(typonine._config.feature_groups.numeral_dropdown[j], j, tmp_selected);
			numeral_dd.appendChild(tmp_el);
			typonine._queries[i]['features'][j] = typonine._config.layouts[active_layout].features[j]; //--
		}
		if (numeral_dd.childNodes.length > 0) {
			numeral_fs_div.appendChild(numeral_dd);
		}
		
		// form - textbox - numeral fieldset - dropdown denominators
		var numeral_ot = typonine.get_select('', 'numtype_dropdown_' + i, typonine.update_select);
		var tmp_el = typonine.get_option('Extra Numerals', '');
			numeral_ot.appendChild(tmp_el);
		for (var j in feature_groups.numtype_dropdown) {
			var tmp_selected = typonine._config.layouts[active_layout].features[j];
			var tmp_el = typonine.get_option(typonine._config.feature_groups.numtype_dropdown[j], j, tmp_selected);
			numeral_ot.appendChild(tmp_el);
			typonine._queries[i]['features'][j] = typonine._config.layouts[active_layout].features[j]; //--
		}
		if (numeral_ot.childNodes.length > 0) {
			numeral_fs_div.appendChild(numeral_ot);
		}
		
	// form - selector - stylistic set dropdown
		if (typonine._object(typonine._config.layouts[active_layout].styles[active_style])) {
			var stylistic_set = typonine.get_select('', 'stylistic_set_' + i, typonine.update_select);
			//var tmp_el = typonine.get_option('Stylistic Sets', '');
			//	stylistic_set.appendChild(tmp_el);
			for (var j = 0; j < typonine._config.layouts[active_layout].styles[active_style].length; j++) {
				var id = typonine._config.layouts[active_layout].styles[active_style][j];
				var name = typonine._config.feature_groups.stylistic_sets[id];
				if (typonine._undefined(name)) {
					continue;
				}
				var tmp_el = typonine.get_option(name, id);
				stylistic_set.appendChild(tmp_el);
				typonine._queries[i]['features'][id] = false; //--
			}
			if (stylistic_set.childNodes.length > 0) {
				numeral_fs_div.appendChild(stylistic_set);
			}
		}
		
		// form - textbox - numeral fieldset - checkboxes
		for (var j in feature_groups.numeral_checkboxes) {
			var disabled = false;
			var tmp_el = typonine.get_checkbox('', 'numeral_checkbox_' + i + '_' + j.split('|').join('_'), j, typonine._config.layouts[active_layout].features[j], disabled, typonine.update_checkbox);
			var tmp_el_label = typonine.get_label('numeral_checkbox_' + i + '_' + j.split('|').join('_'), typonine._config.feature_groups.numeral_checkboxes[j], tmp_el, true);
			numeral_fs_div.appendChild(tmp_el_label);
			if (j.indexOf('|') != -1) {
				var tmp_j = j.split('|');
				for (var k = 0; k < tmp_j.length; k++) {
					if (!typonine._undefined(typonine._queries[i]['features'][tmp_j[k]]) || typonine._undefined(typonine._config.feature_groups.numeral_checkboxes[tmp_j[k]])) {
						if (typonine._queries[i]['features'][tmp_j[k]] || typonine._config.layouts[active_layout].features[k]) {
							typonine._queries[i]['features'][tmp_j[k]] = 1;
						} else {
							typonine._queries[i]['features'][tmp_j[k]] = 0;
						}
					} else {
						typonine._queries[i]['features'][tmp_j[k]] = typonine._config.layouts[active_layout].features[k]; //--
					}
				}
			} else {
				typonine._queries[i]['features'][j] = typonine._config.layouts[active_layout].features[j]; //--
			}
			
		}
		
		if (numeral_fs_div.childNodes.length > 1) {
			numeral_fs.appendChild(numeral_fs_div);
			textbox.appendChild(numeral_fs);
		}
		
		if (textbox.childNodes.length > 0) {
			form.appendChild(textbox);
		}
		
		typonine.update_query(i);
		
	},
	
	update_query: function(i) {
		var tmp_query = typonine._queries[i];
		var path = typonine._script;
			path += '&layout=' + tmp_query.font;
		if (tmp_query.style.indexOf('|') == -1) {
			var tmp_style = [tmp_query.style];
		} else {
			var tmp_style = tmp_query.style.split('|');
		}
		for (var j = 0; j < tmp_style.length; j++) {
			path += '_' + typonine._capitalize(tmp_style[j]);
		}
			path += '&tb1=' + tmp_query.text;
		
		for (var j in tmp_query.features) {
			path += '&tf1=' + j + '.' + typonine._binary(tmp_query.features[j]);
		}
		typonine.update_image(i, path);
		
	},
	
	img_loading: function(img, path) {
		if (img.src == path) {
			return;
		} else {
			alert(img.src + ' ' + path);
			setTimeout("typonine.img_loading(img, path)", 1000);
		}
	},
	
	update_image: function(i, path) {
		
		var loading_holder = typonine._images[i].lastChild;
		if (loading_holder != null && loading_holder.nodeName.toLowerCase() == 'div') {
			var par = loading_holder.parentNode;
				par.removeChild(loading_holder);
		}
		var loading_holder = typonine.get_div('', 'loader');
			loading_holder.appendChild(document.createTextNode('LOADING'));
			loading_holder.style.width = typonine._sizes[i][0] - 20;
			typonine._images[i].appendChild(loading_holder);
		
		var img = new Image();
			img.onload = function() {
				var size = typonine._config.layouts[typonine._active_layouts[i]].size;
				typonine._images[i].style.width = size[0];
				typonine._images[i].style.height = size[1];
				typonine._sizes[i] = size;
				typonine._images[i].innerHTML = '';
				typonine._images[i].appendChild(img);
			}
			
			img.src = path;
	},
	
	update_input_text: function() {
		var i = typonine.get_i(this);
		typonine._queries[i]['text'] = this.value == '' ? typonine._default_text : this.value;
		typonine.update_query(i);
	},
	
	update_checkbox: function() {
		var i = typonine.get_i(this);
		var p = this.parentNode;
		while(p.nodeName.toLowerCase() != 'fieldset') {
			p = p.parentNode;
			if (p.nodeName.toLowerCase() == 'body') {
				break;
			}
		}
		var cbs = p.getElementsByTagName('input');
		
		var value = this.value;
		if (value.indexOf('|') != -1) {
			value = value.split('|');
			for (var j = 0; j < value.length; j++) {
				var neighbour = null;
				for (var k = 0; k < cbs.length; k++) {
					if (cbs[k].value == value[j]) {
						neighbour = cbs[k];
						break;
					}
				}
				if (neighbour != null) {
					if (this.checked || neighbour.checked) {
						typonine._queries[i].features[value[j]] = 1;
					} else {
						typonine._queries[i].features[value[j]] = 1;
					}
				} else {
					if (this.checked) {
						typonine._queries[i].features[value[j]] = 1;
					} else {
						typonine._queries[i].features[value[j]] = 0;
					}
				}
			}
		} else {
			var tmp_checked = 0;
			for (var j = 0; j < cbs.length; j++) {
				if (cbs[j].value.indexOf(value) != -1 && cbs[j] != this) {
					tmp_checked = cbs[j].checked ? 1 : tmp_checked;
				}
			}
			if (this.checked || tmp_checked) {
				typonine._queries[i].features[value] = 1;
			} else {
				typonine._queries[i].features[value] = 0;
			}
		}
		
		typonine.update_query(i);
	},
	
	update_select: function() {
		var i = typonine.get_i(this);
		var items = {};
		
		for (var j = 0; j < this.childNodes.length; j++) {
			var option = this.childNodes[j];
			var value = option.value;
			if (value == '') {
				continue;
			}
			if (value.indexOf('|') != -1) {
				value = value.split('|');
				for (var k = 0; k < value.length; k++) {
					for (var l = 0; l < this.childNodes.length; l++) {
						if (value[k] == this.childNodes[l].value) {
							if (j == this.selectedIndex || l == this.selectedIndex) {
								items[value[k]] = 1;
							} else {
								items[value[k]] = 0;
							}
						}
					}
				}
			} else {
				if (j == this.selectedIndex) {
					items[value] = 1;
				} else {
					items[value] = 0;
				}
			}
		}
		
		if (this.name.indexOf('numeral_dropdown') != -1 || this.name.indexOf('stylistic_set') != -1 || this.name.indexOf('numtype_dropdown') != -1) {
			for (var j in items) {
				typonine._queries[i].features[j] = items[j];
			}
		} else if (this.name.indexOf('font_name') != -1) {
			var cnt = 0;
			for (var j in items) {
				if (items[j] == 1) {
					typonine._queries[i].font = j;
					typonine._active_layouts[i] = cnt;
					typonine._active_styles[i] = undefined;
					/*
					for (var k in typonine._config.layouts[cnt].styles) {
						typonine._active_styles[i] = k;
						break;
					}
					*/
					typonine.update_application(i);
					return;
				}
				cnt++;
			}
		} else if (this.name.indexOf('font_style') != -1) {
			for (var j in items) {
				if (items[j] == 1) {
					typonine._queries[i].style = j.split('_').join('|');
					typonine._active_styles[i] = typonine._queries[i].style;
					typonine.update_stylistic_set(i);
					break;
				}
			}
		}
		
		typonine.update_query(i);
	},
	
	update_stylistic_set: function(i) {
		
		var form = typonine._forms[i];
		var selects = form.getElementsByTagName('select');
		var select;
		for (var j = 0; j < selects.length; j++) {
			if (selects[j].name.indexOf('stylistic_set') != -1) {
				select = selects[j];
				break;
			}
		}
		if (typonine._undefined(select)) {
			return;
		}
		
		for (var j in typonine._config.feature_groups.stylistic_sets) {
			if (!typonine._undefined(typonine._queries[i].features[j])) {
				typonine._queries[i].features[j] = undefined;
			}
		}
		
		if (typonine._object(typonine._config.layouts[typonine._active_layouts[i]].styles[typonine._active_styles[i]])) {
			var stylistic_set = typonine.get_select('', 'stylistic_set_' + i, typonine.update_select);
			//var tmp_el = typonine.get_option('Stylistic Sets', '');
			//	stylistic_set.appendChild(tmp_el);
			for (var j = 0; j < typonine._config.layouts[typonine._active_layouts[i]].styles[typonine._active_styles[i]].length; j++) {
				var id = typonine._config.layouts[typonine._active_layouts[i]].styles[typonine._active_styles[i]][j];
				var name = typonine._config.feature_groups.stylistic_sets[id];
				if (typonine._undefined(name)) {
					continue;
				}
				var tmp_el = typonine.get_option(name, id);
				stylistic_set.appendChild(tmp_el);
				typonine._queries[i]['features'][id] = false; //--
			}
			if (stylistic_set.childNodes.length > 0) {
				select.parentNode.replaceChild(stylistic_set, select);
			}
		}
		
		typonine.update_query(i);
	},
	
	get_i: function(el) {
		while(el.id.indexOf('form') == -1) {
			el = el.parentNode;
			if (el.nodeName.toLowerCase() == 'body') {
				return null;
			}
		}
		return Number(el.id.split('_')[1]);
	},
	
	get_div: function(id, c) {
		id = typonine._string(id);
		c = typonine._string(c);
		var el = document.createElement('div');
			el.setAttribute('id', id);
		if (c != '') {
			el.setAttribute('class', c);
			el.setAttribute('className', c);
		}
		return el;
	},
	
	get_checkbox: function(id, name, value, checked, disabled, onchange_function) {
		name = typonine._string(name);
		id = typonine._string(id);
		value = typonine._defined(value);
		checked = typonine._boolean(checked);
		disabled = typonine._boolean(disabled);
		onchange_function = typonine._function(onchange_function);
		var el = document.createElement('input');
			el.setAttribute('name', name);
			el.setAttribute('id', id);
			el.setAttribute('type', 'checkbox');
			el.setAttribute('class', 'checkbox_input');
			el.setAttribute('className', 'checkbox_input');
			el.setAttribute('value', value);
		if (checked) {
			el.setAttribute('defaultChecked', 'defaultChecked');
			el.setAttribute('checked', 'checked');
		}
		if (disabled) {
			el.setAttribute('disabled', 'disabled');
		}
		if (onchange_function) {
			el.onclick = onchange_function;
		}
		return el;
	},
	
	get_fieldset: function(id, c, text) {
		id = typonine._string(id);
		text = typonine._string(text);
		var el2 = document.createElement('fieldset');
			el2.setAttribute('id', id);
		if (text != '') {
			var txt = document.createTextNode(text);
			var el1 = document.createElement('legend');
				el1.appendChild(txt);
			el2.appendChild(el1);
		}
		return el2;
	},
	
	get_label: function(label_for, text, input_el, after) {
		label_for = typonine._string(label_for);
		var i_el = typonine._object(input_el);
		after = typonine._boolean(after);
		text = typonine._string(text);
		var txt = document.createTextNode(text);
		var el = document.createElement('label');
		if (i_el && after) {
			el.appendChild(input_el);
		}
		if (!i_el) {
			el.setAttribute('htmlFor', label_for);
			el.setAttribute('for', label_for);
		}
			el.appendChild(txt);
		if (i_el && !after) {
			el.appendChild(input_el);
		}
		return el;
	},
	
	get_select: function(id, name, onchange_function) {
		name = typonine._string(name);
		id = typonine._string(id);
		onchange_function = typonine._function(onchange_function);
		var el = document.createElement('select');
			el.setAttribute('name', name);
			el.setAttribute('id', id);
		if (onchange_function) {
			el.onchange = onchange_function;
		}
		return el;
	},
	
	get_option: function(text, value, selected) {
		value = typonine._defined(value);
		text = typonine._string(text);
		selected = typonine._boolean(selected);
		var txt = document.createTextNode(text);
		var el = document.createElement('option');
			el.setAttribute('value', value);
		if (selected) {
			el.setAttribute('selected', 'selected');
		}
			el.appendChild(txt);
		return el;
	},
	
	get_input_text: function(id, name, value, onchange_function) {
		id = typonine._string(id);
		name = typonine._string(name);
		value = typonine._string(value);
		onchange_function = typonine._function(onchange_function);
		var el = document.createElement('input');
			el.setAttribute('name', name);
			el.setAttribute('id', id);
			el.setAttribute('value', value);
			el.setAttribute('type', 'text');
			el.setAttribute('class', 'text_input');
			el.setAttribute('className', 'text_input');
			
			el.onfocus = function() {
				if (this.value == typonine._default_text) {
					this.value = '';
				}
			}
			el.onblur = function() {
				if (this.value == '') {
					this.value = typonine._default_text;
				}
			}
		if (onchange_function) {
			el.onchange = onchange_function;
		}
		if (el.defaultValue == '') {
			el.defaultValue = typonine._default_text;
		}
		return el;
	},
	
	_string: function(x) {
		return (typeof x != 'string' ? '' : x);
	},
	_number: function(x) {
		return (typeof x != 'number' ? false : x);
	},
	_defined: function(x) {
		return (typeof x == 'undefined' ? '' : x);
	},
	_undefined: function(x) {
		return (typeof x == 'undefined' ? true : false);
	},
	_boolean: function(x) {
		return (typeof x != 'boolean' ? false : x);
	},
	_function: function(x) {
		return (typeof x != 'function' ? false : x);
	},
	_binary: function(x) {
		return (x == true ? '1' : '0');
	}
	,
	_object: function(x) {
		return (typeof x != 'object' ? false : true);
	},
	_add_event: function(el, ev, fn) {
		var p_ev = el[ev];
		if (!typonine._function(p_ev)) {
			el[ev] = fn;
		} else {
			el[ev] = function(e) {
				p_ev(e);
				fn(e);
			}
		}
	},
	_capitalize: function(x) {
		return x.charAt(0).toUpperCase() + x.substr(1).toLowerCase();
	}
	
};