/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */



//configuration variables
var numMonths = 2;
var evtPnl = new EventPanel();
var intvlMsg = new IntervalLoadingMsg("Loading Content");


$(document).ready( function() {
    evtPnl.initializeDts(numMonths);
    evtPnl.retrieveEventData();
});



function EventPanel(){
   
    this.stDt = new Date();
    this.endDt = new Date();
    this.dtFmt = new DateFormatUtils();
    this.eventBuff = new Array();
    this.startDtObj = this.dtFmt.createMilDateTimeString(
        this.stDt.getFullYear(),
        this.stDt.getMonth() + 1,
        this.stDt.getDate(),
        this.stDt.getHours(),
        this.stDt.getMinutes()
        );

    this.endDtObj = this.dtFmt.createMilDateTimeString(
        this.endDt.getFullYear(),
        this.endDt.getMonth() + 1,
        this.endDt.getDate(),
        this.endDt.getHours(),
        this.endDt.getMinutes()
        );

    this.findSetTime = function(dtTime, eventKey){
        eventKey[this.evtAtr.time] = dtTime.substring(8, 12);
    };
    this.findSetDt = function(dtTime, eventKey){
        eventKey[this.evtAtr.date] = dtTime.substring(0, 8);
    }
    this.setIndex = function(i, eventKey){
        eventKey[this.evtAtr.id] = i + 1;
    }
    /**
     * <b>retrieveEventData()</b> culls a list of events from JSON data array stored in EventData.js file. Once
     *  Data is gathered two functions are ran:<br/> <i>populateEventFormat(this.eventBuff, index)</i><br/> and
     *  <br/><i>loopIteration(this.eventBuff, index)</i>,<br/> which populate and continually iterate through events.
     */
    this.retrieveEventData = function(){        
        var i = 0;
        for (var eventDt in eventData ){
            var eventKey = eventData[eventDt];
            this.findSetTime(eventDt, eventKey)
            this.findSetDt(eventDt, eventKey);
            this.setIndex(i, eventKey);

            var dataFormat = "<tr id='eachEvent_" + eventDt + "'>"+
            "<td colspan='3' class=\"eventTitle\">" + eventKey[this.evtAtr.title] + " ~ " + eventKey[this.evtAtr.place] +"</td>"+
            "<td class=\"eventCount\">"+ eventKey[this.evtAtr.id] + " - " + eventUtils.size() + "</td>" +
            "</tr>"+
            "<tr>"+
            "<td colspan='1' class=\"eventDateTime\">" +
            this.dtFmt.standardDtFormat( eventKey[this.evtAtr.date] ) + "<br/> " +
            this.dtFmt.findAMPMTime( eventKey[this.evtAtr.time] ) +
            "</td>"+
            "<td colspan='1' class=\"eventDescription\">" + eventKey[this.evtAtr.description] + "</td>"+
            "<td rowspan='3' class=\"eventPictUrl\">" +
            "<img alt=\"" + eventKey[this.evtAtr.pictureUrl] + "\" src=\"" + eventKey[this.evtAtr.pictureUrl] + "\"/>"+
            "</td>"+
            "</tr><tr><td colspan=\"4\"><a href=\"" + eventKey[this.evtAtr.link] + "\" class=\"eventLink\">Read More?</a></td></tr>" ;
            this.eventBuff[i++] = dataFormat;
           
        }
        
        var index = 0;
        intvlMsg.loadMsg("#eventPanel", true);
        //        this.populateEventFormat(this.eventBuff, index);
        this.loopIteration(this.eventBuff, index);
        

    }
    this.loopIteration = function(eventBuff, index){   
        var intervalId2 = 0;
        var time = 8000;
        intervalId2 = setInterval(increment, time);
        function increment(){
            intvlMsg.loadMsg("#eventPanel", false);
//alert(intervalId2);
            intvlMsg.loadMsg("#eventPanel", true);
            if ( index < eventBuff.length ){
                intvlMsg.loadMsg("#eventPanel", false);
                index = evtPnl.populateEventFormat(eventBuff, index);
                
            } else {
                index = 0;
                index = evtPnl.populateEventFormat(eventBuff, index);
            }
        }
    }

    this.populateEventFormat = function(evtBuffArray, index){         
        var eventFomat = "<table id=\"eventGroup\" cellpadding=\"2\" >" + evtBuffArray[index] +"</table>"
        intvlMsg.loadMsg("#eventPanel", false);
        $("#eventPanel").html(eventFomat).slideDown(500).delay(7000).fadeOut(500).hide();
        var newIndex = index+=1;
        return newIndex;
    }
    
    this.initializeDts = function(numMonths){
        var tmpDt = new Date();
        
        this.stDt.setYear(tmpDt.getFullYear());
        this.stDt.setMonth(tmpDt.getMonth());

        this.endDt.setYear(tmpDt.getFullYear());
        this.endDt.setMonth(tmpDt.getMonth() + numMonths);
    }

    this.evtAtr = {
        id    : "id",
        title : "title",
        date  : "date",
        time  : "time",
        place : "place",
        description: "description",
        pictureUrl : "pictureUrl",
        link:"link"
    }
    
}

function IntervalLoadingMsg(msg){
    this.intervalId = 0;
    var loadingMsg = msg;
    this.setLoadingMsg = function(thisMsg){
        loadingMsg = thisMsg;
    }
    this.getLoadingMsg = function(){
        return loadingMsg;
    }
    this.loadMsg = function(cssId, flag){
        var j = 0;
        var msg = this.getLoadingMsg();
        var refreshedMsg = this.getLoadingMsg();
        var indexer = 0;
        if ( flag ){
            clearInterval(this.intervalId);
            this.intervalId = setInterval(resetLoadingMsg, 400);
            //alert(this.intervalId);
        } else {
            clearInterval(this.intervalId);
        }
        function clearAllIntervals(){
            for (var id = 0; id < 12; id++){
                clearInterval(id);
            }
        }
        function resetLoadingMsg(){
            if ( indexer < 5 ){
                msg = msg + ".";
                $(cssId).html(msg).show();
            } else {
                indexer = 0;
                msg = refreshedMsg;
                $(cssId).html(msg).show();
            }
            indexer++;
        }
        return false;
    }
}
function DateFormatUtils(){
    /**
     * createMilDateTimeString(yyyy,mm,dd,hr,min) : returns a military date time stamp.
     * YYYYMMDDHRMM - ensuring that appropriate spacer 0's are added where needed.
     * @param yyyy - Full Year
     * @param mm - Two digit Month (01-12)
     * @param dd - Two digit Day
     * @param hr - Two digit Hr
     * @param min - Two digit Min
     * @return 10 digit YYYYMMDDHRMM format (e.g. 2010105251345)
     */
    this.createMilDateTimeString = function(yyyy, mm,dd, hr, min){
        return this.createMilDateString(yyyy, mm, dd) + this.createHrMinString(hr, min);
    }
    this.createMilDateString = function(year, month, day){
        return year + this.createTwoDigitNum(month) + this.createTwoDigitNum(day);
    }
    this.createHrMinString = function(hr, min){
        return this.createTwoDigitNum(hr) + this.createTwoDigitNum(min);
    }
    this.createTwoDigitNum = function(num){
        if ( num.length < 1 ){
            return ("0" + num);
        } else {
            return num;
        }
    }
    this.standardDtFormat = function(yyyymmdd){
        var tempDt = new Date();
        var _yyyymmdd = yyyymmdd.substring(0,8);
        var formatString = "%B %d";
        tempDt.setFullYear(
            _yyyymmdd.substring(0, 4),
            _yyyymmdd.substring(4, 6) -1,
            _yyyymmdd.substring(6, 8))
        //return tempDt.toLocaleFormat(formatString) + ", " + tempDt.getFullYear();
        return tempDt.getDate() + " " + this.findFullMonth( tempDt.getMonth() ) + " " + tempDt.getFullYear();
    }
    this.findFullMonth = function(monthIndex){
        if ( monthIndex == 0 ){
            return "January";
        } else if ( monthIndex == 1 ){
            return "February";
        } else if ( monthIndex == 2 ){
            return "March";
        } else if ( monthIndex == 3 ){
            return "April";
        } else if ( monthIndex == 4 ){
            return "May";
        } else if ( monthIndex == 5 ){
            return "June";
        } else if ( monthIndex == 6 ){
            return "July";
        } else if ( monthIndex == 7 ){
            return "August";
        } else if ( monthIndex == 8 ){
            return "September";
        } else if ( monthIndex == 9 ){
            return "October";
        } else if ( monthIndex == 10 ){
            return "November";
        } else if ( monthIndex == 11 ){
            return "December";
        } else {
            return "";
        }
    }
    this.findAMPMTime = function(hhmm) {
        if ( hhmm < 0100 ){
            return 12 + hhmm.substring(2, 4) + " AM";
        } else if ( hhmm <= 1159 ){
            return  hhmm + " AM";
        } else if ( hhmm > 1159 && hhmm < 1259 ) {
            return hhmm + " PM";
        } else if ( hhmm.substring( 0, 2 ) == 13 ){
            return "1:" + hhmm.substring( 2, 4 ) + " PM";
        } else if ( hhmm.substring( 0, 2 ) == 14 ){
            return "2:" + hhmm.substring( 2, 4 ) + " PM";
        } else if ( hhmm.substring( 0, 2 ) == 15 ){
            return "3:" + hhmm.substring( 2, 4 ) + " PM";
        } else if ( hhmm.substring( 0, 2 ) == 16 ){
            return "4:" + hhmm.substring( 2, 4 ) + " PM";
        } else if ( hhmm.substring( 0, 2 ) == 17 ){
            return "5:" + hhmm.substring( 2, 4 ) + " PM";
        } else if ( hhmm.substring( 0, 2 ) == 18 ){
            return "6:" + hhmm.substring( 2, 4 ) + " PM";
        } else if ( hhmm.substring( 0, 2 ) == 19 ){
            return "7:" + hhmm.substring( 2, 4 ) + " PM";
        } else if ( hhmm.substring( 0, 2 ) == 20 ){
            return "8:" + hhmm.substring( 2, 4 ) + " PM";
        } else if ( hhmm.substring( 0, 2 ) == 21 ){
            return "9:" + hhmm.substring( 2, 4 ) + " PM";
        } else if ( hhmm.substring( 0, 2 ) == 22 ){
            return "10:" + hhmm.substring( 2, 4 ) + " PM";
        } else if ( hhmm.substring( 0, 2 ) == 23 ){
            return "11:" + hhmm.substring( 2, 4 ) + " PM";
        } else {
            return "not set";
        }
    }
}