// this will be taken care of in the namespace method in BODOG.js
if (!BODOG) { var BODOG = {}; }
if (!BODOG.components) { BODOG.components = {}; }

/*
two panels within #time-zone-menu:
#time-zone-select
#time-zone-confirm
*/

BODOG.components.timeZoneSelection = function() {
	
    /** These are all private */

    var _showDeny = null;
    var _postMode = null;
    var _shortTimeZone = null;
    var _timeStamp = null;
		
	return {
	
		/** These are all public */
		
		settings: {
			timeZoneID: "#time-zone",
			timeZoneSelectID: "#time-zone-select",
			timeZoneMenuID: "#time-zone-menu",
            timeZonePanelID: "#time-zone-panel",
            timeZoneChangeDenyID: "#time-zone-change-deny",
			clockID: "#clock",
			
			upArrowPath: "/publish/bodogcom/images/template/time-zone-up-arrow.gif",
			downArrowPath: "/publish/bodogcom/images/template/time-zone-down-arrow.gif",
			
			timeZoneClosedClass: "time-zone-closed",
			timeZoneOpenClass: "time-zone-open",
			
			// passed in to init() from JSP
			postMode: '',
			shortTimeZone: '',
			timeStamp: null
		},
		
		//create div object
		$divs: {},
		
		panel_off : true,
		
				
		init: function(options) {
		//console.log("init");
			var that = this;
			$.extend(this.settings, options || {});
			
			this.setPostMode(this.settings.postMode);
			this.setShortTimeZone(this.settings.shortTimeZone);
			
			//var newDate = new Date(this.settings.timeStamp).valueOf();
			//this.setTimeStamp(newDate);
			
			var newDate = this.settings.timeStamp;
			this.setTimeStamp(newDate);
			
			//this.setTimeStamp(this.settings.timeStamp);
			
			//add properties of this.settings to $div object
			this.assignDivs();
			
			this.tick();
			
			setInterval(function() {
				that.tick();
			}, 1000);
			
			if($('#time-zone')){
		    	this.$divs.tzSelect.hide();
				
				if(_postMode == '') {
			    	this.$divs.tzMenu.click(function(){
						that.openTimeZonePanel();
						return false;
					});
		    	}
	
				$("#time-zone-menu").hover(function () {
					$(this).addClass("over");
				}, function () {
					$(this).removeClass("over");
				});
			}
		},
		
		assignDivs: function() {
            
            this.$divs = {
                tz: $(this.settings.timeZoneID),
				tzSelect: $(this.settings.timeZoneSelectID),
				tzMenu: $(this.settings.timeZoneMenuID),
				tzPanel: $(this.settings.timeZonePanelID),
				tzChangeDeny: $(this.settings.timeZoneChangeDenyID),
				clock: $(this.settings.clockID)
            }
            
        },
		
		openTimeZonePanel: function(detectOption) {
		
			var that = this;
			
			// open panel select
			if (detectOption == 'select') {
				this.panel_off = true;
				showConfirm = false;
			}
			  
			if (this.panel_off) {
			    // toggle main panel
			    this.panel_off = false;
			    
			    // handle look and feel change in open mode
				this.$divs.tzMenu.removeClass(this.settings.timeZoneClosedClass).addClass(this.settings.timeZoneOpenClass);
				
			    this.$divs.tzPanel.show();
				
				//should be changing class instead of img src
				$("img[name='droparrow'], this.$divs.tzPanel").each(function(){
					$(this).attr("src",  that.settings.upArrowPath);
				});
			
			    // determine which panel to show
			    if(_showDeny) {
					this.$divs.tzChangeDeny.show();
					this.$divs.tzSelect.hide();
			    } else {
					this.$divs.tzChangeDeny.hide();
					this.$divs.tzSelect.show();
				}
				
			} else {
				this.closeTimeZonePanel();
			}
		},
		
		closeTimeZonePanel: function() {
		
			var that = this;
		
			// toggle main panel
			this.panel_off = true;
			
			//should be changing class instead of img src
			$("img[name='droparrow'], this.$divs.tzPanel").each(function(){
				$(this).attr("src",  that.settings.downArrowPath);
			});
			
			this.$divs.tzMenu.removeClass(this.settings.timeZoneOpenClass).addClass(this.settings.timeZoneClosedClass);
			
			// hide all sub panels
			this.$divs.tzPanel.hide();
			this.$divs.tzChangeDeny.hide();
		},
		
        // handles clock tick
        tick: function() {
          //TODO error checking on jsp set variables. eg. shortTimeZone and timeStamp.
            var date = new Date(_timeStamp);
            var dayOfWeek = "";
            var month = "";
            
            switch(date.getDay())
            {
            case 0:
                dayOfWeek = "Sun";
                break;
            case 1:
                dayOfWeek = "Mon";
                break;
            case 2:
                dayOfWeek = "Tue";
                break;
            case 3:
                dayOfWeek = "Wed";
                break;
            case 4:
                dayOfWeek = "Thu";
                break;
            case 5:
                dayOfWeek = "Fri";
                break;
            case 6:
                dayOfWeek = "Sat";
                break;
            default:
                dayOfWeek = "";
            }
            
            switch(date.getMonth())
            {
            case 0:
                month = "Jan";
                break;
            case 1:
                month = "Feb";
                break;
            case 2:
                month = "Mar";
                break;
            case 3:
                month = "Apr";
                break;
            case 4:
                month = "May";
                break;
            case 5:
                month = "Jun";
                break;
            case 6:
                month = "Jul";
                break;
            case 7:
                month = "Aug";
                break;
            case 8:
                month = "Sep";
                break;
            case 9:
                month = "Oct";
                break;
            case 10:
                month = "Nov";
                break;
            case 11:
                month = "Dec";
                break;
            default:
                month = "";
            }
            
            //function to pad hours, minutes, and seconds with a leading zero
            function PadDigits(n, totalDigits) 
            { 
                n = n.toString(); 
                var pd = ''; 
                if (totalDigits > n.length) 
                { 
                    for (i=0; i < (totalDigits-n.length); i++) 
                    { 
                        pd += '0'; 
                    } 
                } 
                return pd + n.toString(); 
            } 
                        
            $("#clock").html(dayOfWeek+" "+month+" "+date.getDate()+" "+date.getFullYear()+" "+
                PadDigits(date.getHours(),2)+":"+PadDigits(date.getMinutes(),2)+":"+PadDigits(date.getSeconds(),2)+
                " "+_shortTimeZone );
            _timeStamp += 1000;        
        },
		
		
		/* Setters */
		setShowDeny: function(value){
			_showDeny = value;
		},
		setPostMode: function(value){
			_postMode = value;
		},
		setShortTimeZone: function(value){
			_shortTimeZone = value;
		},
		setTimeStamp: function(value){
			_timeStamp = value;
		}

	}// end return object
}();


function populateTimeZone(timezones, tzSelect, position) {
	
	for( i=0; i<timezones.length; i++ ) {
		var timezone = timezones[i];
		var tzDesc = timezone.defaultDescription;
		var tzId = timezone.preferenceOptionId;
		tzSelect.options[i] = new Option( tzDesc,tzId );
		if( tzId == position ){
			tzSelect.options[i].selected=true;
		}
		
	}	
}


