﻿var newsPage=1;
var lastPage=2;
var currentPage=1;
var nextPage=2;
var prevPage=0;
var pagekey=false;
var prevDate='';


function changeLoc(loc)
{
	document.location=loc;
}
function getNews(page)
{
	//$("#jTplOutput").attr('innerHTML','<center><img src=/i/preloader.gif></center>');

	currentPage=page;
	document.location.hash='page'+page;
	prevPage=currentPage-1;
	nextPage=currentPage+1;
	$.ajax(
	{
		type: "GET",
		url: "/execplugin/?plg=news&useProperties=4&nosmarty=1&p="+page,
		dataType:"json",
		success: function(msg)
		{

			if (msg)
			{
				$("#jTplOutput").setTemplateElement("jtplnewsnews");
				$("#jTplOutput").processTemplate(msg);


			}

		}
	}
	);
}




function changeHash(hassh)
{
	document.location.hash=hassh
}

function turnPageKey(getBlockFunc)
{
	hashPage = parseInt(document.location.hash.replace('#page',''));
	if (hashPage>0)
	{
		if (currentPage!=hashPage)
		{
			getNews(hashPage);
		}
	}
	$(document).bind('keypress',function(e)
	{
		pagekey=false;
		keyCode = e.which;
		if (keyCode>48)
		{
			if (keyCode<58)
			{
				pagekey = keyCode-48;
				window[getBlockFunc](pagekey);
			}

		}

		//alert(pagekey)
		if (!pagekey)
		{
			switch(e.keyCode)
			{
				case 0x25: if (prevPage>0) window[getBlockFunc](prevPage); break;
				case 0x27:  window[getBlockFunc](nextPage); break;
			}
		}
		return pagekey;
	}
	)
}

function getComments(event,page)
{

	currentPage=page;
	$.ajax(
	{
		type: "GET",
		url: "/execplugin/?plg=comments&nosmarty=1&useProperties=7&plgBlock=plugin2&p="+page+"&afisha="+event,
		dataType:"json",
		success: function(msg)
		{

			if (msg)
			{

				$("#jTplOutput").setTemplateElement("jtplcommentsview");
				$("#jTplOutput").processTemplate(msg);

			}
		}
	}
	);
}


function leaveComment(event,comment,table)
{

	if (comment.length>0)
	{
		
		//comment=comment.replace('+','&#043;');
		//comment=comment.replace('%','&#037;');
		//comment=escape(comment);

		//alert (comment);

		$.ajax(
		{
			type: "POST",
			url: "/execplugin",
			data:"plg=comments&plugFunc=leaveComment&funcParams="+event+'@@'+comment+'@@'+table,
			dataType:"script",
			success: function(msg)
			{
				$('#comment').attr('value','');
				getComments(event,1);
			}
		}
		);
	}
	else
	{
		alert('Пустое сообщение!');
	}
}



function rateComment (cmnt,rate)
{
	$('#rateblock'+cmnt).hide();
	$('#rating'+cmnt).attr('innerHTML',parseInt($('#rating'+cmnt).attr('innerHTML').replace('+',''))+parseInt(rate));
	if (parseInt($('#rating'+cmnt).attr('innerHTML').replace('+',''))>0)
	{
		$('#rating'+cmnt).attr('innerHTML','+'+$('#rating'+cmnt).attr('innerHTML'))
	}
	else
	{
		$('#rating'+cmnt).attr('className','h_red');
	}

	$.ajax(
	{
		type: "GET",
		url: "/execplugin/?plg=comments&plugFunc=setCommentRate&funcParams="+cmnt+'@@'+rate,
		dataType:"json",
		success: function(msg)
		{
			alert('Preved');

		}
	}
	);

}

function getCalendar(month,year,prop)
{
	$.ajax(
	{
		type: "GET",
		url: "/execplugin/?plg=jCalendar&nosmarty=1&m="+month+"&y="+year+"&useProperties="+prop,
		dataType:"json",
		success: function(msg)
		{
			$("#jCalendDiv").setTemplateElement("jtplcalendarcalendar");
			if (msg)
			$("#jCalendDiv").processTemplate(msg);

		}
	}
	);
}

function igo(event)
{

	$('#vis_'+event).children(".zgo").hide();
	$.ajax(
	{
		type: "GET",
		url: "/execplugin/?plg=afisha&plugFunc=igo&funcParams="+event,
		dataType:"json",
		success: function(msg)
		{
			$('#usercounter_'+event).attr('innerHTML',parseInt($('#usercounter_'+event).attr('innerHTML'))+1);
			$('#vis_'+event).children("#zgo_"+event).before('<a href=/mypage/ class=user_'+msg.id+'></a> ');
			$('#vis_'+event).children('.user_'+msg.id).attr('innerHTML',msg.user);
			$('#vis_'+event).children(".znotgo").show();


			alert('Вы участник!');
		}
	}
	);
}

function idontgo(event,me)
{

	$('#vis_'+event).children(".znotgo").hide();
	$.ajax(
	{
		type: "GET",
		url: "/execplugin/?plg=afisha&plugFunc=idontgo&funcParams="+event,
		dataType:"script",
		success: function(msg)
		{
			$('#usercounter_'+event).attr('innerHTML',parseInt($('#usercounter_'+event).attr('innerHTML'))-1);
			//alert($('#user_'+me));

			$('#vis_'+event).children('.user_'+me).remove();
			$('#vis_'+event).children(".zgo").show();

			alert('Вы отказались от участия!');
		}
	}
	);
}

function getPhotoEvents(date)
{
	if (date!=prevDate)
	{
		prevDate=date;
		eventSelector = document.getElementById('eventName');
		eventSelector.innerHTML='<option value=0>Выбрать</option>';
		$.ajax(
		{
			type: "GET",
			url: "/execplugin/?plg=photo&plugFunc=getAfishaByDate&date="+date,
			dataType:"json",
			success: function(msg)
			{
				eventSelector.disabled=false;
				for (i=0; i<msg.rows.length; i++)
				{
					hoption = document.createElement('option');
					hoption.innerHTML=msg.rows[i].c_title;
					hoption.value=msg.rows[i].id;
					eventSelector.appendChild(hoption);
				}
			}
		}
		)
	}
}
