~jstys-z/helioviewer.org/client5

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
<!DOCTYPE html>
<html lang="en">
<head>
    <title>Helioviewer.org - Latest Movies</title>
    <meta charset="utf-8" />
    <script src="../lib/swfobject/swfobject.js"></script>
    <style type='text/css'>
    	body {
			background-image: url('../resources/images/backgrounds/gradient_v5-optimized.png');
			background-color: #20242F;
			background-repeat: repeat-x;
    		text-align: center;
    	}
    	#header {height: 50px;}
    	#moviePlayer {}
    </style>
</head>
<body>
	<div id="header">
		<h1 id="title">Helioviewer.org - Latest Movies</h1>
	</div>
    <div id="player"></div>

<!-- Main -->
<script src="http://code.jquery.com/jquery-1.7.2.min.js" type="text/javascript"></script>
<script>
	// Global playlist
	var playlist = {
        videos: [],
        current_video: 0,
        player: null
    };

	$(function () {
		// Query params
		var params = {
			action: 'getUserVideos',
			num: 30
		};
	    $.post("../api/index.php", params, parseUserVideos, "json");
	});
	
	function parseUserVideos(response) {
        var params, attrs, startURL, width, height;
        
        playlist.videos = parseYoutubeVideoIds(response);
        
        // Load SWFObject
        params = {
        	allowScriptAccess: "always",
        	allowfullscreen: "true"
       	};
        attrs = {id: "moviePlayer"};
        
        playlist.current_video = 0;
        
        startURL = "http://www.youtube.com/v/" + playlist.videos[0] + "?" + $.param({
        	enablejsapi: 1,
        	playerapiid: "ytplayer",
        	rel: 0
        });
        
        width  = $(window).width() * 0.8;
        height = $(window).height() * 0.8;
        
        swfobject.embedSWF(startURL, "player", width, height, "8", "../lib/swfobject/expressinstall.swf", null, params, attrs);
    }
    
    //suggestedQuality

	// YouTube JavaScript Player Callbacks
	// https://developers.google.com/youtube/js_api_reference
    function onYouTubePlayerReady(playerId) {
        playlist.player = document.getElementById("moviePlayer");
        playlist.player.addEventListener("onStateChange", "onStateChange");
        playlist.player.playVideo();
    }

    function onStateChange(newstate) {
    	if (newstate !== 0) {
			return;		
    	}

		// Re-query latest videos once we get to the end of the loop
    	if ((playlist.current_video + 1) === playlist.videos.length) {
			// Query params
			var params = {
				action: 'getUserVideos',
				num: 30
			};

	    	$.post("../api/index.php", params, function(response) {
	    		playlist.videos = parseYoutubeVideoIds(response);
	    		
	    		loadVideo(0);
	    	}, "json");
    	} else {
    		// Otherwise play the next video in the queue
    		loadVideo((playlist.current_video + 1) % playlist.videos.length);
    	}
    }
    
    function loadVideo(i) {
    	// Load next video
        playlist.current_video = i;
        playlist.player.loadVideoById(playlist.videos[playlist.current_video], null, "highres");
    }
    
    function parseYoutubeVideoIds(response) {
        var ids = [];
        
        $.each(response, function (i, video) {
        	var id = video.url.split("=").pop();        	
        	ids.push(id);
        });
        
        return ids;
    };

</script>
</body>
</html>