/**
 * Vote box
 */
$(function(){

	$('.vote_up').click(function(){

		element_id = $(this).attr('rel');

		$.get('vote_action.php',
			  {id:element_id, type:'comment', vote:1}, 
			  function(data){

				$('div#result_' + element_id).html(data);

			  }
		)

		deactivate_vote_box(element_id);
		return false;
	});


	$('.vote_dwn').click(function(){

		element_id = $(this).attr('rel');

		$.get('vote_action.php',
			  {id:element_id, type:'comment', vote:0}, 
			  function(data){
				$('div#result_' + element_id).html(data);
			  }
		)

		deactivate_vote_box(element_id);
		return false;
	});


	function deactivate_vote_box(element_id)
	{
		$('#vote_box_' + element_id).css('display', 'none');
		$('#vote_box_' + element_id + '_inactive').css('display', 'block');
	}


	//////////////////////////////////////////////
	// Questions - activate time counter
	$('div.meanwhileCalculator').each(function(){

		var pointer = this;
		var meanwhile = $(this).attr('id');

		$(pointer).html(calcDuration(meanwhile));

		setInterval(function(){
			$(pointer).html(calcDuration(meanwhile));
		  }, 1000);
	})


	//////////////////////////////////////////////
	// User report
	// prepare modal win
	$("div#userReportWindow").dialog({
		width: 340,
		height: 280,
		autoOpen: false,
		modal: true
	});

	// open modal win
	$('a.userReport').click(function(){

		var tmp = $(this).attr('rel').split('_');
		var type = tmp[0];
		var id = tmp[1];

		$.get('ajax_userReport.php',
			  {id:id, type:type, action:'load'}, 
			  function(data){
				$("div#userReportWindow").html(data).dialog('open');
			  }
		)

		return false;
	});


	// show/hide textarea in modal win
	$('input#userReportOther').live('change', function(){
		if($(this).attr('checked') == true)
		{
			$('textarea#userReportOtherField').css('display', 'inline');
		}
		else
		{
			$('textarea#userReportOtherField').css('display', 'none');
		}
	})


	// send data on submit
	$('form#userReportForm').live('submit', function(){

		var info = $(this).serialize();

		$.get(
			'ajax_userReport.php',
			{info:info}, 
			function(data){
				$("div#userReportWindow").html(data);
			}
		)

		return false;
	});


	// evaluation vote
	$('div.evaluateVote a').click(function(){

		var pointer		= $(this).parent().parent().parent();
		var rowId		= $('.rowId', pointer).val();
		var vote		= $(this).attr('rel');

		$.get(
			'ajax_evaluateVote.php',
			{rowId:rowId, vote:vote}, 
			function(data){
				// ok, load result

				$.getJSON(
					'ajax_evaluateResult.php',
					{rowId:data}, 
					function(data){
						$('div#evaluateVote_' + data.rowId).html(data.body);
					}
				)
			}
		)

		return false;
	});

})


function evaluateResult(rowId)
{
	$.get(
		'ajax_evaluateResult.php',
		{rowId:rowId}, 
		function(data){
			return data;
		}
	)
}


function calcDuration(timestamp)
{
	var seconds = parseInt((new Date().getTime() / 1000), 10) - timestamp;

	var day = (Math.floor(seconds/86400))%86400;
	var hrs = (Math.floor(seconds/3600))%24;
	var min = (Math.floor(seconds/60))%60;
	var sec = (Math.floor(seconds/1))%60;

	var out = '';

	if(day != 0)
	{
		out += day + (day == 1 ? ' ден ' : ' дни') + ', ';
	}

	if(hrs != 0)
	{
		out += hrs + (hrs == 1 ? ' час ' : ' часа') + ', ';
	}

	if(min != 0)
	{
		out += min + ' мин' + ', ';
	}

	out += sec + ' сек';


	return out;
}
