YAHOO.namespace("hub.calendar");
YAHOO.namespace("hub.dayPanel");
  
YAHOO.hub.calendar.init = function() {
//AJAX event handling.
function eventRequestSuccess (a) {
  YAHOO.hub.dayPanel.panel.setBody(a.responseText);
}

function eventRequestFailure (a) {
  alert("failure: " + a);
}

var months = ['jan', 'feb', 'mar', 'apr', 'may', 'jun', 'jul', 'aug', 'sep', 'oct', 'nov', 'dec'];

function daySelectHandler(type,args,obj) {
  	var selected = args[0];
  	var selDate = this.toDate(selected[0]);

  	var selectedDate = this.getSelectedDates()[0];
  	var selectNum = this.getCellIndex(selectedDate);
  	var theCell = this.cells[selectNum];

  	YAHOO.hub.dayPanel.panel.cfg.setProperty("context",[theCell.id, "br", "tl"]);
  	
  	//Set up event request.
  	
  	var sURL = "/events/" + selectedDate.getFullYear() + "/";
  	sURL = sURL + months[selectedDate.getMonth()] + "/";
  	if(selectedDate.getDate() < 10){
  	  sURL = sURL + "0";
  	}
  	sURL = sURL + selectedDate.getDate() + "/";
  	//alert(sURL);
  	var callback =
    {
      success: eventRequestSuccess,
      failure: eventRequestFailure,
      argument: args
    };
  	YAHOO.hub.dayPanel.panel.setBody("<div id=\"progressIndicator\"></div>");
  	var transaction = YAHOO.util.Connect.asyncRequest('GET', sURL, callback, null);
  	
  	YAHOO.hub.dayPanel.panel.show();
  };
  
  function loadEventInfo () {
    
  }

  function dayDeselectHandler(type, args, obj) {
	var deselected = args[0];
	var deselDate = this.toDate(deselected[0]);
  	YAHOO.hub.dayPanel.panel.hide();
};

  // Instantiate a Panel from markup
  //The second argument passed to the
  //constructor is a configuration object:
  YAHOO.hub.dayPanel.panel = new YAHOO.widget.Panel("dayPanel", {
  	width:"400px",
  	fixedcenter: true,
  	//constraintoviewport: true, 
  	underlay:"none", 
  	close:true, 
  	visible:false, 
  	draggable:false} );
  //YAHOO.hub.dayPanel.panel.body.style.height = "200px";
  YAHOO.hub.dayPanel.panel.body.style.overflow = "hidden";
  //If we haven't built our panel using existing markup,
  //we can set its content via script:
  
  
  YAHOO.hub.dayPanel.panel.setBody("<div id=\"progressIndicator\"></div>");
    
  //Although we configured many properties in the
  //constructor, we can configure more properties or 
  //change existing ones after our Panel has been
  //instantiated:
  YAHOO.hub.dayPanel.panel.cfg.setProperty("underlay","matte");

  YAHOO.hub.dayPanel.panel.render();

  YAHOO.hub.dayPanel.panel.cfg.setProperty("fixedcenter",false);

  YAHOO.hub.calendar.cal1 = new YAHOO.widget.Calendar("cal1", "cal1Container");

  YAHOO.hub.calendar.cal1.selectEvent.subscribe(daySelectHandler, YAHOO.hub.calendar.cal1, true);
  YAHOO.hub.calendar.cal1.deselectEvent.subscribe(dayDeselectHandler, YAHOO.hub.calendar.cal1, true);

  YAHOO.hub.calendar.cal1.render();
}

YAHOO.util.Event.onDOMReady(YAHOO.hub.calendar.init);
