var isIE = false;
var isIE7 = false;

function nav() {
    if ($('#nav').length) {
        if (!isIE) $('#nav').find("a.active span").css({
            'border-bottom': 0,
            'margin-bottom': '3px'
        });
        $('#nav').find("a[class!='active']").css({
            color: '#fff'
        }).hover(function () {
            if (!isIE) $(this).find('span').stop().animate({
                'color': '#5cc5ed'
            }, 'fast')
        }, function () {
            if (!isIE) $(this).find('span').stop().animate({
                'color': '#ffffff'
            }, 'fast')
        });
        if (!isIE) $(function () {
            $("#nav ul").lavaLamp({
                fx: "backout",
                speed: 1000,
                padding: 10
            })
        })
    }
}
function slideTabs() {
    if (!isIE) {
        if ($('.slide').length && $('.slide').length > 1) {
            var e = $('.slide');
            var f;
            e.hide();
            $('.slide:first').show();
            $('.slide:first').before('<ul id="tabs"></ul>');
            var g = $('#tabs');
            e.each(function (a) {
                var b = $(this).attr('id').slice(0, 1).toUpperCase() + $(this).attr('id').slice(1);
                var c = this;
                var d = 'button' + b;
                g.append('<li><a href="javascript:;" id="' + d + '">' + b + '</a></li>');
                g.find('a:first').addClass('active').css({
                    paddingLeft: '12px',
                    paddingRight: '12px'
                });
                $('#' + d).bind('click', function () {
                    if (!$(c).is(':visible')) {
                        f.each(function () {
                            this.className = ""
                        });
                        $(this).addClass('active');
                        e.filter(':visible').stop().slideToggle();
                        $(c).stop().slideToggle(function () {
                            if (isIE) {
                                g.css({
                                    zoom: 1
                                })
                            }
                        })
                    }
                })
            });
            f = g.find('a');
            f.each(function () {
                $(this).bind('click', function () {
                    f.not('[class=active]').stop().animate({
                        paddingLeft: '6px',
                        paddingRight: '6px'
                    }, 'fast')
                });
                $(this).hover(function () {
                    $(this).not('[class=active]').stop().animate({
                        paddingLeft: '12px',
                        paddingRight: '12px'
                    }, 'fast')
                }, function () {
                    $(this).not('[class=active]').stop().animate({
                        paddingLeft: '6px',
                        paddingRight: '6px'
                    }, 'fast')
                })
            })
        }
    }
}
function expiration_dates(a) {
    var b = a.getTime() / 1000;
    for (var i = 0; i < expirations.length; i++) {
        if (b == expirations[i]) {
            return [true, 'selectable']
        }
    }
    return [false, '']
}
function postDate(a) {
    document.location = curArticle + '?datum=' + a
}
function calendars() {
    var a = new Date();
    if (typeof(curDate) == 'object') {
        a = new Date(curDate["y"], curDate["m"], curDate["d"])
    }
    $.datepicker.setDefaults($.extend({
        onSelect: postDate,
        beforeShowDay: expiration_dates,
        showMonthAfterYear: false,
        defaultDate: a,
        showOptions: {
            direction: 'right'
        },
        dateFormat: 'dd.mm.yyyy'
    }, $.datepicker.regional['de']));
    if ($('#kalender').length) {
        $("#kalender").datepicker()
    }
}
function bildergalerie() {
    if ($('.gallery .slideshowgal').length) {
        if ($('.gallery .element').length > 1) {
            $('.gallery .slideshowgal').cycle({
                slideExpr: '.element',
                timeout: 5000,
                pager: '#gallerynav',
                easing: 'easeinout',
                prev: '.gallery .prev a',
                next: '.gallery .next a',
                pause: 1,
                cleartype: true,
                cleartypeNoBg: true
            })
        }
    }
}
function eventtipps() {
    if ($('#eventtipp .slideshow').length) {
        if ($('#eventtipp .element').length > 1) {
            $('#eventtipp .slideshow').cycle({
                slideExpr: '.element',
                fx: 'scrollRight',
                timeout: 8000,
                pager: '#eventtippnav',
                easing: 'backinout',
                pause: 1,
                cleartype: true,
                cleartypeNoBg: true
            })
        }
    }
}
var counterEvents = 0;
var maxEvents = 0;
var events;
var duration = 500;

function _eventsAnimation() {
    if (maxEvents != counterEvents) {
        var a = parseInt($(window).scrollTop());
        if (a >= 0) duration = 500;
        if (a >= 1000) duration = 400;
        if (a >= 2000) duration = 250;
        if (a >= 3000) duration = 100;
        if (a >= 4000) duration = 0;
        $(events[counterEvents]).animate({
            opacity: 1
        }, duration, function () {
            counterEvents++;
            _eventsAnimation()
        })
    }
}
function events() {
    if (!isIE7) {
        if ($('#events .box').length) {
            if ($('#events .box').length > 1) {
                maxEvents = $('#events .box').length;
                events = $('#events .box');
                events.each(function (a) {
                    $(events[a]).css({
                        opacity: 0
                    })
                });
                _eventsAnimation()
            }
        }
    }
}
function killCSS(a, b, c) {
    if ($(a).length) {
        $(a).find(b).css(c)
    }
}
function rePosition(a, b) {
    var c = $(window).height();
    var d = $(window).scrollTop();
    var y = (c + d) - b;
    if (!isIE) {
        $(a).stop().animate({
            'top': y + 'px'
        }, 750, 'easeinout').find('a').stop().animate({
            'opacity': 0.5
        })
    } else {
        $(a).css({
            'top': y + 'px'
        }).find('a').stop().css({
            'opacity': 0.5
        })
    }
}
function backToTop() {
    if ($('#backtotop').length) {
        var a = $('#backtotop');
        var b = '71';
        var c = $(window).height();
        var d = $(window).scrollTop();
        var y = (c + d) - b;
        $(a).css({
            'position': 'absolute',
            'left': '920px',
            'top': y + 'px'
        }).find('a').css({
            'opacity': 0
        }).hover(function () {
            if (!isIE) {
                $(this).stop().animate({
                    'opacity': 1
                }, 500)
            } else {
                $(this).css({
                    'opacity': 1
                })
            }
        }, function () {
            if (!isIE) {
                $(this).stop().animate({
                    'opacity': 0.5
                }, 500)
            } else {
                $(this).css({
                    'opacity': 0.5
                })
            }
            rePosition($('#backtotop'), b)
        });
        rePosition($('#backtotop'), b);
        $(window).bind('scroll', function () {
            rePosition($('#backtotop'), b)
        });
        $(window).bind('resize', function () {
            rePosition($('#backtotop'), b)
        })
    }
}
function handleIE6() {
    if ($.browser.msie && parseInt($.browser.version, 0) < 7) {
        $(":text").addClass("ie6textinput");
        $("textarea").addClass("ie6textinput");
        $("input[type='password']").addClass("ie6textinput");
    }
}
function externalLinks() {
    if (!document.getElementsByTagName) {
        return;
    }
    var a = document.getElementsByTagName("a");
    for (var i = 0; i < a.length; i++) {
        var b = a[i];
        if (b.getAttribute("href") && b.getAttribute("rel") == "blank") {
            b.target = "_blank";
        }
    }
}
function smoothscroll() {
    if (!isIE) {
        $('a[href*=#]').click(function () {
            var a = 1000;
            var b = 'easeinout';
            if (location.pathname.replace(/^\//, '') == this.pathname.replace(/^\//, '') && location.hostname == this.hostname && this.hash.replace(/#/, '')) {
                var c = this.hash;
                var d = $(c).offset().top;
                $("html:not(:animated),body:not(:animated)").animate({
                    scrollTop: d
                }, a, b, function () {
                    location.hash = c
                });
                return false;
            }
        })
    }
}
function facebookLinks() {
    if ($('.facebook').length) {
        $('.facebook').click(function () {
            u = location.href;
            t = document.title;
            window.open('http://www.facebook.com/sharer.php?u=' + encodeURIComponent(u) + '&t=' + encodeURIComponent(t), 'sharer', 'toolbar=0,status=0,width=626,height=436');
            return false;
        })
    }
}
function bgslideshow() {
    if ($('#bgslideshow .element').length > 1) {
		$('#bgslideshow').cycle({
			slideExpr: '.element',
			timeout: 20000,
			speed:3000,
			cleartype: true,
			cleartypeNoBg: true
		})
	}
}

/*
* filter mobilyselect
*/
function filter(){
	$('#programmfilter a').click(function(e){ e.preventDefault(); });
	var hash='alle';
	var setHash = function(){
		hash = $('#programmfilter').find('a.active').attr('rel');
		$(location).attr('hash',hash)
	}
	var getHash = function(){
		if($(location).attr('hash')) hash = $(location).attr('hash').replace(/#/gi, "");
		$('#programmfilter').find('a[rel="'+hash+'"]').click();
	}
	$('#events').mobilyselect({
		listClass: 'items',
		btnsClass: 'filters',
		btnActiveClass: 'active',
		animation: 'plain',
		onChange: function(el){ setHash();/* doLis();*/ }
	});
	getHash();
	if($('#events').find('.konzert:first').length==0)$('#programmfilter').find('a[rel="Konzert"]').hide(); 
	if($('#events').find('.party:first').length==0)$('#programmfilter').find('a[rel="Party"]').hide(); 
	if($('#events').find('.sonstiges:first').length==0)$('#programmfilter').find('a[rel="Sonstiges"]').hide(); 
} 

$(document).ready(function () {
    if ($.browser.msie && parseInt($.browser.version, 0) < 7) isIE = true;
    if ($.browser.msie && parseInt($.browser.version, 0) < 8) isIE7 = true;
	filter();
    eventtipps();
    //events();
    bildergalerie();
    calendars();
    slideTabs();
    killCSS('#nav', 'li:last', {
        'background': 'none',
        'padding-right': '0'
    });
    backToTop();
    externalLinks();
    smoothscroll();
    handleIE6();
    facebookLinks()
    bgslideshow();
});
$(window).load(function () {
    nav()
});
