/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

$().ready( function() {


    // Player Init

    VideoJS.setupAllWhenReady();

    var v = document.getElementById("html5Player");

    $("video").bind("ended", function() {
       
        v.pause();
        afterShowLayer();
    });
    
    $('video').bind('play', function(event, param1, param2) {
        vid = $('#vid').attr('title');
        $.ajax({
            url: "/frontend/ajax/mediaPlayCount.php?vid=" + vid
        });
    });



    $('#btn_replay').click(function() {
        playAgain();
        v.currentTime = 0;
        v.play();
    });
    
    $("#moreInfo").bind("click", function(event) {
        
        event.preventDefault();
        afterShowLayer();
        v.pause();
        
    });



    $('#star').raty({
        start : 3,
        half:  true,
        path : '/resources/img/raty/',
        click : function(score, evt) {
            $.ajax({
                url: "/frontend/ajax/rate.php?rate=" + score + "&vid=" + $('#vid').attr('title') ,
                context: document.body,
                success: function(response){
                    //$.fn.raty.click(response, '#star');
                    $.fn.raty.readOnly(true, '#star');
                    //alert('Vielen Dank für Ihre Bewertung.' + response);
                    $('#average').replaceWith(response);

                    

                }
            });

            


            $('#rateResult').html('Vielen Dank für Ihre Bewertung')
        },
        starHalf:     'stern_halb.gif' ,                                 // The image of the half star.
        starOff:      'stern_leer.gif' ,                                  // Name of the star image on.
        starOn:       'stern_voll.gif' ,
        hintList:     ['schlecht', 'geht', 'normal', 'gut', 'hervorragend']

    });


  
    // Carousel Init
    jQuery('#carousel').jcarousel({
        scroll : 1,
        start : 1,
        auto: 0,
        wrap: 'circular',
        animation: 'slow'
    });

    $('a[rel*=facebox]').facebox({
        loadingImage : '/resources/img/link-arrow-red.jpg',
        closeImage   : '/resources/css/skins/facebox-sfptv/btn_schliessen.gif',
        style : 'left:400px, top: 120px'

    });



    // carouselItems mouseover mediaItem
    var $paneTarget = $('#pane-target');

    $('.mediaItem').mouseenter(function(){
        $(this).stop().scrollTo( $('#head'), 600 );
    });
    
    
    
    $('.mediaItem').mouseleave(function(){
        $(this).stop().scrollTo( 0, 600 );
    });

    /*$('.mediaItemPlayer').mouseenter(function(){
        $(this).stop().scrollTo($('#head1'), 600 );
    });*/

    $(".mediaItemPlayer").bind("mouseenter", function(event) {
        id = $(this).attr('id');
        elm = '#head' + id;




        $(this).stop().scrollTo($(elm), 600 );
       
    //event.preventDefault();
    //afterShowLayer();
    //v.pause();

    });


    $('.mediaItemPlayer').mouseleave(function(){
        $(this).stop().scrollTo( 0, 600 );
    });

    //Navigation Mouseover
    $('.item').bind("mouseenter",function(event){
        id = $(this).attr('id');
        elm_over = '#over_' + id;
        elm_out = '#img_' + id;

        //$(elm_out).css('display', 'none');
        //$(elm_over).css('display', 'block');

        
        $(elm_over).fadeIn(500, function() {
               //alert('test');
               //$(elm_out).css('display', 'none');
        });
     
        //$(elm_out).fadeOut(300);

    });

    $('.item').bind("mouseleave",function(event){
        id = $(this).attr('id');
        elm_over = '#over_' + id;
        elm_out = '#img_' + id;

        //$(elm_out).css('display', 'block');
        //$(elm_over).css('display', 'none');
        
        $(elm_out).fadeIn(500, function() {
           $(elm_over).css('display', 'none');
       
    });
     //$(elm_over).fadeOut(300);
        

});


//Right Dock: Credits
$('#rightDock').click( function() {
    if($('#rightDock').hasClass('')) {

        $('#rightDock').addClass('open');
        $('#rightDock').animate({
            width: '300px'
        });
        $('#rightDock .content').fadeIn(1000);

    } else {

        $('#rightDock .content').fadeOut();
        $('#rightDock').removeClass('open');
        $('#rightDock').animate({
            width: '28px'
        });
    }
});

$("#searchSlot").autocomplete("/frontend/ajax/ac_search.php", {
    minChars: 1,
    width: 260,
    max: 8,
    highlight: false,
    scroll: true,
    scrollHeight: 600,

    formatItem: function(data, i, n, value) {
           
        var html ='';
        //mediaId = value.split(';')[0];
        //imageUrl = value.split(';')[1];
        //mediaTitle = value.split(';')[2];
        //mediaDesc = value.split(';')[3];
        //alert(mediaTitle);

        html += "<div class='ac_media'>";
            
        html += "<div class='ac_image'>";
        html += "<a href='index.php?vid=" + value.split(";")[0] + "'>";
        html += "<img src='" + value.split(";")[1] + "' width='62' height='37' /></a></div>";
        html += "<div class='ac_title'><h1>" + value.split(";")[2] + "</h1>" + value.split(";")[3] + "</div>"
                        
        //html +=  "ID: " + value.split(';')[0] + "<img src='/resources/img/" + value.split(";")[1] + "' width='62' height='37' /> ";
        //html += value.split(";")[2] + value.split(";")[3] ;
        html += '<div style="clear:left;"></div>'
        html += '</div>';
        return html;
    },
    formatResult: function(data, value) {
        //alert("formatResult value:" + value);
        $('#mediaId').attr('value', value.split(";")[0]);
        return value.split(";")[2];
        "formatResult:" + data
    }
});

$("#searchSlot").change(function() {
   
    $("#search").submit();
});

});


function afterShowLayer() {

    $("video").stop();
    $("#videoStage").hide();
    $("#afterShowLayer").fadeIn(1000);
}

function playAgain() {
    //alert('change');
    $("video").stop();
    $("#videoStage").fadeIn(1000);
    $("#afterShowLayer").fadeOut(500);

}

function playCount() {
    alert('playCount');
}



