// JavaScript Document

var prevYear = 0;
var nextYear = 0;
var prevMonth = 0;
var nextMonth = 0;

var calenderData = Array();

firstCell = 0;

var mX=0, mY=0;
var IE = document.all ? true : false;

var xmlhttp=false;
/*@cc_on @*/
/*@if (@_jscript_version >= 5)
// JScript gives us Conditional compilation, we can cope with old IE versions.
// and security blocked creation of the objects.
 try {
  xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
 } catch (e) {
  try {
   xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
  } catch (E) {
   xmlhttp = false;
  }
 }
@end @*/
if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
	try {
		xmlhttp = new XMLHttpRequest();
	} catch (e) {
		xmlhttp=false;
	}
}
if (!xmlhttp && window.createRequest) {
	try {
		xmlhttp = window.createRequest();
	} catch (e) {
		xmlhttp=false;
	}
}




function alterDivContent(div, content) {
	if (document.all) {
		eval("document.all."+div).innerHTML=content;
		
	} else if (document.getElementById) {
		rng = document.createRange();
		el = document.getElementById(div);
		rng.setStartBefore(el);
		htmlFrag = rng.createContextualFragment(content);
		while (el.hasChildNodes())
			el.removeChild(el.lastChild);
		el.appendChild(htmlFrag);
	}
}

function hideDiv(div) {
	el = document.getElementById(div);
	if(el) {
		el.style.visibility = 'hidden';
		el.style.display = 'none';
	}
}

function showDiv(div) {
	el = document.getElementById(div);
	if(el) {
		el.style.visibility = 'visible';
		el.style.display = 'block';
	}
}

function setDivStyle(div, s) {
	el = document.getElementById(div);
	if(el) {
		el.className = s;
	}
}





function cUpdateCalender(data) {
	var firstDay = 0;
	var numDays = 1;
	//alert(data);
	
	
	// DATA CHUNK *********************************************************************
	// break out part from data
	var pos = data.indexOf("|");
	if(pos != -1) {
		var curData = data.substr(0, pos);
		data = data.substr(pos+1, data.length);
	} else {
		var curData = data.substr(0, data.length);
		data = "";
	}
	
	// split into pieces
	var temp = curData.split(',');//curData.substr(0, data.indexOf(","));
	
	alterDivContent('curMonthCell', '<a href="javascript: cLoadCalender();" onMouseOver="alterDivContent(\'tooltipDiv\', \'Klicka h&auml;r f&ouml;r att &aring;terg&aring; till nuvarande m&aring;nad och &aring;r\'); showDiv(\'tooltipDiv\');" onmouseOut="hideTooltip();">'+temp[0]+'</a>');
	
	firstDay = temp[1]*1;
	maxDate = temp[2]*1;
	
	prevYear = temp[5]*1;
	nextYear = temp[3]*1;
	prevMonth = temp[6]*1;
	nextMonth = temp[4]*1;
	
	today = temp[7];
	year = temp[8];
	month = temp[9];
	
	
	
	// EVENT CHUNK *********************************************************************
	
	var eventData = Array();
	eventData.length = 0;
	
	for(i=1; i < 32; i++) {
		calenderData[i] = null;
		calenderData[i] = Array();
	}
	
	temp = "";
	if(data.length > 1) {
		temp = data.split('|');
	}
	var test = "";
	
	for(i = 0; i < temp.length; i++) {
		var ePieces = temp[i].split("\n");
		//alert(ePieces);
		if(!eventData[ePieces[0]]) {
			eventData[ePieces[0]] = Array();
		}
		eventData[ePieces[0]].push({title:ePieces[1], text:ePieces[2], linkto:ePieces[3], type:ePieces[4]});
		calenderData[ePieces[0]].push({title:ePieces[1], text:ePieces[2], linkto:ePieces[3], type:ePieces[4]});
		//calenderData
	}
	
	
	
	// TIME TO PRINT  *********************************************************************
	delay = 0;
	curDate = 1;
	
	firstCell = -1;
	
	for(i = 0; i< 42; i++) {
		
		var isCurDate = ((delay >= firstDay) && (today == (year+"-"+month+"-"+curDate)));
		var isEvent = eventData[i-firstDay+1];
		
		if(isCurDate && isEvent)
			setDivStyle('cDateCell_'+i, "cEventCurDateCell");
		else if(isEvent) {
			setDivStyle('cDateCell_'+i, "cEventCell");
		} else if(isCurDate)
			setDivStyle('cDateCell_'+i, "cCurDateCell");
		else
			setDivStyle('cDateCell_'+i, "cDateCell");
		
		var txt = "";
		
		if(i % 7 == 6) txt += '<span class="cRed">';	//påbörja röd dag
		
		if(delay < firstDay) {			//kolla om vi fortfarande är på tomma celler
			txt += '&nbsp;';
			//document.getElementById('cDateCell_'+i).onmouseover = new Function() { checkTooltip(0); }
			delay++;
		} else {
			if(curDate <= maxDate) {	//om inte kolla så vi inte skriver ut felr datum än vad som ska
				if(firstCell < 0) firstCell = i;
				txt += curDate;
				curDate++;
			} else {
				txt += '&nbsp;';
				//document.getElementById('cDateCell_'+i).onmouseover = new Function() { checkTooltip(0); }
			}
		}
		
		if(i % 7 == 6) txt += '</span>';	//avsluta röd dag
		
		alterDivContent('cDateCell_'+i, txt);
	}
	
	
	//alert(temp.length);
	//alert(eventData[8][1].title);
	//alterDivContent(div, content);
	
	showDiv('prevArrow');
	showDiv('nextArrow');
	showDiv('cCalenderCell');
	hideDiv('cLoadCell');
}





function cLoadCalender(y, m) {
	alterDivContent('curMonthCell', 'Laddar...');
	hideDiv('prevArrow');
	hideDiv('nextArrow');
	hideDiv('cCalenderCell');
	showDiv('cLoadCell');
	hideTooltip();
	
	if(!y || !m)
		xmlhttp.open("GET", ("getCalenderData.php"),true);
	else
		xmlhttp.open("GET", ("getCalenderData.php?y="+y+"&m="+m),true);
		
	xmlhttp.onreadystatechange=function() {
		if (xmlhttp.readyState==4 || xmlhttp.readyState == "complete") {
			cUpdateCalender(xmlhttp.responseText);
		}
	}
	xmlhttp.send(null);
}



var calTypes = Array("KFUM", "Idrott", "Utbildning", "Läger & Lägerskola", "Motorgården");
function checkTooltip(id) {
	//alert(mX + ", " + mY);
	if(calenderData[id-firstCell+1] && calenderData[id-firstCell+1].length > 0) {
		var txt = "";
		for(i=0; i<calenderData[id-firstCell+1].length; i++) {
			if(i > 0) {
				//txt += '<br><br>';
				txt += '<hr>';
			}
			
			if(calenderData[id-firstCell+1][i].title.length < 1) {
				txt += calenderData[id-firstCell+1][i].text;
			} else {
				txt += '<span class="cTooltipTitle">';
				txt += calenderData[id-firstCell+1][i].title;
				txt += '</span><br>';
				txt += '<span class="cTooltipType">';
				txt += calTypes[calenderData[id-firstCell+1][i].type*1];
				txt += '</span><br>';
				txt += calenderData[id-firstCell+1][i].text;
			}
		}
		alterDivContent('tooltipDiv', txt);
		showDiv("tooltipDiv");
	}
	//alert(id-firstCell+1);
}


function hideTooltip() {
	hideDiv("tooltipDiv");
}


function tt_Move(t_ev) {
	if (IE) { 
		mX = event.clientX + document.body.scrollLeft
		mY = event.clientY + document.body.scrollTop
	} else {  // grab the x-y pos.s if browser is NS
		mX = t_ev.pageX
		mY = t_ev.pageY
	}  
	// catch possible negative values in NS4
	if (mX < 0){mX = 0}
	if (mY < 0){mY = 0} 
	
	var obj = document.getElementById("tooltipDiv");
	if(obj) {
obj.style.left = (mX-220)+"px";
obj.style.top = (mY-5-obj.offsetHeight)+"px";
	}
}


function initTT() {
	document.onmousemove = tt_Move;
}



function checkAndNavigate(id) {
	if(calenderData[id-firstCell+1] && calenderData[id-firstCell+1].length > 0) {
		window.location.href = calenderData[id-firstCell+1][0].linkto;
	}
}


function checkForNavigation(id2) {
	var id = id2-firstCell+1;
	var obj = document.getElementById("tooltipMenuDiv");
	var obj2 = document.getElementById("tooltipDiv");
	var txt = "";
	if(obj && obj2) {

		for(i=0; i<calenderData[id].length; i++) {
			if(i > 0) {
				txt += '<br>';
			}
			
			txt += '<a href="' + calenderData[id][i].linkto + '" onMouseOver="showDiv(\'tooltipMenuDiv\');">' + calenderData[id][i].title + '</a>';
		}
		
		alterDivContent("tooltipMenuDiv", txt);
		
		obj.style.left = (parseInt(obj2.style.left, 10) + 20) + "px";
		obj.style.top = (parseInt(obj2.style.top, 10) + obj2.offsetHeight) + "px";
	}
	showDiv("tooltipMenuDiv");
}

