/***************************************************************************

	Name:		piratplayer.js
    Site:		Pirat TV - http://www.dr.dk/pirat
	Author:		Anders Hellerup Madsen
	Date:		may - july 2009

	Table of Contents:
	=================
	1 - =PiratPlayer
	2 - =CategorysDropdown

****************************************************************************/
/*jslint laxbreak: true, eqeqeq: true, browser: true, undef: true */
/*extern $$, Class, Element */
/*extern Uplayer, trace */
/*global DR, PIRAT_CURRENT_PLAYLIST, ROOT */


/*
 * Class: DR.Ung.PiratPlayer
 *      Wrapper for UPlayer that parses Pirat HTML and generates a player with proper playlists
 *
 *  Arguments:
 *      - el: [element or id] the div to insert the player in
 */
DR.Ung.PiratPlayer = new Class({
    Extends: DR.Ung.UPlayer

    ,options: {
        height: "348",
        width: "620",
        version:"9.0.114",
        smoothing: true,
        swf: UPLAYER,
        //swf: ROOT + 'flash/uplayer.swf',
        autoplay: true,
        useTransitions: false,
        playlist: [],
        modules: [
            { className: "Gemius", parameters: { encoding: 'utf-8', identifier: 'p9AwR.N.S86s_NjaJKdww7b.fdp8ky90ZnrKpgLHOUn.s7', hitCollector: 'http://sdk.hit.gemius.pl', playerId: ''} },
            { className: "JavaScript" },
            { className: "KeyboardShortcuts" },
            { className: "PiratControl", parameters: { playlist: true, fullscreen: true, soundSlider: true } },
            { className: "Persistent", parameters: { pause: true, volume: true } },
        ]
    }

    ,initialize: function (el, options) {

        this.setOptions({ playlist: PIRAT_CURRENT_PLAYLIST });

        this.parent(el, options);
        this.options.modules[0].parameters.playerId = el;

        var list = $$('#clipPlaylist li[class^=clip]');
        var current_idx = list.indexOf( list.filter('.selected')[0] );

        if(current_idx == -1) {
            current_idx = 0;
        }
        if (current_idx !== 0) {
            this.prev = $$('#clipPlaylist li[class^=clip]')[current_idx - 1].getElement('a').get('href'); 
        }

        if (list[current_idx + 1]) {
            this.next = $$('#clipPlaylist li[class^=clip]')[current_idx + 1].getElement('a').get('href'); 
        }

        this.addEvents({
			onComplete: this.complete.bind(this)
			,onModuleEvent: this.moduleEvent.bind(this)
			,onLoad: this.load.bind(this)
			,onStateChange: this.stateChange.bind(this)
        });
    }

    ,complete: function () {
        this.moduleEvent('next');
    }

    ,moduleEvent: function (direction) {
        if (direction === "next" && this.next) {
            this.container.setStyles( this.container.getCoordinates() );
            this.container.set('html', '');
            window.location = this.next;
        } else if (direction === "prev" && this.prev) {
            this.container.setStyles( this.container.getCoordinates() );
            this.container.set('html', '');
            window.location = this.prev;
        }
    }
    ,load: function() {
        this.clipViewRegistered = false;
        this.currentState = "";
    }
    ,stateChange: function(state) {
        if(!this.clipViewRegistered) {
            if(this.currentState == "" && state == "stateBuffering") {
                this.currentState = state; return;
            }
            if(this.currentState == "stateBuffering" && state == "play") {
                this.currentState = state;
                this.clipViewRegistered = true;
                var req = new Request.JSON({
                    url: JSON_API_URL + 'clipwatch/1/' + PIRAT_CURRENT_PLAYLIST[0].id,
                    method: 'POST'
                });
                req.send();
            }
        }
	}
});

Element.implement({
    createPiratPlayer: function (options) {
        return new DR.Ung.PiratPlayer(this, options);
    }
});


