/* Diverse
================================================= */
addSpecialChar = function(s, obj){
	obj.innerText = obj.innerText + s;
}
clickButton = function(e, buttonid){
	try{
		var bt = $(buttonid);
		if (typeof bt == 'object'){
			if (e.keyCode == 13){ 
				bt.click(); 
				return false; 
			}
		}
    }
    catch (e) {
        return true;
    }
}
/* ImageSwitcher
================================================= */
initImageSwitcher = function(){
	var switchers = $$('.image-switcher a');
		
	switchers.each(
		function(obj){
			if (obj.href == $(obj.rel).src){ obj.className = 'active' }
			obj.onclick=function(){switchImage(obj);return false}
		}
	)
}
switchImage = function(obj){
	var switchers = $$('.image-switcher a');

	switchers.each(function(s){s.className = '';})
	var img = $(obj.rel);
	img.src = obj.href;
	img.alt = obj.title;
	obj.className='active';
	try {
		$('image-text').innerHTML = '<p>'+obj.title+'</p>'
	}
	catch (e) {
	}
}
/* TabControl
================================================= */
initTabControl = function() {
	var tabs = $$('.items .item h3 a');
	var tabInfos = $$('.items .item .item-info');	
	tabInfos.each(
		function(obj, index){obj.setAttribute('id','item'+index)}
	)
	tabs.each(
	function(obj,index){
		obj.setAttribute('rel','item'+index);
		obj.onclick=function(){switchTab(obj);return false};
	}
	)
}
switchTab = function(obj){
	var tabs = $$('.items .item h3 a');
	var tabInfos = $$('.items .item .item-info');
	var content = $(obj.rel);
	
	tabs.each(function(tab){tab.className = '';})
	tabInfos.each(function(tabInfo){tabInfo.className='item-info hide';})
	obj.className = 'active';
	content.className = 'item-info show';
}
initNewWindowLinks= function(){
	var links = $$('.new-win');
		
	links.each(
		function(obj){
			obj.onclick=function(){window.open(obj.href, '_blank');return false}
		}
	)
}
/* hack för att undvika att RAD editor förstör layouten */
removeTextAreaAttributes = function(){
	var txtArea = $$('#rad-editor textarea');
	
	txtArea.each(
		function(obj){
			obj.removeAttribute('cols');
			obj.removeAttribute('rows');
			obj.style.width = '345px';
		}
	)
}
/* Hämtar title från bilden och skapar en passpartou med bild och bildtext. */
addCaptions = function(){
	addCaption("left");	
	addCaption("right");
	addCaption("center");
}
addCaption = function(floatTo){
	$$(".caption-" + floatTo).each(
		function(elem){
			elem.removeClassName("caption-" + floatTo);
			var container = new Element("div",{style: "width: " + elem.width + "px"}).addClassName("passepartou-" + floatTo);
			elem.wrap(container);
			if(!elem.getAttribute("longdesc") == "" || !elem.getAttribute("longdesc") == "undefined"){
				container.insert(new Element("p").update(elem.getAttribute("longdesc")));
			}
		}
	);
}
/* OnLoad
================================================= */
Event.observe(window, "load",function(){
	initImageSwitcher();
	initNewWindowLinks();
	removeTextAreaAttributes();
	addCaptions();
});
