//YAHOO.namespace("wp");
//  A lot of this is from http://izumoff.j29.net/yui_calendar/index.html


YAHOO.widget.Calendar.prototype.hide = function(e, cal) {
	if (! cal)
	{
		cal = this;
	}
	cal.oDomContainer.style.display = "none";
	YAHOO.example.cal_simple.cur_cal = null;
};

YAHOO.example.cal_simple = function() {
			return {
				
				cal1: [],
				cur_cal: null,

				formatDate: function (_date) {
					//alert(_date);
					if ( _date instanceof Array ) { _date = _date[0] }
					var date_separator = "/";
					var day_ = _date.getDate();
					var day = ( day_ < 10 ) ? "0" + day_ : day_;
					var month_ = _date.getMonth()+1;
					var month = ( month_ < 10 ) ? "0" + month_ : month_;
					var year = _date.getFullYear();
					return month + date_separator + day + date_separator + year;
				},
				
				setDate: function() {
					var cal_num = YAHOO.example.cal_simple.cur_cal;
					 // alert(cal_num);
          var next_cal = cal_num + 1;
           // alert(next_cal);
          $("id_calendar_date_" + (++cal_num)).value = YAHOO.example.cal_simple.formatDate(this.getSelectedDates()[0]);
					//alert(cal_num + " date:" + this.getSelectedDates()[0]);
					if ( 1 == cal_num ) { 
						// if this is calendar 1, and there exists a calendar 2, then set the minimum possible value for that calendar
            // also, set calendar 2 to be the value of calendar 1
            if (YAHOO.example.cal_simple.cal1[1]) {
							YAHOO.example.cal_simple.cal1[1].minDate = this.getSelectedDates()[0]; 
              $("id_calendar_date_" + (++next_cal)).value = YAHOO.example.cal_simple.formatDate(this.getSelectedDates()[0]);    
							YAHOO.example.cal_simple.cal1[1].render();
            }
          } else {
						if (YAHOO.example.cal_simple.cal1[0]) {
							// This line can set a max date to select
              //YAHOO.example.cal_simple.cal1[0].maxDate = this.getSelectedDates()[0]; 
							YAHOO.example.cal_simple.cal1[0].render();
						}
					};
					this.hide();
				},
				
				renderTitle: function() {
						//this.oDomContainer.firstChild
						
						if (! this.title) {
							this.title = "Title";
						}
						
						if ( !this.titleDiv )
						{
							this.titleDiv = document.createElement("DIV");
							if (this.title == "") {
								this.titleDiv.style.display = "none";
							}
							
							this.titleDiv.className = "title simple";
							this.titleDiv.innerHTML = this.title;
						
							var linkClose = document.createElement("A");
							linkClose.href = "javascript:void(null)";
							YAHOO.util.Event.addListener(linkClose, "click", this.hide, this);
					
							var imgClose = document.createElement("IMG");
							imgClose.src = YAHOO.widget.Calendar_Core.IMG_ROOT + "us/my/bn/x_d.gif";
							imgClose.className = "close-icon";
					
							linkClose.appendChild(imgClose);
					
							this.linkClose = linkClose;
							this.titleDiv.appendChild(linkClose);
							this.oDomContainer.firstChild.insertBefore(this.titleDiv, this.oDomContainer.firstChild.firstChild);
						}
				},
				


					init: function( cal_num, title, date ) {
            if (!cal_num) { return; }
					  if (!title) { title = "Calendar"; }
					  // Add Calendar Contatiner to DOM
					  var body = document.getElementsByTagName('body')[0];

  					var cal_container = document.createElement('div');
	  				cal_container.id = "id_cal1Container_" + cal_num;
		  			cal_container.className = "id_cal1Container";
			  		//body.appendChild(cal_container);
					
				  	// Set CurDate into Input
				  	$("id_calendar_date_"+cal_num).value = this.formatDate(new Date());
  					var cal_id = "YAHOO.example.cal_simple.cal1["+this.cal1.length +"]";
	  				this.cal1.push(new YAHOO.widget.Calendar(cal_id, "id_cal1Container_"+cal_num)); 
            var len_val = this.cal1.length-1;
			  		this.cal1[len_val].onSelect = this.setDate;
				  	this.cal1[len_val].onRender = this.renderTitle;
					  this.cal1[len_val].title = title;

  					this.cur_cal = len_val;
            this.cal1[len_val].select(date);
            this.cur_cal = null;
					
			  		this.cal1[len_val].addRenderer("1.1,1.6,5.1,8.15,10.3,10.31,12.25,12.26,03.26", 					this.cal1.renderCellStyleHighlight1);
				  	this.cal1[len_val].customConfig = function() {

						//can add local config settings
            };

				
					this.cal1[len_val].setupConfig();	
					if ( len_val == 1 ) {
							this.cal1[1].minDate = new Date(this.cal1[0].getSelectedDates()[0]); 
					};
       
          this.cal1[len_val].render(); 
				},
			
				showCalendar: function ( cal_num ) {
					if ( this.cur_cal !== null) { return; }
					var link1 = $("id_calendar_button_" + cal_num);	
					var pos = YAHOO.util.Dom.getXY(link1);
					var img_height = parseInt(YAHOO.util.Dom.getStyle(link1, "height"));
					cal_num--;
					this.cal1[cal_num].oDomContainer.style.top  = pos[1] + img_height - 1 + "px"; //
					this.cal1[cal_num].oDomContainer.style.left = pos[0] + "px";
					this.cal1[cal_num].oDomContainer.style.display = "block";
          this.cur_cal = cal_num;
				}
			}
		} ();

function Init_Cals(day) {
	var cals = getElementsByClass("id_calendar_button", null, "a");
  var inputElement;
  var inputValue;
  var date_field;
  
  for ( i = 0, j = 1; i < cals.length; i++,j++) {

		
		 if (day == 'today' || day == null) {
    date_field = cals[i].previousSibling.value;
		date_field = ( date_field == "" || date_field == undefined ) ? new Date() : new Date (date_field);
    } else if (day == 'empty') {
    } else {
    date_field = day;
    }
		
    //the input field is named id_calendar_date_1.  Get the value of it.
    inputElement = document.getElementById('id_calendar_date_' + j);
    inputValue = inputElement.value;
    //alert(inputValue);
    YAHOO.example.cal_simple.init(j, cals[i].alt, date_field);
	}
};


// the two functions below were separated out of the script file and
// originally placed in the html header. 

  function $() {
	var elements = new Array();
	for (var i = 0; i < arguments.length; i++) {
		var element = arguments[i];
		if (typeof element == 'string')
			element = document.getElementById(element);
		if (arguments.length == 1)
			return element;
		elements.push(element);
	}
	return elements;
};

function getElementsByClass(searchClass,node,tag) {
	var classElements = new Array();
	if ( node == null )
		node = document;
	if ( tag == null )
		tag = '*';
	var els = node.getElementsByTagName(tag);
	var elsLen = els.length;
	var pattern = new RegExp("(^|\\s)"+searchClass+"(\\s|$)");
	for (i = 0, j = 0; i < elsLen; i++) {
		if ( pattern.test(els[i].className) ) {
			classElements[j] = els[i];
			j++;
		}
	}
	return classElements;
};



