/**********************************
* функции ajax запросов           *
**********************************/	
			//инициализация ajax
			var req = Create();
			var popup;
			function Create()
			{
    			try 
				{
    				req = new ActiveXObject("Msxml2.XMLHTTP");
  				} 
				catch (e) 
				{
    				try 
					{
      					req = new ActiveXObject("Microsoft.XMLHTTP");
    				} 
					catch (E) 
					{
      					req = false;
    				}
  				}
 				if (!req && typeof XMLHttpRequest!='undefined') 
				{
    				req = new XMLHttpRequest();
  				}
  				return req;
			}
			
			//отправка запроса на сервер
			function Request(query)
			{
    			req.open('post', '/ajax/calendar.php' , true );
    			req.onreadystatechange = chackeCall;
    			req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=utf-8");
    			req.send(query);
			}
			
			function monthSend(month, action)
			{
				if(action == 'next')
				{
					query="nextMonth="+month;
					$(".items").append('<div id="load"><p class="month">Загрузка...</p></div>');
				}
				else
				{
					query="prevMonth="+month;
					$(".items").prepend('<div id="load"><p class="month">Загрузка...</p></div>');
				}
					
				Request(query);
			}
			
			//получает ответ от сервера
			function chackeCall()
			{
    			if( req.readyState == 4 )
				{
        			if (req.status == 200)
					{
						
						var answer = req.responseText.split("&");
						if(answer[0] == 'next' || answer[0] == 'prev')
							SlideItemCreate(answer);
						else
							popupWin(answer);
        			} 
					else if (req.status == 404) 
					{
         				//alert ("Requested URL is not found.");
       				} 
					else if (req.status == 403) 
					{
         				//alert("Access denied.");
       				} 
					else
					{
         				//alert("status is " + req.status);
					}
				}
				
			}
			
			function Hover(day, mon, year)
			{
				var call = 'date='+day+'-'+mon+'-'+year;
				Request(call);
			}
//функция создания слайда для календаря
			function SlideItemCreate(answer)
			{	
				var html="<div id='p-"+answer[2]+"-"+answer[3]+"'>"
				html +='<div class="calendar"><p class="month">';
				html +=answer[1]+' '+answer[3]+'</p><div class="week">';
				html +='<span>П</span>\n';
				html +='<span>В</span>\n';
				html +='<span>С</span>\n';
				html +='<span>Ч</span>\n';
				html +='<span>П</span>\n';
				html +='<span>С</span>\n';
				html +='<span>В</span>\n</div><div class="days">';
				html +=answer[4]+'\n';
				html +='</div></div></div>';
				$(".items").attr({id: answer[2]+'-'+answer[3]})	
				
				if(answer[0] == "next")
					$("#load").replaceWith(html);
				else
					$("#load").replaceWith(html);
					
				$(".popup").hover(
					
        			function(){ 
					$(".cal_img").remove();
					$("#cal_h4").empty();
					$("#cal_p").empty();
					var date = $(this).attr('id').split(',');
					var call = 'date='+date[0]+'-'+date[1]+'-'+date[2];
					Request(call);
					$(this).mousemove(function(e) {
          				var x = e.pageX;
          				var y = e.pageY;
						$(".visible").fadeIn(100).css({left:x-517, top:y+15});
     				});
					
					
        			},
        			function(){ 
						$(".visible").fadeOut(100);
						$(".visible").load(popupClose());
            			$(".visible").stop(true, false).load(popupClose());
            			
        			}
    			);	
				
			}
			
			
			function popupWin(answer)
			{
				
				var src='<img class ="cal_img" onerror="$(this).remove();" src="/calendar/image/image_'+answer[0]+'.jpg"  width="146" alt="" />';
				var h4=answer[1];
				var p=answer[2];
				$("#img").append(src);
				$("#cal_h4").append(h4);
				$("#cal_p").append(p);
						
			}
			function popupClose()
			{
				
				$(".cal_img").remove();
				$("#cal_h4").empty();
				$("#cal_p").empty();
				
			}
//работа с jquery
			$(function(){
				
				
				$(".next").click(
					function () {
						
						var month = $(".items").attr("id");
						
						if($("#p-"+month).next().attr("id") === undefined)
						{
							monthSend($(".items").attr("id"), 'next');
						}
						else
						{
							var nextId = $("#p-"+month).next().attr("id").split('-');;
							
							$(".items").attr({id: nextId[1]+'-'+nextId[2]})	;
						}
						
					}
				);
				$(".prev").click(
					function () {
						var month = $(".items").attr("id");	
						
						if($("#p-"+month).prev().attr("id") === undefined)
						{
							monthSend($(".items").attr("id"), 'prev');
						}
						else
						{
							var prevId = $("#p-"+month).prev().attr("id").split('-');;
							
							$(".items").attr({id: prevId[1]+'-'+prevId[2]})	;
						}
					}
				);
				$(".popup").hover(
        			function(){ 
					$(".cal_img").remove();
					$("#cal_h4").empty();
					$("#cal_p").empty();
					var date = $(this).attr('id').split(',');
					var call = 'date='+date[0]+'-'+date[1]+'-'+date[2];
					Request(call);
					$(this).mousemove(function(e) {
          				var x = e.pageX;
          				var y = e.pageY;
						$(".visible").removeAttr("style").css({left:x-517, top:y+15});
     				});
					
					
        			},
        			function(){ 
						$(".visible").fadeOut(100);
						$(".visible").load(popupClose());
            			$(".visible").stop(true, false).load(popupClose());
            			
        			}
    			);	
			});
			
		

