if ( Revolution == null )
	var Revolution = new Object();

Revolution.Calendar = {
	
	dialog: null,
	calendar: null,
	
	oCaller: null,
	cfg: null,
	
	/**
	 * Init calendar
	 */
	init: function() {	

		// Create dialog
		this.dialog = new YAHOO.widget.Dialog("cal_container", {
			visible:false,
			context:["show", "tl", "bl"],
			draggable:true,
			close:true,
			navigator:true
		});

		this.dialog.setHeader('Select Date');
		this.dialog.setBody('<div id="revolution_calendar"></div>');
		this.dialog.render(document.body);
		
		// Create calendar	
		this.calendar = new YAHOO.widget.CalendarGroup("revolution_calendar", {
			iframe:false,
			hide_blank_weeks:true,
			pages:2
		});
		this.calendar.selectEvent.subscribe(this.dateSelect, this.calendar, true);
		//this.calendar.render();

		// Tell Dialog it's contents have changed, which allows container to redraw the underlay (for IE6/Safari2)
		this.calendar.renderEvent.subscribe(function() {
			Revolution.Calendar.dialog.fireEvent("changeContent");
		});
		
		this.setDefault();
	},

	/**
	 * setup calendar data
	 */
	setup: function(oCaller, cfg) {
			
		if ( this.calendar == null )
			this.init();
		
		this.cfg = cfg;	
		
		// Set config options
		if ( cfg != null ) {
			
			// set min date to 'today'
			if ( cfg.datemin == 'today' )
				this.dateSetMinToday();
			
			// Set currently selected date
			var elValue = document.getElementById(cfg[oCaller].value);
			if ( elValue != null && elValue.value != null ) {			
				var date = new Date(elValue.value);
				var pageDate = (date.getMonth() + 1) + "/" + date.getFullYear();
				var selDate = this.getDateValueStr(date);

				this.calendar.cfg.setProperty("pagedate", pageDate, false);
				this.calendar.cfg.setProperty("selected", selDate);
			}
			
			// Setup title
			if ( cfg[oCaller].title != null ) {
				this.dialog.setHeader(cfg[oCaller].title);
				this.dialog.render();
			}
		}

		this.calendar.render();
	},
	
	/**
	 * open dialog
	 */
	show: function(oCaller, cfg) {

		this.oCaller = oCaller;
		this.setup(oCaller, cfg);
		
		var pos = this.findPos(document.getElementById(cfg.calImgId));
		
		this.dialog.cfg.setProperty("y", cfg.top == null ? pos.curtop + 20 : pos.curtop + cfg.top);
		this.dialog.cfg.setProperty("x", cfg.left == null ? pos.curleft : pos.curleft + cfg.left);
		this.dialog.render();

		this.dialog.show();
	},
	
	/**
	 * Select date
	 */
	dateSelect: function(type, args, obj) {			
		
		var oCaller = Revolution.Calendar.oCaller;
		var elValue = document.getElementById(Revolution.Calendar.cfg[oCaller].value);
		var elText = document.getElementById(Revolution.Calendar.cfg[oCaller].text);
			
		var dates = args[0]; 
	    var date = dates[0]; 
	    var year = date[0], month = date[1], day = date[2]; 
	 	
		elValue.value = year + "/" + month + "/" + day;		
	    elText.value = aMonth[month-1] + " " + (day > 9 ? day : '0' + day) + ", " + year;

		// set interval of 3 days if pickup > dropoff
		if ( Revolution.Calendar.cfg['pickup'].value != null &&  Revolution.Calendar.cfg['dropoff'].value != null) {
			var elValue = document.getElementById(Revolution.Calendar.cfg['pickup'].value);
		
			var elDropoffValue = document.getElementById(Revolution.Calendar.cfg['dropoff'].value);
			var elDropoffText = document.getElementById(Revolution.Calendar.cfg['dropoff'].text);
			
			var datePickup = new Date(elValue.value);
			var dateDropoff = new Date(elDropoffValue.value);

			if ( datePickup > dateDropoff ) {
				dateDropoff = datePickup;
				dateDropoff.setDate(datePickup.getDate() + 3);

				elDropoffValue.value = Revolution.Calendar.getDateValueStr(dateDropoff, true);
				elDropoffText.value = Revolution.Calendar.getDateTextStr(dateDropoff);
			}
		}

		Revolution.Calendar.dialog.hide();
	},
	
	/**
	 * Set defaults
	 */
	setDefault: function() {
		this.calendar.cfg.setProperty('mindate', null);
	},
	
	// Set min date to today
	dateSetMinToday: function() {
		this.calendar.cfg.setProperty("mindate", this.getDateValueStr(new Date()));
	},
	
	findPos:function (obj) {
		var curleft = curtop = 0;
		
		if (obj.offsetParent) {
			do {
			curleft += obj.offsetLeft;
			curtop += obj.offsetTop;
			
			} while (obj = obj.offsetParent);
		}

		return {curleft: curleft,curtop: curtop};
	},
	
	/**
	 * Get date string from Date object
	 */
	getDateValueStr: function(date, isYearFirst) {	
		if ( isYearFirst )
			return date.getFullYear() + '/' + (date.getMonth() + 1) + '/' + date.getDate();
		else
			return (date.getMonth() + 1) + '/' + date.getDate() + '/' + date.getFullYear();
	},
	
	getDateTextStr: function(date) {	
		return aMonth[date.getMonth()] + ' ' + (date.getDate() > 9 ? date.getDate() : '0' + date.getDate()) + ', ' + date.getFullYear();
	}
}

// Init Calendar on DOM ready
YAHOO.util.Event.onDOMReady(function(){	Revolution.Calendar.init();	 });