// 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 formatted = date.toString();
	
			if (formatted.indexOf("GMT") == -1) {
				$("#clock").html( formatted.substring(0,19) + " " + date.getYear() + _shortTimeZone );
			}
			else {
				$("#clock").html( formatted.substring(0, formatted.indexOf("GMT")) + _shortTimeZone );
			//this.$divs.clock.
			}
			_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;
		}
		
	}	
}

