var thumbRotTimeout = null;
var thumbRot = null;
var thumbRotIndex = 0;

function thumbnailRotator() {
    if (thumbRot == null)
        return;

    var old = thumbRot.attr('src');
    var path = old.replace(/thumb.*/, "");
    thumbRotIndex += 1;
    index = thumbRotIndex;
    if (index < 10)
        index = '0' + index;

    path = path + 'thumb-' + index + '.jpg';
    thumbRot.attr('src', path);
    if (thumbRotIndex == 10)
        thumbRotIndex = 0;
    thumbRotTimeout = window.setTimeout("thumbnailRotator()", 1000);
}

function showTooltip(e) {
    var text = $(this).attr('title');
    $(this).attr('title', '');
    $('body').append('<div class="tooltip-text">' + text + '</div>');
    $('body > .tooltip-text').css('left', (e.pageX + 10 )+ 'px');
    $('body > .tooltip-text').css('top', (e.pageY + 10 ) + 'px');
    $('body > .tooltip-text').css('display', 'block');
}
function hideTooltip(e) {
    text = $('body > .tooltip-text').text();
    $(this).attr('title', text);
    $('body > .tooltip-text').remove();
}

$(document).ready(function() {
    $(".channel .video img").mouseover(function() {
        thumbRot = $(this);
        thumbnailRotator();
    });

    $(".channel .video img").mouseout(function() {
        window.clearTimeout(thumbRotTimeout);
        thumbRot = null;
        thumbRotIndex = 0;
    });


    /** CHANNEL WIDGET **/
    $(".WidgetSlot-content-lead div.scrollable").scrollable({size: 3, clickable: false}); 
    $("#content-column-two div.scrollable").scrollable({ size: 1, clickable: false}); 
    $("#content-column-one div.scrollable").scrollable({size:2, clickable:false});
    

    /** VIDEO TABS **/ 
    $('.tab').click(function(e) {
        e.preventDefault();
        $('.tab').removeClass('tab-active');
        $(this).addClass('tab-active');

        $('.tab-content-active').removeClass('tab-content-active');
        var id = $(this).attr('href');
        $(id).addClass('tab-content-active');
    });


    $('.tooltip').mouseover(showTooltip);

    $('.tooltip').mouseout(hideTooltip);


    $(".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) {
            alert('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) {
                alert(data);
            }, 'html');

       return false;
    });


    $(".channel .video").mouseenter(function() {
        $(this).find('.front').hide();
        $(this).find('.back').show();
    });
    $(".channel .video").mouseleave(function() {
        $(this).find('.back').hide();
        $(this).find('.front').show();
    });

    $("#event a").attr("target", "_blank");
});

