function createNewAppointmentDiv(leftPos,topPos,width,height,title,color)
{
	var div = document.createElement('DIV');
	div.onclick = setElementActive;
	//div.ondblclick = editEventWindow;
	div.className='calendar_event_opacity'; //last
	if(color > 0)div.style.backgroundColor = colors[color][2];
	if(color > 0)div.style.border = "1px solid "+ colors[color][0];

	div.style.left = leftPos + 'px';
	div.style.top = topPos + 'px';
	div.style.width = width + 'px';
	div.onmousedown = initToggleView;

	if(height)div.style.height = height + 'px';

	var header = document.createElement('DIV');
	header.className= 'calendar_event_header';
	if(color > 0)header.style.backgroundColor = colors[color][0];
	header.innerHTML = '<span></span>';
	//header.onmousedown = initMoveAppointment;
	header.style.cursor = 'pointer';

	var timeDiv = document.createElement('DIV');
	timeDiv.className='calendar_event_time';
	timeDiv.innerHTML = '<span></span>';
	header.appendChild(timeDiv);

	div.appendChild(header);


	var span = document.createElement('DIV');
	//span.onclick = showBubble;
	var innerSpan = document.createElement('SPAN');
	//innerSpan.innerHTML = '<span style="float:right;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp&nbsp;&nbsp;&nbsp;</span>' + title;
	innerSpan.innerHTML =  title;
	span.appendChild(innerSpan);
	span.className = 'calendar_event_txt';
	//span.onclick = showToolTip;
	div.appendChild(span);

	var footerDiv = document.createElement('DIV');
	footerDiv.className='calendar_event_footer';
	footerDiv.style.cursor = 'n-resize';
	footerDiv.innerHTML = '<span></span>';
	//footerDiv.onmousedown = initResizeAppointment;
	div.appendChild(footerDiv);


	events_container.appendChild(div);

	return div;
}

function createNewAllDayAppointmentDiv(leftPos,topPos,width,height,title,color)
{
	var div = document.createElement('DIV');
	div.onclick = setElementActive;
	div.ondblclick = editEventWindow;
	div.className='calendar_all_dayevent'; //last
	if(color > 0)div.style.backgroundColor = colors[color][2];
	//if(color > 0)div.style.border = "1px solid "+ colors[color][0];
	if(color > 0)div.style.border = "1px solid #ffffff";

	div.style.left = leftPos + 'px';
	div.style.top = topPos + 'px';
	div.style.width = width + 'px';
	
	if(activeCalendarView == 'month') {
		div.style.height = '64px';
	}
	//div.onmousedown = initToggleView;

	var span = document.createElement('DIV');
	var innerSpan = document.createElement('SPAN');
	innerSpan.innerHTML = title;
	span.className = 'calendar_all_dayevent_txt';
	span.appendChild(innerSpan);
	//span.onclick = showToolTip;

	//div.onmousedown = initMoveAppointment;
	//div.onclick = initMoveAppointment;
	div.style.cursor = 'pointer';
	div.appendChild(span);

	//if(height)div.style.height = height + 'px';

	/*var header = document.createNewAllDayAppointmentDivcreateElement('DIV');
	header.className= 'calendar_event_header';
	if(color > 0)header.style.backgroundColor = colors[color][0];
	header.innerHTML = '<span></span>';
	header.onmousedown = initMoveAppointment;
	header.style.cursor = 'pointer';

	var timeDiv = document.createElement('DIV');
	timeDiv.className='calendar_event_time';
	timeDiv.innerHTML = '<span></span>';
	header.appendChild(timeDiv);

	div.appendChild(header);


	var span = document.createElement('DIV');
	var innerSpan = document.createElement('SPAN');
	innerSpan.innerHTML = '<span style="float:right;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp&nbsp;&nbsp;&nbsp;</span>' + title;
	span.appendChild(innerSpan);
	span.className = 'calendar_event_txt';
	//span.onclick = showToolTip;
	div.appendChild(span);

	var footerDiv = document.createElement('DIV');
	footerDiv.className='calendar_event_footer';
	footerDiv.style.cursor = 'n-resize';
	footerDiv.innerHTML = '<span></span>';
	footerDiv.onmousedown = initResizeAppointment;
	div.appendChild(footerDiv);

	*/
		var oldDiv = document.getElementById(height);
		if(oldDiv)
			margin_alldays_container.replaceChild(div,oldDiv);
		else
			margin_alldays_container.appendChild(div);



	return div;
}



//This function clears all appointments from the screen - Used when switching from one week to another
//wywoływane przy tygodniach
function clearAppointments()
{
	for(var prop in appointmentProperties){
		if(appointmentProperties[prop]['id']){
			if(document.getElementById(appointmentProperties[prop]['id'])){
				var obj = document.getElementById(appointmentProperties[prop]['id']);
				obj.parentNode.removeChild(obj);
			}
			appointmentProperties[prop]['id'] = false;
		}

	}
}


function clearAppointments2(calid)
{

	for(var prop in appointmentProperties){
		if(appointmentProperties[prop]['id'] && appointmentProperties[prop]['calendar_id'] == calid){
			if(document.getElementById(appointmentProperties[prop]['id'])){
				var obj = document.getElementById(appointmentProperties[prop]['id']);
				obj.parentNode.removeChild(obj);

				/*if(appointmentProperties[prop]['allday'] == '1'){
					var day = appointmentProperties[prop]['eventStartDate'].getDay(); if(day==0)day=7;
					allday_array[day] = allday_array[day]-1;
				}*/
			}
			appointmentProperties[prop]['id'] = false;
		}

	}
}

function clearAppointments3()
{
	for(var prop in appointmentProperties){
		if(appointmentProperties[prop]['id'] && appointmentProperties[prop]['allday'] == '1'){
			if(document.getElementById(appointmentProperties[prop]['id'])){
				var obj = document.getElementById(appointmentProperties[prop]['id']);
				obj.parentNode.removeChild(obj);
			}
			appointmentProperties[prop]['id'] = false;
		}

	}
}

function clearAppointments4()
{
	for(var prop in appointmentProperties){
		if(appointmentProperties[prop]['id']){
			if(document.getElementById(appointmentProperties[prop]['id']).id.indexOf('new_')>=0){
				var obj = document.getElementById(appointmentProperties[prop]['id']);
				obj.parentNode.removeChild(obj);
			}
			appointmentProperties[prop]['id'] = false;
		}

	}
}
function deleteEventFromView(index)
{
	if(weekSchedule_ajaxObjects[index].response=='OK'){
		activeEventObj.parentNode.removeChild(activeEventObj);
		activeEventObj = false;
		 ajax_message('Wydarzenie zostalo usuniete');
	}else{
		// Error handling - event not deleted
		alert('Could not confirm that event has been deleted. Make sure that the script is configured correctly');
	}
}


function getTopPos(inputObj)
{
  var returnValue = inputObj.offsetTop;
  while((inputObj = inputObj.offsetParent) != null){
  	if(inputObj.tagName!='HTML')returnValue += inputObj.offsetTop;
  }
  return returnValue;
}

function getLeftPos(inputObj)
{

  var returnValue = inputObj.offsetLeft;
  while((inputObj = inputObj.offsetParent) != null){
  	if(inputObj.tagName!='HTML')returnValue += inputObj.offsetLeft;
  }
  return returnValue;
}


//obliczanie y mając godzine i minute wywołane z parseItemsFromServer
function getYPositionFromTime(hour,minute){
	return (hour * 60) + minute;
	//return Math.floor((hour - weekplannerStartHour) * (itemRowHeight+1) + (minute/60 * (itemRowHeight+1)));
}

//zwraca minute na podstawie topPos
function getMinute(topPos)
{
	var time = topPos;
	var hour = Math.floor(time/60);
	var minute = time - (hour * 60);

	return minute;
}

//zwraca time div
function getCurrentTimeDiv(inputObj)
{
	var subDivs = inputObj.getElementsByTagName('DIV');
	for(var no=0;no<subDivs.length;no++){
		if(subDivs[no].className=='calendar_event_time'){
			return subDivs[no];
		}
	}
}

//zwraca content div
function getCurrentAppointmentContentDiv(inputDiv)
{
	var divs = inputDiv.getElementsByTagName('DIV');
	for(var no=0;no<divs.length;no++){
		if(divs[no].className=='calendar_event_txt')return divs[no];
	}
}

function getCurrentAppointmentAllDayContentDiv(inputDiv)
{
	var divs = inputDiv.getElementsByTagName('DIV');
	for(var no=0;no<divs.length;no++){
		if(divs[no].className=='calendar_all_dayevent_txt')return divs[no];
	}
}

//zwraca header div
function getCurrentAppointmentHeaderDiv(inputDiv)
{
	var divs = inputDiv.getElementsByTagName('DIV');
	for(var no=0;no<divs.length;no++){
		if(divs[no].className=='calendar_event_header')return divs[no];
	}
}

//odczytanie daty z left wydarzenia
function getAppointmentDate(inputObj)
{
	var leftPos = getLeftPos(inputObj);
	var topPos = getTopPos(inputObj);

	var d = new Date();
	if(activeCalendarView == 'day'){
	var tmpTime = dateStartOfDay.getTime();
	}
	if(activeCalendarView == 'week'){
	var tmpTime = dateStartOfWeek.getTime();
	tmpTime = tmpTime + (1000*60*60*24 * Math.floor((leftPos-appointmentsOffsetLeft) / (dayPositionArray[1] - dayPositionArray[0])));
	}
	if(activeCalendarView == 'month'){
	var tmpTime = dateDisplayOfMonth.getTime();

	leftPos = leftPos - appointmentsOffsetLeft;
	var szerokosc = alldayMonthPositionLeftArray[1] -  alldayMonthPositionLeftArray[0];
	var wysokosc = alldayMonthPositionTopArray[1] -  alldayMonthPositionTopArray[0];
	//alert(topPos);
	//alert(alldayMonthPositionLeftArray);
	//alert(alldayMonthPositionTopArray);

	for(var i = 0; i<alldayMonthPositionLeftArray.length;i++){
		if(leftPos  >= alldayMonthPositionLeftArray[i] && leftPos  <= alldayMonthPositionLeftArray[i]+szerokosc && topPos >= alldayMonthPositionTopArray[i] && topPos <= alldayMonthPositionTopArray[i]+wysokosc){
				day = i;
		}
	}
	//el_y = alldayMonthPositionTopArray[days] - appointmentsOffsetTop + 20;

	tmpTime = tmpTime + (1000*60*60*24 * day);
	}

	d.setTime(tmpTime);
;
	return d;
}

//odczytanie godzin i zapis do tablicy mając top i height
function getTimeAsArray(inputObj)
{
	var startTime = getTopPos(inputObj) - appointmentsOffsetTop;
	var startHour = Math.floor(startTime/60);
	var startMinute = startTime - (startHour * 60);


	var endTime = (startTime + inputObj.offsetHeight);
	var endHour = Math.floor(endTime/60);
	var endMinute = endTime - (endHour * 60);

	return Array(startHour,startMinute,endHour,endMinute);
}

//zwraca godzine i minute
function getTime(inputObj)
{
	var startTime = (getTopPos(inputObj) - appointmentsOffsetTop);

	var startHour = Math.floor(startTime/60);
	var hourPrefix = "";
	if(startHour<10)hourPrefix = "0";

	var startMinute = startTime - (startHour * 60);
	var startMinutePrefix = "";
	if(startMinute<10)startMinutePrefix="0";

	var endTime = (startTime + inputObj.offsetHeight);

	var endHour = Math.floor(endTime/60);

	var endHourPrefix = "";
	if(endHour<10)endHourPrefix = "0";

	var endMinute = endTime - (endHour * 60);
	var endMinutePrefix = "";
	if(endMinute<10)endMinutePrefix="0";

	return hourPrefix + startHour + ':' + startMinutePrefix + "" + startMinute + '-' + endHourPrefix + endHour + ':' + endMinutePrefix + "" +  endMinute;

}

//startujemy z rozszerzaniem
function initMoveAppointment(e,inputObj)
{
	if(document.all)e = event;
	if(!inputObj)inputObj = this.parentNode;
	currentAppointmentDiv = inputObj;
	currentAppointmentContentDiv  = getCurrentAppointmentContentDiv(currentAppointmentDiv);
	currentZIndex = currentZIndex + 1;
	currentAppointmentDiv.style.zIndex = currentZIndex;
	moveAppointmentCounter = 0;
	el_x = getLeftPos(inputObj);
	el_y = getTopPos(inputObj);
	elWidth = inputObj.offsetWidth;
	mouse_x = getMouseX(e);
	mouse_y = getMouseY(e);

	timerMoveAppointment();
	return false;

}


//start dla nowego wydarzenia
function initNewAppointment(e,inputObj)
{
	if(document.all)e = event;
	if(!inputObj)inputObj = this;
	newAppointmentCounter = 0;
	el_x = getLeftPos(inputObj);
	el_y = getTopPos(inputObj);
	elWidth = inputObj.offsetWidth-daysMarginSize;

	mouse_x = getMouseX(e);
	mouse_y = getMouseY(e);

	timerNewAppointment();

	return false;
}

//start dla nowego wydarzenia dla całego dnia
function initNewAllDayAppointment(e,inputObj)
{
	if(document.all)e = event;
	if(!inputObj)inputObj = this;
	newAppointmentCounter = 0;
	el_x = getLeftPos(inputObj);
	el_y = getTopPos(inputObj);
	elWidth = inputObj.offsetWidth-daysMarginSize;

	mouse_x = getMouseX(e);
	mouse_y = getMouseY(e);

	timerNewAllDayAppointment();

	return false;
}

//startujemy z zmianą wysokości
function initResizeAppointment(e)
{
	if(document.all)e = event;
	currentAppointmentDiv = this.parentNode;
	currentAppointmentContentDiv  = getCurrentAppointmentContentDiv(currentAppointmentDiv);
	currentZIndex = currentZIndex + 1;
	currentAppointmentDiv.style.zIndex = currentZIndex;
	resizeAppointmentCounter = 0;
	el_x = getLeftPos(currentAppointmentDiv);
	el_y = getTopPos(currentAppointmentDiv);
	mouse_x = getMouseX(e);
	mouse_y = getMouseY(e);
	resizeAppointmentInitHeight = currentAppointmentDiv.style.height.replace('px','')/1;
	//resizeAppointmentInitHeight = currentAppointmentDiv.offsetHeight;

	timerResizeAppointment();
	return false;
}


function parseItemsFromServer(ajaxIndex)
{
	//var itemsToBeCreated = xml2array(weekSchedule_ajaxObjects[ajaxIndex].responseXML);
	var xotree = new XML.ObjTree();
	var itemsToBeCreated = xotree.parseDOM(weekSchedule_ajaxObjects[ajaxIndex].responseXML)
	//alert(dump(itemsToBeCreated));
	//if(!configToBeCreated['#document']['error']){
	weekSchedule_ajaxObjects[ajaxIndex] = false;


	if(itemsToBeCreated['#document']['wydarzenia']){
		if(!itemsToBeCreated['#document']['error']){

		var ile = 1;
		var allday_count = 0;

		for (var i in itemsToBeCreated['#document']['wydarzenia']['item']) {
			if(!isNaN(i)) ile++;
		}

		if(ile>1){
			itemsToBeCreated = itemsToBeCreated['#document']['wydarzenia']['item'];
		}else{
			itemsToBeCreated = itemsToBeCreated['#document']['wydarzenia'];
		}


		for (var i in itemsToBeCreated) {
			if(itemsToBeCreated[i]['id']){
				eventStartDate = new Date(itemsToBeCreated[i]['eventStartDate']);
				eventEndDate = new Date(itemsToBeCreated[i]['eventEndDate']);

				/*if(eventEndDate.getDay()!=eventStartDate.getDay() && (eventEndDate.getHours()>0 || eventEndDate.getMinutes()>0)){
					eventEndDate.setMinutes(0);
					eventEndDate.setHours(0);
					eventEndDate.setDate(eventStartDate.getDate()+1);
					eventEndDate.setMonth(eventStartDate.getMonth());
					eventEndDate.setFullYear(eventStartDate.getFullYear());
				}*/
				//alert(itemsToBeCreated[i]['allday']);
				//tablica left pozycja krawędzi jednego dnia

				var day = eventStartDate.getDay(); if(day==0)day=7;
				if(activeCalendarView == 'day')
				el_x = dayPositionArray[0];
				else
				el_x = dayPositionArray[day - 1];


				//top pozycja obliczana na podstawie tylko czasu godzina, minuta
				topPos = getYPositionFromTime(eventStartDate.getHours(),eventStartDate.getMinutes());

				//długość wydarzenia obliczna na podstawie czasu Minuty - 2
				var elHeight = ((eventEndDate.getTime() - eventStartDate.getTime()) / (60*1000)) - appointmentsMarginBottom;
				//alert(elHeight);
				//elHeight = elHeight < 15 ? 15 : elHeight;

				//utworzenie nowego wydarzenia

				currentAppointmentDiv = createNewAppointmentDiv((el_x - appointmentsOffsetLeft),topPos,(newAppointmentWidth-appointmentMarginSize),elHeight,itemsToBeCreated[i]['title'],configProperties[itemsToBeCreated[i]['calendar_id']]['color']);
				currentAppointmentDiv.id = itemsToBeCreated[i]['id'];
				currentAppointmentDiv.className='calendar_event'; //last

				currentAppointmentDiv.style.zIndex = currentZIndex+ 1;
				currentTimeDiv = getCurrentTimeDiv(currentAppointmentDiv);
				currentTimeDiv.style.display='block';
				if(elHeight-15 < 0 ) appoheight=0; else appoheight=elHeight-15;
				currentAppointmentContentDiv  = getCurrentAppointmentContentDiv(currentAppointmentDiv);
				currentAppointmentContentDiv.style.height = appoheight + ' px';

				currentTimeDiv.innerHTML = '<span>' +getTime(currentAppointmentDiv) + '</span>';

				if(elHeight < 15){
					currentAppointmentDiv.style.height = '15 px';
				}

				currentAppointmentDiv = false;
				currentTimeDiv = false;

				var newIndex = itemsToBeCreated[i]['id'];
				appointmentProperties[newIndex] = new Array();
				appointmentProperties[newIndex]['id'] = itemsToBeCreated[i]['id'];
				appointmentProperties[newIndex]['calendar_id'] = itemsToBeCreated[i]['calendar_id'];
				appointmentProperties[newIndex]['user_id'] = itemsToBeCreated[i]['user_id'];
				appointmentProperties[newIndex]['title'] = itemsToBeCreated[i]['title'] ? itemsToBeCreated[i]['title'] : '';
				appointmentProperties[newIndex]['description'] = itemsToBeCreated[i]['description'] ? itemsToBeCreated[i]['description'] : '';
				appointmentProperties[newIndex]['place'] = itemsToBeCreated[i]['place'] ? itemsToBeCreated[i]['place'] : '';
				appointmentProperties[newIndex]['type'] = itemsToBeCreated[i]['type'];
				appointmentProperties[newIndex]['allday'] = itemsToBeCreated[i]['allday'];
				appointmentProperties[newIndex]['eventStartDate'] = eventStartDate;
				appointmentProperties[newIndex]['eventEndDate'] = eventEndDate;
				appointmentProperties[newIndex]['object'] = currentAppointmentDiv;

		}

		}

		}else{
			alert(configToBeCreated['#document']['error']);
		}

	}


}

function parseItemsAllDayFromServer(ajaxIndex)
{
	//var itemsToBeCreated = xml2array(weekSchedule_ajaxObjects[ajaxIndex].responseXML);
	var xotree = new XML.ObjTree();
	var itemsToBeCreated = xotree.parseDOM(weekSchedule_ajaxObjects[ajaxIndex].responseXML)
	//alert(dump(itemsToBeCreated));
	//if(!configToBeCreated['#document']['error']){
	weekSchedule_ajaxObjects[ajaxIndex] = false;

	allday_count = 0;
	allday_array = new Array(0,0,0,0,0,0,0,0);

	if(itemsToBeCreated['#document']['wydarzenia']){
		if(!itemsToBeCreated['#document']['error']){

		var ile = 1;

		for (var i in itemsToBeCreated['#document']['wydarzenia']['item']) {
			if(!isNaN(i)) ile++;
		}

		if(ile>1){
			itemsToBeCreated = itemsToBeCreated['#document']['wydarzenia']['item'];
		}else{
			itemsToBeCreated = itemsToBeCreated['#document']['wydarzenia'];
		}


		for (var i in itemsToBeCreated) {
			if(itemsToBeCreated[i]['id']){
				eventStartDate = new Date(itemsToBeCreated[i]['eventStartDate']);
				eventEndDate = new Date(itemsToBeCreated[i]['eventEndDate']);

				allday_count ++;
				var day = eventStartDate.getDay(); if(day==0)day=7;
				if(activeCalendarView == 'day')
				el_x = dayPositionArray[0];
				else
				el_x = dayPositionArray[day - 1];


				//alert(allday_array.getMax());
				//alert(allday_array);

				currentAppointmentDiv = createNewAllDayAppointmentDiv((el_x - appointmentsOffsetLeft),(allday_array[day]*15)+1,(newAppointmentAllDayWidth-appointmentMarginSize-4),itemsToBeCreated[i]['id'],itemsToBeCreated[i]['title'],configProperties[itemsToBeCreated[i]['calendar_id']]['color']);

				//if(activeCalendarView == 'day')
				//allday_array[0] = allday_array[day]+1;
				//else
				
				
				///allday_array[day] = allday_array[day]+1;



				currentAppointmentDiv.id = itemsToBeCreated[i]['id'];
				currentAppointmentDiv.style.zIndex = currentZIndex+ 1;
				currentAppointmentDiv.onclick = editEventCalendar;

				currentAppointmentContentDiv  = getCurrentAppointmentAllDayContentDiv(currentAppointmentDiv);

				currentAppointmentDiv = false;
				currentTimeDiv = false;

				var newIndex = itemsToBeCreated[i]['id'];
				appointmentProperties[newIndex] = new Array();
				appointmentProperties[newIndex]['id'] = itemsToBeCreated[i]['id'];
				appointmentProperties[newIndex]['calendar_id'] = itemsToBeCreated[i]['calendar_id'];
				appointmentProperties[newIndex]['user_id'] = itemsToBeCreated[i]['user_id'];
				appointmentProperties[newIndex]['title'] = itemsToBeCreated[i]['title'] ? itemsToBeCreated[i]['title'] : '';
				appointmentProperties[newIndex]['description'] = itemsToBeCreated[i]['description'] ? itemsToBeCreated[i]['description'] : '';
				appointmentProperties[newIndex]['place'] = itemsToBeCreated[i]['place'] ? itemsToBeCreated[i]['place'] : '';
				appointmentProperties[newIndex]['type'] = itemsToBeCreated[i]['type'];
				appointmentProperties[newIndex]['allday'] = itemsToBeCreated[i]['allday'];
				appointmentProperties[newIndex]['eventStartDate'] = eventStartDate;
				appointmentProperties[newIndex]['eventEndDate'] = eventEndDate;
				appointmentProperties[newIndex]['object'] = currentAppointmentDiv;

		}

		}


		}else{
			alert(configToBeCreated['#document']['error']);
		}

	}

		if(allday_count == 0){
			setSizeWeekCalendarAllDays(16);
		}else{
			setSizeWeekCalendarAllDays(16*( allday_array.getMax() +1 ));
		}
}

function parseItemsAllDayMonthFromServer(ajaxIndex)
{
	//var itemsToBeCreated = xml2array(weekSchedule_ajaxObjects[ajaxIndex].responseXML);
	var xotree = new XML.ObjTree();
	var itemsToBeCreated = xotree.parseDOM(weekSchedule_ajaxObjects[ajaxIndex].responseXML)
	//alert(dump(itemsToBeCreated));
	//if(!configToBeCreated['#document']['error']){
	weekSchedule_ajaxObjects[ajaxIndex] = false;

	allday_count = 0;
	allday_array = new Array(0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0);

	if(itemsToBeCreated['#document']['wydarzenia']){
		if(!itemsToBeCreated['#document']['error']){

		var ile = 1;

		for (var i in itemsToBeCreated['#document']['wydarzenia']['item']) {
			if(!isNaN(i)) ile++;
		}

		if(ile>1){
			itemsToBeCreated = itemsToBeCreated['#document']['wydarzenia']['item'];
		}else{
			itemsToBeCreated = itemsToBeCreated['#document']['wydarzenia'];
		}


		for (var i in itemsToBeCreated) {
			if(itemsToBeCreated[i]['id']){
				eventStartDate = new Date(itemsToBeCreated[i]['eventStartDate']);
				eventEndDate = new Date(itemsToBeCreated[i]['eventEndDate']);


				//alert(alldayMonthPositionLeftArray);
				//alert(alldayMonthPositionTopArray);

				var days = daysBetween(dateDisplayOfMonth,eventStartDate);
				//alert(days);

				allday_count ++;
				//var day = eventStartDate.getDay(); if(day==0)day=7;


				el_x = alldayMonthPositionLeftArray[days] - appointmentsOffsetLeft;
				el_y = alldayMonthPositionTopArray[days] - appointmentsOffsetTop + 20;
				//alert(allday_array);
				/*alert(el_x);
				alert(el_y);*/
				var ujeb;
				if(activeCalendarView == 'month'){
					ujeb = 3;
				}else{
					ujeb = 4;
				}

				currentAppointmentDiv = createNewAllDayAppointmentDiv(el_x ,el_y+((allday_array[days]*15)+1),newAppointmentAllDayWidth-appointmentMarginSize-ujeb,itemsToBeCreated[i]['id'],itemsToBeCreated[i]['title'],configProperties[itemsToBeCreated[i]['calendar_id']]['color']);
				allday_array[days] = allday_array[days]+1;

				currentAppointmentDiv.id = itemsToBeCreated[i]['id'];


				currentAppointmentDiv = false;
				currentTimeDiv = false;

				var newIndex = itemsToBeCreated[i]['id'];
				appointmentProperties[newIndex] = new Array();
				appointmentProperties[newIndex]['id'] = itemsToBeCreated[i]['id'];
				appointmentProperties[newIndex]['calendar_id'] = itemsToBeCreated[i]['calendar_id'];
				appointmentProperties[newIndex]['user_id'] = itemsToBeCreated[i]['user_id'];
				appointmentProperties[newIndex]['title'] = itemsToBeCreated[i]['title'] ? itemsToBeCreated[i]['title'] : '';
				appointmentProperties[newIndex]['description'] = itemsToBeCreated[i]['description'] ? itemsToBeCreated[i]['description'] : '';
				appointmentProperties[newIndex]['place'] = itemsToBeCreated[i]['place'] ? itemsToBeCreated[i]['place'] : '';
				appointmentProperties[newIndex]['type'] = itemsToBeCreated[i]['type'];
				appointmentProperties[newIndex]['allday'] = itemsToBeCreated[i]['allday'];
				appointmentProperties[newIndex]['eventStartDate'] = eventStartDate;
				appointmentProperties[newIndex]['eventEndDate'] = eventEndDate;
				appointmentProperties[newIndex]['object'] = currentAppointmentDiv;

		}

		}


		}else{
			alert(configToBeCreated['#document']['error']);
		}

	}

		/*if(allday_count == 0){
			setSizeWeekCalendarAllDays(16);
		}else{
			setSizeWeekCalendarAllDays(16*( allday_array.getMax() +1 ));
		}*/
}


//zaznaczanie aktywnego elementu, wywołana z createNewAppointmentDiv.onclick
//wyszukanie w aktywnym div który ma klase calendar_event_header
//zamiana klasy na calendar_event_headerActive
//jeżeli activeEventObj zaznczony jakiś div to go odznacz o ile to nie nasz div
function setElementActive(e,inputDiv)
{


	var doc=calendar_container.ownerDocument;
	if(!inputDiv)inputDiv = this;
	var subDivs = inputDiv.getElementsByTagName('DIV');
	for(var no=0;no<subDivs.length;no++){
		if(subDivs[no].className=='calendar_event_header'){
			subDivs[no].className = 'calendar_event_headerActive';
			subDivs[no].style.backgroundColor = colors[configProperties[appointmentProperties[inputDiv.id]['calendar_id']]['color']][1];;
		}
	}
	if(activeEventObj && activeEventObj!=inputDiv){
		setElementInactive(activeEventObj);
	}
	activeEventObj = inputDiv;

	if(!isUpdate)
		showBubble(activeEventObj);

}

//odznaczanie diva jeżeli inny jest zaznaczony wywołana z setElementActive
function setElementInactive(inputDiv)
{
	var subDivs = inputDiv.getElementsByTagName('DIV');
	for(var no=0;no<subDivs.length;no++){
		if(subDivs[no].className=='calendar_event_headerActive'){
			subDivs[no].className = 'calendar_event_header';
			subDivs[no].style.backgroundColor = colors[configProperties[appointmentProperties[inputDiv.id]['calendar_id']]['color']][0];
		}
	}

}

function schedulerMouseUp()
{
	isUpdate = false;
	if(newAppointmentCounter>=0){

		if(newAppointmentCounter==10){
			if(!currentAppointmentDiv)return;
			else{
				//tutaj zaraz po utworzeniu

				//showBubble2(currentAppointmentDiv);
				//activeEventObj.parentNode.removeChild(activeEventObj);
				saveAnItemToServer(currentAppointmentDiv.id);
				currentAppointmentDiv.className = 'calendar_event'; //last
			}

		}

		/*if(newAppointmentCounter==100){
			if(!currentAppointmentDiv)return;
			else{
				//saveAllDayAnItemToServer(currentAppointmentDiv.id);
				currentAppointmentDiv.className = 'calendar_event'; //last
			}

		}*/
	}

	if(snapToMinutes && currentAppointmentDiv && moveAppointmentCounter==10){
		topPos = getTopPos(currentAppointmentDiv) - appointmentsOffsetTop;

		var minute = getMinute(topPos);
		var rest = (minute % snapToMinutes);
		if(rest>(snapToMinutes/2)){
			topPos = topPos + (snapToMinutes/60*(itemRowHeight+1)) - ((rest/60)*(itemRowHeight+1));
		}else{
			topPos = topPos - ((rest/60)*(itemRowHeight+1));
		}
		var minute = getMinute(topPos);
		var rest = (minute % snapToMinutes);
		if(rest!=0){
			topPos = topPos - ((rest/60)*(itemRowHeight+1));
		}

		var minute = getMinute(topPos);
		var rest = (minute % snapToMinutes);
		if(rest!=0){
			topPos = topPos - ((rest/60)*(itemRowHeight+1));
		}
		currentAppointmentDiv.style.top = topPos + 'px';
		currentTimeDiv.innerHTML = '<span>' + getTime(currentAppointmentDiv) + '</span>';
	}

	if(currentAppointmentDiv && snapToMinutes && (resizeAppointmentCounter==10 || newAppointmentCounter)){
		//autoResizeAppointment();
	}


	if(currentAppointmentDiv && !contentEditInProgress && newAppointmentCounter!=100){
		//alert(resizeAppointmentCounter);
		//alert(moveAppointmentCounter);


		repositionFooter(currentAppointmentDiv);
		if(resizeAppointmentCounter == 10 || moveAppointmentCounter == 10){
			//alert(resizeAppointmentCounter);
			//alert(moveAppointmentCounter);
			updateAppointmentProperties(currentAppointmentDiv.id);
			isUpdate = true;
		}
	}
	//if(currentTimeDiv)currentTimeDiv.style.display='none';



	currentAppointmentDiv = false;
	currentTimeDiv = false;
	moveAppointmentCounter = -1;
	resizeAppointmentCounter = -1;
	newAppointmentCounter = -1;
	toggleViewCounter = -1;
}


function schedulerMouseMove(e)
{
	if(document.all)e = event;
	if(newAppointmentCounter==10){
		if(!currentAppointmentDiv)return;
		var tmpHeight = e.clientY - mouse_y

		if((getTopPos(currentAppointmentDiv) - appointmentsOffsetTop) + tmpHeight > 1438){
			tmpHeight = 1438 - getTopPos(currentAppointmentDiv) + appointmentsOffsetTop;
		}

		//alert(currentAppointmentDiv.id);
		/*if(getTopPos(currentAppointmentDiv) - appointmentsOffsetTop + tmpHeight >= document.getElementById('weekScheduler_content').scrollTop + 510){

			document.getElementById('weekScheduler_content').scrollTop +=5;
			mouse_y -=5;

		}*/
		if(currentAppointmentDiv.id.indexOf('new_')>=0){
			currentAppointmentDiv.style.height = Math.max(13,tmpHeight) + 'px';
			currentTimeDiv.innerHTML = '<span>' + getTime(currentAppointmentDiv) + '</span>';
		}
	}

	if(moveAppointmentCounter==10){
		var topPos = (e.clientY - mouse_y + el_y - appointmentsOffsetTop);
		if(topPos < 0) topPos=0;
		if((topPos+currentAppointmentDiv.offsetHeight)>1440) topPos = 1440 - currentAppointmentDiv.offsetHeight;

		if(topPos <= content_container.scrollTop){
			scroll();
		}

		currentAppointmentDiv.style.top = topPos + 'px';

		var destinationLeftPos = false;
		for(var no=0;no<dayPositionArray.length;no++){
			if(e.clientX>dayPositionArray[no])destinationLeftPos = dayPositionArray[no];
		}
		if(destinationLeftPos)
			currentAppointmentDiv.style.left = (destinationLeftPos + appointmentMarginSize - appointmentsOffsetLeft) + 'px';
			currentTimeDiv.innerHTML = '<span>' + getTime(currentAppointmentDiv) + '</span>';
	}

	if(resizeAppointmentCounter==10){
		rozmiar = resizeAppointmentInitHeight + e.clientY - mouse_y;

		if((getTopPos(currentAppointmentDiv) + rozmiar) - appointmentsOffsetTop > 1438){
			rozmiar = (1438 + appointmentsOffsetTop) - getTopPos(currentAppointmentDiv);
		}

		currentAppointmentContentDiv.style.height = (Math.max((rozmiar),10)-8) + 'px';
		currentAppointmentDiv.style.height = Math.max((rozmiar),13) + 'px';
		currentTimeDiv.innerHTML = '<span>' + getTime(currentAppointmentDiv) + '</span>';
	}

}

//odświezenie daty i godziny po przeniesieniu lub złapaniu
//wywoływane z saveAnItemToServer i schedulerMouseUp
function updateAppointmentProperties(id)
{
	var obj = document.getElementById(id);
	var timeArray = getTimeAsArray(obj);
	var startDate = getAppointmentDate(obj);
	var endDate = new Date();
	endDate.setTime(startDate.getTime());

	startDate.setHours(timeArray[0]);
	startDate.setMinutes(timeArray[1]);

	endDate.setHours(timeArray[2]);
	endDate.setMinutes(timeArray[3]);

	/*
	var startDateString = startDate.toGMTString().replace('UTC','GMT');
	var endDateString = endDate.toGMTString().replace('UTC','GMT');
	*/
	appointmentProperties[obj.id]['eventStartDate'] = startDate;
	appointmentProperties[obj.id]['eventEndDate'] = endDate;


	//if(instantSave && appointmentProperties[obj.id]['description'].length>0){
	if(instantSave && id.indexOf('new_') < 0){
		saveAnItemToServer(obj.id);
	}
}

function updateAllDayAppointmentProperties(id)
{

	var obj = document.getElementById(id);
	//var timeArray = getTimeAsArray(obj);
	var startDate = getAppointmentDate(obj);
	var endDate = new Date();
	endDate.setTime(startDate.getTime());

	//startDate.setHours(0);
	//startDate.setMinutes(1);

	//endDate.setHours(23);
	//endDate.setMinutes(59);
	//alert(startDate);
	//alert(endDate);
	/*
	var startDateString = startDate.toGMTString().replace('UTC','GMT');
	var endDateString = endDate.toGMTString().replace('UTC','GMT');
	*/
	//alert(obj.id);
	appointmentProperties[obj.id]['eventStartDate'] = startDate;
	appointmentProperties[obj.id]['eventEndDate'] = endDate;


	//if(instantSave && appointmentProperties[obj.id]['description'].length>0){
	if(instantSave && id.indexOf('new_') < 0){
		saveAllDayAnItemToServer(obj.id);
	}
}
