var voting = false;
$(document).ready(function(){
	
	$('.vote-button').hover(function(){
		$(this).attr('src', $(this).attr('src').replace('-shadow', ''));
	}, function(){
		$(this).attr('src', $(this).attr('src').replace('.png', '-shadow.png'));
	});
	
	$('.promote-vote').hover(function(){
		$(this).attr('src', $(this).attr('src').replace('vote', 'voted'));
	}, function(){
		$(this).attr('src', $(this).attr('src').replace('voted', 'vote'));
	});
	
	
	$('.vote-button').click(function(){
		if(voting == false){
			voting = true;
			str = $(this).attr('src');
			if(str.search(/voted/i) != -1) {
				voting = false;
				return false;
			}
			$(this).attr('src', $(this).attr('src').replace('vote', 'voted'));
			var id = $(this).attr('id').replace('button-vote-', '');
			var duel_id = $('#video-'+id+' .video-duel_id').val();
			var video_id = $('#video-'+id+' .video-video_id').val();
			$.post(webroot+'duels/vote/'+duel_id+'/'+video_id, null,
				function(data){
					$('#duel-'+duel_id+' .video-left .vote-button').attr('src', $('#duel-'+duel_id+' .video-left .vote-button').attr('src').replace('voted', 'vote'));
					$('#duel-'+duel_id+' .video-right .vote-button').attr('src', $('#duel-'+duel_id+' .video-right .vote-button').attr('src').replace('voted', 'vote'));
					if(data.error != '') {
						alert(data.error);
						$('#button-vote-'+id).attr('src', $('#button-vote-'+id).attr('src').replace('voted', 'vote'));
					} else {
						//alert(data.sucess);
						$('#button-vote-'+id).attr('src', $('#button-vote-'+id).attr('src').replace('vote', 'voted'));
						updatevote(data.duelo_id);
					}
					voting = false;
				},
				'json'
			);
		} else {
			alert('Registrando voto');
		}
	});
	/*
	$('.yt-player').each( function(){
		var id = $(this).attr('id');
		alert(id);
		document.getElementById(id).addEventListener("onStateChange", "onytchange");
	});
	*/
});

function updatevote(id) {
	$('#duel-'+id+' .container-bar').clone().appendTo('#duel-'+id).addClass('container-antigo');
	$('#duel-'+id+' .container-antigo').hide();
	$('#duel-'+id+' .container-bar').html('<div class="updating"> Atualizando votos </div>');
	$.post(webroot+'duels/updatevote/'+id, null,
		function(data){
			if(data == '') {
				$('#duel-'+id+' .container-bar').html($('#duel-'+id+' .container-antigo').html());
				$('#duel-'+id+' .container-antigo').remove();
			} else {
				$('#duel-'+id+' .container-bar').html(data);
				$('#duel-'+id+' .container-antigo').remove();
				Cufon.replace(".render",{ fontFamily : "myriadboldcondensed", hover:true });
				$('.bar').corner("left").corner("right");
			}
		}
	);
}

function comment(id, dir) {
	if($("#AuthUser").val() == 'undefined') {
		alert('Você precisar estar logado para comentar');
		return true;
	}
	$('#post-comment-'+dir).clone().attr('id','post-comment-'+dir+'-'+id).appendTo('#comment-'+id+' .comment-info:eq(0)');
	var name = $('#comment-'+id+' .name').html();
	$('#post-comment-'+dir+'-'+id+' label').html('Comentário em resposta a <b>'+name+'</b>');
	$('#post-comment-'+dir+'-'+id+' #CommentParentId').val(id);
	$('#post-comment-'+dir+'-'+id+' #CommentComment').focus();
	$('#post-comment-'+dir+'-'+id+' input[type="button"]').remove();
	$('<input type="button" onclick="javascript:post_comment(\'post-comment-'+dir+'-'+id+'\');" value="Enviar"><input type="button" onclick="javascript:cancelar('+id+',\''+dir+'\');" value="Cancelar">').appendTo('#post-comment-'+dir+'-'+id+' form');
	//$('#post-comment-'+dir+'-'+id' #CommentComment').bind('blur', function(){ if($('#post-comment-'+dir+'-'+id' #CommentComment').val() == '') { $('#post-comment-'+dir+' #CommentParentId').val(0); $('#post-comment-'+dir+' label').html('Comentário:'); } });
}

function cancelar(id, dir) {
	$('#post-comment-'+dir+'-'+id).remove();
}

function denunciar(id) {
	if($("#AuthUser").val() == 'undefined') {
		alert('Você precisar estar logado para denunciar');
		return false;
	}
	if(confirm('Deseja denunciar o comentário #'+id+'?')) {
		location.href = $("#den-cm-"+id).attr('href');
	} else {
		return false;
	}
}

function promote(id, vote) {
	$.post(webroot+'comments/vote/'+id+'/'+vote, null,
		function(data){
			if(data.error != '') {
				alert(data.error);
			} else {
				$('#promote-off-'+id).attr('src', $('#promote-off-'+id).attr('src').replace('voted', 'vote'));
				$('#promote-ok-'+id).attr('src', $('#promote-ok-'+id).attr('src').replace('voted', 'vote'));
				if(vote=='N'){
					$('#promote-off-'+id).attr('src', $('#promote-off-'+id).attr('src').replace('vote', 'voted'));
					var val = Number($('#votes-'+id).val());
					val = val-1;
				} else {
					$('#promote-ok-'+id).attr('src', $('#promote-ok-'+id).attr('src').replace('vote', 'voted'));
					var val = Number($('#votes-'+id).val());
					val = val+1;
				}
				$('#votes-'+id).html(val);
				if(val > 0) {
					$('#votes-'+id).removeClass('neg').addClass('pos');
					$('#votes-'+id).html('+ '+val);
				} else {
					$('#votes-'+id).removeClass('pos').addClass('neg');
				}
			}
		},
		'json'
	);
}

function post_comment(type) {
	$.post(webroot+'comments/add', $('#'+type+' form').serialize(),
		function(data){
			if(data.error != '') {
				alert(data.error);
			} else {
				$('#comment-0').remove();
				$('#'+type+' form textarea').val('').focus().blur();
				var append = '';
				if(data.parent_id == "0") {
					append = '#'+type;
					$.post(webroot+'comments/show/'+data.comment_id+'/'+type, null, function(html){$(append).before(html); $('#comment-'+data.comment_id).hide().fadeIn('slow'); });
				} else {
					append = '#comment-'+data.parent_id;
					$.post(webroot+'comments/show/'+data.comment_id+'/'+type, null, function(html){$(append).append(html); $('#comment-'+data.comment_id).hide().fadeIn('slow'); });
					$('#'+type).remove();
				}
			}
		},
		'json'
	);
}

function onYouTubePlayerReady(playerId) {
	ytplayer = document.getElementById(playerId);
	var str = "onytchange"+playerId;
	ytplayer.addEventListener("onStateChange", str);
	$(".yt-top-img").show();
	//ytplayer.pauseVideo();
	//ytplayer.seekTo(0.5, true);
}
