var readmonths = new Array();
var showmonths = new Array();
var readmonths_c = new Array();
var showmonths_c = new Array();
var caldays = new Array();
var qid = 0;
var is_working = false;
var has_timer = false;
var startreadyears  = new Array();
var startreadmonths = new Array();
var _currentyear = 0;
var _currentmonth = 0;
var selectedday = '';
var read_counter = 0;
var active = 0;
var processing = 0;
var timer = 0;

function include(script_filename) {
    // Function from: http://www.phpied.com/javascript-include/
    var html_doc = document.getElementsByTagName('head').item(0);
    var js = document.createElement('script');
    js.setAttribute('language', 'javascript');
    js.setAttribute('type', 'text/javascript');
    js.setAttribute('src', script_filename);
    html_doc.appendChild(js);
    return false;
}

function readmonth(year, month) {
    startreadyears[startreadyears.length] = year;
    startreadmonths[startreadmonths.length] = month;
    /*if (!readmonths['' + year + '-' + month]) {
        include('?act=ajax_days&year=' + year + '&month=' + month);
    }*/
}

function do_readmonth(sryears, srmonths, srways) {
	if (processing>0) return;
	act_needed = 0;
	act = '?act=ajax_days&currentyear=' + _currentyear + '&currentmonth=' + _currentmonth + '&qid=' + qid;
	for (i=0; i<sryears.length; i++)
	{
        if (!showmonths['' + sryears[i] + '-' + srmonths[i]]) {
			act_needed++;
			processing++;
		    act += '&year[]=' + sryears[i] + '&month[]=' + srmonths[i] + '&acts[]=month';
			showmonths_c['' + sryears[i] + '-' + srmonths[i]] = true;
		} else if (_currentyear==sryears[i]&&_currentmonth==srmonths[i]) {
            document.getElementById('calendar').innerHTML = showmonths['' + sryears[i] + '-' + srmonths[i]];
		}
        if (!readmonths['' + sryears[i] + '-' + srmonths[i]]) {
			act_needed++;
			processing++;
		    act += '&year[]=' + sryears[i] + '&month[]=' + srmonths[i] + '&acts[]=day';
			readmonths_c['' + sryears[i] + '-' + srmonths[i]] = true;
		}
    }
	//window.alert(act);
	if (act_needed>0)
	{
		//document.getElementById('calload').innerHTML = 'Loading ' + qid + ': ' + act;
		include(act);
		qid++;
		return false;
	} else {
		return true;
	}
}

function do_timer() {
	timer--;
	do_readmonths();
}

function start_timer(time) {
	if (!time) time=1000;
	read_counter++;
	if (timer>0) return;
	timer++;
	setTimeout("do_timer", time);
}

function check_month(year, month) {
	while (month>11) { year++; month-=12; }
	while (month<0)  { year--; month+=12; }
	sryears  = new Array();
	sryears[0] = year;
	srmonths = new Array();
	srmonths[0] = month;
	ret = do_readmonth(sryears, srmonths);
	if (ret) start_timer();
	return ret;
}

function do_readmonths() {
	active++;
	//document.getElementById('calload').innerHTML = '';
	if (!check_month(_currentyear,   _currentmonth)); else
	if (!check_month(_currentyear,   _currentmonth+1)); else
	if (!check_month(_currentyear,   _currentmonth-1)); else
	if (!check_month(_currentyear,   _currentmonth+2)); else
	if (!check_month(_currentyear+1, _currentmonth)); else
	if (!check_month(_currentyear-1, _currentmonth)); else
	if (!check_month(_currentyear,   _currentmonth+3)); else
	if (!check_month(_currentyear+2, _currentmonth)); else
	if (!check_month(_currentyear-2, _currentmonth));
	active--;
}

function readmonth_request(currentyear, currentmonth) {
	read_counter = 0;
	_currentyear = currentyear;
	_currentmonth = currentmonth;
    do_readmonths(true);
	/*if (startreadyears.length == 0)
	{
		return;
	}
	if (is_working&&!has_timer&&read_counter<10)
	{
		if (read_counter < 10) {
			setTimeout("readmonth_request(" + currentyear + ", " + currentmonth + ")", 100);
			read_counter++;
		} else {
			window.alert("Timer overflow!");
		}
		has_timer = true;
	}
	has_timer = false;
	is_working = true;
	if (!(showmonths_c['' + currentyear + '-' + currentmonth]&&readmonths_c['' + currentyear + '-' + currentmonth]))
	{
		sryears  = new Array();
		sryears[0] = currentyear;
        srmonths = new Array(currentmonth);
		srmonths[0] = currentmonth;
		backedup = true;
		if (read_counter < 10) {
			setTimeout("readmonth_request(" + currentyear + ", " + currentmonth + ")", 100);
			read_counter++;
		} else {
			window.alert("Timer overflow!");
		}
	} else {
		sryears  = startreadyears;
        srmonths = startreadmonths;
		backedup = false;
	}
	_currentyear = currentyear;
	_currentmonth = currentmonth;

    do_readmonth(sryears, srmonths);

	if (!backedup)
	{
		startreadyears  = new Array();
		startreadmonths = new Array();
	}
	is_working = false;*/
}

function showmonth(year, month, show) {
	if (!show) return;
    if (showmonths['' + year + '-' + month]) {
        document.getElementById('calendar').innerHTML = showmonths['' + year + '-' + month];
    }
	_currentyear = year;
	_currentmonth = month;
	do_readmonths();
	/*if (show)
	{
	}*/
    /*if (!showmonths['' + year + '-' + month]) {
		if (show) qid++;
        PassAjaxResponseToFunction("?act=ajax_cal&year="+year+"&month="+month+"&qid="+qid, "showmonthr", year + ", " + month + ", " + (show ? 'true' : 'false') + ", " + qid);
        //SetInnerHTMLFromAjaxResponse("?act=ajax_cal&year="+year+"&month="+month, "calendar");
    } else if (show) {
        //document.getElementById('calendar').innerHTML = showmonths['' + year + '-' + month];
    }*/
}

function showday(year, month, day) {
    if (caldays['' + year + '-' + month + '-' + day]) {
        document.getElementById('calendar_info').innerHTML = caldays['' + year + '-' + month + '-' + day];
    } else {
		selectedday = '' + year + '-' + month + '-' + day;
        document.getElementById('calendar_info').innerHTML = caldays['common' + '-' + '' + '-' + 'loading'];
    }
}

function showmonthr(monthdata, year, month, show, iqid) {
    showmonths['' + year + '-' + month] = monthdata;
    if (_currentyear==year&&_currentmonth==month) {
        document.getElementById('calendar').innerHTML = monthdata;
    }
	processing--;
	read_counter--;
	if (processing==0&&!active) start_timer(1);
}

function readmonth_done(year, month) {
    readmonths['' + year + '-' + month] = true;
	processing--;
	read_counter--;
	if (processing==0&&!active) start_timer(1);
}

function set_up_calday(year, month, day, content) {
    caldays['' + year + '-' + month + '-' + day] = content;
	if (selectedday=='' + year + '-' + month + '-' + day)
	{
		showday(year, month, day);
	}
}