/**
 * imTextBoxCalendar - Text Box Calendar widget for Yahoo User Interface Library
 * @author Les Green
 * copyright (c) 2009, Intriguing Minds, Inc. All Rights Reserved
 *
 * Version 0.5
 * 
 *   This program is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program.  If not, see <http://www.gnu.org/licenses/>.

 *   Demo and Documentation can be found at:   
 *   http://www.grasshopperpebbles.com
 */
 
  
 YAHOO.namespace('INTRIG');
(function() {
	var Dom = YAHOO.util.Dom,
		$ = Dom.get,
        Event = YAHOO.util.Event,
        Element = YAHOO.util.Element,
        Dialog = YAHOO.widget.Dialog,
        Overlay = YAHOO.widget.Overlay,
        lang = YAHOO.lang,
        Calendar = YAHOO.widget.Calendar;
      
	var imInputCal = function(oConfig) {
    	//this.selected = '';
    	//this.dialog = '';
		this.fields = oConfig.fields;
		this.cal = oConfig.cal;
    	this.calTB = '';
    	this.container = oConfig.container;
    	this.selected = '';
    	this.calOptions = oConfig.calOptions;
    	this.dateFormat = (oConfig.dateFormat) ? oConfig.dateFormat : {order: "mdy", month: "numeric", year:"long", delimiter: "/"}
    	this.buttons = (oConfig.buttons) ? oConfig.buttons : false;
    	this.ctnrPosition = (oConfig.ctnrPosition) ? oConfig.ctnrPosition : 'element';
    	self = this;
    	this.init(oConfig);
    };
    
    YAHOO.INTRIG.imInputCal = imInputCal;
    
    YAHOO.INTRIG.imInputCal.prototype = {
    	
    	onCalOk:function() {
            if (self.cal.getSelectedDates().length > 0) {
            	self.selectDate(self.cal.getSelectedDates()[0]);
            } else {
                $(self.selected).value = "";
            }
            this.hide();
        },
        
        onCalCancel:function() {
            this.hide();
        },
        selectDate:function(dte) {
        	var selDate = dte;
    	    var dStr = selDate.getDate();
            if ((self.dateFormat.month == "short") || (self.dateFormat.month == "numeric")) {
            	var mStr = self.cal.cfg.getProperty("MONTHS_SHORT")[selDate.getMonth()];	
            } else if (self.dateFormat.month == 'long') {
            	var mStr = self.cal.cfg.getProperty("MONTHS_LONG")[selDate.getMonth()];
            }
            var yStr = selDate.getFullYear();
            if (self.dateFormat.year == "short") {
            	yStr = yStr.substr(2,2);
            }
            var d = self.dateFormat.delimiter;
            if (self.dateFormat.order == "mdy") {
            	//automatically add comma if using long date: November 13, 2009
            	var dd = (self.dateFormat.month == 'long') ? ', ' : d;
            	$(self.selected).value = mStr + ' ' + dStr + dd + yStr;
            } else if (self.dateFormat.order == "ymd") {
            	$(self.selected).value = yStr + d + mStr + d + dStr;
            }
        },
        createCalContainer:function() {
        	//this.cal = new Calendar(this.cal, {
            //	iframe:false          // Turn iframe off, since container has iframe support.
            	//hide_blank_weeks:true  // Enable, to demonstrate how we handle changing height, using changeContent
        	//});
		
        	this.cal = new Calendar(this.cal, this.calOptions);
        	if (this.dateFormat.month == 'numeric') {
        		this.cal.cfg.setProperty("MONTHS_SHORT", ["01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12"] );
        	}
        					
        	this.container = new Dialog(this.container, {
	            //context:[self.selected, "tl", "bl"],
	          //  buttons:[ {text:"Select", isDefault:true, handler: this.onCalOk}, 
	          //            {text:"Cancel", handler: this.onCalCancel}],
	            width:"16em",  // Sam Skin dialog needs to have a width defined (7*2em + 2*1em = 16em).
	            //fixedcenter : true,
				visible : false, 
			  	//constraintoviewport : true,
				//modal: true,
	            draggable:false,
	            close:true
	        });					
        	
        	if (this.ctnrPosition == 'fixed') {
        		this.container.cfg.queueProperty("fixedcenter", true);
        		this.container.cfg.queueProperty("constraintoviewport", true);
        		this.container.cfg.queueProperty("modal", true);
        		this.container.cfg.fireQueue();
        	}
        	// turn on buttons
        	if (this.buttons == true) {
        		var btns = [ {text:"Select", isDefault:true, handler: this.onCalOk}, 
	                      {text:"Cancel", handler: this.onCalCancel}];
        		this.container.cfg.queueProperty("buttons", btns);
        		this.container.cfg.fireQueue();
        	}
			// Pressing the Esc key will hide the Calendar Menu and send focus back to 
			// its parent Button
			
			Event.on(this.container, "keydown", function (p_oEvent) {
				if (Event.getCharCode(p_oEvent) === 27) {
					this.container.hide();
					//this.focus();
				}
			}, null, this);

			this.cal.render();
	        this.container.render();
	
	        // Using dialog.hide() instead of visible:false is a workaround for an IE6/7 container known issue with border-collapse:collapse.
	        this.container.hide();
	
	        this.cal.renderEvent.subscribe(function() {
	            // Tell Dialog it's contents have changed, Currently used by container for IE6/Safari2 to sync underlay size
	            this.container.fireEvent("changeContent");
	        });
	        this.cal.selectEvent.subscribe(function(sel) {
	        	if (!self.buttons) {
		            self.selectDate(self.cal.getSelectedDates()[0]);
		            self.container.hide();
	        	}
	        });
			this.attachFields();
        },
        
        attachFields:function() {
        	Event.addListener(this.fields, "click", this.showCal);
        },
        
        showCal:function(e) {
    		self.selected = this.id;
    		if (self.ctnrPosition == 'element') {
    			self.container.cfg.setProperty("context", [self.selected, "tl", "bl"]);
    		}
			self.container.show();
    	},
    	
    	init:function(oConfig) {
    		this.createCalContainer();
    	}
	};
})();
YAHOO.register("imInputCal", YAHOO.INTRIG.imInputCal, {version: "0.99", build: "1"});
