//
(function() {
    var ns = jwyre.module("wendigo");
    var vns = jwyre.module("wendigo.VideoLoader");
        
    var isDebug = jwyre.parseBoolean("false", false);
    ns.loadMappings("wendigo", "do");
    var req = ns.REQUEST_GET_VIDEO;
	
	// maintain mapping of player ids to playlists
	vns._playlists = {};
	
    /**
     * Used to load the JSON rendering of the Video Player with given appId, via Ajax.
     * 
     * @param {string} appId
     * @param {function} callback
     */
    vns.loadVideo = function(appId, callback) {	   
       // by default, use Flowplayer
       callback = (!callback) ? vns.createFlowplayer : callback;
       
       ns.doAjax({
           "request" : req,
           "paramAdder" : function(qry) {
               qry.add("appId", appId);         
           },
           "doWait" : false,
           "callback" : function(json) {
               var videoPlayer = JSON.parse(json.videoPlayer, function(k,v) { return v; });
               callback(videoPlayer);           
           }
       });
    };
	
	/**
	 * Used to load a Flowplayer instance using the given config object.
	 * 
	 * @param {Object} videoPlayer
	 */
	vns.createFlowplayer = function(videoPlayer) {
		var id = videoPlayer.tagId;
		var playlist = videoPlayer.items;
		// need to convert internal props to Flowplayer props in playlist
		for (var i = 0; i < playlist.length; i++) {
			var clip = playlist[i];
			clip.url = clip.link || clip.url;
			clip.title = clip.name || clip.title;
		}
		vns._playlists[id] = playlist;
		
        $f(id, "/_wendigoHtml/scripts/core/flowplayer/flowplayer.swf", {
            clip : buildClip(),
            playlist : playlist,
            plugins : {
                controls : {                            
                    url: 'flowplayer.controls.swf',
                    autoHide : false,
                    playlist: true
                },
                myContent : {
                    url: "flowplayer.content.swf",
                    height: "75%",
                    style : {
                        "a" : {
                            display: "block",
                            fontSize: "12",
                            fontWeight : "bold",
                            fontFamily: "Arial, sans-serif",
                            textDecoration : "none",
                            marginLeft : "5"
                        },                                  
                        "a:hover" : {
                            textDecoration : "underline"
                        },
                        ".sub" : {
                            fontSize : "10",
                            marginLeft : "10"
                        }
                    }
                }
            },
            onLoad : function() {
                content.hide();
            }
        });
		
        var player = $f(id);
        var content = player.getPlugin("myContent");
        var html = $("<p></p>");
        jwyre.array(player.getPlaylist()).each(function() {
            $(html).append("<a href='javascript:wendigo.video.loadClip(\""+id+"\", "+this.index+")'>"+this.title+"</a><br /><span class='sub'>("+this.url+")</span><br />");
        });
        html = html[0].innerHTML;
        
        player.play();
        //TODO: this is a temp hack...need to figure out hot to display player
        // w/out playing
        function _() {
        	if (player.isLoaded()) {
        		player.stop();
        		return;
        	}
	        window.setTimeout(_, 20);        	
        }
        _();
                
        function buildClip(obj) {
            var clip = {
                baseUrl : "http://www.hopesb.org/_uploads/video/",
                bufferLength : 3,
                duration : 0,
                fadeInSpeed : 1000,
                fadeOutSpeed : 1000,
                scaling : "scale",
                subTitle: "",
                onStop : showPlaylist,
                onFinish : showPlaylist
            };
            if (obj) {
                for (var i in obj) {
                    if (obj.hasOwnProperty(i)) {
                        clip[i] = obj[i];
                    }
                }
            }
            return clip;
        }
		function showPlaylist(clip) {
		    var pl = this.getPlaylist();
		    if (clip.index == pl.length - 1) {
		        player.getPlugin("screen").hide();
		        content.setHtml(html);
		        content.fadeIn(1000);
		    }
		}
	    /**
	     * Used to load a clip with the provided index for the Flowplayer with the 
	     * given id.
	     * 
	     * @param {Object} player The id of the Flowplayer instance.
	     * @param {Object} index The Clip to play's index.
	     */
	    jwyre.module("wendigo.video").loadClip = function(player, index) {
	        var playlist = vns._playlists[player];
	        $f(player).setClip(buildClip(playlist[index]));
	        $f(player).getPlugin("myContent").fadeOut(500,
	           function() {
	                $f(player).getPlugin("screen").show();
	                $f(player).play();
	           }
	        );
	    };		
    };
})();
