/* DATEV Unternehmen online objects
------------------------------------------------------------------------------------------------ */


$.datev_uo = function() {

    $.datev_uo.ad_video();
    $.datev_uo.demo_video();
    $.datev_uo.slideshow();
    $.datev_uo.product_menu();
    $.datev_uo.analysis_overview();

};

// DATEV ad video

$.datev_uo.ad_video = function() {

    if (!$('#datev_ad_video').length)
        return;

    var flashvars       = {},
        attributes      = {},
        params          = {};

    flashvars.minutes               = 0;
    flashvars.seconds               = 49;
    flashvars.movieurl              = datev_ad_video;
    flashvars.introimageurl         = datev_ad_intro;
    flashvars.videotitle            = 'Illona Mayer, Gastronomin';
    flashvars.autoplay              = 1;
    flashvars.startfullscreen       = 1;
    params.allowfullscreen          = 'true';
    params.wmode                    = 'transparent';

    swfobject.embedSWF(datev_videoplayer, 'datev_ad_video', datev_ad_width, datev_ad_height, '9.0.0', '', flashvars, params, attributes);
};

// DATEV demovideos

$.datev_uo.demo_video = function() {

    var demovideo = '.datev .demovideo';

    if (!$(demovideo).length)
        return;

    $(demovideo).live('click', function() {

        var height      = this.rel.split(',')[1],
            width       = this.rel.split(',')[0];

        window.open(this.href, '', 'dependent=yes, height=' + height + ', location=no, menubar=no, resizable=no, scrollbars=no, status=no, toolbar=no, width=' + width);

        return false;
    });

};

// DATEV slideshow

$.datev_uo.slideshow = function() {

    var demovideo       = '.datev .slideshow .demovideo',
        demovideos      = '.datev .slideshow .demovideos';

    if (!$(demovideos).length)
        return;

    // Create slideshow buttons etc.

    var width = 0,
        demovideolinks = $(demovideo);

    demovideolinks.each(function() {
        width = width + $(this).outerWidth(true);
        $(this).clone().appendTo($(demovideos));
    });

    var slideshow_box = $(demovideos).css({
        'width' : width * 2
    }).wrap(
        $('<div>').addClass('slideshow_box')
    );

    $(slideshow_box).after(
        prev = $('<a>').attr({
            'class'     : 'btn previous',
            'href'      : '#',
            'title'     : 'Vorriges Video'
        }).text('Vorriges Video'),
        next = $('<a>').attr({
            'class'     : 'btn next',
            'href'      : '#',
            'title'     : 'Nächstes Video'
        }).text('Nächstes Video')
    ).wrap(
        $('<div>').addClass('slideshow_videos')
    );

    // Next and previous image

    $(prev).unbind().click(function() {
        if (!$(demovideos).is(':animated')) {
            var currentMarginLeft = parseInt($(demovideos).css('margin-left'));
            if ($(demovideos).css('margin-left') == 'auto') // IE fix
                currentMarginLeft = 0;
            if (currentMarginLeft == 0) {
                currentMarginLeft = 0-width;
                $(demovideos).css('margin-left', currentMarginLeft);
            }
            var newMarginLeft = currentMarginLeft + $(demovideo).outerWidth(true);
            $(demovideos).animate({
                'margin-left' : newMarginLeft
            });
        }
        return false;
    });

    $(next).unbind().click(function() {
        var currentMarginLeft = parseInt($(demovideos).css('margin-left'));
        if ($(demovideos).css('margin-left') == 'auto')
            currentMarginLeft = 0;
        var newMarginLeft = currentMarginLeft - $(demovideo).outerWidth(true);
        if (!$(demovideos).is(':animated')) {
            $(demovideos).animate({
                'margin-left' : newMarginLeft
            }, function() {
                if (Math.abs(newMarginLeft) >= width)
                    $(demovideos).css('margin-left', 0);
            });
        }
        return false;
    });
};

// DATEV product menu

$.datev_uo.product_menu = function() {

    var product     = '.datev.slidemenu',
        menu        = '.datev.slidemenu .menu';

    if(!$(menu).length)
        return;

    var menuPosition = $(menu).offset().top,
        maxOffset = $(product).outerHeight(true) - $(menu).outerHeight(true);
    
    $(window).scroll(function() {
        if (menuPosition >= $(window).scrollTop()) {
            $(menu).stop().animate({
                'top' :0
            }, 'fast');
        }
        if (maxOffset >= (($(window).scrollTop() - menuPosition) + 20) && menuPosition <= $(window).scrollTop()) {
            $(menu).stop().animate({
                'top' : ($(window).scrollTop() - menuPosition) + 20
            }, 'fast');
        }
    });
};

// DATEV analysis overview

$.datev_uo.analysis_overview = function() {

    var overview = '.datev.analysis_online .accordion';

    if(!$(overview).length)
        return;

    $('.text', overview).hide();

    $('li', overview).each(function() {
        var li = this
        if($('ul', li).length){
            ul = $('> ul', li).css('cursor', 'default').remove();
            atext = $(li).text();
            $(li).toggle(function(){
                $(this).addClass('open').removeClass('close').children('ul').show();
            }, function(){
                $(this).addClass('close').removeClass('open').children('ul').hide();
            }).addClass('close').css('cursor', 'pointer').html(
                $('<a>').attr('href', '#').text(atext).after(ul.hide())
            );
        }
    });

    $('.header', overview).click(function() {
        var el = this;
        if($(el).next('.text').length){
            $(el).next('.text').slideToggle('slow', function(){
                if(!$(el).next('.text').is(':visible'))
                    $(el).parent('.entry').addClass('close').removeClass('open');
            });
            $(el).parent('.entry').addClass('open').removeClass('close');
              $(el).parent('.entry').siblings('.entry:visible').addClass('close').removeClass('open').children('.text').slideUp('slow');
        }
        return false;
    }).not('.notext').css('cursor', 'pointer').not('.notext').wrapInner(
        $('<a>').attr('href', '#')
    );
};

