﻿$(document).ready(ConcursesInit);
function ConcursesInit()
{
	if (typeof $('a.fancybox').fancybox == 'function')
	{
		$('a.fancybox').fancybox();
	}
}
function PollAnswer(work_id)
{
	var data = {};
	data.work_id = work_id;
	var data_string = JSON.stringify(data);
	$.post(site_url + 'concurses', {func: 'PollAnswer', data: data_string}, PollAnswerResult, 'json');
}
function PollAnswerResult(result)
{
	if (result.status == 'ok')
	{
		document.location.href = result.redirect;
		return false;
	}
	else
		alert(result.message);
}
function AnswerAnswer(concurse_id)
{
	var data = {};
	data.concurse_id = concurse_id;
	data.answers = '';
	$('.answer').each(function(i)
	{
		data.answers += 'Ответ ' + (i + 1) + ': ' + $(this).attr('value') + '\n';
	});
	data.fio = $('#fio').attr('value');
	data.tel = $('#tel').attr('value');
	data.mail = $('#mail').attr('value');
	
	var data_string = JSON.stringify(data);
	$.post(site_url + 'concurses', {func: 'AnswerAnswer', data: data_string}, AnswerAnswerResult, 'json');
}
function AnswerAnswerResult(result)
{
	if (result.status == 'ok')
	{
		document.location.href = result.redirect;
		return false;
	}
	else
		alert(result.message);
}
function PollOrder(order)
{
	setCookie('concurses_order', order, 1, '/');
	document.location.reload();
}
function ConcursesCaptchaReload()
{
	$('.captcha').empty();
	$('.captcha').append('<img src="'+site_url+'captcha/'+Math.round(Math.random() * 1000)+'" alt="Защита от роботов" title="Защита от роботов" width="100" height="60" /><br /><br /><a href="javascript: ConcursesCaptchaReload()">Обновить</a>');
}
var image;
function PollFileUpload(id)
{
	$.ajaxFileUpload
	(
		{
			url: site_url+'concurses/upload/',
			secureuri: false,
			fileElementId: id,
			dataType: 'json',
			success: function (data, status)
			{
				if (data.action == 'ok')
				{
					image = data.name;
					$('#upload').html('Изображение загружено');
				}
				else
					alert(data);
			},
			error: function (data, status, e)
			{
				alert(e);
			}
		}
	)
}
function PollImageAdd()
{
	var data = {};
	data.image = image;
	data.text1 = $('#text1').attr('value');
	data.text2 = $('#text2').attr('value');
	data.fio = $('#fio').attr('value');
	data.tel = $('#tel').attr('value');
	data.mail = $('#mail').attr('value');
	data.captcha = $('#captcha').attr('value');
	var data_string = JSON.stringify(data);
	$.post(window.location+'/', {func: 'PollImageAdd', data: data_string}, PollImageAddResult, 'json');
}
function PollImageAddResult(result)
{
	if (result.status == 'ok')
	{
		alert("Спасибо\nПосле проверки администрацией сайта Ваша работа будет опубликована");
		document.location.href = result.redirect;
		return false;
	}
	else
		alert(result.message);
}
function PollCheckText1()
{
	count = $('#text1').attr('value').length;
	if (count <= 30)
	{
		$('.text1_count').css('backgroundColor', '#83B108');
		$('.text1_count').css('width', count * 2.5 + '%');
	}
	if (count > 30 && count < 40)
	{
		$('.text1_count').css('backgroundColor', '#f7dc21');
		$('.text1_count').css('width', count * 2.5 + '%');
	}
	if (count >= 40)
	{
		$('.text1_count').css('backgroundColor', 'red');
		$('.text1_count').css('width', '100%');
	}
	$('#text1').attr('value', $('#text1').attr('value').substr(0, 40));
}
function PollCheckText2()
{
	count = $('#text2').attr('value').length;
	if (count <= 260)
	{
		$('.text2_count').css('backgroundColor', '#83B108');
		$('.text2_count').css('width', count / 3 + '%');
	}
	if (count > 260 && count < 300)
	{
		$('.text2_count').css('backgroundColor', '#f7dc21');
		$('.text2_count').css('width', count / 3 + '%');
	}
	if (count >= 300)
	{
		$('.text2_count').css('backgroundColor', 'red');
		$('.text2_count').css('width', '100%');
	}
	$('#text2').attr('value', $('#text2').attr('value').substr(0, 300));
}

