var todaysDate;
var size = 10;
    
function init() {
    var content = document.getElementById( "content" );
    var leftNav = document.getElementById( "leftNav" );
    var rightNav = document.getElementById( "rightNav" );
    
    if( content.clientHeight > 390 ) {
        rightNav.style.height = content.clientHeight + "px";
    }
}

function adjustFontSize( key , ele ) {
    var children = ele.childNodes;
    var size = null;
    
    if( !children || children == null || children.length <= 0 )
        return;
    
    for( var i = 0; i < children.length; i++ ) {
        try {
            if( children[i].nodeType == 1 ) {
                
                //console.log( "Element Node:: nodeName[" + children[i].nodeName + "] iteration[" + i + "]" );

                size = children[i].style.fontSize;
                
                if( !size )
                    size = window.getComputedStyle( children[i] , null ).fontSize;
                    
                //console.info( "fontSize[" + size + "]" );
                
                if( size )
                    size = parseInt( size );
                    
                //console.info( "parseInt fontSize[" + size + "]" );

                var grandChildren = children[i].childNodes;
                if( grandChildren && grandChildren.length > 0 )
                    adjustFontSize( key , children[i] );
                    
                if( !size || size <= 0 ) continue;
                    
                if( key == "up" ) {
                    children[i].style.fontSize = ++size + "px";
                    //console.debug( "new fontSize[" + children[i].style.fontSize + "]" );
                } else if( key == "down" ) {
                    children[i].style.fontSize = --size + "px";
                    //console.debug( "new fontSize[" + children[i].style.fontSize + "]" );
                }
            }
        } catch( ex ) {
            //console.error( ex );
        }
    }
    init();
}
    
function buildCalendar( obj ) {
    var days = new Array( "S" , "M" , "T" , "W" , "T" , "F" , "S" );
    var month = new Array( "JANUARY" , "FEBRUARY" , "MARCH" , "APRIL" , "MAY" , "JUNE" , "JULY" , "AUGUST" , "SEPTEMBER" , "OCTOBER" , "NOVEMBER" , "DECEMBER" );
    
    var modDate;
    if( obj == null ) {
        todaysDate = document.getElementById( "todaysDate" ).firstChild.nodeValue.split( ":" );
        modDate = new Date();
        modDate.setFullYear( todaysDate[2] , todaysDate[0] - 1 , 1 );
    } else {
        modDate = obj;
    }

    var numDays = howManyDays( modDate.getMonth() , modDate.getYear() );

    /*********************************************************************
        * Initialize the variables
        ********************************************************************/
    var calTable = document.getElementById( "calendar" );
    
    var table = document.createElement( "table" );
    var tableBody = document.createElement( "tbody" );
    
    table.style.width = "350px";
    table.style.border = "0";
    table.cellSpacing = "1";
    table.cellPadding = "2";
    table.style.textAlign = "center";

    var tableRow = document.createElement( "tr" );
    var tableCell = document.createElement( "td" );
    var spanCell = document.createElement( "div" );

    /*********************************************************************
        * Set the attributes of the table cell
        ********************************************************************/        
    tableCell = setHeaderCellAttributes( tableCell );
    
    spanCell.onclick = rollMonth;
    spanCell.className = "monthRoll";
    spanCell.appendChild( document.createTextNode( "<") );
    tableCell.appendChild( spanCell );
    tableRow.appendChild( tableCell );
    
    /*********************************************************************
        * Reset the nodes needed to build the next portion of the table
        ********************************************************************/
    spanCell = document.createElement( "div" );
    tableCell = document.createElement( "td" );
    
    /*********************************************************************
        * Set the attributes of the table cell
        ********************************************************************/
    tableCell.style.textAlign = "center";
    tableCell.style.verticalAlign = "middle";
    tableCell.style.backgroundColor = "#235198";
    tableCell.colSpan = "5";
    
    spanCell.style.color = "#FFFFFF";
    spanCell.appendChild( document.createTextNode( month[modDate.getMonth()] + " - " + modDate.getFullYear() ) );
    tableCell.appendChild( spanCell );
    tableRow.appendChild( tableCell );

    /*********************************************************************
        * Reset the nodes needed to build the next portion of the table
        ********************************************************************/
    spanCell = document.createElement( "div" );
    tableCell = document.createElement( "td" );

    tableCell = setHeaderCellAttributes( tableCell );

    spanCell.onclick = rollMonth;
    spanCell.className = "monthRoll";
    spanCell.appendChild( document.createTextNode( ">" ) );
    tableCell.appendChild( spanCell );
    tableRow.appendChild( tableCell );
    
    tableBody.appendChild( tableRow );

    tableRow = document.createElement( "tr" );

    for( var i = 0; i < 7; i++ ) {
        spanCell = document.createElement( "div" );
        tableCell = document.createElement( "td" );
        
        tableCell = setHeaderCellAttributes( tableCell );

        spanCell.appendChild( document.createTextNode( days[i] ) );
        tableCell.appendChild( spanCell );
        tableRow.appendChild( tableCell );
    }

    tableBody.appendChild( tableRow );

    for( var i = 1; i < 43; i++ ) {
        if( ( i - 1 ) % 7 == 0 ) {
            tableRow = document.createElement( "tr" );
        }

        spanCell = document.createElement( "div" );
        tableCell = document.createElement( "td" );
        tableCell = setDayCellAttributes( tableCell );

        if( ( i - 1 ) >= modDate.getDay() && ( i - modDate.getDay() ) <= numDays ) {
            var month = ( modDate.getMonth() + 1 ) < 10 ? "0" + ( modDate.getMonth() + 1 ) : ( modDate.getMonth() + 1 );
            var year = modDate.getFullYear();
            var day = ( i - modDate.getDay() ) < 10 ? "0" + ( i - modDate.getDay() ) : ( i - modDate.getDay() );
            var anchor = document.getElementById( month + "/" + day + "/" + year );

            if( anchor == null ) {
                spanCell.appendChild( document.createTextNode( ( i - modDate.getDay() ) ) );
            } else {
                var link = document.createElement( "a" );
                link.href = "index.php?page=events_calendar#" + month + "/" + day + "/" + year;
                link.appendChild( document.createTextNode( i - modDate.getDay() ) );
                spanCell.appendChild( link );
            }
        } else {
            spanCell.appendChild( document.createTextNode( " " ) );
        }
        tableCell.appendChild( spanCell );
        tableRow.appendChild( tableCell );

        if( ( i - 1 ) % 7 == 0 ) {
            tableBody.appendChild( tableRow );
        }
    }
    table.appendChild( tableBody );
    calTable.appendChild( table );
    hideEvents();
    showEvents();
}

function setHeaderCellAttributes( obj ) {
    obj.style.textAlign = "center";
    obj.style.verticalAlign = "middle";
    obj.style.backgroundColor = "#C8D3E5";
    return obj;
}

function setDayCellAttributes( obj ) {
    obj.style.textAlign = "center";
    obj.style.verticalAlign = "middle";
    obj.style.backgroundColor = "#EBF1FA";
    return obj;

}

function rollMonth( e ) {
    if (!e) var obj = window.event.srcElement;
    else var obj = e.target;

    var modDate = new Date()
    if( obj.firstChild.nodeValue == '<' ) {
        if( ( parseInt( todaysDate[0] , 10 ) - 1 ) == 0 ) {
            modDate.setFullYear( parseInt( todaysDate[2] , 10 ) - 1 , 11 , 1 );
            todaysDate[2] = modDate.getFullYear();
            todaysDate[0] = parseInt( modDate.getMonth() , 10 ) + 1;
        } else {
            modDate.setFullYear( todaysDate[2] , ( ( parseInt( todaysDate[0] , 10 ) - 1 ) - 1 ) , 1 );
            todaysDate[0] = parseInt( modDate.getMonth() , 10 ) + 1;
        }
    } else {
        if( ( parseInt( todaysDate[0] , 10 ) - 1 ) == 11 ) {
            modDate.setFullYear( parseInt( todaysDate[2] , 10 ) + 1 , 0 , 1 );
            todaysDate[2] = modDate.getFullYear();
            todaysDate[0] = parseInt( modDate.getMonth() , 10 ) + 1;
        } else {
            modDate.setFullYear( todaysDate[2] , todaysDate[0] , 1 );
            todaysDate[0] = parseInt( modDate.getMonth() , 10 ) + 1;
        }
    }

    removeChildren( document.getElementById( "calendar" ) );
    buildCalendar( modDate );
}

function removeChildren( obj ) {
    obj.textContent = "";
    obj.innerHTML = "";
}

function howManyDays( month , year ) {
    if( month == 1 ) {
        if( year % 4 == 0 && ( year % 100 != 00 || year % 400 == 0 ) ) {
            return 29;
        } else {
            return 28;
        }
    } else {
        if( month == 0 || month == 2 || month == 4 || month == 6 || month == 7 || month == 9 || month == 11 ) {
            return 31;
        } else { 
            return 30;
        }
    }
}

function showEvents() {
	var newsEvent = null;
	for( var i = 1; ; i++ ) {
		try {
			newsEvent = document.getElementById( "News-" + i );
			var eventDate = newsEvent.getElementsByTagName( "input" )[0].value.split( "/" );

			if( eventDate[0] == todaysDate[0] && eventDate[2] == todaysDate[2] )
				newsEvent.className = "NewsSummary";
		} catch ( ex ) {
			break;
		}
	}
}
    
function hideEvents() {
	for( var i = 1; ; i++ ) {
		try {
			var newsEvent = document.getElementById( "News-" + i );
			newsEvent.className = "NewsSummary hide";
		} catch ( ex ) {
			break;
		}
	}
}