var hrURL =localStorage['serviceURLHR'];
function loadholidaybranch(){
	var holdybranch = $('#holidaybranch').val(); 
		var user_type=$('#hiddenuser_type').val();
		if(user_type != 'Superadmin')
		{
			if(holdybranch=='' || holdybranch=='undefined' || !holdybranch)
			{
			var holdybranch=$('.sessionbranch_id').val();
			}
		}
		$.getJSON(hrURL + 'branch.php?action=load&branch_cal='+holdybranch,function(data){
			branch = data.branch;
			$('.holiday_branch').empty();
			$('.holiday_branch').append('<option value="">---All Branches-----</option>');
			$.each(branch, function(index, branch) {
				$('.holiday_branch').append('<option value="'+branch.branch_id+'">'+branch.branch_name	+'</option>');
			});	
		});
}
////////////////holiday/////////////
var loggedUserId = $('.userid').val();
var holdybranch;
function searchcalendar(){  

	 holdybranch = $('#holidaybranch').val();
	holidayCalendars(holdybranch);	
}

holidayCalendars();

function holidayCalendars(holdybranch){



$('#calendars').empty();


	if(holdybranch=='' || holdybranch=='undefined' || !holdybranch){
	 holdybranch=$('.sessionbranch_id').val();
	}
	monthlyView(holdybranch);

		
}




function yearlyView(holdybranch,yr){
	
	if(holdybranch=='' || holdybranch=='undefined' || !holdybranch){	
		var holdybranch=$('.sessionbranch_id').val();
	}
	
	  var date = new Date();
        var d = date.getDate();
        var m = date.getMonth();
        var y = date.getFullYear();
        var calendar = new Array();



var yr = (typeof yr === "undefined") ? y : yr;
console.log(yr);

	$('#calendars').html('');
	
	$.getJSON(hrURL + "holidatManagement.php?act=list&branchid="+holdybranch,function(data){
	
		
		
      for (var i = 0; i < 12; i++) {
		$('#calendars').append(' <div class="fc fc-ltr calander-list" id="calendar'+i+'" month="0" style="font-size:13px;border:#333333 solid thin; margin:5px;"></div>');
		$('#calendar'+i).fullCalendar({
			header: {
				left: 'prev,next today',
				center: 'title',
				right: 'month,basicWeek,basicDay'
			},
			editable: true,
			year: yr,
			month: i,
			date: 1,
			eventDrop: function(event, delta) {
				date = new Date(event.start).format("yyyy-mm-dd");
				$.get("holidat.php?act=update&title"+event.title+"&start="+date+"&id="+event.id);
			},
			dayRender: function (date, cell) {
					var today = new Date();
					var end = new Date();
						var dates = data;
					$.each(data,function(i,dates){
						if(moment(date).format("DD-MM-YYYY") === moment(new Date(dates.start)).format("DD-MM-YYYY")) {
							cell.css("background-color", "rgb(99, 192, 245)").attr('data-title',dates.title).addClass('event-listed');
						}
					});  
				}
			/*events:[]*/
		});
      }    
	  $('.event-listed').live('mouseenter',function(){
			$this = $(this);
			totalwidth = $this.parent().parent().width();
			
			cellLeft = $this[0].offsetLeft;
			var positionCss;
			
			
			totalheight = $this.parent().parent().height();
			cellTop = $this[0].offsetTop;
			
			if((parseInt(totalheight)-50) < parseInt(cellTop)){
				var lessheight = parseInt(totalheight) -80;
				positionTopCss = " top:"+ lessheight +"px;" ;	
			}else{
				positionTopCss = "";	
			}
			
		//	console.log(positionTopCss);
		//	console.log('Width:'+totalwidth+",Left:"+cellLeft+"W:"+150);
			
			  
			if((parseInt(totalwidth)-150) < parseInt(cellLeft)){
				var lesswidth = parseInt(totalwidth) -150;
				positionCss = " left:"+ lesswidth +"px;" ;	
			}else{
				positionCss = "";	
			}
			
		//	console.log(positionCss);
			
			$(this).append('<div class="event-populated"'+
						   'style="position:absolute; width:150px; min-height:50px;z-index:100;'+
						   positionCss+
						   positionTopCss+
						   'padding:10px;'+
						   '-webkit-box-shadow: 2px 2px 10px 0px rgba(0,0,0,0.75);'+
							'-moz-box-shadow: 2px 2px 10px 0px rgba(0,0,0,0.75);'+
							'box-shadow: 2px 2px 10px 0px rgba(0,0,0,0.75);'+
						   'background:#fff; ">'+$(this).attr('data-title')+'</div>')										
		});
	   $('.event-listed').live('mouseleave',function(){
			$('.event-populated').remove();									
		});
	  
	   $('.prev-year').attr('data-yr',parseInt(yr)-1);
	  $('.nxt-year').attr('data-yr',parseInt(yr)+1); 


		  $('.fc-header-left').css({'display':'none'});
		  $('.fc-header-right').css({'display':'none'});
		  $('.fc-grid .fc-day-number').css({'float': 'right','padding':' 0 2px','font-size':' 12px'});
		  $('.fc-widget-content').css({'border': '1px solid #ddd','height':'20px','line-height':'20px'});
		  $('.fc-day-header').css({'background-color':'#535353','font-size': '12px'});
	  
	  
	  });
	  
	 
			

  
}

function monthlyView(holdybranch){
$('#calendars').html('');
var user_type=$('#hiddenuser_type').val();
	var calendar = $('#calendars').fullCalendar({
			eventSources: [ {url:hrURL + "holidatManagement.php?act=list&branchid="+holdybranch+"&loggedUserId="+loggedUserId}],
			eventDrop: function(event, delta) {
				date = moment(new Date(event.start)).format("YYYY-MM-DD");
				$.get("holidatManagement.php?act=update&title"+event.title+"&start="+date+"&id="+event.id+"&branchid="+holdybranch);
			},
			selectable: true,
			selectHelper: true,
			select: function(start, end, allDay) {

				var title = prompt('Event Title:');
				if (title) {
					date = moment(new Date(start)).format("YYYY-MM-DD");
					$.getJSON(hrURL + "holidatManagement.php?act=addNew&title="+title+"&start="+date+"&branchid="+holdybranch+"&loggedUserId="+loggedUserId,function(data){
					
					$('#calendars').fullCalendar('renderEvent',{
							id:data.id,
							title: data.title,
							start: data.date,
							eid: data.employee_id
						},true );
					 });
				}

			},
			eventClick: function (calEvent, jsEvent, view) { 

			if(user_type == 'Superadmin' || calEvent.eid == loggedUserId ){
				 if(confirm('Are you sure from removing the event?')){
					 $.get(hrURL + "holidatManagement.php?act=remove&id="+calEvent._id+"&branchid="+holdybranch)
					 
						 $('#calendars').fullCalendar('removeEvents', calEvent._id);
				 }
			}
			}});
}

$('.month-view').live('click',function(){ holidayCalendars(); });

$('.year-view').live('click',function(){
	yearlyView();
});

loadholidaybranch();