// custom easing called "custom" 
$.easing.custom = function(x, t, b, c, d) {
    var s = 1.50158;
    if ((t /= d / 2) < 1) return c / 2 * (t * t * (((s *= (1.525)) + 1) * t - s)) + b;
    return c / 2 * ((t -= 2) * t * (((s *= (0.525)) + 1) * t + s) + 2) + b;
}

var now = new Date();
var prettyDate = new String;
var days = now.getDate();
var months = now.getMonth() + 1;
if (days < 10) { days = '0' + days }
if (months  < 10) { months = '0' + months }
prettyDate = (days + '.' + months + '.' + now.getFullYear());

$(document).ready(function() {

    //Navigation
    $('ul#nav ul').css({ 'display': 'block' }); // Fallback remove
    $('ul#nav ul').hide();
    $('ul#nav li').hover(
		    function() {
		        $(this).children('ul').stop().height('auto').css('padding', '20px 0px 10px 0').slideDown(200);
		    },
		    function() {
		        $(this).children('ul').stop().slideUp(100);
		    }
	    );

    /* Bildunterschriften erstellen */
    $('#content img.art').each(function() {
        var alttext = $(this).attr('alt');
        $(this).wrap('<div class="aside">').after('<span>' + alttext + '</span>');
    });

    //Datepicker
    //Delete Date Config for using english format
    Date.abbrDayNames = ['So', 'Mo', 'Di', 'Mi', 'Do', 'Fr', 'Sa'];
    Date.abbrMonthNames = ['Jan', 'Feb', 'Mar', 'Apr', 'Mai', 'Jun', 'Jul', 'Aug', 'Sep', 'Okt', 'Nov', 'Dez'];
    Date.monthNames = ['Januar', 'Februar', 'M' + unescape("%E4") + 'rz', 'April', 'Mai', 'Juni', 'Juli', 'August', 'September', 'Oktober', 'November', 'Dezember'];
    Date.firstDayOfWeek = 1;
    Date.format = 'dd.mm.yyyy';
    $('input.datepicker').datePicker({ startDate: '01/01/1900', endDate: prettyDate, clickInput: true });

    /* Lightboxen */
    if ($("a.lightbox").length > 0) {
        $('a.lightbox').overlay({
            target: '#gallery',
            expose: {
                maskId: 'exposeMask',
                color: '#533524',
                opacity: 0.95
            }
        }).gallery({
            speed: 800
        });
    }
    if ($("a.artikelversenden").length > 0) {
        $('a.artikelversenden').overlay({
            target: '#sitesend',
            expose: {
                maskId: 'exposeMask',
                color: '#533524',
                opacity: 0.95
            }
        });
    }

    /* Autoscroll */
    $('#topnews ul').cycle({
        fx: 'scrollLeft',
        prev: '#topnews .prev',
        next: '#topnews .next',
        timeout: 5000,
        easing: 'custom'
    });

    $('.playreal a').click(function() {
        var videol = $(this).attr("rel")
        var backgr = $(this).children("img").attr("alt");
        $("#player").empty();
        $f("player", { src: "flowplayer-3.1.5.swf" }, {
            clip: {
                url: $(this).attr("rel"),
                autoPlay: true,
                onFinish: function() {
                    $("#player").empty();
                    $('#player').attr({ "rel": videol });
                    $('#player').css({ 'background': 'url(' + backgr + ') no-repeat' });
                    $("#player").html("<img src='App_Themes/images/icon/play.png' alt='" + backgr + "' />");
                }
            },
            plugins: {
                controls: {
                    autoHide: 'always',
                    hideDelay: '2000',
                    bufferGradient: 'none', progressGradient: 'medium', durationColor: '#ffffff', sliderGradient: 'none', sliderColor: '#000000', tooltipTextColor: '#ffffff', tooltipColor: '#5F747C', backgroundGradient: 'none', backgroundColor: '#222222', volumeSliderGradient: 'none', bufferColor: '#445566', borderRadius: '0px', volumeSliderColor: '#000000', timeColor: '#01DAFF', progressColor: '#112233', buttonColor: '#5F747C', buttonOverColor: '#728B94', timeBgColor: '#555555',
                    height: 20,
                    opacity: 0.5
                }
            }
        });
    });

    $('.playlist').cycle({
        fx: 'scrollLeft',
        prev: '#audiovideo .prev',
        next: '#audiovideo .next',
        timeout: 0,
        easing: 'custom'
    });

    $("#topnews a").hover(function() {
        $(this).animate({ opacity: 0.20 }, 100);
        /* $('#topnews ul').cycle('pause');*/
    }, function() {
        $(this).animate({ opacity: 1 }, 100);
        /* $('#topnews ul').cycle('resume', true); */
    });

    $(".fade").hover(function() {
        $(this).animate({ opacity: 0.40 }, 100);
    }, function() {
        $(this).animate({ opacity: 1 }, 100);
    });

    $("#audiovideo .player a").hover(function() {
        $("#audiovideo .player a img").animate({ opacity: 0.40 }, 100);
    }, function() {
        $("#audiovideo .player a img").animate({ opacity: 1 }, 100);
    });

    $(".advice a").hover(function() {
        $(this).children("p").animate({ opacity: 0.60 }, 100);
    }, function() {
        $(this).children("p").animate({ opacity: 1 }, 100);
    });

    $("#accordion").tabs("#accordion a.pane", { tabs: 'h3', effect: 'slide', initialIndex: null });

    $("#accordion2").tabs("#accordion2 .pane2", { tabs: 'li', effect: 'slide', initialIndex: null });

    $("#accordion3").tabs("#accordion3 .pane2", { tabs: 'li', effect: 'slide', initialIndex: null });

    $('#ml_Benutzer').focus(
	    function() { $('#ml_Benutzer').val('') }
	);
    $('#ml_Benutzer').blur(
		function() { if ($('#ml_Benutzer').val() == '') { $('#ml_Benutzer').val('Email') } }
	);
    $('#ml_Kennwort').focus(
	    function() { $('#ml_Kennwort').val('') }
	);
    $('#ml_Kennwort').blur(
		function() { if ($('#ml_Kennwort').val() == '') { $('#ml_Kennwort').val('123456') } }
	);

    $('.comment_open').click(function() {
        setTimeout(function() {
            $('.commenting').slideToggle('fast');
            $('.comment_open').slideToggle('fast');
            if ($('.commenting').is(':hidden')) {
                $target = $('.commenting');
                var targetOffset = $target.offset().top - 55;
                $('html,body').animate({ scrollTop: targetOffset }, 1000);
            }
        }, 200);
        return false;
    });

});