var posX = 0;
var posY = 0;

function ietruebody(){
return (document.compatMode && document.compatMode!="BackCompat")? document.documentElement : document.body
};

function createCalendar(el,evt,datePoczatek){
var divParentCalendar = document.getElementById('parentCalendar');
if(divParentCalendar.style.visibility == 'visible')
closeCalendar();
else{
objEdytorText.divTextData = el.previousSibling;
objEdytorText.datePoczatek = datePoczatek;
/*
if(objEdytorText.datePoczatek == 1){
objEdytorText.year = objEdytorText.wyborYearS;
objEdytorText.month = objEdytorText.wyborMonthS;
}
else if(objEdytorText.datePoczatek == 2){
objEdytorText.year = objEdytorText.wyborYearE;
objEdytorText.month = objEdytorText.wyborMonthE;
}
*/

var contentMonth = document.getElementById('selMonth_content');
var contentYear = document.getElementById('selYear_content');
contentMonth.innerHTML = objEdytorText.tabMonth[objEdytorText.month-1];
contentYear.innerHTML = objEdytorText.year;
  
objEdytorText.aktualizujKalendarz();

if(evt.pageX || evt.pageY){
posX = evt.pageX;
posY = evt.pageY;}
else if(evt.clientX || evt.clientY){
posX = evt.clientX+ietruebody().scrollLeft;
posY = evt.clientY+ietruebody().scrollTop;;}

pokazCalendar();
}
}; 

function pokazCalendar(){

var divParentCalendar = document.getElementById('parentCalendar');
var divShadowCalendar = document.getElementById('parentCalendarSadow');
var divTopCalendar = document.getElementById('calendarTop');

divParentCalendar.style.visibility='visible';
divShadowCalendar.style.visibility='visible';

divParentCalendar.style.top = posY+'px';
divParentCalendar.style.left = posX+'px';

divShadowCalendar.style.top = posY+4+'px';
divShadowCalendar.style.left = posX+4+'px';

DragCalendar.init(divParentCalendar, divParentCalendar,divShadowCalendar);
};


function closeCalendar(){
objEdytorText.wyborYearS = objEdytorText.oldYearS;
objEdytorText.wyborMonthS = objEdytorText.oldMonthS;
objEdytorText.wyborYearE = objEdytorText.oldYearE;
objEdytorText.wyborMonthE = objEdytorText.oldMonthE;
var divParentCalendar = document.getElementById('parentCalendar');
var divShadowCalendar = document.getElementById('parentCalendarSadow');

divParentCalendar.style.visibility='hidden';
divShadowCalendar.style.visibility='hidden';
};


var DragCalendar = {
obj : null,
objTlo : null, 
init : function(o, oRoot,tlo, minX, maxX, minY, maxY, bSwapHorzRef, bSwapVertRef, fXMapper, fYMapper){
o.onmousedown	= DragCalendar.start;
o.hmode			= bSwapHorzRef ? false : true ;
o.vmode			= bSwapVertRef ? false : true ;
o.root = oRoot && oRoot != null ? oRoot : o ;
	if (o.hmode  && isNaN(parseInt(o.root.style.left  ))) o.root.style.left   = "0px";
	if (o.vmode  && isNaN(parseInt(o.root.style.top   ))) o.root.style.top    = "0px";
	if (!o.hmode && isNaN(parseInt(o.root.style.right ))) o.root.style.right  = "0px";
	if (!o.vmode && isNaN(parseInt(o.root.style.bottom))) o.root.style.bottom = "0px";
o.minX	= typeof minX != 'undefined' ? minX : null;
o.minY	= typeof minY != 'undefined' ? minY : null;
o.maxX	= typeof maxX != 'undefined' ? maxX : null;
o.maxY	= typeof maxY != 'undefined' ? maxY : null;
o.xMapper = fXMapper ? fXMapper : null;
o.yMapper = fYMapper ? fYMapper : null;
o.root.onDragStart	= new Function();
o.root.onDragEnd	= new Function();
o.root.onDrag		= new Function();
},
start : function(e){
var o = DragCalendar.obj = this;
DragCalendar.objTlo = document.getElementById('parentCalendarSadow');
e = DragCalendar.fixE(e);
var y = parseInt(o.vmode ? o.root.style.top  : o.root.style.bottom);
var x = parseInt(o.hmode ? o.root.style.left : o.root.style.right );
o.root.onDragStart(x, y);
o.lastMouseX	= e.clientX;
o.lastMouseY	= e.clientY;
	if (o.hmode) {
		if (o.minX != null)	o.minMouseX	= e.clientX - x + o.minX;
		if (o.maxX != null)	o.maxMouseX	= o.minMouseX + o.maxX - o.minX;
	} else {
		if (o.minX != null) o.maxMouseX = -o.minX + e.clientX + x;
		if (o.maxX != null) o.minMouseX = -o.maxX + e.clientX + x;}
	if (o.vmode) {
		if (o.minY != null)	o.minMouseY	= e.clientY - y + o.minY;
		if (o.maxY != null)	o.maxMouseY	= o.minMouseY + o.maxY - o.minY;
	} else {
		if (o.minY != null) o.maxMouseY = -o.minY + e.clientY + y;
		if (o.maxY != null) o.minMouseY = -o.maxY + e.clientY + y;}
document.onmousemove	= DragCalendar.DragCalendar;
document.onmouseup		= DragCalendar.end;
	return false;
},
DragCalendar : function(e){
e = DragCalendar.fixE(e);
var o = DragCalendar.obj;
var ey	= e.clientY;
var ex	= e.clientX;
var y = parseInt(o.vmode ? o.root.style.top  : o.root.style.bottom);
var x = parseInt(o.hmode ? o.root.style.left : o.root.style.right );
var nx, ny;
if (o.minX != null) ex = o.hmode ? Math.max(ex, o.minMouseX) : Math.min(ex, o.maxMouseX);
if (o.maxX != null) ex = o.hmode ? Math.min(ex, o.maxMouseX) : Math.max(ex, o.minMouseX);
if (o.minY != null) ey = o.vmode ? Math.max(ey, o.minMouseY) : Math.min(ey, o.maxMouseY);
if (o.maxY != null) ey = o.vmode ? Math.min(ey, o.maxMouseY) : Math.max(ey, o.minMouseY);
nx = x + ((ex - o.lastMouseX) * (o.hmode ? 1 : -1));
ny = y + ((ey - o.lastMouseY) * (o.vmode ? 1 : -1));
if (o.xMapper)		nx = o.xMapper(y);
else if (o.yMapper)	ny = o.yMapper(x);    
DragCalendar.obj.root.style[o.hmode ? "left" : "right"] = nx + "px";
DragCalendar.obj.root.style[o.vmode ? "top" : "bottom"] = ny + "px";
DragCalendar.obj.lastMouseX	= ex;
DragCalendar.obj.lastMouseY	= ey;
DragCalendar.objTlo.style[o.hmode ? "left" : "right"] = nx+ 3 + "px";
DragCalendar.objTlo.style[o.vmode ? "top" : "bottom"] = ny + 3 + "px";
DragCalendar.obj.root.onDrag(nx, ny);
	return false;
},
end : function(){
document.onmousemove = null;
document.onmouseup   = null;
DragCalendar.obj.root.onDragEnd(parseInt(DragCalendar.obj.root.style[DragCalendar.obj.hmode ? "left" : "right"]), 
parseInt(DragCalendar.obj.root.style[DragCalendar.obj.vmode ? "top" : "bottom"]));
DragCalendar.obj = null;
},
fixE : function(e){
if (typeof e == 'undefined') e = window.event;
if (typeof e.layerX == 'undefined') e.layerX = e.offsetX;
if (typeof e.layerY == 'undefined') e.layerY = e.offsetY;
	return e;
}
};
