// Raneček s buchtama a vším dalším potřebným...

var showLength = 220;
var countLetters = 0;
var classForHide = 'hiddenContent1234Act';
var classForMoreButton = 'moreButton1234Act';

/**
* Vypíše novinku. Pokud je moc dlouhá, vypíše pouze začátek
* a vloží tlačítko "více", které umožní vypsat celou novinku.
* @param id id novinky
* @param act text novinky
*/
function showActualityCut(id) {
	countLetters = 0;
	var divAct = document.getElementById('dAct' + id);
	var act = divAct.innerHTML;
	var textLength = act.length;
	if (textLength > showLength) {
		var countChilds = divAct.childNodes.length;
		if (countChilds == 1) {
			act = spanViceHide(act, id, 0, showLength, textLength);
			divAct.innerHTML = act;
		} else {
			processInNode(divAct, id);
		}
		
	}
}

function processInNode(node, id) {
	if (node.innerHTML == undefined) {
		var textLength = node.nodeValue.length;
		if (textLength != 0) {
       countLetters = countLetters + textLength;
  		if (countLetters > showLength) {
  			var newspan = document.createElement('span');
  			if (countLetters - textLength <= showLength) { // Přidat "... Více"
  				newspan.innerHTML = spanViceHide(node.nodeValue, id, 0, showLength, textLength);
  				newspan.setAttribute('id', 'lastWordAct'+id);
  				node.parentNode.replaceChild(newspan, node);
  			} else {
  				if (!isChild(document.getElementById('lastWordAct'+id), node.parentNode)
  					&& node.parentNode != document.getElementById('dAct'+id)) {
  					addClass(node.parentNode, classForHide);
  				} else {
  					newspan.innerHTML = spanHide(node.nodeValue, 0, textLength);
  					node.parentNode.replaceChild(newspan, node);
  				}
  			}
  		}
    }
		
	} else {
		for (var i = 0; i < node.childNodes.length; i++) {
			processInNode(node.childNodes[i], id);
		}
	}
	// Pro <br />
	if (node.tagName == "BR") {
		if (countLetters > showLength) {
			var newspan2 = document.createElement('span');
			addClass(newspan2, classForHide);
			newspan2.innerHTML = "<br>";
			node.parentNode.replaceChild(newspan2, node);
		}
	}
}

function isChild(child, parent) {
	var m = parent.childNodes;
	for (var i = 0; i < m.length; i++) {
		if (m.item(i).innerHTML != undefined && m.item(i).getAttribute('id') == child.getAttribute('id')) {
			return true;
		}
	}
	return false;
}

function spanViceHide(act, id, start, middle, end) {
	return act.substr(start, middle) + "<span class='" + classForMoreButton + "'>... <a href=\"javascript:showWholeAct(" + id + ")\">Více »</a></span>" + spanHide(act, middle, end);
}

function spanHide(act, middle, end) {
	return "<span class='" + classForHide + "'>" + act.substr(middle+1, end) + "</span>";
}

/**
* Schová "... Více" a zobrazí zbytek celé novinky
* @param id id novinky
*/
function showWholeAct(id) {
	var divAct = document.getElementById('dAct' + id);
	var hidden = getElementsByClassName(classForHide, divAct);
	for (var i = 0; i < hidden.length; i++) {
		removeClass(hidden[i], classForHide);
	}
	var moreButton = getElementsByClassName(classForMoreButton, divAct);
	for (var i = 0; i < moreButton.length; i++) {
		moreButton[i].style.display = 'none';
	}
}



/* from snipplr.com... */
function hasClass(ele,cls) {
	return ele.className.match(new RegExp('(\\s|^)'+cls+'(\\s|$)'));
}

function addClass(ele,cls) {
	if (!this.hasClass(ele,cls)) ele.className += " "+cls;
}

function removeClass(ele,cls) {
	if (hasClass(ele,cls)) {
    	var reg = new RegExp('(\\s|^)'+cls+'(\\s|$)');
		ele.className=ele.className.replace(reg,' ');
	}
}

function getElementsByClassName(classname, node)  {
    if(!node) node = document.getElementsByTagName("body")[0];
    var a = [];
    var re = new RegExp('\\b' + classname + '\\b');
    var els = node.getElementsByTagName("*");
    for(var i=0,j=els.length; i<j; i++)
        if(re.test(els[i].className))a.push(els[i]);
    return a;
}


