jQuery(function($) {
    //prebeta
	var works = [];
	var playlist = [];
	if (typeof playlist_search_works != "undefined")
		works = playlist_search_works;
	var i, l = works.length;
	for (i=0;i<l;i++)
		playlist[i] = {url: works[i].path, autoPlay: true, image: false};
	remote.init(works, playlist);
});		
		
    var remote = {
    	works: [],
    	playlist: [],
    	init: function(works, playlist)
    	{
    		this.works = works;
    		this.playlist = playlist;
    	},
    	old_remote: {
    		works: null,
    		work_titles: null,
    		work_authors: null,
    		works_for_fav: null,
    	},
    	old_remoteReset: function(playlist)
    	{
    		if (this.old_remote.works == null)
    			this.old_remote.works = playerRemote.works; 
    		if (this.old_remote.work_titles == null)
    			this.old_remote.work_titles = playerRemote.work_titles; 
    		if (this.old_remote.work_authors == null)
    			this.old_remote.work_authors = playerRemote.work_authors; 
    		if (this.old_remote.works_for_fav == null)
    			this.old_remote.works_for_fav = playerRemote.works_for_fav;
    		var work_id, i, w, k = 0, l = playlist.length, lw = this.works.length, lrw = this.old_remote.works.length;
    		var works = [], work_titles = [], work_authors = [], works_for_fav = [];
    		for (i=0;i<l;i++)
    		{
    			work_id = 0;
    			for (w=0;w<lw;w++)
    				if (this.works[w].path == playlist[i].url)
    					work_id = this.works[w].id;
    			if (Number(work_id))
        			for (w=0;w<lrw;w++)
        				if (this.old_remote.works[w] == work_id)
        				{
        					works[k] = this.old_remote.works[w];
        					work_titles[k] = this.old_remote.work_titles[w];
        					work_authors[k] = this.old_remote.work_authors[w];
        					works_for_fav[k] = this.old_remote.works_for_fav[w];
        					k++;
        				}
    		}
    		playerRemote.init(works, work_titles, work_authors, works_for_fav);
    	},
    	player: {
    		init: function(playlist)
    		{    	
    			flowplayer("player", {src: "/player/player.swf", wmode: "opaque"}, {
    				key: '',
    				logo: {
    					url: '/images/wmark.png',
    					fullscreenOnly: false,
    					zIndex:2,
    					bottom: '60',
    					left: '48',
    					opacity: 0.8
    				},
    				playlist: playlist,
    				canvas:  {
    					backgroundColor:'#000000', 
    					backgroundGradient: [0, 0, 0]
    				},
    				onError: function(errorCode) {  
    					   alert("error code = " + errorCode);
    				},    				
		            clip: {
		                image: false,
		                autoPlay: true,
		                onBegin: function(clip) { 
		                    if (typeof playerRemote != "undefined")
		                    {
		                      playerRemote.afterClipStarted(clip.index);
		                    }
		                },
		                onFinish: function(clip) { 
		                	if (typeof playerRemote != "undefined")
		                	{
		                		playerRemote.afterClipFinished(clip.index);
		                	}
		                },            
		                scaling: "fit"
    				},
    				play: { 
    					url: '/player/play.swf', 
    					opacity: 0.9,
    					label: null, 
    					replayLabel: null, 
    					fadeSpeed: 500,
    					rotateSpeed: 50
    				},     
    				plugins: {
    					audio: {
    						url: '/player/flowplayer.audio-3.1.2.swf',
    						zIndex: 1
    					}, 
    					controls: { 
    						zIndex: 4,
    						title_panel: false,
                            url: '/player/flowplayer.controls-0.0.1.swf',
                            playlist: true
                        },
                        spectrum: {
			                url: '/player/flowplayer.spectrum.swf',
			                zIndex: 2
                        },
                        fpaddress: {
                        	url: '/player/flowplayer.fpaddress.swf',
                        	zIndex: 3
                        }          
    				}
    			});    	
    		}
    	},
    	filterPlaylist: function(author)
    	{
    		var playlist = [];
    		var i, j = 1, l = this.works.length;
			playlist[0] = {url: "/images/player/bus.jpg", autoPlay: true, image: true};
    		for (i=0;i<l;i++)
    			if (this.works[i].author == author)
    			{
    				playlist[j] = {url: this.works[i].path, autoPlay: true, image: false};
    				$(this.works[i].rquery).show();
    				j++;
    			}
    			else
    			{
    				$(this.works[i].rquery).hide();
    			}
    		if (!playlist.length)
    		{
    			playlist = this.playlist;
    			$(".plst_ltbl tr").show();
    		}	
    		console.log(playlist);
    		this.player.init(playlist);
    		this.old_remoteReset(playlist);    		
    	}
    };
    
    function player_filter_playlist(tags_author)
    {
    	remote.filterPlaylist(tags_author);
    	return true;
    }     	

    function player_spectrum_configuration(tags_user, tags_title)
    {
    	return "http://cyberbrothers.tv/player/spectrum_video.xml";
    }     	
    
    function player_reset()
    {
		remote.player.init(remote.playlist);
		remote.old_remoteReset(remote.playlist);    		
    	return true;
    }     	
    
    //~

