// JavaScript Document

//--CALENDAR---------------------------------------------------------------------------------------------------
/*
@date = rok-měsíc-den
*/
function Calendar(id, date) {
	
	//datum	
	var inputDate = date.split("-");	
	var year = parseInt(inputDate[0]);
	var month = parseInt(inputDate[1]);	
	if(inputDate[2]) var day = parseInt(inputDate[2]); else var day = 1;
	
	//datumy pro přepnutí
	var prewYear = year;
	var prewMonth = month - 1;	
	if(prewMonth == 0) { prewMonth = 12; prewYear = year - 1; }
	
	var nextYear = year;
	var nextMonth = month + 1;
	if(nextMonth == 13) { nextMonth = 1; nextYear = year + 1; }

	//počet dnů v měsíci	
	var dayCount = 32 - new Date(year, month-1, 32).getDate();
	
	//počet dnů v předchozím měsíci
	var dayCountPrewMonth = 32 - new Date(prewYear, prewMonth-1, 32).getDate();
	
	//počáteční den měsíce
	var cellDayStart = new Date(year, month-1, 1).getDay();
	if(cellDayStart == 0) cellDayStart = 7;
	
	//přístup k objektům
	eraseTag(id);
	var calendarELM = document.getElementById(id);
	
	//vytvoření základního DIVU pro případ, že je kalendář volán jen JavaScriptem
	if(!calendarELM) {
		var calendarELM = document.createElement('div');
			calendarELM.setAttribute('id', id);
			calendarELM.setAttribute('class', 'div_calendar');
			
		var parentELM = document.getElementById(parentID);
			parentELM.appendChild(calendarELM);
	}
	
	//--VYTVOŘENÍ TABULKY--------------------------------------------------------------
	var tableELM = document.createElement('table');
		tableELM.setAttribute('class', 'table_butt');
		tableELM.setAttribute('cellpadding', '0');
		tableELM.setAttribute('cellspacing', '0');
		calendarELM.appendChild(tableELM);	
	
	//--SESTAVENÍ OVLÁDÁNÍ-------------------------------------------------------------	
	var trELM = document.createElement('tr');			
	
	//předchozí měsíc
	var tdELM = document.createElement('td');
		tdELM.setAttribute('class', 'td_left');
	
	var aELM = document.createElement('a');
		aELM.setAttribute('href', 'javascript:Calendar(\'' + id + '\', \'' + prewYear + '-' + prewMonth + '\')');
		aELM.setAttribute('class', 'a_prew_month');
		aELM.setAttribute('title', words['previous month']);	
		
		tdELM.appendChild(aELM);
		trELM.appendChild(tdELM);		
		
	//název měsíce
	var tdELM = document.createElement('td');
		tdELM.setAttribute('class', 'td_month');
	
	var tdTXT = document.createTextNode(wordsMonths[month] + ' ' + year); 
		tdELM.appendChild(tdTXT);
		trELM.appendChild(tdELM);
	
	//další měsíc
	var tdELM = document.createElement('td');
		tdELM.setAttribute('class', 'td_right');
	
	var aELM = document.createElement('a');
		aELM.setAttribute('href', 'javascript:Calendar(\'' + id + '\', \'' + nextYear + '-' + nextMonth + '\')');
		aELM.setAttribute('class', 'a_next_month');
		aELM.setAttribute('title', words['next month']);	
		
		tdELM.appendChild(aELM);
		trELM.appendChild(tdELM);		
		
		tableELM.appendChild(trELM);
	//---------------------------------------------------------------------------------
	
	//--SESTAVENÍ BUŇEK DNŮ------------------------------------------------------------
	//--VYTVOŘENÍ TABULKY--------------------------------------------------------------
	var tableELM = document.createElement('table');
		tableELM.setAttribute('class', 'table_days');
		tableELM.setAttribute('cellpadding', '0');
		tableELM.setAttribute('cellspacing', '0');
		calendarELM.appendChild(tableELM);
		
	var tbodyELM = document.createElement('tbody');
		tableELM.appendChild(tbodyELM);	
	
	var trELM = document.createElement('tr');
		trELM.setAttribute('class', 'tr_day');
	for(var i=1; i<wordsDaysShort.length; i++) {
		var tdELM = document.createElement('td');
		var tdTXT = document.createTextNode(wordsDaysShort[i]);
			tdELM.appendChild(tdTXT);
			trELM.appendChild(tdELM);			
	}
	tbodyELM.appendChild(trELM);	
	
	var dayCell = 0;			
	var start = false;
	var end = false;

	//řádky
	for(var r=1; r<=6; r++) {
		
		var trELM = document.createElement('tr');
		
		//buňky
		for(var c=1; c<=7; c++) {		
			
			//určení čísla dne
			if(start == false) dayCell = dayCountPrewMonth - (cellDayStart - (1 + c));
			if(start == false && cellDayStart == c) { dayCell = 0; start = true; }
			if(start == true && dayCell == dayCount) { dayCell = 0; end = true; }
			if(start == true && dayCell < dayCount) ++dayCell;
			
			//styl buňky
			var tdStyle = '';
			if(start == false || end == true) tdStyle = 'td_out';
			if(dayCell == eval('JavaScriptVARS_' + id)['dayAC'] && (year == eval('JavaScriptVARS_' + id)['yearAC'] && month == eval('JavaScriptVARS_' + id)['monthAC']) && (start == true && end == false)) tdStyle = 'td_ac';
			
			//styl odkazu
			var cellStyle = '';
			if(c == 6) cellStyle = 'a_sat';
			if(c == 7) cellStyle = 'a_sun';			

			var tdTXT = document.createTextNode(dayCell);
			var tdELM = document.createElement('td');
			tdELM.setAttribute('class', tdStyle);
			
			//odkaz
			if(start == true && end == false) {
				
				var popisData = wordsDays[c] + ', ' + dayCell +'.' + month + '.' + year;
				var myID = eval('JavaScriptVARS_' + id)['JavaScriptID'] + 'day' + dayCell;
				
				aELM = document.createElement('a');
				aELM.setAttribute('class', cellStyle);
				aELM.setAttribute('id', myID);
				
				if(eval('JavaScriptVARS_' + id)['JSdayClick']) {
					aELM.setAttribute('onclick', eval('JavaScriptVARS_' + id)['JSdayClick'] + '(\'' + myID + '\',' + year + ',' + month + ',' + dayCell + ');');
				}else{
					aELM.setAttribute('href', eval('JavaScriptVARS_' + id)['URL_DAY'] + year + '-' + month + '-' + dayCell);	
				}
				aELM.setAttribute('title', popisData);

				aELM.appendChild(tdTXT);
				tdELM.appendChild(aELM);				
				
			}else{
								
				tdELM.appendChild(tdTXT);	
			}
			
			//AKCE
			if(eval('JavaScriptVARS_' + id)['ACTION']) {
				if(start == true && end == false) {
					var divELM = document.createElement('div');	
						divELM.setAttribute('id', id + 'day_' + dayCell);
						divELM.setAttribute('class', 'div_square');
						tdELM.appendChild(divELM);
				}
			}
						
			trELM.appendChild(tdELM);
		}		
		tbodyELM.appendChild(trELM);
	}
	
	//načtení akcí
	if(eval('JavaScriptVARS_' + id)['ACTION']) {		
		getPreloader(id);		
		ajaxConnect(eval('JavaScriptVARS_' + id)['ACTION_AJAX_URL'], 'getActions', 'id=' + id + '&year=' + year + '&month=' + month);
	}
	//---------------------------------------------------------------------------------
}
//-------------------------------------------------------------------------------------------------------------

//--GET ACTIONS------------------------------------------------------------------------------------------------
function getActions(http_request) {
	
	if(http_request.readyState == 4 && http_request.status == 200) {
		
		//XML data
		var xmlResponce = http_request.responseXML;
		
		//root node
		var rootNODE = xmlResponce.getElementsByTagName('data').item(0);
		
		//config
		var configNODE = rootNODE.getElementsByTagName('config').item(0);
		var configID = configNODE.getElementsByTagName('id').item(0).firstChild.data;
		
		//actions
		var actionsELM = rootNODE.getElementsByTagName('actions').item(0).childNodes;

		for(var i=0; i<actionsELM.length; i++) {
			
			var actionELM = actionsELM.item(i);
			var day = actionELM.getAttribute('day');
			var linkURL = actionELM.getAttribute('link');
			var src = actionELM.getAttribute('src');
			var imgSize = actionELM.getAttribute('imgSize');
			var name = actionELM.firstChild.data;
			
			var idDIV = configID + 'day_' + day;
			var divELM = document.getElementById(idDIV);
			
			//vytvoření odkazu
			var aELM = document.createElement('a');
				aELM.setAttribute('href', linkURL);									
			
			var imgELM = document.createElement('img');
				imgELM.setAttribute('src', src);
				imgELM.setAttribute('width', imgSize);
				imgELM.setAttribute('height', imgSize);
				imgELM.setAttribute('alt', name);
				imgELM.setAttribute('title', name);
				aELM.appendChild(imgELM);
				divELM.appendChild(aELM);
		}
		
		//smazání preloaderu
		removePreloader();
	}	
}
//-------------------------------------------------------------------------------------------------------------

//--HTTP REQUEST-----------------------------------------------------------------------------------------------
function ajaxConnect(ajaxURL, functionName, post){
	
	if(window.XMLHttpRequest){
		http_request = new XMLHttpRequest();
	}else if(window.ActiveXObject){
		try{
			http_request = new ActiveXObject("Msxml2.XMLHTTP");
		}catch (eror){
			http_request = new ActiveXObject("Microsoft.XMLHTTP");
		}
	}

	if(functionName) http_request.onreadystatechange = function() { eval(functionName + '(http_request)'); };	
	http_request.open('POST', ajaxURL, true);
	http_request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	http_request.send(post);	
}
//-------------------------------------------------------------------------------------------------------------

//--PRELOADER--------------------------------------------------------------------------------------------------
function getPreloader(parentID){
	
	var parent = document.getElementById(parentID);
	
	var elmDIV = document.createElement('div');
		elmDIV.setAttribute('id', 'preloader');
		elmDIV.setAttribute('class', 'div_preloader');
		
	var imgELM = document.createElement('img');
		imgELM.setAttribute('src', eval('JavaScriptVARS_' + parentID)['preloaderIMG']);
		elmDIV.appendChild(imgELM);
		
	parent.appendChild(elmDIV);		
}

function removePreloader(){
	
	var preloader = document.getElementById('preloader');
		preloader.parentNode.removeChild(preloader);		
}
//-------------------------------------------------------------------------------------------------------------

//--ERASE TAG--------------------------------------------------------------------------------------------------
function eraseTag(id){
	var elm = document.getElementById(id);	
	if(elm){
		while(elm.hasChildNodes()){
			elm.removeChild(elm.lastChild);
		}
	}
}
//-------------------------------------------------------------------------------------------------------------
