/* ========================================
* The A.D.E. - theade.js
* Author: http://www.webkitchen.cz/
========================================== */

var d = document;

d.documentElement.className += 'js-on';

function init() {
	if (d.getElementById && d.getElementsByTagName && d.createElement) {
		createPrintLink();
		pisne.init();
		checkFormsInit();

		if (d.getElementById('policko')) {
			d.getElementById('policko').value = 2;
		}
	}
}


function createPrintLink() {
	appendLink(createJsLink(window.print,'Vytisknout', 'Stránka je připravena pro tisk'), 'toTop', true)
}

function createJsLink(akce, txt, tit) {
	var newLink = d.createElement('a');
	newLink.setAttribute('href','#');
	newLink.setAttribute('title', tit);
	newLink.appendChild(d.createTextNode(txt));
	newLink.onclick = function() {akce();return false;}
	return newLink;
}

function appendLink(link, targetId, sep) {
	if (link && d.getElementById(targetId)) {
		var target = d.getElementById(targetId);
		target.parentNode.insertBefore(link,target);
		if(sep) target.parentNode.insertBefore(d.createTextNode(' | '), target);
	}
}




/*  ========================== Texty pisni ======================== */

var pisne = {

	motherId 				: 'texty',
	alba 					: ['iguo', 'babylon'],
	opened 					: null,
	infoElId 				: 'pisneInfo',
	controlClass 			: 'pisneControl',
	headerEl 				: 'h3',
	hashed 					: null,

	closedTitle 			: 'Kliknutím zobrazíte text skladby',
	closedClass 			: 'pisen closed',
	openedClass 			: 'pisen opened',
	openedTitle 			: 'Kliknutím skryjete',
	openAllText 			: 'Zobrazit vše',
	openAllTitle 			: 'Zobrazit všechny texty z této desky',
	closeAllText 			: 'Skrýt vše',
	closeAllTitle 			: 'Skrýt všechny texty z této desky',
	infoText 				: 'Kliknutím na název skladby zobrazíte její text.',


	init : function() {
		var mother, hdrs, hdr, myHash, hashed;

		mother = d.getElementById(pisne.motherId);

		if (!mother) {
			return;
		}
		pisne.initHashed();
		pisne.createInfo();

		hdrs = mother.getElementsByTagName(pisne.headerEl);
		for (var i = 0; i < hdrs.length; i++) {
			pisne.hide(hdrs[i]);
		}



		for (var i = 0; i < pisne.alba.length; i++) {
			pisne.createControl(pisne.alba[i]);
		}

		if (pisne.hashed) {
			window.location = window.location.hash;
			pisne.hashed = null;
		}

		pisne.fixGecko();
	},


	createControl : function(tId) {
		if (d.getElementById(tId)) {
			var tEl = d.getElementById(tId);
			var p = d.createElement('p');
			var a = createJsLink(function() {pisne.toggleAll(pisne.show,tEl)}, pisne.openAllText, pisne.openAllTitle);
			var a2 = createJsLink(function() {pisne.toggleAll(pisne.hide,tEl)}, pisne.closeAllText, pisne.closeAllTitle);
			p.className = pisne.controlClass;
			p.appendChild(a);
			p.appendChild(d.createTextNode(' | '));
			p.appendChild(a2);
			tEl.insertBefore(p, tEl.getElementsByTagName('h2')[0].nextSibling);
		}
	},

	createInfo : function() {
		var  p = d.createElement('p');
		p.appendChild(d.createTextNode(pisne.infoText));
		d.getElementById(pisne.infoElId).appendChild(p);
	},

	show : function (header, hideOpened) {
		var pisen = header.parentNode;

		if (pisne.opened && hideOpened) {
			// je nejaka otevrena a ma se skryt
			pisne.hide(pisne.opened);
		}

		pisen.className = pisne.openedClass;

		header.title = pisne.openedTitle;
		header.onclick = function() { pisne.hide(this); }


		// refence na otevrenou pisen
		pisne.opened = header;
		pisne.fixGecko();
	},

	hide : function (header) {
		var pisen = header.parentNode;

		if (pisne.hashed) {
			if (pisne.hashed == header) {
				pisne.show(pisne.hashed);
				return;
			}
		}

		pisen.className = pisne.closedClass;

		header.title = pisne.closedTitle;
		header.onclick = function() { pisne.show(this, true); }
		pisne.fixGecko();
	},

	toggleAll : function (akce, parent) {
		var header, div, divs = parent.getElementsByTagName('div');
		for (var i = 0; i< divs.length; i++) {
			div = divs[i];
			if (/schovka/.test(div.className)) {
				header = div.parentNode.getElementsByTagName(pisne.headerEl)[0];
				akce(header, false);
			}
		}

	},

	initHashed : function() {
		var myHash, hashed;
		if (window.location.hash) {
			myHash = window.location.hash.substr(1);
			if (d.getElementById(myHash)) {
				hashed = d.getElementById(myHash);

				if (hashed.tagName.toLowerCase() == pisne.headerEl) {
					pisne.hashed = hashed;
				}
			}
		}
	},

	fixGecko : function() {
		if (!d.all && !window.opera) {
			// fix long scrollbar
			d.getElementById('page').style.minHeight =  Math.ceil(Math.random() * 100)  + 'px';
		}
	}

}


/*  ========================== Formulare ========================= */

String.prototype.trim = function() {
	return this.replace(/^\s*/g, '').replace(/\s*$/g, '')
}

String.prototype.isEmpty = function() {

	return (this == '' || this.trim() == '');
}

String.prototype.hasStar = function() {
	return (this.indexOf('*') != -1)
}

String.prototype.checkFormat = function(format) {
	switch (format) {
		case 'datum' :
			return /^2[0-9]{3}-[0-9]{1,2}-[0-3][0-9]$/.test(this);
			break;
		case 'email' :
			return /^[a-z0-9_.-]+@[a-z0-9_.-]+\.[a-z]{2,6}/i.test(this);
			break;
		default :
			alert('Neznámý formát: '+format);
	}
}

function checkField(field, checkingFormat) {
	field.value = field.value.trim();

	if (!checkingFormat) {
		if (field.value.isEmpty()) {
			alert(errors[field.name]);
			field.focus();
			return false;
		}
		if (field.name == 'datum' || field.name == 'email') {
			if (!checkField(field, true)) {
				return false;
			}
		}
	} else {
		if (!field.value.checkFormat(field.name)) {
			alert(errors.invalid[field.name]);
			field.focus();
			return false;
		}
	}

	return true;
}

function checkFormsInit() {
	var fs = d.getElementsByTagName('form');
	for (var i = 0; i < fs.length; i++) {
		if (!/aTools/.test(fs[i].className)) {
			fs[i].onsubmit = checkForm;
		}
	}
}

function checkForm() {
	var el;
	var els = this.elements;
	var labels = this.getElementsByTagName('label');

	for (var i = 0; i < labels.length; i++) {
		labels[labels[i].htmlFor] = labels[i].firstChild.data;
	}

	for (var i = 0; i <els.length; i++) {
		el = els[i];
		if (el.type == 'text' || el.type == 'textarea') {
			el.value = el.value.trim();
			if (labels[el.id] && labels[el.id].hasStar()) {
				if (!checkField(el)) {
					return false;
				}
			}
		}
	}

	return true;
}

var errors = {
	jmeno : 		'Vyplňte, prosím, své jméno',
	prijmeni : 		'Vyplňte, prosím, své příjmení',
	kusu : 			'Vyplňte, prosím, počet kusů',
	vzkaz : 		'Napište, prosím, nějaký vzkaz',
	nadpis : 		'Vyplň, prosím, nadpis novinky',
	text : 			'Vyplň, prosím, text novinky',
	datum : 		'Vyplň, prosím, datum',
	mesto : 		'Vyplňte, prosím, město',
	psc : 			'Vyplňnte, prosím, PSČ',
	ulice : 		'Vyplňte, prosím, ulici a číslo',
	username : 		'Vyplň, prosím, své uživatelské jméno',
	level : 		'Level nesmí být prázdný',
	email :       'Vyplňte, prosím, svůj e-mail',

	invalid : {
		datum : 'Datum není ve formátu RRRR-MM-DD, prosím, uprav ho',
		email : 'E-mail není ve správném formátu, prosím, upravte ho'
	}
}





window.onload = init;
