
var movie;
var play_pause_btn;
var playing = false;

//flash setup


function pad1(str){
    str = str + '';
    if(str.length === 0){ return '00'; }
    if(str.length === 1){ return '0' + str; }
    return str;
}

function time2str(time){
    time  = Math.round(time);
    var sec = time / 60;
    sec = Math.round((sec - Math.floor(sec)) * 60);
    var min = Math.floor(time / 60);
    logged = true;
    return pad1(min) + ':' + pad1(sec);
}

function play_pause() {
    if(playing){
        if(movie){ movie.pause(); }
    } else {
        if(movie){ movie.resume(); }
    }
}

function makeMouseOver(obj){
    obj.addEvent('mouseenter', function(){
        try{
            obj.removeClass('transOff');
            obj.addClass('transOn');
        }catch(err){
            //console.log(err);
        }
    });
    obj.addEvent('mouseleave', function(){
        try{
            obj.addClass('transOff');
            obj.removeClass('transOn');
        }catch(err){
            //console.log(err);
        }
    });
}

function init_movie(){
    movieDiv = $('movie');
    if(!movieDiv) return;

    try{
        var videoWidth = movieDiv.getProperty('videoWidth');
        var videoHeight = movieDiv.getProperty('videoHeight');
        var playImage = Asset.image('ctrls/play_btn.png', {'class': 'btn_img transOff'});
        var pauseImage = Asset.image('ctrls/pause_btn.png', {'class': 'btn_img transOff'});
        var videoLoaded = false;
        var bytesTotal = 0;
        var bytesLoaded = 0;
        var duration = 0;
        var time = 0;
        var time_view = $('time');
        var load_bar = $('progress_bar');
        var time_bar = $('time_bar');

        var btn_elem = $('play_pause_btn').getChildren()[0];

        movie = new FlashVideo('movie', {
            flash_src:  movieDiv.getProperty('src'),
            src: movieDiv.getProperty('movie'),
            width: movieDiv.getProperty('width'),
            height: movieDiv.getProperty('height'),
            videoWidth: videoWidth,
            videoHeight: videoHeight,
            deblocking: 2,
            smoothing: true,
            onload: function(obj){
                obj.setVideoSize(1,1);
            },

            flashListener: {
                metaData: function(meta){
                    movie.setVideoSize(videoWidth, videoHeight);
                    duration = meta.duration;
                    $('duration').set('text', time2str(duration));
                },
                enterFrame: function(){
                    time = movie.time();
                    time_view.set('text', time2str(time));
                    if(!videoLoaded){
                        bytesTotal = movie.bytesTotal();
                        bytesLoaded = movie.bytesLoaded();
                        var width = videoWidth * bytesLoaded / bytesTotal;
                        if(bytesTotal == bytesLoaded){ videoLoaded = true; }
                        load_bar.setStyle('width', width);
                    }

                    var time_bar_width = videoWidth * time;
                    if(duration > 0){ time_bar_width /= duration; }
                    else{ time_bar_width = 0; }
                    time_bar.setStyle('width', time_bar_width);
                },
                play: function (){
                        playing = true;
                        pauseImage.replaces( play_pause_btn.getChildren()[0] );
                },
                pause: function (){
                        playing = false;
                        playImage.replaces( play_pause_btn.getChildren()[0] );
                },
                netStatus: function(stat){
                    if(stat.info.code == "NetStream.Play.Stop"){
                        movie.pause();
                        movie.seek(0);
                    }
                }
            }
        });

        load_bar.addEvent('click', function(evt){
            var x = evt.page.x - load_bar.getPosition().x;
            movie.seek(x * duration / videoWidth);
        });

        play_pause_btn = $('play_pause_btn');
        play_pause_btn.addEvent('click', play_pause );
        makeMouseOver(play_pause_btn.getChildren()[0]);
        makeMouseOver(playImage);
        makeMouseOver(pauseImage);
        playing = true;

    }catch(e){
        alert(e);
    }
}

window.addEvent('domready', function() {
    init_movie();
});

