//Deconstructed Calendar

//indexOf for Arrays
[].indexOf || (Array.prototype.indexOf = function(v,n){
  n = (n==null)?0:n; var m = this.length;
  for(var i = n; i < m; i++)
    if(this[i] == v)
       return i;
  return -1;
});

//global variables
var unitVal = '';
var currentDate = '';
var currentYear = '';
var currentMonth = '';
var mode = 0;

//Setup a hierarchy for clearing divs
var monthChildren = ['dayNav','postNav'];
var dayChildren = ['postNav'];

//Pattern for RegEx matching
var pattern = new RegExp('navA');

//Build Month and Day Arrays
var month = ['January','February','March','April','May','June','July','August','September','October','November','December'];
var day = new Array();
for (i=0;i<31;i++){
	day[i] = (i+1).toString();
}

//retrieve the years of the posts and construct the cal
function initCalendar(){
    params = '?unit=year';
	unitVal = 'year';
	mode = 0;
    xmlPrepare('lib/archive.php', params, construct, 'GET', 'constructyear');
}

//u = unit (year,month,day,post)
//m = mode 0 for new, 1 for rebuilding
//n = increment
function construct(u,m) {
	u = u || unitVal;
	m = m || mode;
	var dataSet = DataString['construct' + u].split('#!#');
	var navDiv = document.getElementById(u + 'Nav');
	if (m == 0){ 
		switch(u){
			case 'month':
				values = month;
				break;
			case 'day':
				values = day.slice();
				calcDays(values);
				break;
			default:
				values = dataSet; 
		}
		n = u == 'post' ? 2 : 1;
		for (i=0; i<values.length;i=i+n){
			var lnk = n > 1 ? 'http://eat8bit.com/comments.php?id=' + values[i+n-1] : '#';
			buildNav(values[i],navDiv,lnk);
		}
	}
	else {
		for(i=0;i<navDiv.childNodes.length;i++){
			if(pattern.test(navDiv.childNodes[i].className) == true){
				getMatch(navDiv.childNodes[i],u);
			}
		}
	}
	if(u != 'post'){
		setEventHandlers();
	}
}
//v = value of year, month, day, or post
//d = div where the link is appended to
function buildNav(v,d,l){
	var a = document.createElement('a');
	a.href = l;
	a.title = v;
	a.innerHTML = v;
	d.appendChild(a);
	getMatch(a,unitVal);
}

//a = array of days
function calcDays(a){
	if (['April','June','September','November'].indexOf(currentMonth) != -1){
		a.length = 30;
	}
	else if (currentMonth == 'February'){
		a.length = ((currentYear % 4 == 0 && currentYear % 100 != 0) || currentYear % 400 == 0) ? 29 : 28;
	}
	else {
		a.length = 31;
	}
}

//set EventHandlers only for active links
function setEventHandlers(){
	var a = document.getElementsByTagName('a');
	for (i=0; i<a.length; i++){
		if (pattern.test(a[i].className) == true){
			//unregister all event handlers
			a[i].onclick = null;
			//register new event handlers
			if(a[i].className != 'navA inactive'){
				a[i].onclick = activate;
				if (a[i].captureEvents) a[i].captureEvents(Event.CLICK);
			}
		}
	}
}

//Add active style to clicked element
//deactivate any other elements within the same category
//build next layer of interactivity
//e = event
function activate(e){
	if (!e) var e = window.event;
	var id = this.parentNode.id;
	var u = id.split('Nav')[0];
	var cn = this.parentNode.childNodes;
	for (i=0; i<cn.length; i++){
		if (pattern.test(cn[i].className) == true){
			if (cn[i].className != 'navA match'){
				getMatch(cn[i],u);
			}
		}
	}
	var cu = '';
	switch(u){
		case 'year':
			cu = 'month';
			currentYear = this.innerHTML;
			currentDate = currentYear;
			break;
		case 'month':
			cu = 'day';
			currentMonth = this.innerHTML;
			currentDate = currentYear + '-' + currentMonth;
			break;
		case 'day':
			cu = 'post';
			currentDate = currentYear + '-' + currentMonth + '-' + this.innerHTML;
			break;
		default:
	}
	getDates(cu,currentDate);
	this.className = 'navA active';
	
	e.cancelBubble = true;
	if (e.stopPropagation) e.stopPropagation();
}

//Build next layer if it doesn't exist else restyle it
//Clear post div if picking new year or month, day div if new year
function getDates(u,v){
	var navDiv = document.getElementById(u.toLowerCase() + 'Nav');
	var params = '?unit=' + u + '&value=' + v;
	unitVal = u;
	if (navDiv.hasChildNodes()){
		mode = 1;
		var ary = window[u+'Children'];
		for(d in ary){
			o = document.getElementById(ary[d]);
			while(o.hasChildNodes()){
				o.removeChild(o.firstChild);
			}
		}
		if (u == 'post'){
			mode = 0;
			while(navDiv.hasChildNodes()){
				navDiv.removeChild(navDiv.firstChild);
			}
		}
		else if (u == 'day'){
			d = day.slice();
			cd = navDiv.childNodes.length;
			calcDays(d);
			if (cd > d.length) {
				for(i=d.length;i<cd;i++){
					navDiv.removeChild(navDiv.lastChild);
				}
			}
			else {
				for(i=cd;i<d.length;i++){
					buildNav(d[i],navDiv,'#');
				}
			}
		}
	}
	else {
		mode = 0;	
	}
	xmlPrepare('lib/archive.php', params, construct, 'GET', 'construct' + u);
}
function getMatch(o,u){
	var val = o.innerHTML;
	var matchData = DataString['construct' + u].split('#!#');
	var q = matchData.indexOf(val);
	o.className = q != -1 ? 'navA match' : 'navA inactive';
}