/**
 * @author Rafał Wrzeszcz <rafal.wrzeszcz@wrzasq.pl>
 * @copyright 2010 (C) by Rafał Wrzeszcz - Wrzasq.pl.
 * @version 0.0.1
 * @since 0.0.1
 * @package PiesTV
 */

/**
 * Single playlist entry.
 *
 * @author Rafał Wrzeszcz <rafal.wrzeszcz@wrzasq.pl>
 * @copyright 2010 (C) by Rafał Wrzeszcz - Wrzasq.pl.
 * @version 0.0.1
 * @since 0.0.1
 * @package PiesTV
 */
var Entry = Class.create( {
    /**
     * Movie title.
     *
     * @var {String}
     * @version 0.0.1
     * @since 0.0.1
     */
    title: null,

    /**
     * Movie URL.
     *
     * @var {String}
     * @version 0.0.1
     * @since 0.0.1
     */
    movie: null,

    /**
     * Movie link.
     *
     * @var {String}
     * @version 0.0.1
     * @since 0.0.1
     */
    link: null,

    /**
     * Image element.
     *
     * @var {Element}
     * @version 0.0.1
     * @since 0.0.1
     */
    element: null,

    /**
     * Initializes object.
     *
     * @param {Element} element Playlist image element.
     * @version 0.0.1
     * @since 0.0.1
     */
    initialize: function(element)
    {
        var data = /sites\/([0-9]+)\/([0-9a-f]+)\.jpg/.exec( element.readAttribute("src") );

        this.title = element.readAttribute("title");
        this.movie = "/upload/sites/" + data[1] + "/" + data[2] + ".flv";
        this.link = "/index/movie?id=" + data[1];
        this.element = element;
    },

    /**
     * Movie title.
     *
     * @return {String} Movie title.
     * @version 0.0.1
     * @since 0.0.1
     */
    getTitle: function()
    {
        return this.title;
    },

    /**
     * Movie URL.
     *
     * @return {String} Movie URL.
     * @version 0.0.1
     * @since 0.0.1
     */
    getMovie: function()
    {
        return this.movie;
    },

    /**
     * Movie link.
     *
     * @return {String} Movie link.
     * @version 0.0.1
     * @since 0.0.1
     */
    getLink: function()
    {
        return this.link;
    },

    /**
     * Image element.
     *
     * @return {Element} Image element.
     * @version 0.0.1
     * @since 0.0.1
     */
    getElement: function()
    {
        return this.element;
    },

    /**
     * Binds movie action.
     *
     * @param {Player} player Player instance.
     * @param {Element} bar Title bar.
     * @return {Entry} Current instance.
     * @version 0.0.1
     * @since 0.0.1
     */
    bind: function(player, bar)
    {
        // title display
        this.element.observe("mouseover", function() {
            bar.select("p").first().update(this.title).observe("click", function() {
                document.location = this.link;
            }.bind(this) );
        }.bind(this) );

        // movie play
        this.element.observe("click", function() {
            player.play(this.movie);
        }.bind(this) );

        return this;
    }
} );

// FlowPlayer initialization
document.observe("dom:loaded", function() {
    // UI elements
    var player = $("homePlayer");
    var playlist = player.select(".list").first().hide();
    var bar = player.select(".playtitle").first().hide();
    var entries = [];
    var video;

    // effects
    var stopEffects = function()
    {
        Effect.Queues.get("home.player.list").invoke("cancel");
        Effect.Queues.get("home.player.title").invoke("cancel");
    };
    var showPlaylist = function()
    {
        stopEffects();
        Effect.Appear(playlist, {
            duration: 0.4,
            queue: {
                position: "end",
                scope: "home.player.list"
            }
        } );
        Effect.Appear(bar, {
            duration: 0.4,
            queue: {
                position: "end",
                scope: "home.player.title"
            }
        } );
    };
    var hidePlaylist = function()
    {
        stopEffects();
        Effect.Fade(playlist, {
            duration: 0.4,
            queue: {
                position: "end",
                scope: "home.player.list"
            }
        } );
        Effect.Fade(bar, {
            duration: 0.4,
            queue: {
                position: "end",
                scope: "home.player.title"
            }
        } );
    };

    // creates entries
    playlist.select("img").each( function(element) {
        entries.push( new Entry(element) );
    } );

    // initial playback
    var first = entries.first();

    video = $f(player, {
        src: "/javascript/flowplayer/flowplayer.swf",
        wmode: "opaque"
    }, {
        clip: {
            // show playlist
            onFinish: showPlaylist,

            // hide playlist
            onBegin: hidePlaylist
        },

        playlist: [
            // splash image
            {
                url: first.getElement().readAttribute("src")
            },

            // movie clip
            {
                url: first.getMovie(),
                autoPlay: false
            }
        ],

        // recover HTML structure
        onLoad: function() {
            player.insert(playlist).insert(bar);
        }
    } ).play();
    bar.select("p").first().update( first.getTitle() );

    // binds playlist
    entries.each( function(entry) {
        entry.bind(video, bar);
    } );

    // playlist for mouse trigger
    player.observe("mouseover", function(event) {
        if( video.isPlaying() )
        {
            showPlaylist();
        }
    } );
    player.observe("mouseout", function(event) {
        if( !event.relatedTarget.descendantOf(player) && video.isPlaying() )
        {
            hidePlaylist();
        }
    } );

    var content = playlist.select("ul").first();
    var prev = playlist.select(".prevplay").first();
    var next = playlist.select(".nextplay").first();

    // playlist slide
    var single = 74;
    var checkScroll = function()
    {
        // prevplay class
        if(content.scrollTop <= 0)
        {
            prev.removeClassName("active");
        }
        else
        {
            prev.addClassName("active");
        }

        // nextplay class
        if(content.scrollTop >= content.scrollHeight - content.offsetHeight)
        {
            next.removeClassName("active");
        }
        else
        {
            next.addClassName("active");
        }
    };
    prev.observe("click", function() {
        // scroll to previous clip
        if(content.scrollTop > 0)
        {
            content.scrollTop = content.scrollTop - single;
        }

        checkScroll();
    } );
    next.observe("click", function() {
        // scroll to next clip
        if(content.scrollTop < content.scrollHeight - content.offsetHeight)
        {
            content.scrollTop = content.scrollTop + single;
        }

        checkScroll();
    } );
} );

