
var accordion=false;
var activeToggler=3;
var stop=false;
var myVerticalSlide=false;
var isLoading=false;
var nextPage=false;
function getActiveToggler(){
    if($$('#left a.active').length==0){
        if(window.location.href.match(/#(\d+)/)){
            if(myid=window.location.href.match(/#(\d+)/)[1]){
                try{
                    $$('#left a[href$=='+myid+']')[0].className='active';
                }catch(e){}
            }
        }else
            return activeToggler;
    }
    activeLi=0;
    try{
        activeLi=$$('#left a.active').parents('li.toggler');
    }catch(e){
        return activeToggler;
    }
    if(!activeLi)
        return activeToggler;
    $('li.toggler').each(function(el){
        if(this==activeLi)
            stop=true;
        if(!stop)
            activeToggler++;
        $(this).css('cursor','default');
        try{
            this.childNodes[0].setStyle('cursor','default');
        }catch(e){}
    });
    return activeToggler;
}
function myVerticalSlide_obj(){};

myVerticalSlide_obj.prototype.slideOut=function(){
    $('content').fade(0.1);
    $('header').fade(0.1);
}
var myVerticalSlide=new myVerticalSlide_obj();
function myVerticalSlide_obj2(){};

myVerticalSlide_obj2.prototype.slideIn=function(){
    $('content').fade('in');
    $('header').fade('in');
    window.setTimeout(function(){
        $('load').style.display='none';
    },400);
}
var myVerticalSlide2=new myVerticalSlide_obj2();
function setlvl2Height(){
    var maxheight=0;
    $$('.lvl2').each(function(el){
        maxheight=Math.max(maxheight,el.getHeight());
    });
    $$('.lvl2').setStyle('height',maxheight+'px');
}
var historylastpage=window.location.href;

function preparePrettyPhoto() {
    // Effekt fuer YouTube-Filme...
    jQuery('a[rel^="prettyPhoto"], a[href^="http://www.youtube.com/"]').prettyPhoto();
}

function prepareSlimBox() {

    jQuery("#content a[rel^='lightbox'], #content a[target='thePicture']").each(function() {
        if (this.target == 'thePicture' && this.href.match(/index.php/)) {
            this.href = this.href.match(/file=(.*?)&/)[1].replace(/%2F/g, '/');
            this.rel  = "lightbox";
        }
        if (jQuery(this).nextAll('.caption').length) {
            this.title = jQuery(this).nextAll('.caption').html();
        }
        jQuery(this).attr('onclick', '');
    });

    jQuery("#content a[rel^='lightbox']").slimbox({/* Put custom options here */}, null, function(el) {
        return (this == el) || ((this.rel.length > 8) && (this.rel == el.rel));
    });
}

function openPic(el) {
    return false;
}

function prepareCarousel() {
if (!jQuery('.karussell').length)
	return;


	var imageList = '<ul class="roundabout">';

	jQuery('.karussell img').each(function() {
		if (jQuery(this).attr('src').match(/^typo3temp|uploads/)) {
			imageList += '<li><img src="'+jQuery(this).attr('src')+'" alt="'+jQuery(this).nextAll('.caption').html()+'"></li>';
		}
	}); 
	imageList += '</ul>';

	jQuery('.karussell').html(imageList).find('img').fadeIn();
	jQuery('.karussell .roundabout').roundabout({
		tilt: 0.6,
		minScale: 0.6,
		minOpacity: 0.6,
		duration: 400,
		bearing: 0,
		focusBearing: 5,
		clickToFocusCallback: function() {
			jQuery('.karussell ul li').unbind('mouseup');
			jQuery('.karussell ul li.roundabout-in-focus').bind('mouseup', function() {
				var url = window.location.href;
				var id = jQuery('img', this).attr('alt');
				if (url.match(/#/)) {
					window.location.href = url.replace(/#.*/, id);
				} else {
					window.location.href = url + id;
				}
			});
		},
		easing: 'easeOutQuad',
		enableDrag: true,
		dropEasing: 'easeOutBounce'
	}, function() {
		jQuery(this).fadeTo(500, 1);
		jQuery('.karussell ul li.roundabout-in-focus').bind('mouseup', function() {
				var url = window.location.href;
				var id = jQuery('img', this).attr('alt');
				if (url.match(/#/)) {
					window.location.href = url.replace(/#.*/, id);
				} else {
					window.location.href = url + id;
				}
			});
	});
}

function prepareBody(){
    preparePrettyPhoto();
    prepareSlimBox();

    window.setInterval(function(){
        if(historylastpage!=window.location.href){
            link=document.createElement('a');
            link.href=historylastpage=window.location.href;
            loadPage(link);
        }
    },500);
    prepareScroller();
    $$('.submenu').each(function(el){
        el.setStyles({
            'display':'block',
            'height':'0px'
        });
    });
    $('content').setStyle('overflow','hidden');
    if(window.location.href.match(/#(\d+)/)){
        if(myid=window.location.href.match(/#(\d+)/)[1]){
            $('content').innerHTML=$('waiting_msg')?$('waiting_msg').innerHTML:'Please wait...';
            $('header').innerHTML='';
            loadPage(window.location);
        }
    }
    accordion=new Accordion($$('.toggler'),$$('.submenu'),{
        display:getActiveToggler()
    });
    checkTabs();
    prepareLinks();
    setlvl2Height();
    $$('.toggler').addEvent('mouseenter',function(){
        this.fireEvent('click');
    });
    $$('.lvl2 a, #menu_top a').each(function(el){
        if(el.className!='flag'){
            if(isIntLink(el)){
                curP={
                    href:document.URL
                };

                newhref='';
                if(getId(curP)!=getId(el)){
                    newhref="?id="+getId(curP);
                }
                newhref+="#"+getId(el);
                el.id='link_'+getId(el);
                el.href=newhref;
                if(!window.location.href.match(/index.php/))
                    el.href=el.href.replace(/index.php/,'');
                el.addEvent('click',function(event){
                    loadPage(el);
                })
            }else{
                el.addEvent('click',function(event){
                    event.stop();
                });
            }
        }
    });
}
var lastPage=false;
function loadPage(sender){
    curpos=0;
    if(!isIntLink(sender))
        return;
    isLoading=$('content').getStyle('opacity')!=1;
    if(isLoading){
        if(nextPage!=lastPage)
            nextPage=sender;
        return;
    }else{
        nextPage=false;
    }
    if(lastPage==sender||sender.className=='active')
        return;
    lastPage=sender;
    $('content').style.height='525px';
    if($$('.yui_outer_content')[0]){
        $$('.yui_outer_content')[0].style.overflow='hidden';
    }
    $('load').style.display='block';
    $$('a.active').each(function(el){
        el.className='';
    })
    var id=getIdFromAnker(sender);
    myVerticalSlide.slideOut();
    myRequest=new Request.HTML({
        url:'index.php?id='+id+'&T=99',
        onSuccess:function(nodes,elements,html){
            if($('link_'+id)){
                $('link_'+id).className='active';
                sender=$('link_'+id);
            }
            $('content').style.paddingRight='25px';
            $('content').innerHTML=html;
            $('content').style.paddingLeft='40px';
            $('header').style.display='block';
            $('header').innerHTML=($('content_title'))?$('content_title').innerHTML:sender.innerHTML;
            if($$('.yui_outer_content')[0]){
                $('header').style.display='none';
                $$('.yui_outer_content')[0].style.overflow='hidden';
                if($$('ul.yui-nav li')[0]){
                    $$('ul.yui-nav li')[0].className='firstChild';
                }
                $('content').style.paddingLeft='7px';
            }
            try {
                if ($$('#content img[src="typo3conf/ext/captcha/captcha/captcha.php"]').length) {
                    $$('#content img[src="typo3conf/ext/captcha/captcha/captcha.php"]')[0].addEvent('load', function() {
                        if ($$('#content img[src="typo3conf/ext/captcha/captcha/captcha.php"]').length)
                            $$('#content img[src="typo3conf/ext/captcha/captcha/captcha.php"]')[0].src = 'typo3conf/ext/captcha/captcha/captcha.php?'+ new Date().getTime();
                    });
                    if ($$('#content input[name="captchaResponse"]').length) {
                        $$('#content input[name="captchaResponse"]')[0].style.width='70px';
                        $$('#content input[name="captchaResponse"]')[0].maxLength='5';
                    }
                }
            }catch(e){}
            checkTabs();
            prepareLinks();
            myVerticalSlide2.slideIn();
            prepareSlimBox();
				
            preparePrettyPhoto();
            $('content').scrollTo(0,0);
        }
    });
    myRequest.send();
    sender.className='active';
}
function prepareLinks(){
	prepareCarousel();

    $$('#content a, #content area').each(function(el){
        if(isIntLink(el)){
            curP={
                href:document.URL
            };

            newhref='';
            if(getId(curP)!=getId(el)){
                newhref="?id="+getId(curP);
            }
            newhref+="#"+getId(el);
            el.href=el.href.replace(/\?.*/,newhref);
            if(!window.location.href.match(/index.php/))
                el.href=el.href.replace(/index.php/,'');
            el.target='';
            el.addEvent('click',function(event){
                $('content').style.height='525px';
                if($$('.yui_outer_content')[0]){
                    $$('.yui_outer_content')[0].style.overflow='hidden';
                }
                loadPage(el);
            })
        }
    });
    $$('form').each(function(el){
        el.action=el.action.replace(/&T=99/,'');
    });
}
function getId(sender){
    if(sender.href.match(/sword=/))
        return false;
    var Ausdruck=/\?id=(\d+)/;
    Ausdruck.exec(sender.href);
    return RegExp.$1;
}
function getIdFromAnker(sender){
    var Ausdruck=/\#(\d+)/;
    Ausdruck.exec(sender.href);
    return RegExp.$1;
}
function isIntLink(sender){
    if(!sender.href||sender.href=='#'||sender.href.substr(sender.href.length-1,1)=='#'){
        return false;
    }
    id=getId(sender);
    idfa=getIdFromAnker(sender);
    if(parseInt(id)||parseInt(idfa))
        return true;
    return false;
}
function checkTabs(){
    if($$('.my_tab_div')[0]){
        $$('.my_tab_div')[0].setStyle('height','30px');
        if(window.location.href.match(/#(\d+)/)){
            $$('.tx-troyuitabs-pi1')[0].setStyle('left','0px');
        }
        $$('ul.yui-nav li')[0].className='firstChild';
        YAHOO.example.init=function(){
            var tabView=new YAHOO.widget.TabView($$('.yui-navset')[0].id,{
                orientation:"top",
                activeIndex:'0'
            });
            tabView.on('activeTabChange',function(ev){
                checkTabContentBG();
                $(scrolldiv).scrollTop='0';
                curpos=0;
            });
        };

        $('content').style.height='600px';
        $('content').style.paddingRight='0px';
        $('content').style.paddingBottom='0px';
        $('header').style.display='none';
        YAHOO.example.init();
        if($$('.yui_outer_content')[0]){
            el=$$('.yui_outer_content')[0];
            el.id='yui_outer_content';
            scrolldiv='yui_outer_content';
            checkTabContentBG();
        }
    }else{
        scrolldiv='content';
    }
}
function checkTabContentBG(){
    el=$$('.yui_outer_content')[0];
    if($$('.firstChild')[0].className.match(/selected/)){
        if(Browser.Engine.trident)
            $$('.firstChild')[0].setStyle('border-bottom','1px solid #fff');
        el.className='yui_outer_content firstChildSelected';
    }else{
        el.className='yui_outer_content';
        if(Browser.Engine.trident)
            $$('.firstChild')[0].setStyle('border-bottom','1px solid #6a6a6a');
    }
}
var curpos=0;
var done=false;
var scrollTimer=false;
var up=false;
var scrollspeed=17;
var scrolldiv='content';
var wheelspeed=24;
function prepareScroller(){
    $('scroller').setStyle('display','block');
    [$('scrollup'),$('scrolldown')].each(function(el){
        el.addEvent('mouseup',function(){
            done=true;
        })
    });
    $('scrollup').addEvent('mousedown',function(){
        up=true;
        done=false;
        scrollTimer=false;
        scroll();
    });
    $('scrolldown').addEvent('mousedown',function(){
        up=false;
        done=false;
        scrollTimer=false;
        scroll();
    });
    $('content').addEvent('mousewheel',function(event){
        event=new Event(event);
        if(event.wheel>0){
            curpos=Math.max(curpos-wheelspeed,0)
        }
        else if(event.wheel<0){
            curpos=Math.min(curpos+wheelspeed,$(scrolldiv).scrollHeight-$(scrolldiv).getHeight());
        }
        $(scrolldiv).scrollTop=curpos;
    });
}
function toggleScroller(){
    if(curpos>=$(scrolldiv).scrollHeight-$(scrolldiv).getHeight())
        $('scrolldown').style.visibility='hidden';else
        $('scrolldown').style.visibility='visible';
    if(curpos==0)
        $('scrollup').style.visibility='hidden';else
        $('scrollup').style.visibility='visible';
}
function scroll(){
    if(done){
        window.clearTimeout(scrollTimer);
        return false;
    }
    if(up)
        curpos=Math.max(curpos-scrollspeed,0)
    else
        curpos=Math.min(curpos+scrollspeed,$(scrolldiv).scrollHeight-$(scrolldiv).getHeight());
    $(scrolldiv).scrollTop=curpos;
    scrollTimer=window.setTimeout('scroll()',25);
}

