var i_url = 'http://yar-life.ru/i/';
var u_url = 'http://yar-life.ru/upload/';
var emotions = {
"1":"смешно",
"2":"интересно",
"3":"красиво",
"4":"удивил",
"5":"не понравилось",
"6":"бред",
"7":"согласен",
"8":"не согласен",
"9":"ужас",
"10":"неправда",
"11":"понравилось",
"12":"правильно"
};
$(document).ready(function()
{
$("#compet_join").click(function(){
$("#assist").show(20);
document.location.href = '#assist';
$("#compet_join").hide();
});
$(".vote_but").click(function(){
var pid = $(this).attr("id").replace(/vote_(\d+)$/, "$1");
$.ajax({
url: '/js/compl_upload.php',
dataType: 'json',
data  : {'action':'vote', 'cuid':pid},
success: function(data) {
if (data.error)
alert(data.error);
else
{
if (data.status == 'ok')
{
$("#vote_"+pid).html('Спасибо! Голос принят.').removeClass('vote_but').unbind("click");
$("#votes").html(data.votes);
}
else
{
$("#vote_"+pid).html('Вы уже голосовали сегодня.').removeClass('vote_but').unbind("click");
}
}
}
});
});
$(".small", '.compl_userphotos').click(function(){
var pid = $(this).attr("id").replace(/photo_(\d+)$/, "$1");
var bpid = $(".big", '.compl_userphotos').attr("id").replace(/photo_(\d+)$/, "$1");
var palt = $(this).attr("alt");
var bpalt = $("#photo_"+bpid).attr("alt");
var cid = $("#cid").val();
var cuid = $("#cuid").val();
$("#photo_"+bpid).attr("src",u_url+"competition/"+cid+"/u"+cuid+"/"+pid+"_med.jpg").attr("id", "photo_"+pid).attr('alt',palt);
$(this).attr("src",u_url+"competition/"+cid+"/u"+cuid+"/"+bpid+"_small.jpg").attr("id","photo_"+bpid).attr('alt',bpalt);
$("#balt").html(palt);
});
$(".del_comp_photo").live("click", function(){
var p_id = $(this).attr("id").replace(/cphoto_(\d+)_\d+/, "$1");
var k = $(this).parent("li").attr("id").replace(/ch_photo_(\d+)/, "$1");
var cu_id = $(this).attr("id").replace(/cphoto_\d+_(\d+)/, "$1");
var cid = $("#cid").val();
var res = confirm("Подтвердите действие.");
if (res)
{
$.ajax({
url: '/js/compl_upload.php',
dataType: 'json',
data  : {'action':'del', 'pid':p_id,'cuid':cu_id},
success: function(data) {
if (data.error)
alert(data.error);
else
{
$("#alert").html('<p class="alert">Фотография удалена</p>');
$("#ch_photo_"+k).html('<form method="post" enctype="multipart/form-data" onsubmit="return false;"><input type="file" name="photo'+k+'" onchange="loadCompetPhoto(this.form.photo'+k+', '+k+', '+cid+');"/></form>');
if ($("img", "#assist").length == 0)
{
$("#submit_comp").hide();
}
}
}
});
}
else
return false;
});
$(".cphoto_alt").live("change", function(){
var p_id = $(this).attr("id").replace(/cphoto_alt(\d+)/, "$1");
var cid = $("#cid").val();
$.ajax({
url: '/js/compl_upload.php',
dataType: 'json',
data  : {'action':'save_text', 'pid':p_id, 'cid':cid,'alt':$(this).val()},
success: function(data) {
if (data.error)
alert(data.error);
else
{
$("#alert").html('<p class="alert">Подпись сохранена.</p>');
}
}
});
});
$("#person_slide_from").val("0");
l_active('manage_person');r_active('manage_person');
$(".emo_do").toggle(function(){
//news_id = $(this).attr("id").replace(/emo_(\d+)/, "$1");
$('#votesn').show();
}, function(){
$('#votesn').hide();
});
$(".emo_vote").click(function(){
news_id = $(this).attr("id").replace(/emovote_\d+_(\d+)/, "$1");
emo_id = $(this).attr("id").replace(/emovote_(\d+)_\d+/, "$1");
$.getJSON("/js/ajax/news/ajax.php", {action:"emo_vote", news_id: news_id, emo_id:emo_id}, function (results){
$("#votes_res").empty().append('<img src="'+i_url+'emo.png" title="Эмоции" alt="Эмоции" width="16" height="16"/>');
$.each(results, function(i,item){
$("#votes_res").append('<span class="emo">'+emotions[item.emotion]+'<sup>'+item.chislo+'</sup></span>&nbsp; ');
});
$("#votesn").empty().hide();
$("#emo_"+news_id).empty();
});
});
$("#add_question").submit(function(){
if ($("#question").val() == '')
{
alert("Не введен вопрос;");$("#question").focus();return false;
}
//return false;
});
});
function loadCompetPhoto(value, k)
{
var f_input = $("#ch_photo_"+k).html();
var cid = $("#cid").val();
var req = new JsHttpRequest();
req.onreadystatechange = function() {
if (req.readyState == 4)
{
if (req.responseText) alert(req.responseText);
if (req.responseJS){
$("#alert").html('');
if(req.responseJS.status == 'fail')
{
alert(req.responseJS.text);
$("#ch_photo_"+k).html(f_input);
if ($("img", "#assist").length == 0)
{
$("#submit_comp").hide();
}
}
else
{
$("#alert").html('<p class="alert">Спасибо, Ваша фотография загружена.</p>');
$("#ch_photo_"+k).html('<img src="'+u_url+'competition/'+cid+'/u'+req.responseJS.cuser+'/'+req.responseJS.photo_id+'_small.jpg" align="middle" class="marg_5"/> <span class="del_comp_photo plink size8" id="cphoto_'+req.responseJS.photo_id+'_'+req.responseJS.cuser+'">удалить</span><br /><input type="text" id="cphoto_alt'+req.responseJS.photo_id+'" class="cphoto_alt" />');
if ($("img", "#assist").length > 0)
{
$("#submit_comp").show();
}
}
}
else
{
$("#alert").html('');
$("#ch_photo_"+k).html(f_input);
}
$("body").css("cursor","auto");
}
}
$("#alert").html('<img src="'+i_url+'l.gif" alt="Загрузка..." title="Загрузка...">');

$("body").css("cursor","wait");
req.open(null, '/js/compl_upload.php', true);
req.send( {action:'load_photo', q:value, 'k':k, 'cid':cid} );
}
