var slash="/"; //Разделитель даты
var see=11;    //Доступные месяца к прсмотру (вперед)




// begin script

var cTime=new Date(),curM=cTime.getMonth();
var nDay=cTime.getDay(),nNum=cTime.getDate();
var year=cTime.getFullYear(),cDat=new Date(year,curM,1,0,0,0,0);

var i,howM,howY,howD,val1,val2,res;
var bDat=new Date(year,curM,0,0,0,0,0);

var state=false,howP;
var g;

function clndr(){

var width="495px"; //Ширина календаря
var widthM="238px"; //Ширина месяца календаря
var height="265px" //Высота календаря

if ('\v'=='v')   
{
 
	widthM="232px"; //Ширина месяца в календаре для ИЕ
	width="495px"; //Ширина   календаре для ИЕ
	height="270px"; //Высота календаря

}

var fV=$("#in");
var fS=$("#out");
var bLeft=$('#bottonLeft'),bRight=$('#bottonRight')
bLeft.css({"width":"40px","cursor":"pointer","float":"left","zIndex":"2"});

bRight.css({"width":"40px","display":"inline","cursor":"pointer","float":"right","zIndex":"2"});    
var dynamic=$("#dynamic").css({"clear":"both","display":"block","margin":"0","padding":"0","height":"240px"});

var daysname=['Вс','Пн','Вт','Ср','Чт','Пт','Сб'];
var namDays="<tr><th>"+ daysname[1] +"</th><th>"+ daysname[2] +"</th><th>"+ daysname[3] +"</th><th>"+ daysname[4] +"</th><th>"+ daysname[5] +"</th><th>"+ daysname[6] +"</th><th>"+ daysname[0] +"</th></tr>";


function updstyle(){
var ctable=$("#calendar .block ").css({
	"width":"100%",
	"dispalay":"inline",	
	"marginTop":"0px",
	"borderCollapse":"collapse"
	});
$(".block table").css({"border":"1px solid #dddddd","borderCollapse":"collapse"});	
var dis=$(".disabled");dis.css({"backgroundColor":"#eeeeee","color":"#a6a6a6"});
var th=$("tr,th" ).css({"border":"1px solid #dddddd","margin":"1px","padding":"5px"});   
// var tb=$("#noborder,#noborder1,#noborder2,#noborder3,#noborder4").css({"border":"0px none"});    
var active=$(".active");active.css({"color":"#06c","cursor":"pointer"});  
var block=$("#calendar .block");   
block.css({"float":"left","width":widthM,"margin":"4px","dispalay":"inline","padding":"0"});      
var th=$("#calendar th");
active.hover(function(){
	if($(this).css("backgroundColor")=="rgb(255, 255, 255)" ||
	$(this).css("backgroundColor")=="#fff" ||
	$(this).css("backgroundColor")=="transparent" ||
	$(this).css("backgroundColor")=="rgba(0, 0, 0, 0)")
{$(this).css({"backgroundColor":"#8cd4fa"})}
},function(){
if($(this).css("backgroundColor")=="rgb(140, 212, 250)" || $(this).css("backgroundColor")=="#8cd4fa"
){$(this).css({"backgroundColor":"#fff"})}});
$(".block h4").css({"margin":"2px","font-weight":"600","height":"18px"});
ctable.hover(function(){nyear=$(this).attr('year');nm=$(this).attr('nm');r=nm;r++},function(){});
active.click(function(){
value=$(this).html();
howD=value;
howM=nm;
howY=nyear;
value=parseInt(value);
res = new Date(howY,howM,howD,0,0,0,0);
state=true;
howP = $(this);
return;})
dis.click(function(){
state=false;
})} //end style


function forMon (curM, year1) {
var mon=['Январь','Февраль','Март','Апрель','Май','Июнь','Июль','Август','Сентябрь','Октябрь','Ноябрь','Декабрь'];   

var first=new Date(year1,curM,0,0,0,0);

var firstDay=first.getDay();
var next=new Date(year1,curM + 1,0,0,0,0);
var perv=new Date(year1,curM - 1, 1,0,0,0,0);
var howDays=(next - first) / (1000 * 60 * 60 * 24);
var pervDays=(first - perv) / (1000 * 60 * 60 * 24);
pervDays=pervDays.toFixed(0);pervDays=parseInt(pervDays);pervDays++;
var last=7 - firstDay;pervDays=pervDays-7+last+1;
l="<div class='block' nm='"+ curM +"' year='" + year1 +"'><h4 align='center'>" + mon[curM] + " " + year1 + "</h4>";
l=l + "<table nm='" + curM + "' year='" + year1 + "'>" + namDays;fir=true;other=1;i=1;
for (var rows=1; rows < 6; rows++) {if (fir){l=l + "<tr>";for (var g=0; g < firstDay; g++) {l=l + "<th class='disabled'>" + pervDays++ + "</th>";}
var last=7 - firstDay;for (var g2=0; g2 < last; g2++) {l=l + "<th class='active'>"+ i++ +"</th>";}fir=false;
l=l + "</tr><tr>";}for (var col=0; col < 7; col++){if(i < howDays || i == howDays){l=l + "<th class='active'>" + i++ + "</th>";}
else {l=l + "<th class='disabled'>" + other++ + "</th>";}}
l=l + "</tr>";}l=l + "</table></div>";
return l;} //end forMon


function updcal(){
bRight.attr({"year":year});
dynamic.html(forMon(curM,year));
curM++;
if (curM==12) 
{
	curM=0; 
	year++;
}
bRight.attr({"year":year});
bRight.attr({"mn":curM});
dynamic.append(forMon(curM,year));
curM++;
if (curM==12) curM=0;
}

updcal();

updstyle();

calendar_loaded=1;
 
$(".avtive").css({"backgroundColor":"#fff"});
var body=$('#calendar').css({
	"position":"relative",	
	"width":width,
	"height":height,
	"border":"1px solid #cccccc",
	"padding":"5px",
	"margin":"1px",
	"MozBorderRadius":"7px",
	"WebkitBorderRadius":"7px",
	"fontSize":"1em",
	"backgroundColor":"#fff"});

var table=$("#calendar table"),obs=$("#calendar .active"),nyear,ndays; 

var ty=false,
obR=[],
obL=[],
chitL=0,
chitR=0,
rM=$(bRight).attr("mn"),
rY=year,
lY=$(bRight).attr("year");


bRight.click(function(){
if(see == 0){return false}
see--;
var lM=rM; lM--;if(lM==-1){lM=11;}
if(rM==12){rM=0;rY++} ;
if(rM==1){lY=rY};
 
var block="#calendar .block[nm="+lM+"][year="+lY+"]";

block=$(block);
chitR++;
obR[chitR]=block;
block.hide();
rM++;
if(rM==12){rM=0;rY++} ;


//show block

if(chitL==0){dynamic.append(forMon(rM,rY));updstyle()}else{
obL[chitL].show();chitL--;}
//end show block
}); //ebd clicl right



bLeft.click(function(){
if(chitR == 0){return;}
see++;

var block="#calendar .block[nm="+rM+"][year="+rY+"]";
//alert (block)
block=$(block).hide();
chitL++;
obL[chitL]=block;
rM--;if(rM==-1){rM=11;rY--;}if(rM==1){lY--;}
obR[chitR].show();
chitR--;
}); //ebd clicl left





function seldays(begin,end){
var bbb;
var min = end-begin; 
if(min>0){
min++;
for(min; min>0;min--){
bbb = ".active:eq("+begin+")";
$(bbb).css("backgroundColor","#8fb5f7");
begin++;}return;}
min--; min = min*(-1);
for(min; min>0;min--){
bbb = ".active:eq("+end+")";
$(bbb).css({"backgroundColor":"#8fb5f7"});
end++;}return;}
var who=1,res1,res2,begin,end;
$("#dynamic").click(function(){
howM++;
if(state==false){return;}
if(who==1){who++;
fS.val('');
$(".active").css({"backgroundColor":"#fff"});
begin = howP.css({"backgroundColor":"#8fb5f7"});
val1=howD+slash+howM+slash+howY;
fV.val(val1);
res1=res;
i=0;
$(".active").each(function(){
if($(this).css("backgroundColor")== "rgb(143, 181, 247)" 
|| $(this).css("backgroundColor")== "#8fb5f7"){begin=i; return false;}
i++;
});}
else{who--;
val2=howD+slash+howM+slash+howY;
res2=res;
fS.val(val2);
$(".active").css({"backgroundColor":"#fff"});
end = howP.css({"backgroundColor":"#8fb5f7"});
i=0;
$(".active").each(function(){
if($(this).css("backgroundColor")== "rgb(143, 181, 247)" || 
$(this).css("backgroundColor")== "#8fb5f7"){end=i; return false;}
i++;});
seldays(begin,end);
if(res2-res1 > 0){fS.val(val2);return;}
fS.val(val1),fV.val(val2);return
}});
} 


if (!((op) || (ie))) clndr();





