﻿	var playerRemote = {
		is_authenticated: false,
		is_exposed: false,
		works: [],
		work_titles: [],
		works_for_fav: [],
		current_playlist_index: -1,
		init: function(works, work_titles, work_authors, works_for_fav) {
			this.works = works;
			this.work_titles = work_titles;
			this.work_authors = work_authors;
			this.works_for_fav = works_for_fav;
			var wi;
			for (wi=0;wi<this.works.length;wi++)
			{
				this.initPlayAndStopsForWork(this.works[wi], wi);
			}
		},
		getCurrentClipId: function()
		{
			if (this.current_playlist_index > -1)
				return this.works[this.current_playlist_index];
			return 0;
		},
		setWorkForFavById: function(work_id, val)
		{
			for (wi=0;wi<this.works.length;wi++)
				if (this.works[wi] == work_id)
					this.works_for_fav[wi] = val;
			this.checkFavButton();
		},
		setWorkForFavCurrent: function(val)
		{
			if (this.current_playlist_index > -1)
			{
				this.works_for_fav[this.current_playlist_index] = val;
				this.checkFavButton();
			}
		},		
		initPlayAndStopsForWork: function(work_id, playlist_index)
		{
			$(".player_remote_pns_" + work_id).attr("playlist_index", playlist_index).click(function(){
				if ($(this).hasClass("stop"))
				{
					playerRemote.afterClipFinished($(this).attr("playlist_index"));
					$f().stop();
				}
				else if ($(this).hasClass("play"))
				{
					playerRemote.afterClipStarted($(this).attr("playlist_index"));
					playerRemote.playClip($(this).attr("playlist_index"));
				}
			});
		},
		playClip: function(playlist_index)
		{
			var c = Number(playlist_index);
			if (this.works.length == 1)
				c = null;
			$f().play(c);
		},
		afterClipStarted: function(work_index){
			var $artifact_info = $(".artifact-infopanel-content");
			if ($artifact_info.length && this.getCurrentClipId() > 0 && this.current_playlist_index != work_index)
			{
				$("#cur_work_id").val(this.works[work_index]);
				$(".artifact-infopanel-content").addClass("processing").html("");
		        $.ajax({
		            url: "/artifact/" + this.works[work_index] + "/info",
		            work_id: $("#cur_work_id").val(),
		            success: function(result){
		        		if ($("#cur_work_id").val() != this.work_id)
		        			return false;		        	
		        		$(".artifact-infopanel-content").removeClass("processing").html(result);
		                $(".vkladka-p").show();		        		
		            }
		        });								
				$("#work_comments").removeClass("loaded");
				if ($("#work_comments").is(":visible"))
					update_comments_block();
			}
			
			if (this.current_playlist_index != work_index)
				playerRemote.afterClipFinished(this.current_playlist_index);
			
			this.current_playlist_index = work_index;			
			
			this.checkFavButton();
			
			$(".player-title-panel-name").html(this.work_titles[work_index]).show();
			$(".player-title-panel-author").html(this.work_authors[work_index]).show();	
			
			var $remote_btn = $(".player_remote_pns_" + this.works[work_index]);
			$(".player_remote_btn.stop", $remote_btn.parents(".plst_ltbl")).not($remote_btn).each(function(){
				playerRemote.afterClipFinished($(this).attr("playlist_index"));
			});
			$(".showarr", $remote_btn.parents(".work-row")).addClass("showarr-visible");
			$remote_btn.removeClass("play");
			$remote_btn.addClass("stop");		
		},
		afterClipFinished: function(work_index){
			var $remote_btn = $(".player_remote_pns_" + this.works[work_index]);
			$remote_btn.removeClass("stop");
			$remote_btn.addClass("play");
			$(".showarr-visible", $remote_btn.parents(".work-row")).removeClass("showarr-visible");
		},
		favControlIsEnabled: function ()
		{
			return true;
		},
		checkFavButton: function()
		{
			if (!this.is_authenticated)
				return false;
			if (this.current_playlist_index > -1 && this.works_for_fav[this.current_playlist_index] === true)
				this.showFavButton();
			else
				this.hideFavButton();
		},
		hideFavButton: function()
		{
			$f().getPlugin("controls").hideFavButton();
		},
		showFavButton: function()
		{
			$f().getPlugin("controls").showFavButton();
		}		
	}

	function cbPlayerClipIsControlledByRemote()
	{
		return playerRemote.favControlIsEnabled();
	}
	
    function cbPlayerBGLightsIsOff()
    {
        return playerRemote.is_exposed;
    }
	
    function cbPlayerBGLightsOff()
    {
    	playerRemote.is_exposed = true;
        $("body").addClass("lights-off");
        $("#player").expose().load();
    }
    
    function cbPlayerBGLightsOn()
    {
    	playerRemote.is_exposed = false;
        $("body").removeClass("lights-off");
        $("#player").expose().close();
    }
    
    function cbPlayerShare()
    {
    	var work_id;
    	if (work_id = playerRemote.getCurrentClipId())
    	{
        	$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 cbPlayerEmbed()
    {
    	$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" + playerRemote.getCurrentClipId()  + "' type='application/x-shockwave-flash'>" +
	        		"<param name='movie' value='http://cyberbrothers.tv/player/player.swf?config=http%3A%2F%2Fcyberbrothers.tv%2Fplayer%2Fembed%2F" + playerRemote.getCurrentClipId()  + "' />" +
	        		"<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 cbPlayerFav()
    {
    	var work_id;
    	if (work_id = playerRemote.getCurrentClipId())
    	{
    		$(".add_to_fav_btn[item=" + work_id + "]").click();
    		playerRemote.setWorkForFavCurrent();
    	}
    }

    jQuery(function($) {
    	$("#player").expose({color: '#000', opacity: 0.9});
    });    
    
	