/*
---
description: A plugin to build a video Playlist using Mootools and Google API.

license: MIT-style

authors:
- Nunzio Fiore

requires:
- Mootools 1.2.4
- Mootools more Elements.from function.

*/

            var YoutubePL = new Class({

                Implements: [Options, Events],
                options: {select:'',
						  videoContainer:'demoContainer',
						  videos:null},
                initialize: function(options){
                    this.setOptions(options);
					if (this.options.select==""){
						this.buildSelect();
					}
                },

				buildSelect:function(){
					/*
					var vc = this.options.videoContainer;
					var tpl='<div id="videoControls'+vc+'" style="float:left;margin:2px;">\
                        <select id="videoSelection'+vc+'">\
                        </select>\
                    </div>';
					new Elements.from(tpl).inject($(vc),'before');
					var v = this.options.videos;
					v.each(function(el,i){
						$('videoSelection'+vc).grab(new Element('option',{
							'value':el,
							'html':' Video'+(i+1)
						}));
					});
					this.options.select='videoSelection'+vc;
					*/

				},

                updateHTML: function(elmId, value){
                    $(elmId).set('html', value);
                },

                loadVideo: function(){
                    var selectBox = $(this.options.select);
                    var videoID = selectBox.get('value');
                    ytplayer = $("ytPlayer"+this.options.videoContainer);
					if (ytplayer) {
                        ytplayer.loadVideoById(videoID);
                    }
                },

                onYouTubePlayerReady: function(playerId){
                    ytplayer = $("ytPlayer"+this.options.videoContainer);
                    ytplayer.addEvent("error", function(errorCode){
                        alert("An error occured of type:" + errorCode);
                    });
                },

                loadPlayer: function(videoID){

					var so = new SWFObject("http://www.youtube.com/v/" + videoID + "&enablejsapi=1&playerapiid=player1",
											"ytPlayer"+this.options.videoContainer,
											"480", "289", "9", "#ffffff");
					so.addParam("allowScriptAccess", "always");
					so.write(this.options.videoContainer);

                },

                run: function(){
					try {
						hash = this.options.videos[0];
						this.loadPlayer(hash);
					} catch(e) {}

                },
				play: function(oid){
					hash = this.options.videos[oid];
                    this.loadPlayer(hash);
                }

            });

