var slash="/"; //Разделитель даты
var see=10;    //Доступные месяца к прсмотру (вперед\назад)
var width="700px"; //Ширина календаря
var widthM="225px"; //Ширина месяца в календаре
var height="270px" //Высота календаря

if ('\v'=='v')   widthM="217px"; //Ширина месяца в календаре для ИЕ

// 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;

$(document).ready(function(){

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":"250px"});

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 table").css({
	"width":"100%",
	"dispalay":"inline",
	"border":"1px solid #000000",
	"marginTop":"15px",
	"borderCollapse":"collapse"});
var dis=$(".disabled");dis.css({"backgroundColor":"#eeeeee","color":"#a6a6a6"});
var th=$("table, tr,th").css({"border":"1px solid #dddddd","margin":"1px","padding":"5px"});    
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":"0 0 15px 0"});
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>";l=l + "</h2>";
return l;} //end forMon

function updcal(){
bRight.attr({"year":year});
dynamic.html(forMon(curM,year));
curM++;
if (curM==12) curM=0;
dynamic.append(forMon(curM,year));
curM++;
if (curM==12) curM=0;
dynamic.append(forMon(curM,year));
bRight.attr({"mn":curM});
}
updcal();updstyle();$(".avtive").css({"backgroundColor":"#fff"});
var body=$('#calendar').css({
	"position":"relative",	
	"width":width,
	"height":height,
	"border":"1px solid #cccccc",
	"padding":"5px",
	"margin":"5px",
	"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--;lM--;if(lM==-2){lM=10}if(lM==-1){lM=11;}
var block="#calendar .block[nm="+lM+"][year="+lY+"]";
block=$(block);
chitR++;
obR[chitR]=block;
block.hide();
rM++;if(rM==12){rM=0;rY++} if(rM==2){lY++}
//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+"]";
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
}});
}); //end ready



