/*
	YUI calendar implementation
	Developed by: Ramy Deeb [radeeb@copaair.com]
	Requirements:
		YUI Calendar
		Calendar Language Packs
		jQuery
*/

/*
 * appendCalendar
 *   appends a new calendar to a text box, and an icon.
 * @params:
 *   textBox_id: STRING, id name of the text box that is going to trigger the calendar
 *   icon_id: STRING, id name of the icon triggering the calendar
 *   calName: STRING, name of the resulting calendar and object.
 *            Note: There bust be present a div container already with id calName + "Container"
 *                  Example: calName = testCalendar, then the container should be named textCalendarContainer
 *   initDate: STRING, initial date of our calendar
 *   endDate: STRING, ending date of our calendar
 *   dependantCalendar: YAHOO.widget.CalendarGroup, optional, when the selected date changes updates this calendar
 *   daysDiference: INTEGER. optional, how many diference in days will be between the two calendars
 * @return:
 * 	 calObject: YAHOO.widget.CalendarGroup, resulting calendar object.
 */
var appendCalendar = function(textBox_id, icon_id, calName, initDate, endDate, selectedDate, dependantCalendar, daysDiference){
	//Let's create our calendar object
	var calObject = createCalendar(calName, initDate, endDate);
	//Now let's add the listeners to the click event on the textbox and the icon
  var textBoxId = "#" + textBox_id;
  var iconId = "#" + icon_id;
  $(textBoxId).bind("click", function(){
    showCalendar(calObject)
  });
  $(textBoxId).bind("blur", function(){
    handleBlur(calName + "Container", textBox_id, icon_id, calObject);
  });
  $(iconId).bind("click", function(){
    showCalendar(calObject)
  });
  $(iconId).bind("blur", function(){
    handleBlur(calName + "Container", textBox_id, icon_id, calObject);
  });
	//Now let's register the event that it is going to get trigered by the user selection on the calendar
	calObject.selectEvent.subscribe(function(type,args,obj){
			//Let's search for the selected date by the user
			var selected = args[0];
			var selDate = this.toDate(selected[0]);
      //Let's populate the selected date
			document.getElementById(textBox_id).value = dateToLocaleString(selDate, this);
      //If a calendar depends on this one we have to update it!
      if (dependantCalendar)
      {
        dependantCalendar.cfg.setProperty('MINDATE',selDate);
        var newPage = selDate.getMonth()+ 1 + "/" + selDate.getFullYear();
        var newDate = YAHOO.widget.DateMath.add(selDate, YAHOO.widget.DateMath.DAY, daysDiference);
        dependantCalendar.cfg.setProperty('PAGEDATE', newPage);
        dependantCalendar.select(newDate);
        dependantCalendar.render();
      }
      //And at last hide the calendar
			hideCalendar(calObject);
		}, calObject, true);
    calObject.select(selectedDate);
    hideCalendar(calObject);
  return calObject;
};

/*
 * createCalendar
 * 	This function creates the calendar object
 * @params
 * 	 calObject: name of the resulting calendar
 *   initDate: initial date of our calendar
 *   endDate: ending date of our calendar
 * @return:
 * 	 myCalObject: YAHOO.widget.CalendarGroup, resulting calendar object.
 */
var createCalendar = function(calObject, initDate, endDate){
	//Let's create our calendar
	var myCalObject = new YAHOO.widget.CalendarGroup(calObject,calObject + "Container", { pages:2, close:true, iframe:false } );
	//Now let's localize it
  if (window.months_shorts != undefined)
  {
    myCalObject.cfg.setProperty('MONTHS_SHORT', months_shorts); 
  	myCalObject.cfg.setProperty('MONTHS_LONG', months_long);
  	myCalObject.cfg.setProperty('WEEKDAYS_1CHAR', weekdays_1char); 
  	myCalObject.cfg.setProperty('WEEKDAYS_SHORT', weekdays_short);
  	myCalObject.cfg.setProperty('WEEKDAYS_MEDIUM', weekdays_medium);
  	myCalObject.cfg.setProperty('WEEKDAYS_LONG', weekdays_long);
  }
  myCalObject.cfg.setProperty('HIDE_BLANK_WEEKS', true);
  if (initDate)
    myCalObject.cfg.setProperty('MINDATE',initDate);
  if (endDate)
    myCalObject.cfg.setProperty('MAXDATE',endDate);
	//Now lets render the calendar object
	myCalObject.render();
	//Let's return our object
	return myCalObject;
};

/*
 * show
 *   This function shows the calendar
 * @params:
 *   calObject: The calendar object that it is going to be shown
 * @returns:
 *   NONE
 * Modified by Julio Obispo
 * 06/10/2009
 * added call for bgiframe
 * for create IFrame
 * need to hide dropdowns
 * 
 */
var showCalendar = function(calObject) {
	calObject.show();
	//easier to manipulate with YUI
};

/*
 * hideCalendar
 *   This function hides the calendar
 * @params:
 *   calObject: The calendar object that it is going to be hidden
 * @returns:
 *   NONE
 */
var hideCalendar = function(calObject) {
	calObject.hide();
};

/*
 * dateToLocaleString
 *   Function by Yahoo! to return the locale string
 * @params:
 *   dt: Date object
 *   cal: calendar object
 * @returns:
 *   string: With the localized date
 */
var dateToLocaleString = function(dt, cal) { 
	var dStr = dt.getDate();   
	var mStr = cal.cfg.getProperty('MONTHS_LONG')[dt.getMonth()];   
	var yStr = dt.getFullYear();   
	return (mStr + ' ' + dStr + ', ' + yStr);  
}

/*
 * handleBlur
 *   Function by Yahoo! modified by Ramy Deeb. Hide Calendar if we click anywhere in the document other than the calendar
 * @params:
 *   calContainer: calendar Container
 *   textBox: textBox that showed that trigered the calendar
 *   calObject: Calendar object that we are going to hide
 * @returns:
 *   NONE
 */
// 
var handleBlur = function(calContainer, textBox, icon, calObject){
  var Event = YAHOO.util.Event,
      Dom = YAHOO.util.Dom;
  var textBox = Dom.get(textBox);
  var icon = Dom.get(icon);
  Event.on(document, "click", function(e) {
    var el = Event.getTarget(e);
    var dialogEl = Dom.get(calContainer);
    if (el != dialogEl && !Dom.isAncestor(dialogEl, el) && el != textBox && el != icon && !Dom.isAncestor(textBox, el) && !Dom.isAncestor(icon, el)) {
      hideCalendar(calObject);
    }
  });
}