var ccbnProcessingMonthClick = false;
/* change the month that is displayed on the page */
function ccbnCalendar_ChangeMonth(moduleName, monthToShow, monthToHide, monthToCache){
    //bail out of this if the months are not uploaded yet
    if(!ccbnAllMonthsLoaded) return;    
    //working on a click at the moment, bail out of any subsequent clicks
    if(ccbnProcessingMonthClick) return;
    
    //we are in the middle of processing this click, set this value to true
    ccbnProcessingMonthClick = true;
	//stop the onresize from triggering
	ccbnResizeDisabled=true;
	
	try
	{    
	    var thisAjaxObj, qS;
    	
	    //find the correct ccbnAjaxObject
	    thisAjaxObj=ccbnGetAjaxObjByModuleName(moduleName);
    	
	    //Today was clicked while it was already shown
	    if(monthToShow == monthToHide) monthToShow=monthToHide="";
    	
	    //object already exists on the page, show it		
	    if(ccbnGetObj("month"+monthToShow)){
		    if(monthToShow==monthToHide) return;
		    ccbnShowHide_Display(ccbnGetObj("month"+monthToShow),CCBN_SHOW_DISPLAY)
		    ccbnShowHide_Display(ccbnGetObj("month"+monthToHide),CCBN_HIDE_DISPLAY)
		    //move the divs to be in the correct position
		    ccbnMoveAllObjects(true);
	    }
	    else if(monthToShow){
            //if the month to show is not on the page and we are not trying to get it right now, make an ajax call to retreive it
		    ccbnCalendar_GetAMonth(ccbnModuleName, monthToShow);
    			
		    //try to show the month again after waiting 2 seconds
		    setTimeout("ccbnCalendar_ChangeMonth('"+moduleName+"','"+monthToShow+"','"+monthToHide+"','"+monthToCache+"')",2000);
	    }
    	
	    //object already exists on the page, do not need ajax call	
	    if(ccbnGetObj("month"+monthToCache)){ }
	    else if(monthToCache){
            //if the month to cache is not on the page and we are not trying to get it right now, make an ajax call to retreive it
		    ccbnCalendar_GetAMonth(ccbnModuleName, monthToCache);
	    }
	    
	    //allow onresize to trigger again
	    ccbnResizeDisabled=false;
	    //allow processing of clicks again
        ccbnProcessingMonthClick = false;	    
	}
	catch(err)  //clean up the globals in case something errors
	{
	    //allow onresize to trigger again
	    ccbnResizeDisabled=false;
	    //allow processing of clicks again
        ccbnProcessingMonthClick = false;
	}
}

function ccbnCalendar_LoadMonths(){
	var thisAjaxObj;
	var monthNum;
	var moduleName = ccbnModuleName;	
	
	//find the correct ccbnAjaxObject
	thisAjaxObj=ccbnGetAjaxObjByModuleName(moduleName);
	
	//confirm that container is an object
	thisAjaxObj.container=(typeof(thisAjaxObj.container)!='object')?ccbnGetObj(thisAjaxObj.container):thisAjaxObj.container;
	
	//figure out the current month
	var monthDivs = thisAjaxObj.container.getElementsByTagName('div');
	for(ccbn_i=0;ccbn_i<monthDivs.length;ccbn_i++){
		var monthDivID = monthDivs[ccbn_i].id;
		if(monthDivID.indexOf('month')!= -1 && monthDivs[ccbn_i].style.display != 'none')
			monthNum = parseInt(monthDivID.replace("month",""));
	}
	
	if(monthNum != null)
	{
        //go forward and back six months
        for(ccbn_l=1;ccbn_l<=6;ccbn_l++){
            //forward
            var monthToGet = ccbnCalendar_MonthFwd(monthNum,ccbn_l);
            ccbnCalendar_GetAMonth(ccbnModuleName, monthToGet);     

            //back
            monthToGet = ccbnCalendar_MonthBack(monthNum,ccbn_l);
            ccbnCalendar_GetAMonth(ccbnModuleName, monthToGet);
        }
    }
        
}

function ccbnCalendar_MonthFwd(month,offset){
    var newMonthYear = month + offset;
    var monthYearArray = ccbnCalendar_ExtractMonthYear(newMonthYear);
    var newMonth = monthYearArray[0];
    var newYear = monthYearArray[1];
    if(newMonth>12){
           newYear++;
           newMonth=newMonth-12;
    }
    if(newMonth<10) newMonth = '0'+newMonth;
    return (''+ newYear + newMonth)
}

function ccbnCalendar_MonthBack(month,offset){
    var newMonthYear = month - offset;
    var monthYearArray = ccbnCalendar_ExtractMonthYear(newMonthYear);
    var newMonth = monthYearArray[0];
    var newYear = monthYearArray[1];
    if(newMonth<1 || newMonth>12){ //handle when months are not 1 - 12
        var yearRollOverMonthYear = newMonthYear-100+12;
        var yearRollOverMonthYearArray = ccbnCalendar_ExtractMonthYear(yearRollOverMonthYear);
        newMonth = yearRollOverMonthYearArray[0];
        newYear = yearRollOverMonthYearArray[1];
    }
    if(newMonth<10) newMonth = '0'+newMonth;
    return (''+ newYear + newMonth)
}

/* parses the month and year string and returns the month and year as an array */
function ccbnCalendar_ExtractMonthYear(newMonthYear){
    var newMonth = newMonthYear.toString().substring(4,6);
        if(newMonth.charAt(0)=="0") newMonth=newMonth.charAt(1);
        newMonth = parseInt(newMonth);
    var newYear  = parseInt(newMonthYear.toString().substring(0,4));
    
    return([newMonth,newYear]);
}

/* change the month that is displayed on the page */
function ccbnCalendar_GetAMonth(moduleName, monthToCache){
   var thisAjaxObj, qS;
	
   //find the correct ccbnAjaxObject
   thisAjaxObj=ccbnGetAjaxObjByModuleName(moduleName);
	
   if(monthToCache){
		//create query string
		qS='&ym='+monthToCache; 

		//make the XML Request
		thisAjaxObj.executeXMLReq(thisAjaxObj.objectArrayID,qS,"m_"+monthToCache);		
   }
}

function ccbnCalendar_DayClicked(clickedCell, eventContainerID, hasEvents){
	//the day clicked does not have any events, therefore there is nothing that needs to be done
	if(!hasEvents) return;

	//all divs that are beneath the cell that was clicked
	var clickedDiv = clickedCell.getElementsByTagName('div');

	//copy the day HTML into the eventContainerHTML
	if(ccbnGetObj(eventContainerID)) ccbnSetInnerHtml(ccbnGetObj(eventContainerID),clickedDiv[0].innerHTML);	
}

var monthLoadCount=12;
var ccbnAllMonthsLoaded = false;
function ccbnCalendar_HandleResponse(moduleName,cachedMonth){
	var thisAjaxObj, q;
	
	//find the correct ccbnAjaxObject
	thisAjaxObj=ccbnGetAjaxObjByModuleName(moduleName);
	
	//if the month id was not passed in, try to find it
	if(!cachedMonth){
		for (var monthID in thisAjaxObj.xmlReq){
			if(thisAjaxObj.xmlReq[monthID]!=null && thisAjaxObj.xmlReq[monthID].readyState == 4 && thisAjaxObj.xmlReq[monthID].status == 200){
				cachedMonth=monthID;
				break;
			}
		}
	}	
	
	//add the HTML returned from the XML Request to the DOM
	thisAjaxObj.container.innerHTML+=thisAjaxObj.xmlReq[cachedMonth].responseText;
	
	//reset the xmlReq property
	thisAjaxObj.xmlReq[cachedMonth]=null;
	delete thisAjaxObj.xmlReq[cachedMonth];
	
    //update load count
    monthLoadCount--;
    
	//fix for IE6
	ccbnIE6TurnHoversOn();
	
	//show the calendar navigation
	if(monthLoadCount <= 0){
	    ccbnAllMonthsLoaded=true;
	    var monthTRs = thisAjaxObj.container.getElementsByTagName('tr');
	    for(ccbn_i=0;ccbn_i<monthTRs.length;ccbn_i++){
		    if(monthTRs[ccbn_i].id.indexOf('ccbnCalendarNavigation')!= -1)
			    monthTRs[ccbn_i].id+="_on";
	    }
	}
}

//fix for WebSideStory conflict
var ccbnResizeDisabled;
var ccbnMoveAllDivCount=0;
function ccbnMoveAllObjects(allowMove){
	if(ccbnResizeDisabled==true && allowMove!=true) return;
	var divs = document.getElementsByTagName('div');
	for(ccbn_i=0;ccbn_i < divs.length; ccbn_i++){
		if(divs[ccbn_i].className.indexOf("ccbnCalendarDiv") != -1 || divs[ccbn_i].className.indexOf("ccbnCalendarHelpDiv") != -1){
			var ccbn_pos = ccbnFindPos(divs[ccbn_i].parentNode);
			var ccbn_posDiv = ccbnFindPos(divs[ccbn_i]);
			if(!divs[ccbn_i].id){
			    divs[ccbn_i].id="div"+ccbnModuleName+ccbnMoveAllDivCount;
			    ccbnMoveAllDivCount++;
			}
			
			var ccbn_diffTDandDIV_X = ccbn_pos[0]-ccbn_posDiv[0];
			var ccbn_diffTDandDIV_Y = ccbn_pos[1]-ccbn_posDiv[1];

			var ccbn_moveToX = divs[ccbn_i].offsetLeft+ccbn_diffTDandDIV_X;
			var ccbn_moveToY = divs[ccbn_i].offsetTop+ccbn_diffTDandDIV_Y;

			ccbnMoveObj(divs[ccbn_i].id,ccbnEObj(),ccbn_moveToX,ccbn_moveToY,false,ccbn_pos[0]);
		}
	}
}

/* add new window events for the calendar */
ccbnAddLoadEvent(ccbnMoveAllObjects);
ccbnAddLoadEvent(ccbnIE6TurnHoversOn);
ccbnAddLoadEvent(ccbnCalendar_LoadMonths);
window.onresize = ccbnAddEvent(window.onresize, ccbnMoveAllObjects);
