function headerAccordion(el, tagName){
	if(!$(el)) return false;
	tagName = tagName.toUpperCase();
	
	Element.cleanWhitespace(el);
	var heads = $(el).getElementsByTagName(tagName);
	var divs = $A(heads).collect(function(h){
		var div = document.createElement('div');
		while(h.nextSibling && h.nextSibling.nodeName.toUpperCase() != tagName){
			div.appendChild(h.nextSibling);
			// alert(h.nextSibling.nodeName)
		}
		if(h.nextSibling) h.parentNode.insertBefore(div, h.nextSibling)
		else h.parentNode.appendChild(div);
		h.style.cursor = document.all ? 'hand' : 'pointer';
		return div;
	});
	var myAccordion = new fx.Accordion($A(heads), divs, {opacity:false, duration:400});
	// myAccordion.showThisHideOpen(divs[0]);
	return true;
}

function linkPreview(){
	$A(document.links).each(function(link){
		// Check if link is just a text node
		if(link.childNodes.length == 1 && link.firstChild.nodeType == link.TEXT_NODE){
			var match = link.href.match(/\.([a-z]{2,5})$/);
			if(match) Element.addClassName(link, match[1]);
		}
	});
}

function init(){
	linkPreview();
	
	headerAccordion('acc', 'h3');
	
	if($('search')){
		$('search').onsubmit = function(){
			if($('result')) Element.remove('result');
			var div = document.createElement('div');
			div.id = 'result';
			$('wrapin').parentNode.insertBefore(div, $('wrapin'));
			
			new Insertion.Top(div, '<h2>Rezultati pretraživanja za: '+ $F('inputbox') +'</h2>');
			
			var iframe = document.createElement('iframe');
			var target = this.action+'?'+Form.serialize(this);
			div.appendChild(iframe);
			
			div.fx = new fx.Combo(div, {duration:500,
				onComplete:function(){
					if(!iframe.src){
						iframe.src = target;
						new Insertion.Bottom(this.firstChild, ' <a href="#">(zatvori)</a>');
						this.firstChild.lastChild.onclick = function(){
							div.fx.toggle();
							return false;
						}
					} else {
						Element.remove(this);
					}
				}.bind(div) });
			div.fx.hide();
			setTimeout(function(){
					div.fx.clearTimer();
					div.fx.toggle();
				}
				,100);
			
			return false;
		}
	}
	
	document.getElementsByClassName('arrow', 'acc').each(function(link){
		link.target = '_blank';
		// stop onclick from propaginating?
	});
}

window.onload = init;
