
/*


*/

(function($) {

	 var _player;
	 var _autoPlay;
	 var _defaultVolume;
	 var _vimeoHolder;
	 var _inited=false;
	 var _vimeoVideoIframe;
	 var _videoIFrameSrc;
	 var _vimeoApi = '?api=1';
	 var _self;

	 $.vimeoPlayer = function(data) {
		 
		_autoPlay = data.autoPlay;
		_defaultVolume = data.defaultVolume;
		_vimeoHolder=data.vimeoHolder;
		 
		var vm = new ap_VimeoPlayer(data.mediaPath);
		return vm;
	 };
	 
	 function ap_VimeoPlayer(id){
		 
		_self = $(this);
		
		_videoIFrameSrc = 'http://player.vimeo.com/video/' + id + _vimeoApi;
		
		_vimeoVideoIframe = $('<iframe />', {
			frameborder: 0,
			src: _videoIFrameSrc,
			width: 100 + '%',
			height: 100 + '%'
		});
		
		_vimeoHolder.css('display', 'block');
		_vimeoHolder.append(_vimeoVideoIframe);
		
		_player = Froogaloop(_vimeoVideoIframe[0]);
		_player.addEvent('ready', onVimeoReady);

	 };
	
	 ap_VimeoPlayer.prototype = {
		initVideo:function(id) {
			if(_player){
				_vimeoHolder.css('display', 'block');
				_videoIFrameSrc = 'http://player.vimeo.com/video/' + id + _vimeoApi;
				_vimeoVideoIframe.attr('src', _videoIFrameSrc);
			}
		},
		stop:function() {
			
		},
		play:function() {
			_player.api('play');
		},
		pause:function() {
			_player.api('pause');
		},
		seek:function(val) {
		
		},
		isMuted:function() {
			
		},
		getDuration:function() {
			
		},
		setVolume:function(val) {
			_vimeoPlayer.api('setVolume', val);
		},
		clean:function() {
			_player.removeEvent('finish', onVimeoFinish);
			_vimeoVideoIframe.attr('src', '');
			_vimeoHolder.css('display', 'none');
		}
	}
	
	function onVimeoReady(playerID) {
		//console.log('this._onVimeoReady');
		_player.api('setVolume', _defaultVolume);
		_player.addEvent('finish', onVimeoFinish);
		
		if(!_inited){
			_inited=true;
		}
		if(_autoPlay) _player.api('play');
	}
			
	function onVimeoFinish() {
		//console.log('this._onVimeoFinish');
		_self.trigger('ap_VimeoPlayer.END_PLAY');
	}
			

})(jQuery);



