function channelWidgetNext() {

        var table = $(this).parent().find('.videoCarousel table');
        var carousel = $(this).parent().find('.videoCarousel');

        var q = table.queue("fx");
        if (q.length > 0) {
            return;
        }
        var tableWidth = table.width();
        var visibleWidth = carousel.width();
        var marginLeft = parseInt(table.css("marginLeft"));
        var min = visibleWidth - tableWidth;
        if (marginLeft > min) {
            table.animate({"marginLeft": "-=" + visibleWidth + "px"}, "fast");
            }
}

function channelWidgetPrev() {
        var table = $(this).parent().find('.videoCarousel table');
        var carousel = $(this).parent().find('.videoCarousel');
        var q = table.queue("fx");
        if (q.length > 0) {
            return;
        }
        var visibleWidth = carousel.width();
        var marginLeft = parseInt(table.css("marginLeft"));
        if (marginLeft < 0) {
            table.animate({"marginLeft": "+=" + visibleWidth + "px"}, "fast");
        }
}


$(document).ready(function() {

    /** VIDEO TABS **/ 
    $('#video-info .tab').click(function() {
        $('#video-info .tab-active').removeClass('tab-active');
        $(this).addClass('tab-active');

        $('#video-info .tab-content-active').removeClass('tab-content-active');
        var id = '#video-info #tab-content-' + $(this).attr('id').substring(4);
        $(id).addClass('tab-content-active');
    });


    $('.tooltip').mouseover(function(e) {
        var selector = '#' + $(this).attr('id') + '-tooltip';
        var tooltip = $(selector);
        $('body').append(tooltip.clone(true));
        $('body > .tooltip-text').css('left', (e.pageX + 10 )+ 'px');
        $('body > .tooltip-text').css('top', (e.pageY + 10 ) + 'px');
        $('body > .tooltip-text').css('display', 'block');
    });

    $('.tooltip').mouseout(function() {
        $('body > .tooltip-text').remove();
    });


    $(".EventsWidget .next").click(channelWidgetNext);
    $(".EventsWidget .prev").click(channelWidgetPrev);
    $(".ChannelWidget .next").click(channelWidgetNext);
    $(".ChannelWidget .prev").click(channelWidgetPrev);
 
    $(".LabelChannelWidget .next").click(function() {
        var table = $(".ChannelCarousel table");
        var q = table.queue("fx");
        if (q && q.length > 0) {
            return;
        }
        var tableWidth = table.width();
        var visibleWidth = $(".ChannelCarousel").width();
        var marginLeft = parseInt(table.css("marginLeft"));

        var min = visibleWidth - tableWidth;
        if (marginLeft > min) {
            table.animate({"marginLeft": "-=160px"}, "fast");
        }
    });
    $(".LabelChannelWidget .prev").click(function() {
        var table = $(".ChannelCarousel table");
        var q = table.queue("fx");
        if (q.length > 0) {
            return;
        }

        var marginLeft = parseInt(table.css("marginLeft"));
        if (marginLeft < 0) {
            table.animate({"marginLeft": "+=160px"}, "fast");
        }
    });

    $(".NewsletterWidget .header").click(function() {
        $(".NewsletterWidget .content").slideToggle('slow');
    });

    $('.rate').mouseleave(function() {
        $('.rating').show();
    });
    $('.rate').mouseenter(function() {
        $('.rating').hide();
    });


    /** aufklappen der Felder **/
    $(".button-close").click(function() {
        $(this).parent().slideUp();
    });
    $('.button-embed').click(function() {
        $('.video-embed-form').slideToggle('slow');
        $('.video-comments-form').slideUp('slow');
        $('.video-mail-form').slideUp('slow');
    });
    
    $('.button-mail').click(function() {
        $('.video-comments-form').slideUp('slow');
        $('.video-embed-form').slideUp('slow');
        $('.video-mail-form').slideToggle('slow');
    });
    $('.button-comments').click(function() {
        $('.video-mail-form').slideUp('slow');
        $('.video-embed-form').slideUp('slow');
        $('.video-comments-form').slideToggle('slow');
    });

    $('#button-playlist-add').click(function() {
        $.post('/__user__/playlist/add/', {'video': document.VideoCommentForm.video.value},
            function(data, textStatus) { $('body > .tooltip-text').html(data); }, 'html');
    });

    /** Kommentare verschicken **/
    $('.video-comments-form .formControls input').click(function() {
        var author = document.VideoCommentForm.comment_author.value;
        var text = document.VideoCommentForm.comment_text.value;
        var video = document.VideoCommentForm.video.value;
        if (! author || ! text) {
            alert('Bitte einen Namen und Text eingeben');
            return false;
        }
        $.post('/__user__/comment/', 
            {'video': video, 'author': author, 'text': text},
            function(data, textStatus) {
                $('#video-comments').html(data);
            }, 'html');
        var num_comments = $('.comment').length + 1;
        $('.button-comments').html('(' + num_comments + ')');
        document.VideoCommentForm.comment_author.value = '';
        document.VideoCommentForm.comment_text.value = '';
        return false;
    });

    /** Video bewerten **/
    $('a.rate').click(function() {
        var id = $(this).attr('id');
        var value = id.substring(5);
        var video = document.VideoCommentForm.video.value;
        $.post('/__user__/rate/', {'video': video, 'rate': value}, 
            function(data) {
                if (data.error) {
                    $('body > .tooltip-text').html(data.error);
                }
                else {
                    $(".rating").attr("id", data.as_class);
                    $(".rating-stars").html("(" + data.num_votes + ")");
                    $('body > .tooltip-text').html(data.msg);
                }
            }, 'json');
    });

    /** Video versenden **/
    $('.video-mail-form .formControls input').click(function(){
        var sender_name = document.VideoMailForm.sender_name.value;
        var sender_email = document.VideoMailForm.sender_email.value;
        var recipient = document.VideoMailForm.recipient.value;
        var text = document.VideoMailForm.text.value;
        var video = document.VideoMailForm.video.value;

        if (!sender_name || ! sender_email || ! recipient || ! text) {
            $.jGrowl('Bitte alle Felder ausfÃ¼llen');
            return false;
        }
        
        $.post('/__user__/mail/', 
            {'sender_name': sender_name, 'sender_email': sender_email, 'recipient': recipient, 'text': text, 'video': video},
            function(data, textStatus) {
                $.jGrowl(data);
            }, 'html');

       return false;
    });

});
