var oUserMenu;
var oBookForm;
var oBookFormShadow;
var bIsUserMenuOpenedByMouse;
var bIsBookFormOpenedByMouse;
var bIsMouseOverBookForm;

function ShowUserMenu() {
	oUserMenu.style.display = 'block';
}

function HideUserMenu() {
	oUserMenu.style.display = 'none';
}

function SwitchUserMenu() {
	bIsUserMenuOpenedByMouse = true;
	if(oUserMenu.style.display=='block') 
		HideUserMenu();
	else
		ShowUserMenu();
}


function ShowBookForm() {
	
	document.getElementById('BookFormInner').style.display = 'block';
	document.getElementById('BookFormSuccess').style.display = 'none';

	var iWindowScrollX = document.documentElement.scrollLeft;
	var iWindowScrollY = document.documentElement.scrollTop;
	
	var iWindowWidth = document.documentElement.clientWidth;
	var iWindowHeight = document.documentElement.clientHeight;
	
	
	oBookForm.style.left = "-1000px";
	oBookForm.style.display='block';            
	
	oBookFormShadow.style.width = oBookForm.offsetWidth + "px";
	oBookFormShadow.style.height = oBookForm.offsetHeight + "px";
	oBookFormShadow.style.left = (iWindowScrollX + iWindowWidth/2 - oBookForm.offsetWidth/2+7) + "px";
	oBookFormShadow.style.top = (iWindowScrollY + iWindowHeight/2 - oBookForm.offsetHeight/2-40+8) + "px";
	
	oBookForm.style.left = (iWindowScrollX + iWindowWidth/2 - oBookForm.offsetWidth/2) + "px";
	oBookForm.style.top = (iWindowScrollY + iWindowHeight/2 - oBookForm.offsetHeight/2-40) + "px";

	oBookFormShadow.style.display='block';            
}

function HideBookForm() {
	oBookForm.style.display='none';            
	oBookFormShadow.style.display='none';            
}

function SwitchBookForm() {
	bIsBookFormOpenedByMouse = true;
	if(oBookForm.style.display=='block') 
		HideBookForm();
	else
		ShowBookForm();
}


function Init() {
	oUserMenu = document.getElementById('UserMenu');
	oBookForm = document.getElementById('BookForm');
	oBookFormShadow = document.getElementById('BookFormShadow');
	
	document.onmousemove = function(e) {
		if(document.all) e=event;
		
		if(oBookForm.style.display) {
			if( e.clientX>parseInt(oBookForm.style.left)&&e.clientX<(parseInt(oBookForm.style.left)+oBookForm.offsetWidth+20)
					&& e.clientY>parseInt(oBookForm.style.top)&&e.clientY<(parseInt(oBookForm.style.top)+oBookForm.offsetHeight+40) ) {
				bIsMouseOverBookForm = true;
			} else {
				bIsMouseOverBookForm = false;
			}
		}
	}
	
	document.onclick = function() {
		if(bIsUserMenuOpenedByMouse) bIsUserMenuOpenedByMouse = false;
		else {
			HideUserMenu();
		}

		if(bIsBookFormOpenedByMouse) bIsBookFormOpenedByMouse = false;
		else {
//			if(!bIsMouseOverBookForm&&(window._dynarch_popupCalendar&&window._dynarch_popupCalendar.element.style.display!="block")) HideBookForm();
		}
		
	};
	
}


function RequestBookVacation(sStartDate, sEndDate) {
	
	var sStart = document.getElementById('BookForm_StartField').value;
	var sEnd = document.getElementById('BookForm_EndField').value;
	var iType = document.getElementById('BookForm_VacType').value;

	xAjaxRequest(sHtmlRoot+"vacations/actions_handler.phtml?Start=" + sStart + "&End=" + sEnd + "&Type=" + iType + "&VIEW[Start]=" + sStartDate + "&VIEW[End]=" + sEndDate);
//	alert(sHtmlRoot+"vacations/actions_handler.phtml?Start=" + sStart + "&End=" + sEnd + "&Type=" + iType + "&VIEW[Start]=" + sStartDate + "&VIEW[End]=" + sEndDate);
	
}

function BookVacationResponse(sResultXML) {

	//Успешный ответ AJAX'а. Распарсим тег <error>
	oError = sResultXML.getElementsByTagName('error')[0];
	iErrorCode = parseInt(oError.attributes.getNamedItem("code").value);
	if( iErrorCode==0 ) {
		oVacation = sResultXML.getElementsByTagName('vacation')[0];
		iStartDay = parseInt(oVacation.attributes.getNamedItem("start").value);
		iEndDay = parseInt(oVacation.attributes.getNamedItem("end").value);
		iWorkerID = parseInt(oVacation.attributes.getNamedItem("workerid").value);
		iVacationID = parseInt(oVacation.attributes.getNamedItem("vacationid").value);
		sVacationBox = oVacation.firstChild.data;

		oCalendarShow = sResultXML.getElementsByTagName('calendarshow')[0];
		iShowStartDay = parseInt(oCalendarShow.attributes.getNamedItem("start").value);
		iShowEndDay = parseInt(oCalendarShow.attributes.getNamedItem("end").value);
		
		
		if(iStartDay<=iShowEndDay&&iEndDay>=iShowStartDay) {
			//попали в диапазон отрисовки - начинается самое интересное :)
			iStartDrawingCell = Math.max(iStartDay, iShowStartDay);
			iEndDrawingCell = Math.min(iEndDay, iShowEndDay);
			oStartCell = document.getElementById("CC_"+iWorkerID+"_"+iStartDrawingCell);
			oStartCell.setAttribute("colspan", iEndDrawingCell-iStartDrawingCell+1);
			oStartCell.className += " Vacation Editable";
			for( i=iStartDrawingCell+1; i<=iEndDrawingCell; i++ ) {
				document.getElementById("CC_"+iWorkerID+"_"+i).removeNode(true);
			}
			oStartCell.innerHTML = sVacationBox;
			oStartCell.onclick = function () {
				document.location.href="./?Edit=" + iVacationID;
			}
			
		}

		document.getElementById('BookFormInner').style.display = 'none';
		document.getElementById('BookFormSuccess').style.display = 'block';
	
	} else {
		sErrorText = oError.firstChild.data;
		alert(sErrorText);
	}
}

function EditVacation(VacationID) {
	document.location.href="./?Edit="+VacationID;
}



/*******************
		AJAX
 *******************/

function xAjaxRequest(url) 
{
    // branch for native XMLHttpRequest object
    if (window.XMLHttpRequest) {
        oRequest = new XMLHttpRequest();
        oRequest.onreadystatechange = xAjaxResponse;
        oRequest.open("GET", url, true);
        oRequest.send(null);
    // branch for IE/Windows ActiveX version
    } else if (window.ActiveXObject) {
        try {
            oRequest = new ActiveXObject("Msxml2.XMLHTTP");
        } catch (e) {
            try {
	            oRequest = new ActiveXObject("Microsoft.XMLHTTP");
            } catch (e) {
				bWasError = true;
           	}
        }
        if (oRequest) {
        	try {
	            oRequest.onreadystatechange = xAjaxResponse;
	            oRequest.open("GET", url, true);
	            oRequest.send();
			} catch (e) {
				bWasError = true;
			}
        }
    }
}

function xAjaxResponse() 
{
    if (oRequest.readyState == 4) {
        if (oRequest.status == 200) {
        
			sMethodName = oRequest.responseXML.documentElement.getElementsByTagName('method')[0].firstChild.data;
			eval(sMethodName + '(oRequest.responseXML.documentElement.getElementsByTagName(\'result\')[0])');
        } else {
            alert("Слишком медленная связь с сервером в данный момент, попробуйте еще раз чуть позже");
        }
    }
}


if(window.HTMLElement) {
	HTMLElement.prototype.removeNode = function(removeChildren) {
	  if (Boolean(removeChildren))
		return this.parentNode.removeChild(this);
	  else {
		var r=document.createRange();
		r.selectNodeContents(this);
		return this.parentNode.replaceChild(r.extractContents(),this);
	  }
	}
}
