var prevDate=false;
var mDone=[];
var activeDate='';
var activeZone='';
var currentPage=1;
var videoRepo=false;
function getEvent(eventId,constant)
{
	if (constant)
	{
		funcConstParam='@@1'
	}
	else funcConstParam='';
	document.getElementById('jTplOutput').innerHTML='<br><br><center><img src=/i/preloader.gif></center>';
	$.ajax(
	{
		type: "GET",
		url: "/execplugin/?plg=afisha&plugFunc=getEvent&funcParams="+eventId+funcConstParam,
		dataType:"json",
		success: function(msg)
		{
			$("#jTplOutput").setTemplateElement("jtplviewEvent");
			if (msg)
			$("#jTplOutput").processTemplate(msg);
			$('#eventDiv').fadeIn("slow");
		}
	}
	)
}


function getPhotoEvents(date)
{
	if (date!=prevDate)
	{
		prevDate=date;
		eventSelector = document.getElementById('eventName');
		eventSelector.innerHTML='';
		defOption=document.createElement('option');
		defOption.value=0;
		defOption.innerHTML='Выбрать';
		eventSelector.appendChild(defOption);
		//eventSelector.innerHTML='<option value=0>Выбрать</option>';
		$.ajax(
		{
			type: "GET",
			url: "/execplugin/?plg=photographer&plugFunc=getEventsNames&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_name;
					if (msg.rows[i].c_weekDay)
					{
						hoption.value=-1*msg.rows[i].id;
					}
					else
					{
						hoption.value=msg.rows[i].id;
					}
					eventSelector.appendChild(hoption);
				}
			}
		}
		)
	}
}


function enableLightBox()
{
	$(function() {
		$('a.lightbox-enabled').lightBox();
	});
}

function loadMonthReports()
{
	$("#month > li").mouseover( function(){
		// загружаем список мероприятий для него
		// но только в том случае если они ещё не загружены

		//		if(!$(this).hasClass("active"))
		
		//alert(document.getElementById('ulm'+month).style.display);
		
		if(true)
		{
			var liElem=$(this);

			var year=0;

			$("#years > li").each(function(i){
				if($(this).hasClass("active"))
				{
					year=$(this).text();
				}
			});

			var month=$(this).attr("id");
			
			if (document.getElementById('ulm'+month))
			document.getElementById('ulm'+month).style.display='block';
			
			if(mDone[month]==1)
			{
				return false;
			}

			$.getJSON("/execplugin/",{plg:'reports',plugFunc:'getEventsForMonth', "month": month, "year": year },function (data)
			{
				mDone[month]=1;
				var childText="<ul id=ulm"+month+">";
				if (data)
				{
					for(i=0; i<data.length; i++)
					{
						childText+="<li><span>"+data[i]['c_date']+"</span><a href=javascript:getReportPhotos("+data[i]['id']+",1) class='eventLink'>"+data[i]['c_name']+"</a></li>";
					}
					
					$(liElem).append(childText+"</ul>");
					if (document.getElementById('ulm'+month)) document.getElementById('ulm'+month).style.display='block';
					//alert($(liElem).attr('innerHTML'))
					//$(liElem).attr('innerHTML',$(liElem).attr('innerHTML')+childText+"</ul>");
					//$(liElem).show();
					
				}
			});
			//
		}
	});

	$("#month > li").mouseout( function(){
		var month=$(this).attr("id");
		if (document.getElementById('ulm'+month))
		document.getElementById('ulm'+month).style.display='none';
	}
	);

}

function getReportCovers(month,year)
{
	
	document.getElementById('jTplOutput').innerHTML='<center><img src=/i/preloader.gif></center>';
	$.ajax(
	{
		type: "GET",
		url: "/execplugin/?plg=reports&nosmarty=1&plugFunc=photoReports&month="+month+"&year="+year,
		dataType:"json",
		success: function(msg)
		{

			$("#jTplOutput").setTemplateElement("jtplreports");
			if (msg)
			$("#jTplOutput").processTemplate(msg);

		}
	}
	)
	
	if (document.getElementById('report_foto_right'))
	{
		document.getElementById('report_foto_right').id='report_right';
		document.getElementById('report_right').innerHTML=videoRepo;
		
	}


}

function getVideoCovers(month,year)
{
	
	document.getElementById('jTplOutput').innerHTML='<center><img src=/i/preloader.gif></center>';
	$.ajax(
	{
		type: "GET",
		url: "/execplugin/?plg=reports&nosmarty=1&plugFunc=allVideoReports&month="+month+"&year="+year,
		dataType:"json",
		success: function(msg)
		{

			$("#jTplOutput").setTemplateElement("jtplvideoreports");
			if (msg)
			$("#jTplOutput").processTemplate(msg);

		}
	}
	)
	



}

function showFavs(favData)
{
	var favCont='';
	if (document.getElementById('report_right'))
	{
		var photoDiv = document.getElementById('report_right');
		videoRepo=photoDiv.innerHTML;
	}
	else var photoDiv = document.getElementById('report_foto_right');
	photoDiv.innerHTML='';
	for (i=0; i<favData.length; i++)
	{
		favCont=favCont+'<div class=ab_chapter><a href=javascript:void(0) class=lightbox-enabled><img src=/images/gallery_thumbs/170th'+favData[i]+'.jpg></a></div>';
	}

	photoDiv.innerHTML=favCont;
	photoDiv.id='report_foto_right';
}

function getReportPhotos(report,page)
{

	currentPage=page;
	document.getElementById('jTplOutput').innerHTML='<center><img src=/i/preloader.gif></center>';
	$.ajax(
	{
		type: "GET",
		url: "/execplugin/?plg=reports&nosmarty=1&plugFunc=photoReports&report="+report+"&p="+page,
		dataType:"json",
		success: function(msg)
		{

			$("#jTplOutput").setTemplateElement("jtplviewReport");
			if (msg)
			{
				$("#jTplOutput").processTemplate(msg);
				showFavs(msg.favs);
			}

		}
	}
	)



}

function openVideoPopup(id)
{
	window.open('/viewVideo/viewVideo.'+id+'.html','Просмотр видеоотчёта','height=355,width=455')
}

function getZoneInfo(zoneId)
{
	//if (zoneId==-1) zoneId=activeZone;

	document.getElementById('jTplOutput').innerHTML='<center><img src=/i/preloader.gif></center>';
	$.ajax(
	{
		type: "GET",
		url: "/execplugin/?plg=reserv&nosmarty=1&plugFunc=getZoneInfo&funcParams="+zoneId+'@@'+activeDate,
		dataType:"json",
		success: function(msg)
		{

			$("#jTplOutput").setTemplateElement("jtplviewzone");
			if (msg)
			{
				$("#jTplOutput").processTemplate(msg);
				document.getElementById('dateFld').value=activeDate;
				document.getElementById('inp_txt_zona').value=activeDate;
				//document.getElementById('costdate').innerHTML=activeDate;
			}

		}
	}
	)
	activeZone=zoneId;

}

function activateReservInputs(zone)
{
	//alert(document.getElementById('userAnketa'));
	document.getElementById('form_zona').id='form_zona2';
	document.getElementById('userAnketa').style.display='block';
	document.getElementById('form_zona2').removeChild(document.getElementById('inp_txt_zona'));
	document.getElementById('form_zona2').removeChild(document.getElementById('butt_zona'));
	document.getElementById('form_zona2').removeChild(document.getElementById('pinkBtn'));
	document.getElementById('showsubmit1').style.display='block';
	//document.getElementById('pinkBtn').className=''
	//document.getElementById('pinkBtn').innerHTML='<input type=button value=зарезервировать id=submit1 onclick=sendForm(81,'+zone+')>';
	//document.getElementById('pinkBtn').id='';
}


function sendForm(plugId,zone)
{
	denypost=0;
	$("input.editable").each(function(i)
	{
		if (this.value=='')
		{
			this.className='editable false';
			denypost = 1;
		}
		else
		{
			this.className='editable';
		}

	}
	);
	//alert(denypost);
	if (!denypost)
	{
		//alert($("#form_zona2").rules());
		var textValues = new Array;
		var selectValues=new Array;
		var i=0;
		var n=0;
		var fldName; var fldValue='';
		$("input.editable").each(function(i)
		{
			textValues=textValues+this.name+'='+this.value+'#@@#';
		}
		)

		$("input.editable").attr('disabled',1);

		$.ajax(
		{
			type: "POST",
			url: "/execFormHandle/",
			data:"plgId="+plugId+'&fldValues='+textValues,
			dataType:'json',
			success: function(msg)
			{
				if (msg.rows)
				{
					for (i=0;i<msg.rows.length;i++)
					{
						document.getElementById('cnt_'+msg.rows[i].field).className='false';
						document.getElementById('emcnt_'+msg.rows[i].field).style.display='block';
						document.getElementById('emcnt_'+msg.rows[i].field).innerHTML=msg.rows[i].erroralias;
					}
				}
				else
				{
					$('#zone'+zone).attr('className','a_active');
					$('#form_zona2').attr('innerHTML','')
					$('#form_zona2').attr('innerHTML','<br><span>Спасибо за резерв, наш менеджер свяжется с Вами в ближайшее время</span>');
				}
			}
		} )
	}
}

function getCalendar(month,year)
{
	$.ajax(
	{
		type: "GET",
		url: "/execplugin/?plg=jCalendar&nosmarty=1&m="+month+"&y="+year,
		dataType:"json",
		success: function(msg)
		{
			$("#jCalendDiv").setTemplateElement("jtplcalendar");
			if (msg)
			$("#jCalendDiv").processTemplate(msg);

		}
	}
	);
}

function getCalendarEvent(date)
{
	var datesplit = date.split('-');
	var day = datesplit[2];
	$("td.active").attr('className','');
	$("#td"+day).attr('className','active');
	$("#jtplClndEvent").attr('innerHTML','<p><img src=/i/loading.gif></p>');
	activeDate=date;
	$.ajax(
	{
		type: "GET",
		url: "/execplugin/?plg=afisha&nosmarty=1&plugFunc=getCalendarEvents&funcParams="+date,
		dataType:"json",
		success: function(msg)
		{
			$("#jtplClndEvent").setTemplateElement("jtplcalendarEvent");
			if (msg)
			$("#jtplClndEvent").processTemplate(msg);

		}
	}
	);

	getReservedPlaces(date);

	if (activeZone!='')
	{
		getZoneInfo(activeZone);
	}
}


function getReservedPlaces(date)
{
	$.ajax(
	{
		type: "GET",
		url: "/execplugin/?plg=reserv&nosmarty=1&plugFunc=getAlreadyReserved&funcParams="+date,
		dataType:"json",
		success: function(msg)
		{
			$("div.a_active").attr('className','a_no_active');
			if (msg)
			{
				for (var i=0;i<msg.rows.length;i++)
				{
					document.getElementById('zone'+msg.rows[i].c_zone).className='a_active';
				}
			}

		}
	}


	);
}
function setZonePrices()
{
	var res='';
	$("input[type='text']").each(function(i){res=res+this.name+'='+this.value+'**'});

	$.ajax(
	{
		type: "GET",
		url: "/execplugin/?plg=reserv&nosmarty=1&plugFunc=setDatePrices&funcParams="+res+'@@'+activeDate,
		dataType:"json",
		success: function(msg)
		{
			alert('Цены сохранены!');
		}
	}


	);


}


function getZoneTip(zoneId)
{
	$.ajax(
	{
		type: "GET",
		url: "/execplugin/?plg=reserv&plugFunc=getImage&zone="+zoneId,
		dataType:"script",
		success: function(msg)
		{
			$('#bigImage').attr('innerHTML',msg);
			$('div.a_no_active > a').mopTip({'w':150, 'h':60, 'style':"overOut", 'get':"#bigImage"});
		}
	}
	);
}




