var Livestream = (function(scope) {
    var
    debug=0,

    // needed for flash player
    params = { AllowScriptAccess: 'always', "wmode":"transparent" , "autostart":true},

    isVisible = false,
	testing = false,
    playerInterval = null,
    player= undefined;



    var

    showPlayer = function(width, height) {

		// /index.phtml
		$('#videofull_button_container')
			.css('overflow', 'hidden')
			.fadeIn(150)
			.animate({height: '40px'}, 150);

		if ($('#videoLive_container').length) {

			if (width == null) width = 147;
			if (height == null) height = 110;

			if ( isVisible ) return;
			isVisible = true;
			debug && console.log("[livestream] showing Player");

			playerInterval = setInterval( function() {
				try {
					if (player.startPlayback != NULL) {
						player.startPlayback();
					}
				} catch(e) {}
			},1000);

			if ( ! $('#videoLive').length )
				$('<div>').attr('id','videoLive').appendTo( $('#videoLive_container') );

			var flashvars = { channel: 'godmodetv', play:true };
			swfobject.embedSWF("http://cdn.livestream.com/chromelessPlayer/wrappers/JSPlayer.swf","videoLive", width, height, "9.0.0", "expressInstall.swf", flashvars, params);

		}
    },

    hidePlayer = function() {
		// /index.phtml
		$('#videofull_button_container')
			.css('overflow', 'hidden')
			.animate({height: '1px'}, 150)
			.fadeOut(150);

        isVisible = false;
        debug && console.log("[livestream] hiding Player");

        if ( playerInterval !== null ) {
            clearInterval(playerInterval);
            playerInterval = null;
        }


        swfobject.removeSWF('videoLive');

        player= undefined;

    },



    isLive = function() {
        if ( player === undefined ) return undefined;
        if ( typeof player.isLive !== 'function' ) return undefined;
        return player.isLive();
    },
	
	




    // callback for the flash player (needs to be in global scope)
    livestreamPlayerCallback = function(event) {
        debug && console.log("[livestream] livestreamPlayerCallback received: " + event);


        if (event == "ready") {
            player = document.getElementById("videoLive");


            debug && console.log("[livestream] setting channel and starting playback");
            player.setDevKey('qF2QV9FNKWVHFk9zQ8NOVTK4O1cChDkiJc-KXSn5SmYy5N1UNs7YZUOWLAihRJo_YxAfBpmX6cytSosEojdP4DiPcYhC-jFBo9ZsL8Ck114');
            player.setChannel('godmodetv');
            player.startPlayback();

            player.showPauseButton( false );
            player.showPlayButton( false );
            player.showFullscreenButton(false);




        } else if ( event=='isLiveEvent') {

        } else if ( event=='connectionEvent') {

        } else if ( event=='playbackEvent') {

        }

    };


    scope.livestreamPlayerCallback = livestreamPlayerCallback;


    $( function() {
        if ( !$('#videoLive_container').length && !$('#videofull_button_container').length) return;

        BG.push.subscribe( {event:"state",data:{event:"godmode_livestream"}} ,1, function( d ) {
            try {
                var state = d.data.data.live;
                if ( state || testing) showPlayer();
                else hidePlayer();
            } catch(e) {

            }
        });

    });


    return {
        show:showPlayer,
        test: function(w, h) {testing = true; showPlayer(w, h)},
        hide:hidePlayer,
        isLive:isLive,
        isVisible: function() { return isVisible; },
		setVisibility: function(visible) { return isVisible = visible; },
		clearInterval: function() { clearInterval(playerInterval); playerInterval = null; },
        debug: function (d) {
            console.log("debug ack");
            d = (d===undefined)?1:d;
            debug = d;
        }

    };

})(window);
