	var remote = {
	  works: [],
	  flow: [],
	  playlist: [],
	  playlist_to_works: [],
	  has_audio: false,
	  has_video: false,
	  selectors: {
			active_cls: "active",
			playing_cls: "playing",
			disabled_cls: "disabled",
			activated_cls: "activated",
			loading_cls: "loading",
			tools_panel: ".b-player_share",
			share_button: ".b-player_share_button",
			embed_button: ".b-player_embed_button",
			like_button: ".b-action-favorite",
			rating: ".b-rating",
			rating_mark: ".b-rating .stars",
			rating_votes: ".b-rating span",
			track_info_panel: ".b-player-info",
			track_title: ".b-player-info_title",
			track_author: ".b-player-info_author",
			vote_panel: ".b-rating"
	  },	  
	  active_playlist_index: -1,
	  active_flow_index: -1,
	  is_initiated: false,
	  redirect: function(tags_author, tags_track)
	  {
		  if (this.is_initiated)
			  $.get("/work/slugs/" + tags_author + "/" + tags_track, function(data){
				  location.href = data;
			  });
	  },
	  like: {
		  remote: null,
		  status: null,
		  action: null,
		  selectors: {
			pressed_cls: "b-action-pressed"			
	  	  },
	  	  init: function(remote, status)
	  	  {
	  		  this.remote = remote;
	  		  this.update(status);
	  	  },
	  	  update: function(status)
	  	  {
	  		this.status = status;
	    	if (this.status)
	    		$(this.remote.selectors.like_button).addClass(this.selectors.pressed_cls);
	      	else
	      		$(this.remote.selectors.like_button).removeClass(this.selectors.pressed_cls);			        			
	      	$(this.remote.selectors.like_button).removeClass(this.remote.selectors.disabled_cls);
	  	  },
	  	  like: function(action)
	  	  {
	  		  this.action = action;
			  $.getJSON(action, function(like, action){ return function(data) {
				  if (action == like.action)
					  like.update(data.is_liked);
			  }}(this, action));
	  	  }
	  },
	  tabs: {
		  selectors: {
			active_cls: "active",
			tabs_all: ".b-tabs li",
			tabs_description: ".b-tabs_description",
			tabs_comments: ".b-tabs_comments",
			loading_cls: "loading",
			content: ".b-tabs-content"
	  	  },
	  	  comments: {
	  		is_active: false,
	  		action: null,
	  		selectors: {
	  		  form: "#b-comments-form",
	  		  textarea: "#b-comments-form textarea",
	  		  submit_btn: ".b-comments-add-action .b-action",
	  		  disabled_cls: "disabled",
	  		  loading_cls: "loading",
	  		  comments_list: "#comments_list",
	  		  status: ".b-comments-add_status",
	  		  add_wrapper: ".b-comments-add"
	  	  	},
	  		init: function(wrapper) {
	  		  this.action = $(this.selectors.form).attr("action");
	  		  $(this.selectors.submit_btn).click(function(comments){ return function(){
	  			  if (!comments.is_active) return false;
	  			  $(comments.selectors.add_wrapper).addClass(comments.selectors.loading_cls);
	  			  $(comments.selectors.textarea).attr("disabled");
	  			  comments.deactivate();
	  			  $.post(comments.action, {comment: $(comments.selectors.textarea).val()}, function(comments, action){return function(data){
	  				  if (comments.action == action)
	  				  {
	  					  $(comments.selectors.comments_list).prepend(data);
	  		  			  $(comments.selectors.add_wrapper).removeClass(comments.selectors.loading_cls);
	  		  			  $(comments.selectors.status).show();
	  		  			  $(comments.selectors.textarea).removeAttr("disabled").val("");
	  				  }
	  			  }}(comments, comments.action));
	  		  }}(this));
	  		  $(this.selectors.textarea).keyup(function(comments){return function(){
	  			  var comments_is_active = comments.is_active; 
	  			  comments.is_active = ($(this).val().length > 0);
	  			  if (!comments_is_active && comments.is_active)
	  				  comments.activate();
	  			  if (comments_is_active && !comments.is_active)
	  				  comments.deactivate();
	  		  }}(this));
	  		},
	  		activate: function() {
	  			this.is_active = true;
	  			$(this.selectors.status).hide();
	  			$(this.selectors.submit_btn).removeClass(this.selectors.disabled_cls);
	  		},
	  		deactivate: function() {
	  			this.is_active = false;
	  			$(this.selectors.submit_btn).addClass(this.selectors.disabled_cls);
	  		}
	  	  },
		  remote: null,
		  active_tab: null,
		  tabs_exist: false,
		  init: function(remote) {
	  		if ($(this.selectors.tabs_all).length)
	  			this.tabs_exist = true;
		  	this.remote = remote;
		  	this.active_tab = "description";
		  	$(this.selectors.tabs_comments).click(function(tabs){ return function(){
		  		tabs.active_tab = "comments";
		  		tabs.showActiveTab();
		  	}}(this));
		  	$(this.selectors.tabs_description).click(function(tabs){ return function(){
		  		tabs.active_tab = "description";
		  		tabs.showActiveTab();
		  	}}(this));
	  	  },
	  	  showActiveTab: function()
	  	  {
	  		  if (!this.tabs_exist) return false;
	  		  $(this.selectors.content).html("").addClass(this.selectors.loading_cls);
	  		  $(this.selectors.tabs_all).removeClass(this.selectors.active_cls);
	  		  if (this.active_tab == "description")
	  			  this.showDescription();
	  		  if (this.active_tab == "comments")
	  			  this.showComments();
	  	  },
	  	  showDescription: function()
	  	  {
	  		  $(this.selectors.tabs_description).addClass(this.selectors.active_cls);
	  		  var work = this.remote.getActiveWork();
	  		  if (typeof work.description_url != "undefined")
	  			  $.get(work.description_url, function(tabs) { return function(content) {
	  				  if (tabs.active_tab == "description")
	  					  tabs.showContent(content);
	  			  }}(this));
	  	  },
	  	  showComments: function()
	  	  {
	  		  $(this.selectors.tabs_comments).addClass(this.selectors.active_cls);
	  		  var work = this.remote.getActiveWork();
	  		  if (typeof work.comments_url != "undefined")
	  			  $.get(work.comments_url, function(tabs) { return function(content) {
	  				  if (tabs.active_tab == "comments")
	  				  {
	  					  tabs.showContent(content);
	  					  tabs.comments.init();
	  				  }
	  			  }}(this));
	  	  },
	  	  showContent: function(content)
	  	  {
	  		  $(this.selectors.content).removeClass(this.selectors.loading_cls).html(content);
	  	  }
	  },
	  stat: {
		  history: [], 
		  remote: null,
		  timer: null,
		  timer_time: 30000,
		  init: function(remote) {
		  	this.remote = remote;
	  	  },
	  	  start: function(playlist_index) {
	  		this.timer = window.setInterval(function(stat, playlist_index){return function () {
	  			stat.sendPlayed(playlist_index);
	  		}}(this, playlist_index), this.timer_time);
	  	  },
	  	  sendPlayed: function(playlist_index) {
  			  var work = this.remote.getWorkByPlaylistIndex(Number(playlist_index));
	  		  if (typeof this.history[String(work.id)] == "undefined")
	  		  {
  				  clearInterval(this.timer);
  				  this.timer = null;	  			  
	  			  $.get(work.stat_url, {action: "played"}, function(stat, work_id) { return function(content) {
	  				  stat.history[String(work_id)] = true;
	  			  }}(this, work.id));
	  		  }
	  	  },
	  	  sendPaused: function(playlist_index) {
  			  var work = this.remote.getWorkByPlaylistIndex(Number(playlist_index));
  			  $.get(work.stat_url, {action: "paused"}, function(stat, playlist_index) { return function(content) {
  				  
  			  }}(this, playlist_index));
	  	  }	  	  
	  },
	  showcase: {
		  remote: null,
		  init: function(remote) {
		  	this.remote = remote;
	  	  },
		  dispatch: function(action) {
	  		  switch (action)
	  		  {
	  		  	case "init": return this.onInit();
	  		  	case "pause": return this.onPause();
	  		  	case "play": return this.onPlay();
	  		  	case "finish": return this.onFinish();
	  		  }
	  	  },
	  	  onInit: function()
	  	  {
	  		var work = this.remote.getWorkByPlaylistIndex(0);
  		    var scr = {
		  		  config: "scr",
		  		  scrs: {
		  			scr0: {
		  				_img: work.capture
		  			}
		  		  }
	  		};
	  		return scr;
	  	  },
	  	  onPlay: function()
	  	  {
	  		  var work = this.remote.getActiveWork();
	  		  var scr = {
			  		  config: "scr",
			  		  scrs: {
			  			scr0: {
			  				_img: work.capture
			  			}
			  		  }
		  		  };
	  		  if (work.is_video)
	  			  return null;	  		  
	  		  return scr;
	  	  },
	  	  onPause: function()
	  	  {
	  		  var work = this.remote.getActiveWork();
	  		  var ban = null;
	  		  if (typeof work.ad_banner_pic != "undefined" && work.ad_banner_pic)
		  		  ban = {
			  		  config: "ban",
			  		  bans: {
			  			ban0: {
			  				_img: work.ad_banner_pic,
			  				_title: work.ad_banner_title,
			  				_text: work.ad_banner_body.replace('<br />', '\n'),
			  				_link: work.ad_banner_link
			  			}
			  		  }
		  		  };
	  		  remote.stat.sendPaused(remote.active_playlist_index);
	  		  return ban;
	  	  },
	  	  onFinish: function()
	  	  {
	  		  var pic = {
	  		    config: "pic",
			    pics: {
			  	  pic0: {
	  			  	_img: "/player/test_img/pic1.jpg",
	  			  	_title: "Репортаж с лекции 'Будут ли в будущем искусство и художники?' в ЦСИ Гараж.",
	  			  	_text: "cyberbrother",
	  			  	_link: "http://ya.ru"
			  	  }
			    }
		  	  };
	  		  return null;
	  	  }
	  },
	  init: function(works){
		this.is_initiated = false;
		if (!works.length) return false;
		this.active_playlist_index = -1;
		this.has_audio = false;
		this.has_video = false;
		this.works = works;
		var i, j = 0, l = this.works.length;
		for (i=0;i<l;i++)
		{
			if (!this.has_audio && typeof this.works[i].is_audio != "undefined" && this.works[i].is_audio)
				this.has_audio = true;
			if (!this.has_video && typeof this.works[i].is_video != "undefined" && this.works[i].is_video)
				this.has_video = true;
			if (!i)
				this.playlist[j] = {url: this.works[i].path, autoPlay: false, image: false};
			else
				this.playlist[j] = {url: this.works[i].path, autoPlay: true, image: false};
			this.playlist_to_works[j] = i;

			if (typeof this.works[i].rquery != "undefined" && !$(this.works[i].rquery).hasClass(this.selectors.activated_cls))
			    $(this.works[i].rquery).hover(function(remote){ return function(){
			    	if (!$(this).hasClass(remote.selectors.playing_cls))
			    		$(this).addClass(remote.selectors.active_cls);
			    }}(this), function(remote){ return function(){
			    	if (!$(this).hasClass(remote.selectors.playing_cls))
			    		$(this).removeClass(remote.selectors.active_cls);
			    }}(this)).click(function(index, remote){ return function(){
				  	if (remote.active_flow_index > -1)
				  	{
				  		remote.stopFlowPlaylist(remote.active_flow_index);
				  		if (typeof remote.works_initial != "undefined")
				  		{
				  			remote.init(remote.works_initial);
				  			remote.onBegin(index);
				  		}
				  	}
				  	var pi;
				  	for (pi=0;pi<remote.playlist_to_works.length;pi++)
				  		if (remote.playlist_to_works[pi] == index)
				  		{
				  			$f().play(pi);
				  		}
			    }}(i, this)).addClass(this.selectors.activated_cls);
		    
			j++;		    
		}

		if (typeof controls_flow != "undefined" && !this.flow.length)
			this.initFlow(controls_flow);

		if (!$(this.selectors.share_button, $(this.selectors.tools_panel)).hasClass(this.selectors.activated_cls))
			$(this.selectors.share_button, $(this.selectors.tools_panel)).click(function(remote){return function(){
				return false;
				var work = remote.getWorkByPlaylistIndex(remote.active_playlist_index);
				if (!work) return false;
				remote_share(work.id);
			}}(this)).addClass(this.selectors.activated_cls);
		
		if (!$(this.selectors.like_button, $(this.selectors.tools_panel)).hasClass(this.selectors.activated_cls))
			$(this.selectors.like_button, $(this.selectors.tools_panel)).click(function(remote){return function(){
				var work = remote.getWorkByPlaylistIndex(remote.active_playlist_index);
				if (!work) return false;
				remote.like.like(work.like_url);
			}}(this)).addClass(this.selectors.activated_cls);
		
		if (!$(this.selectors.embed_button, $(this.selectors.tools_panel)).hasClass(this.selectors.activated_cls))
			$(this.selectors.embed_button, $(this.selectors.tools_panel)).click(function(remote){return function(){
				var work = remote.getWorkByPlaylistIndex(remote.active_playlist_index);
				if (!work) return false;
				remote_embed(work.id);
			}}(this)).addClass(this.selectors.activated_cls);
		this.showcase.init(this);
		this.initPlayer(this.playlist);
		this.tabs.init(this);
		this.stat.init(this);
		if ((typeof SWFAddress == "undefined") || !SWFAddress.getValue().length)
			this.onBegin(0);
	  },
	  initFlow: function(flow)
	  {
		  if (!flow.length) return false;
		  this.flow = flow;
		  var i, l = this.flow.length;
		  for (i=0;i<l;i++)
		  {
		    $(this.flow[i].rquery).hover(function(remote){ return function(){
		    	if (!$(this).hasClass(remote.selectors.playing_cls))
		    		$(this).addClass(remote.selectors.active_cls);
		    }}(this), function(remote){ return function(){
		    	if (!$(this).hasClass(remote.selectors.playing_cls))
		    		$(this).removeClass(remote.selectors.active_cls);
		    }}(this)).click(function(index, remote){ return function(){
		    	remote.playFlowPlaylist(index);
		    }}(i, this));			  
		  }		  
	  },
	  playFlowPlaylist: function(flow_index)
	  {
		  if (this.active_flow_index > -1)
			  this.stopFlowPlaylist(this.active_flow_index);
		  if (this.active_playlist_index > -1)
			  this.onFinish(this.active_playlist_index);
		  this.active_flow_index = flow_index;
		  
		  $(this.flow[flow_index].rquery).addClass(this.selectors.loading_cls);
		  $.getJSON(this.flow[flow_index].works_url, function(remote, flow_index){ return function(works) {
	        if (flow_index == remote.active_flow_index)
	        {	        	
	        	$(remote.flow[flow_index].rquery).removeClass(remote.selectors.loading_cls).addClass(remote.selectors.active_cls).addClass(remote.selectors.playing_cls);
	        	if (typeof remote.works_initial == "undefined")
	        		remote.works_initial = remote.works;
	        	remote.init(works);
	        	$f().play();
	        }
		  }}(this, flow_index));		  
	  },
	  stopFlowPlaylist: function(flow_index)
	  {
		this.active_flow_index = -1;
      	$(this.flow[flow_index].rquery).removeClass(this.selectors.loading_cls).removeClass(this.selectors.active_cls).removeClass(this.selectors.playing_cls);		  
	  },
	  getWorkByPlaylistIndex: function(playlist_index)
	  {
		  var work = this.works[this.playlist_to_works[playlist_index]]; 
		  return (typeof work != "undefined")? work: false;
	  },
	  getActiveWork: function()
	  {
		  if (this.active_playlist_index > -1)
			  return this.getWorkByPlaylistIndex(this.active_playlist_index);
		  return false;
	  },
      onBegin: function(playlist_index){
		  	if (this.active_playlist_index == playlist_index)
		  		return false;
			if (this.active_playlist_index > -1)
				this.onFinish(this.active_playlist_index);
			var work = this.getWorkByPlaylistIndex(this.active_playlist_index = playlist_index);
			if (!work) return false;
			$(work.rquery).addClass("active").addClass("playing");
			
			$(this.selectors.like_button).addClass(this.selectors.disabled_cls);
			$(this.selectors.tools_panel).removeClass(this.selectors.disabled_cls);

			var addthis_share_config = {};
			if (typeof work.share_url != "undefined")
				addthis_share_config.url = work.share_url;  
//			addthis_share_config.swfurl = "http://cyberbrothers.tv/player/player.swf?config=http%3A%2F%2Fcyberbrothers.tv%2Fplayer%2Fembed%2F" + work.id;
//			addthis_share_config.width = 606;
//			addthis_share_config.height = 363;
						
			addthis.button('.b-player_share_button', {
				ui_language: 'en',
				ui_cobrand: 'cyberbrothers.tv',
				services_compact: 'facebook,twitter,livejournal,email,myspace,google,delicious,favorites'
			}, addthis_share_config);

			addthis.addEventListener('addthis.menu.open', function(pane){
					if ($("#at15s_inner .addthis_direct_link").length)
						$("#at15s_inner .addthis_direct_link").remove();
					$("#at_hover").before('<div class="addthis_direct_link"><span>Cсылка на работу:</span><br /><input type="text" value="' + addthis_share_config.url + '" class="at_item" readonly /></div>');
					$("#at15s_inner .addthis_direct_link input").focus(function(){
						$(this).select();
					});
			});
						
			$(this.selectors.track_title, $(this.selectors.track_info_panel)).html(work.work_link);
			$(this.selectors.track_author, $(this.selectors.track_info_panel)).html(work.author_link);
			$(this.selectors.track_info_panel).removeClass(this.selectors.disabled_cls);
			
			this.tabs.showActiveTab();
			
			if (typeof work_rating != "undefined" && typeof work.rating_url != "undefined" && work.rating_url.length)
			{
			  $.getJSON(work.rating_url, function(remote, playlist_index){ return function(data) {
			        if (playlist_index == remote.active_playlist_index)
			        {	
			        	var work = remote.getWorkByPlaylistIndex(playlist_index);
						$(remote.selectors.rating).removeClass(remote.selectors.disabled_cls);
			        	work_rating.init($(remote.selectors.rating_mark), $(remote.selectors.rating_votes), work.rating_update_url.length? work.rating_update_url: false);
			        	work_rating.setWork(work.id, data.value, data.votes);
			        	if (typeof work.like_url != "undefined" && work.like_url.length && typeof data.is_liked != "undefined")
			        		remote.like.init(remote, data.is_liked);
			        	if (typeof data.cookies_ad_reel != "undefined" && data.cookies_ad_reel)
			        		$.cookie("ad_reels", data.cookies_ad_reel, {expires: 1})
			        }
				  }}(this, this.active_playlist_index));		  				
			}
	  },
	  onFinish: function(playlist_index){
		  this.active_playlist_index = -1;
		  var work = this.getWorkByPlaylistIndex(playlist_index);
		  if (!work) return false;
		  $(work.rquery).removeClass("active").removeClass("playing");
		  $(this.selectors.tools_panel).addClass(this.selectors.disabled_cls)
		  $(this.selectors.track_info_panel).addClass(this.selectors.disabled_cls);			
		  $(this.selectors.rating).addClass(this.selectors.disabled_cls);
	  },
  	filterPlaylist: function(author)
	{
		var playlist = [], playlist_to_works = [];
		var i, j = 0, l = this.works.length;
		for (i=0;i<l;i++)
			if (this.works[i].author == author || !author.length)
			{
				playlist[j] = {url: this.works[i].path, autoPlay: true, image: false};
				playlist_to_works[j] = i;
				$(this.works[i].rquery).show();
				j++;
			}
			else
			{
				$(this.works[i].rquery).hide();
			}
		if (!playlist.length)
			playlist = this.playlist;
		else
			this.playlist_to_works = playlist_to_works;
		this.initPlayer(playlist);
	},
	setCaptionsForIndex: function(index){
		var work = remote.getWorkByPlaylistIndex(Number(index));		
    	if (typeof work.captions != "undefined" && work.captions.length)
    	{
    		$f().getPlugin("captions").loadCaptions(index, work.captions);
    		$f().getPlugin("controls").enable({all: true});
    	}
    	else
    	{
    		$f().getPlugin("controls").enable({caption: false});
    	}		
	},
	initPlayer: function (playlist){
		this.playlist = playlist;
		var plugins = {
		  showcase: { 
            zIndex: 2,
            url: '/player/gamma/flowplayer.showcase.swf'
          },
          fpaddress: {
            zIndex: 0,
            url: '/player/beta/flowplayer.fpaddress.swf'
	        },
          controls: { 
            zIndex: 6,
            title_panel: false,
            url: '/player/beta/flowplayer.controls.swf',
            playlist: true
          }
		};
		if (playlist.length < 2)
			plugins.controls.playlist = false;
		if (true || this.has_audio)
		{
			plugins.audio = {
	            url: '/player/beta/flowplayer.audio.swf',
	            zIndex: 1					
			};
		}
		
		if (this.has_audio && !this.has_video)
		{
			plugins.spectrum = {
	            url: '/player/beta/flowplayer.spectrum.swf',
	            zIndex: 3
			};			
			plugins.controls.spectrum = true;
			plugins.controls.caption = false;
		}
		else
		{
	        plugins.captions = { 
		        zIndex: 5,
	            url: '/player/beta/flowplayer.captions.swf', 
	            captionTarget: 'content'
			}; 
	        plugins.content = { 
		        zIndex: 4,
	            url:'/player/beta/flowplayer.content.swf', 
	            bottom: 20, 
	            height: 60,
	            backgroundColor: 'transparent', 
	            backgroundGradient: 'none', 
	            border: 0, 
	            textDecoration: 'outline', 
	            style: {  
	                body: {  
	                    fontSize: 16,  
	                    fontFamily: 'Calibri, Arial', 
	                    textAlign: 'center', 
	                    color: '#ffffff' 
	                }
	            }
	        }
			plugins.controls.spectrum = false;
			plugins.controls.caption = true;			
		}
	    flowplayer("player", {src: "/player/player.beta.swf", wmode: "opaque"}, {
	        key: '#$317e698a4208f3f851d',
	        logo: {
	        	url: '/images/wmark.png',
	        	fullscreenOnly: false,
	        	zIndex: 7,
	        	top: '45',
	        	right: '45',
	        	opacity: 0.8              
	      	},
	        playlist: playlist,
            onLoad: function(remote){ return function() {            	
        		remote.is_initiated = true;            	
            	remote.setCaptionsForIndex(0);
      		}}(this),
            onFullscreen: function(remote){ return function() {
        		var work = remote.getWorkByPlaylistIndex(remote.active_playlist_index);		
            	if (typeof work.captions != "undefined" && work.captions.length)
            	{
            		this.getPlugin("content").css({body:{fontSize:42},height:160});            		
            	}
      		}}(this),
            onFullscreenExit: function(remote){ return function() {
        		var work = remote.getWorkByPlaylistIndex(remote.active_playlist_index);		
            	if (typeof work.captions != "undefined" && work.captions.length)
            	{
            		this.getPlugin("content").css({body:{fontSize:15},height:60});            		
            	}
      		}}(this),
	        clip: {
	            onStart: function(remote){ return function(clip) {
	      		}}(this),      			
	            onBegin: function(remote){ return function(clip) {
	            	remote.setCaptionsForIndex(clip.index);
	            	remote.onBegin(clip.index);
	            	remote.stat.start(clip.index);
	      		}}(this),
	            onFinish: function(remote){ return function(clip) {
	            	remote.stat.sendPlayed(clip.index);
	            	remote.onFinish(clip.index);
	      		}}(this),
	            scaling: "fit",
	            bufferLength: 8
	        },
	        play: { 
	            url: '/player/gamma/play.swf', 
	            opacity: 0.9,
	            label: null,
	            replayLabel: null, 
	            fadeSpeed: 500,
	            rotateSpeed: 50
	        },
	        canvas:  {
	            backgroundColor:'#000000', 
	            backgroundGradient: [0, 0, 0]
	        },        
	        plugins: plugins
	    });		
	}
}
	
var exposer = {
	is_exposed: false,
	is_initiated: false,
	init: function() {
		if (!$("#player").length) return false;
		this.is_initiated = true;
		$("#player").expose({color: '#000', opacity: 0.9, closeOnEsc: false, closeOnClick: false, onClose: function(){
			$("#player").parents(".b-block.b-block-no-m").css("z-index", 1);
			$("#player").parents(".b-block.b-block-joined-l").css("z-index", 1);
			exposer.is_exposed = false;
		}});
	},
	expose: function() {
		if (!this.is_initiated)
			this.init();
		if (!this.is_exposed)
		{
			this.is_exposed = true;
			$("#player").parents(".b-block.b-block-no-m").css("z-index", "auto");
			$("#player").parents(".b-block.b-block-joined-l").css("z-index", "auto");
	        $("#player").expose().load();
		}
	},
	dexpose: function() {
		if (this.is_exposed)
		{
	        $("#player").expose().close();
		}
	}
}	
function cbPlayerBGLightsOff() { exposer.expose();}
function cbPlayerBGLightsOn() { exposer.dexpose();}
function cbPlayerBGLightsIsOff() { return exposer.is_exposed; }

jQuery(function($) { exposer.init(); });	
	
	function remote_share(work_id)
    {
    	if (work_id)
    	{
        	$f("player").pause();
	        var $player = $("#player");
	        $("#player_shared_form").remove();
	        $("body").append("<div id='player_shared_form' work_id='" + work_id + "'><a class='cls'></a><h1>" + $("#share-header").html() + "</h1><p>" + $("#share-label-email").html() + ":<br /><input type='text' /></p><p>" + $("#share-label-message").html() + ":<br /><textarea></textarea></p><a class='no-decoration-block btn_send'><div class='crbutt m-r0'>" + $("#share-button").html() + "</div></a></div>");
	        $("#player_shared_form .cls").click(function(){
	        	$("#player_shared_form").html("");
	        	$("#player_shared_form").expose().close();
	        	$("#player_shared_form").remove();
	        	$f("player").play();
	        });
	        $("#player_shared_form .btn_send").click(function(){
	        	
	        	if (!$("#player_shared_form input").val().length || !$("#player_shared_form input").val().match(/^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/))
	        	{
	        		$("#player_shared_form input").focus(function(){
	        			$(this).parents("p").removeClass("error");
	        		}).parents("p").addClass("error");
	        		return false;
	        	}
	        	
	        	if ($(this).hasClass("processing"))
	        		return false;
	        	$(this).addClass("processing");
	        	var $form = $("#player_shared_form");
	        	
	        	$("#player_shared_form .btn_send").hide();
  			  	$("#player_shared_form").append("<div class='processing'></div>");
  			  	
	        	$.post("/share",
	        	  {
	        		work_id: $("#player_shared_form").attr("work_id"),
	        		email: $("#player_shared_form input").val(),
	        		message: $("#player_shared_form textarea").val()
	        	  },
	        	  function(resp){
	        		  if (resp == "1")
	        			  $("#player_shared_form .processing").removeClass("processing").addClass("success").html($("#share-label-sent").html());
	        		  else
	        			  $("#player_shared_form .processing").removeClass("processing").addClass("error").html($("#share-label-error").html());
	        	  });
	        });
	        
	        $("#player_shared_form").expose({color: '#000', zIndex: 11999}).css("top", $player.offset().top).css("left", $player.offset().left);
	        $("#player_shared_form").expose().load();
    	}
    }

    function remote_embed(work_id)
    {
    	$f("player").pause();
        var $player = $("#player");
        $("#player_embed_form").remove();
        $("body").append("<div id='player_embed_form'><a class='cls'></a><h1>Embed</h1><p>Скопируйте следующий код и вставьте его на свой вебсайт:</p><textarea>" +
        		"<object width='606' height='363' data='http://cyberbrothers.tv/player/player.swf?config=http%3A%2F%2Fcyberbrothers.tv%2Fplayer%2Fembed%2F" + work_id  + "' type='application/x-shockwave-flash'>" +
	        		"<param name='movie' value='http://cyberbrothers.tv/player/player.swf?config=http%3A%2F%2Fcyberbrothers.tv%2Fplayer%2Fembed%2F" + work_id  + "' />" +
	        		"<param name='allowfullscreen' value='true' />" +
	        		"<param value='always' name='allowScriptAccess'>" +
	        		"<param value='high' name='quality'>" +
	        		"<param value='#000000' name='bgcolor'>" +
        		"</object>" +
        		"</textarea></div>");
        $("#player_embed_form .cls").click(function(){
        	$("#player_embed_form").html("");
        	$("#player_embed_form").expose().close();
        	$("#player_embed_form").remove();
        	$f("player").play();
        });
        $("#player_embed_form").expose({color: '#000', zIndex: 11999}).css("top", $player.offset().top).css("left", $player.offset().left);
        $("#player_embed_form").expose().load();
    }
    
    function player_get_tags_by_playlist_index(playlist_index)
    {   
    	if (typeof remote == "undefined") return false;
    	var work = remote.getWorkByPlaylistIndex(Number(playlist_index));
    	if (typeof work != "undefined")
    		return {tags_author: work.tags_author, tags_track: work.id};
    	return null;
    }
    
    function player_get_title_by_playlist_index(playlist_index)
    {
    	if (typeof remote == "undefined") return false;
    	var work = remote.getWorkByPlaylistIndex(Number(playlist_index));
    	if (typeof work != "undefined")
    		return {title_author: work.author, title_track: work.title};
        return null;    
    }
    
    function player_get_index_by_tags(tags_author, tags_track)
    {
    	var res = false;
    	if (typeof remote == "undefined") return false;
		for (wi=0;wi<remote.works.length;wi++)
			if (remote.works[wi].id == tags_track)
			{
				for (pi=0;pi<remote.playlist_to_works.length;pi++)
					if (remote.playlist_to_works[pi] == wi)
						return String(pi);
				remote.redirect(tags_author, tags_track);
				return null;
			}
		remote.redirect(tags_author, tags_track);
		return null;
    }

    function player_get_title_by_tags(tags_author, tags_track)
    {
    	if (typeof remote == "undefined") return false;
		for (wi=0;wi<remote.works.length;wi++)
			if (remote.works[wi].id == tags_track)
	    		return {title_author: remote.works[wi].author, title_track: remote.works[wi].title};
    }
    
    function player_get_capture()
    {
    	return remote.works[0].capture;
    }
    
    function player_filter_playlist(tags_author)
    {
    	remote.filterPlaylist(tags_author);
    	return true;
    }

    function player_reset()
    {
    	remote.filterPlaylist("");
    	return true;
    }     	
        
    function player_spectrum_configuration(tags_user, tags_title)
    {
    	if (tags_title == "default")
    		return "http://cyberbrothers.tv/player/spectrum_video.xml";
    	if (tags_title == "default2")
    		return "http://cyberbrothers.tv/player/spectrum_video2.xml";
    	return null;
    }
    
    function player_get_showcase(action)
    {
    	if (typeof remote == "undefined") return false;    	
    	return remote.showcase.dispatch(action);
    }
    
    function player_url(url)
    {
    	location.href = url;
    }
