function onYouTubePlayerReady( playerId ) {
    ytplayer = document.getElementById('myytplayer');
    ytplayer.addEventListener('onStateChange', 'onytplayerStateChange');
}

function onytplayerStateChange( newState ) {
    if( parseInt( newState ) == parseInt( 0 ) ){
        //rel aktualnie granego
        var rel = ytplayer.getVideoUrl();
        rel = rel.replace( /http:\/\/www\.youtube.com\/watch\?v\=/, '' ).replace( /\&eurl=http:\/\/energiczniej.com/, '' ).replace( /\&eurl=http%3A%2F%2Fenergiczniej%2Ecom%2F/, '' ).replace( /\&eurl=http:\/\/www\.energiczniej.com/, '' ).replace( /\&eurl=http%3A%2F%2Fwww\.energiczniej%2Ecom%2F/, '' ).replace( /&feature=player_embedded/, '' ).replace( '/', '' ).split( '#' )[0];
        //usuniecie zaznaczenia z aktualnie granego
        $( '.loadvideo[rel=' + rel + ']' ).parent().removeClass( 'active' );
        
        if( !$( '.loadvideo[rel=' + rel + ']' ).hasClass( 'just-played' ) ) {
            //jesli aktualnie grany byl ostatnim
            if( $( '.loadvideo[rel=' + rel + ']' ).parent().hasClass( 'last' ) ) {
                //odtworzenie pierwszego
                var relnew = $( '#more-movies li:first .loadvideo' ).attr( 'rel' );
                $( '.loadvideo[rel=' + relnew + ']' ).parent().addClass( 'active' );
            } else {
                //odtworzenie nastepnego
                var relnew = $( '.loadvideo', $( '.loadvideo[rel=' + rel + ']' ).parent().next() ).attr( 'rel' );
                $( '.loadvideo[rel=' + relnew + ']' ).parent().addClass( 'active' );
            }
            
            $( '#movie-container' ).html( '<div id="movie" style="width:374px;height:301px;"></div>' );
            
            var params = { allowScriptAccess: 'always' };
            // this sets the id of the object or embed tag to 'myytplayer'. You then use this id to access the swf and make calls to the player's API
            var atts = { id: 'myytplayer' };
            swfobject.embedSWF('http://www.youtube.com/v/' + relnew + '&amp;border=0&amp;enablejsapi=1&amp;autoplay=1&amp;showsearch=0&amp;playerapiid=ytplayer', 
                        'movie', '374', '301', '8', null, null, params, atts);
        }
        else
        {
            $( '.loadvideo[rel=' + rel + ']' ).removeClass( 'just-played' );
        }
    }
}

$( document).ready( function() {
    
    $( '#HomeEmail' ).DefaultValue( 'wpisz adres email znajomego' );
    $( '#HomeDiscountEmail' ).DefaultValue( 'Twój email' );
    
    $( document ).pngFix();
    
    $( '.read-article' ).attr( 'target', '_blank' );
    $( '.kup' ).attr( 'target', '_blank' );
    
    $( '#preview' ).click( function ( e ) {
        e.preventDefault();
        
        var relfirst = $( '#more-movies li:first .loadvideo' ).attr( 'rel' );
        
        $( '#movie-container' ).height( 301 );
        
        // allowScriptAccess must be set to allow the Javascript from one domain to access the swf on the youtube domain
        var params = { allowScriptAccess: 'always' };
        // this sets the id of the object or embed tag to 'myytplayer'. You then use this id to access the swf and make calls to the player's API
        var atts = { id: 'myytplayer' };
        swfobject.embedSWF('http://www.youtube.com/v/' + relfirst + '&amp;border=0&amp;autoplay=1&amp;enablejsapi=1&amp;showsearch=0&amp;playerapiid=ytplayer', 
                            'movie', '374', '301', '8', null, null, params, atts);
        
        $( '.loadvideo[rel=' + relfirst + ']' ).parent().addClass( 'active' );
    } );
    
    $( '.loadvideo' ).click( function ( e ) {
        e.preventDefault();
        
        $( '#movie-container' ).height( 301 );
        
        //jesli nie ma playera
        if( typeof ytplayer == "undefined" ) {
            var rel = $( '#more-movies li:first .loadvideo' ).attr( 'rel' );
            $( '.loadvideo[rel=' + rel + ']' ).parent().addClass( 'active' );
            
            //puszczenie filmu
            $( '#movie-container' ).html( '<div id="movie" style="width:374px;height:301px;"></div>' );
            
            var params = { allowScriptAccess: 'always' };
            // this sets the id of the object or embed tag to 'myytplayer'. You then use this id to access the swf and make calls to the player's API
            var atts = { id: 'myytplayer' };
            swfobject.embedSWF('http://www.youtube.com/v/' + rel + '&amp;border=0&amp;enablejsapi=1&amp;autoplay=1&amp;showsearch=0&amp;playerapiid=ytplayer', 
                        'movie', '374', '301', '8', null, null, params, atts);
        } else {
            //rel aktualnie granego filmu
            var rel = ytplayer.getVideoUrl();
            rel = rel.replace( /http:\/\/www\.youtube.com\/watch\?v\=/, '' ).replace( /\&eurl=http:\/\/energiczniej.com/, '' ).replace( /\&eurl=http%3A%2F%2Fenergiczniej%2Ecom%2F/, '' ).replace( /\&eurl=http:\/\/www\.energiczniej.com/, '' ).replace( /\&eurl=http%3A%2F%2Fwww\.energiczniej%2Ecom%2F/, '' ).replace( /&feature=player_embedded/, '' ).replace( '/', '' ).split( '#' )[0];
            
            $( '.loadvideo[rel=' + rel + ']' ).addClass( 'just-played' );
            
            //usuniecie zaznaczenia aktualnie granego ze wszystkich
            $( '#more-movies li' ).removeClass( 'active' );
            
            //rel tego ktory ma byc puszczony
            var rel = $( this ).attr( 'rel' );
            
            //nadanie zaznaczenia na tym ktory ma byc puszczony
            $( this ).parent().addClass( 'active' );
            
            //puszczenie filmu
            $( '#movie-container' ).html( '<div id="movie" style="width:374px;height:301px;"></div>' );
            
            var params = { allowScriptAccess: 'always' };
            // this sets the id of the object or embed tag to 'myytplayer'. You then use this id to access the swf and make calls to the player's API
            var atts = { id: 'myytplayer' };
            swfobject.embedSWF('http://www.youtube.com/v/' + rel + '&amp;border=0&amp;enablejsapi=1&amp;autoplay=1&amp;showsearch=0&amp;playerapiid=ytplayer', 
                        'movie', '374', '301', '8', null, null, params, atts);
        }
    } );
} );
