// VARS AND INITS
var now = new Date();
var theMonth;
var theDate;
var theDay;
var theYear=now.getFullYear()-2000; // will have to be updated in 2099!
var theMonthNameArray = new Array();
var theMonthDaysArray = new Array();
var theDayNames = new Array();



// TEXT FOR GLOBALIZATION ///////////////////////////////////////////////////////////////////////
// do not edit above this line //////////////////////////////////////////////////////////////////

//MONTH NAMES: 0 is January, 11 is December

theMonthNameArray[0]='January';
theMonthNameArray[1]='February';
theMonthNameArray[2]='March';
theMonthNameArray[3]='April';
theMonthNameArray[4]='May';
theMonthNameArray[5]='June';
theMonthNameArray[6]='July';
theMonthNameArray[7]='August';
theMonthNameArray[8]='September';
theMonthNameArray[9]='October';
theMonthNameArray[10]='November';
theMonthNameArray[11]='December';

//DAY NAMES: 0 is Sunday, 6 is Saturday, surround the name with quotes

theDayNames[0]='Sunday';
theDayNames[1]='Monday';
theDayNames[2]='Tuesday';
theDayNames[3]='Wednesday';
theDayNames[4]='Thursday';
theDayNames[5]='Friday';
theDayNames[6]='Saturday';

// range of available years (8=2008, 9=2009, etc.)
minYear=8;
maxYear=9;

// do not edit below this line //////////////////////////////////////////////////////////////////



// FUNCS -- UTILITY

function addPrecedingZero(theValue) { theValue=(theValue<10) ? theValue=String('0'+theValue) : theValue; return theValue; }

// FUNCS -- MAIN

function changeYear(whichYear) {
    whichYear+=2000; // for modulo calc, will need to be updated in the year 2099!
    theMonthDaysArray=[31,28,31,30,31,30,31,31,30,31,30,31];
    theMonthDaysArray[1]=(whichYear%4>0) ? 28 : 29;
}

function getDateCode() {
    imgDate=theDate;
    imgMonth=theMonth;
    imgYear=theYear;
    if (now.getDay()==0) { //SUNDAY
        imgDate-=1;
    }
    if (imgDate==0) { //IF BACKED INTO PREVIOUS MONTH
            //NEED TO PICK NEW DATE BASED ON PREVIOUS MONTH
            imgMonth-=1;
            if (imgMonth<0) { //IF BACKED INTO PREVIOUS YEAR
                if (imgYear>minYear) { 
                    imgYear--;
                } else {
                    imgYear=minYear;
                }
                imgMonth=11;
            } else if (imgMonth<0) { //IF MOVE INTO NEXT YEAR
                if (imgYear<maxYear) { 
                    imgYear++;
                } else {
                    imgYear=maxYear;
                }
                imgMonth=0;
            }
            imgDate=theMonthDaysArray[imgMonth];
    }
    
    //generate 6-digit date code used for filename and link data
    theDateCode=String(addPrecedingZero(imgMonth+1))+String(addPrecedingZero(imgDate))+String(addPrecedingZero(imgYear));
    return theDateCode;
}

function generateImageTag() {
    //generate image tag
    imageTag='';
    if ((now.getDay()==0) || (now.getDay()==6)) { //IF SAT OR SUN MAKE LINK ACTIVE
            imageTag+='<a href="mailto:InformationCalendar@emc.com">';
    }
    imageTag+='<img src="content/20'+addPrecedingZero(theYear)+'/'+getDateCode()+'.jpg" width="578" height="468" border="0" alt="Loading image for '+Number(now.getMonth()+1)+'/'+now.getDate()+'/'+now.getFullYear()+'..."/>';
    if ((now.getDay()==0) || (now.getDay()==6)) { //IF SAT OR SUN MAKE LINK ACTIVE
            imageTag+='</a>';
    }
    return imageTag;
}

function updateCalendarNav() {
    //DO SHOW-HIDE OF 'NEXT' BUTTONS
    rightNow = new Date();
    $('prev_month_button').disableMe();
    $('prev_day_button').disableMe();
    $('next_day_button').disableMe();
    $('next_month_button').disableMe();
    if (rightNow.getMonth()>theMonth) {
        $('next_month_button').enableMe();
        $('next_day_button').enableMe();
    } else if (rightNow.getDate()>theDate) {
        $('next_day_button').enableMe();
    }
    if (now.getMonth()>0) {
        $('prev_month_button').enableMe();
        $('prev_day_button').enableMe();
    } else if (now.getDate()>1) {
        $('prev_day_button').enableMe();
    }
    if (theYear>minYear) {
        $('prev_month_button').enableMe();
        $('prev_day_button').enableMe();
    }
    if (theYear<maxYear) {
        $('next_month_button').enableMe();
        $('next_day_button').enableMe();
    }
}

function updateCalendar() {
    $('main_cal_image').update(generateImageTag());
    window.location.hash=getDateCode();
    updateCalendarNav();
}
function calendarInitialize() {
    changeYear(theYear);
    now.setYear(theYear);
    now.setFullYear(2000+theYear);
    urlStr=String(window.location.hash);
    fail=true;
    if (urlStr!='') {
        monthPart=Number(urlStr.slice(1,3))-1;
        datePart=Number(urlStr.slice(3,5));
        yearPart=Number(urlStr.slice(5));
        if ((monthPart<now.getMonth()) && (monthPart>=0)) {
            if ((datePart<=theMonthNameArray[monthPart][1]) && (datePart>=1)) {
                theMonth=monthPart;
                theDate=datePart;
                theYear=yearPart;
                fail=false;
            } else {
                fail=true;
            }
        } else if ((monthPart==now.getMonth()) && (monthPart>=0)) {
            if ((datePart<=now.getDate()) && (datePart>=1)) {
                theMonth=monthPart;
                theDate=datePart;
                theYear=yearPart;
                fail=false;
            } else {
                fail=true;
            }
        } else {
            theMonth=now.getMonth();
            theDate=now.getDate();
            theYear=now.getFullYear()-2000;
        }
    }
    if (fail) {
        theMonth=now.getMonth();
        theDate=now.getDate();
        theYear=now.getFullYear()-2000;
    }
    updateNow();
    updateCalendar();
}
Event.observe(window, 'load', calendarInitialize);


//FUNCS

function updateNow() {
    now.setDate(theDate);
    now.setMonth(theMonth);
    now.setYear(theYear+2000);
}
function changeDate(monthChange,dateChange) {
    if (monthChange!=0) {
        if (((now.getDay()==6) || (now.getDay()==0)) && (theDate==theMonthDaysArray[theMonth]) && (monthChange>0)) { // DO 2X IF END OF MONTH IS ON SAT
            theMonth+=monthChange;
        }
        if (((now.getDay()==6) || (now.getDay()==0)) && (theDate==theMonthDaysArray[theMonth]) && (monthChange<0)) {
            theMonth+=-1*monthChange; // DO ZERO TIMES IF END OF MONTH IS SUNDAY GOING BACKWARDS
        }
        theMonth+=monthChange;
        if (theMonth>11) {
            if (theYear<maxYear) {
                theMonth=0;
                theDate=1;
                theYear++;
            } else {
                theMonth=11;
                theDate=31;
                theYear=maxYear;
            }
        } else if (theMonth<0) {
            if (theYear>minYear) {
                theMonth=11;
                theDate=31;
                theYear--;
            } else {
                theMonth=0;
                theDate=1;
                theYear=minYear;
            }
        } else {
            theDate=1;
        }
    }
    if (dateChange!=0) {
        theDate+=dateChange;
        if (theDate>theMonthDaysArray[theMonth]) {
            theMonth+=1;
            if (theMonth>11) {
                if (theYear<maxYear) {
                    theMonth=0;
                    theDate=1;
                    theYear++;
                } else {
                    theMonth=11;
                    theDate=31;
                    theYear=maxYear;
                }
            } else {
                theDate=1;
            }
        }
        if (theDate<1) {
            theMonth-=1;
            if (theMonth<0) {
                if (theYear>minYear) {
                    theMonth=11;
                    theDate=31;
                    theYear--;
                } else {
                    theMonth=0;
                    theDate=1;
                    theYear=minYear;
                }
            } else {
                theDate=theMonthDaysArray[theMonth];
            }
        }
        if (((now.getDay()==6) || (now.getDay()==0)) && (dateChange>0)) {
            theDate+=dateChange; // DO TWICE IF SATURDAY TO SKIP OVER SUNDAY, BUT ONLY IF GOING FORWARD
            if (theDate>theMonthDaysArray[theMonth]) {
                theMonth+=1;
                if (theMonth>11) {
                    theMonth=11;
                    theDate=31;
                } else {
                    theDate=1;
                }
            }
        }
        if ((now.getDay()==1) && (dateChange<0)) {
            theDate+=dateChange; // DO TWICE IF MONDAY TO SKIP OVER SUNDAY, BUT ONLY IF GOING BACKWARD
            if (theDate<1) {
                theMonth-=1;
                if (theMonth<0) {
                    if (theYear>minYear) {
                        theMonth=11;
                        theDate=31;
                        theYear--;
                    } else {
                        theMonth=0;
                        theDate=1;
                        theYear=minYear;
                    }
                } else {
                    theDate=theMonthDaysArray[theMonth];
                }
            }
        }
    }
    updateNow();
    updateCalendar();
}

function noSelect(e) {
    if (typeof e.onselectstart!="undefined") {
        e.onselectstart=function() {
            return false;
        }
    } else if (typeof e.style.MozUserSelect!="undefined") {
        e.style.MozUserSelect="none";
    } else {
        e.onmousedown=function() {
            return false;
        }
    }
}

var navButtonsList=new Object();
    navButtonsList['prev_month_button']=true;
    navButtonsList['next_month_button']=true;
    navButtonsList['prev_day_button']=true;
    navButtonsList['next_day_button']=true;

Behaviour.register({
    '#prev_month_button': function (e) {
        e.onclick=function() {
            if (navButtonsList[e.id]) {
                changeDate(-1,0);
            }
        }
    },
    '#prev_day_button': function (e) {
        e.onclick=function() {
            if (navButtonsList[e.id]) {
                changeDate(0,-1);
            }
        }
    },
    '#next_month_button': function (e) {
        e.onclick=function() {
            if (navButtonsList[e.id]) {
                changeDate(1,0);
            }
       }
    },
    '#next_day_button': function (e) {
        e.onclick=function() {
            if (navButtonsList[e.id]) {
                changeDate(0,1);
            }
        }
    },
    '.navButton' : function (e) {
        e.onmouseover=function() {
            if (navButtonsList[e.id]) {
                e.style.backgroundImage='url(images/'+e.id+'_y.gif)';
            }
        }
        e.onmouseout=function() {
            e.style.backgroundImage='url(images/'+e.id+'_w.gif)';
        }
        e.disableMe=function() {
            e.setStyle({ opacity: 0.40 });
            navButtonsList[e.id]=false;
            noSelect(e);
        }
        e.enableMe=function() {
            e.setStyle({ opacity: 1.0 });
            navButtonsList[e.id]=true;
        }
    }
});
