// JavaScript Document

function getPaddings(x){
	    var padding = 0;
	    var divs = $("a."+x+" div.event")
	    divs.each(function(){
			if (!$(this).parent().parent().hasClass(x)){

                var h = $(this).parent().height()
                var offset = $(this).parent().offset().top-$(this).parent().parent().offset().top
                if (h==0){
                    offset = 0}
				padding = h+ offset;
				}


	    })

	    $(".events."+x).css("padding-top",padding+"px")
	}

var overlapedCalendarEvents = new Array();

$(document).ready(function() {
    function getAllPaddings(x) {
        for (i=0; i < x.length; i++) {
            getPaddings(x[i]);
        }
    }

    getAllPaddings(overlapedCalendarEvents);

	$(".open-calendar").click(function(){
		 $(".calendar-conteiner").addClass("visible")
		 var windowHeight = $(window).height()
		 var calendarHeight = $(".calendar-popup").height()
		 if (calendarHeight <= windowHeight){
			 $("body").addClass("no-scroll-calendar")
			  var top = ($(window).srollTop())
			 $(".calendar-conteiner").css("top", top-10+"px")
			 }
		  else {
			  var calHeight = $(".calendar-popup").height()
			  $(".container").css("height",calHeight-100+"px")
			  $(".container").addClass("no-scroll-calendar")
			  }

		   return false;
		})
	$(".close-popup").click(function(){
		 $(".calendar-conteiner").removeClass("visible")
		  $("body").removeClass("no-scroll-calendar")
		   $(".container").css("height","auto")
		    $(".container").removeClass("no-scroll-calendar")

	})

	$(window).resize(function(){
		 var windowHeight = $(window).height()
		 var calendarHeight = $(".calendar-popup").height()
		 if (calendarHeight <= windowHeight){
			 $("body").addClass("no-scroll-calendar")
			 }
		 else {
			 $("body").removeClass("no-scroll-calendar")
			 }
		getAllPaddings(overlapedCalendarEvents)

		})

	function filter(){
		$(".menu li").click(function(){
			var name = $(this).attr("name")
			if ($(this).hasClass("active")){
				    $(this).removeClass("active")
					$("div[name="+name+"]").hide()
					getAllPaddings(overlapedCalendarEvents)

				}
			else {
				    $(this).addClass("active")
					$("div[name="+name+"]").show()
					getAllPaddings(overlapedCalendarEvents)


				}

			})
		}





	filter()
	$(".event").hover(function(){
		   var thisName = $(this).attr("event")

		   $(".event[event="+thisName+"]").addClass("hover")

	},function(){
		   var thisName = $(this).attr("event")
		   $(".event[event="+thisName+"]").removeClass("hover")
			})


	$(window).resize(function(){

		 $(".holiday, td").each(function(){
		var height = $(this).parent().height();
		$(this).css("height",height+"px")
		})
		$(".calendar-popup tr:last").css("height",  $(".calendar-popup tr:last td:first").height()+1+"px")

		})
	$(".holiday, td").each(function(){
		var height = $(this).parent().height();
		$(this).css("height",height+"px")
		})

     $(".calendar-popup tr:last").css("height",  $(".calendar-popup tr:last td:first").height()+1+"px")


})

