///////////////////////////////////////
//  by Ashus, created for RS banan   //
//     all rights reserved, 2010     //
///////////////////////////////////////


$(function()
    {
    Date.format = "yyyy-mm-dd";

	$(".date-pick")
		.datePicker({
			inline: true,
            createButton: false,
// 			startDate: "2000-01-01",
// 			endDate: (new Date()).addDays(1).asString()
			})

		.each(function(){
        this.platnedata = $(this).attr("newsdates").split(" ");

        this.vybranedatum = $(this).attr("newscur").split("-",3);
		for (var i=0;i<3;i++)
			{this.vybranedatum[i] = parseInt(this.vybranedatum[i], 10);}

		var df = $(this).attr("newsfirst");
		if (df.length>0)
			$(this).dpSetStartDate(df);

		var dl = $(this).attr("newslast");
		if (dl.length>0)
			$(this).dpSetEndDate(dl);

        $(this).dpSetDisplayedMonth(this.vybranedatum[1]-1,this.vybranedatum[0]);
        $(this).dpRerenderCalendar(); // obnovi zasedlost posouvatek mesicu a roku
		})
		.bind("dpMonthChanged",
		 function(e, displayedMonth, displayedYear)
			{
            var platnedata = $(this).context.platnedata;
            var vybranedatum = $(this).context.vybranedatum;

            if (displayedYear == null) // kvuli initu
				{
				displayedYear = vybranedatum[0];
                var displayedM = vybranedatum[1];
				} else {
                var displayedM = (displayedMonth+1);
				}


            $(this).find("td.current-month").each(function(){
				var b = true;
				var thisD = parseInt($(this).text(), 10);
                for (var s in platnedata)
					{
                    var x = platnedata[s].split("-",3);
					if ((displayedYear==parseInt(x[0], 10)) && (displayedM==parseInt(x[1], 10)) && (thisD==parseInt(x[2], 10)))
						{
						b = false;
						break;
						}
					}
				$(this).toggleClass("disabled", b);

				if ((displayedYear==vybranedatum[0]) && (displayedM==vybranedatum[1]) && (thisD==vybranedatum[2]))
                    $(this).addClass("selected");
				})
			}
		)

		.bind("dateSelected",
		 function(e, selectedDate, $td)
			{
			var tid = $(this).attr("newsid");
			location.href = location.pathname+"?newsdate"+tid+"=" + selectedDate.asString("yyyy-mm-dd")+"#newsid"+tid;
			}
		)

		.trigger("dpMonthChanged");     // redraw aktualne pouzitych dnu v mesici a zasednuti ostatnich

    });


$(window).load(function () {
	// je nutne se posunout na kotvu az po vykresleni kalendare, jinak bysme nemuseli byt posunuti dost / v pripade ze novinky jdou niz nez zbytek stranky
	if (location.hash != "")
		{
		$("body").scrollTop( $("a[name="+location.hash.substring(1)+"]").offset().top );
		}

});

