
var eventCalendar;
function initCalendar(calMap,flagHide){
	try{
        // init calendar, 
		//alert('initCalendar');
	     eventCalendar = new YAHOO.widget.Calendar("eventCalendar","calendarDiv",{title:"Choose a date",close:true});
		 eventCalendar.cfg.setProperty("mindate",YAHOO.util.Dom.get("calendarStart").value,false);
		 eventCalendar.cfg.setProperty("maxdate",YAHOO.util.Dom.get("calendarEnd").value,false);
		var today=new Date();
		configCalendar(calMap);
		eventCalendar.render();
		if(flagHide){
			eventCalendar.hide();
		} else{
			eventCalendar.show();
		}
        // callback to submit date on select
        eventCalendar.selectEvent.subscribe(calSelectHandler, eventCalendar, true);
        
        // delegate to close calendar/yelp on click anywhere but calendar/Yelp. Cud move this to index page 
        YAHOO.util.Event.addListener(YAHOO.util.Selector.query('body')[0], "click", popupCloseHandler);
        
        //callback to changePageEvent
        eventCalendar.changePageEvent.subscribe(calChangePageHandler, eventCalendar, true); 
        
        // shows the calendar.If we pass true as the final parameter, the custom object that is passed 
       // is used for the execution scope (so it becomes "this" in the callback).
        YAHOO.util.Event.addListener("calendarButton", "click",calendarButtonHandler, eventCalendar, true);
	}   catch(e){
     	//console.debug('error in init cal----'+e.message);
         	// alert('error in init cal----'+e.message);
         }
    }
  function calChangePageHandler(type,args,obj) {
	//alert('page date cfg  mon   '+eventCalendar.cfg.getProperty("pagedate").getMonth());

	var month = eventCalendar.cfg.getProperty("pagedate").getMonth() + 1;
    var year = eventCalendar.cfg.getProperty("pagedate").getFullYear();
    var lastDate=lastDayOfMonth(year,month-1);
    //alert('last day of page '+lastDate);
   // eventCalendar.cfg.setProperty("mindate",month+"/01/"+year,false);
	//eventCalendar.cfg.setProperty("maxdate", month+"/"+lastDate+"/"+year,false);
	
	//map key
	var mapKey=month+"\/"+year;
	var selectableCalDates=g_calendarMap[mapKey];
	//block out non eventful dates
	blackOutNonEventDates(selectableCalDates);
	eventCalendar.render();
        	
		}
/*
 * position calBox above eventSearchDiv and show 
 */
function calendarButtonHandler(e, obj){	
	try{
	//width of calendar is 170px
		var calBoxH=parseInt(getOffSet( YAHOO.util.Dom.get('eventLocationDiv')).split(',')[1])- 225;
		var locDivLeft= parseInt(getOffSet( YAHOO.util.Dom.get('eventLocationDiv')).split(',')[0]);
		//alert('locDivLeft'+locDivLeft);
		var calBoxL= locDivLeft- 170;
		YAHOO.util.Dom.setStyle('calbox','top',calBoxH+'px');
		YAHOO.util.Dom.setStyle('calbox','left',calBoxL+'px');
		eventCalendar.show();
	}   catch(e){
     	//console.debug('error in calendarButtonHandler----'+e.message);
     }
}
//delegate click to BODY(this) to hide calendar
function calCloseHandler(e, obj) {
	var elTarget = YAHOO.util.Event.getTarget(e);
	while (elTarget != this) {
		if(elTarget.id == "calbox" ||elTarget.id == "calendarButton" ) {
			return;
		} else {
			elTarget = elTarget.parentNode;
		}
	}
	obj.hide();
}

function calSelectHandler(type,args,obj) {
    var selected = args[0];
    var selDate = this.toDate(selected[0]);
    //alert('calSelectHandler'+selDate);
    YAHOO.util.Dom.get("selectedDate").value=selDate.getMonth()+1+"/"+selDate.getDate()+"/"+selDate.getFullYear();
    getEventsByDate(YAHOO.util.Dom.get("selectedDate").value);
}

function configCalendar(calMap){
		//set pagedate/min/max properties called only by initCal
	try{
		var firstDate = new Date();; 
		var mapKey = firstDate.getMonth()+1 + "\/" + firstDate.getFullYear();
		var userSelectDate = YAHOO.util.Dom.get("selectedDate").value;
		 if (userSelectDate!=null && userSelectDate!=''){
			//set to selected date 
		//	alert('userSelectDate field'+userSelectDate);
			eventCalendar.select(userSelectDate);
		} else {
			// default to today
			
			eventCalendar.select(parseInt(firstDate.getMonth()+1)+"/"+firstDate.getDate()+"/"+ firstDate.getFullYear());
			
		}
		
		var selectedDates = eventCalendar.getSelectedDates();
	//	alert('selectedDates'+selectedDates.length);
		if (selectedDates.length > 0) {
			firstDate = selectedDates[0]; 
//			alert(' eventCalendar.getSelectedDates[0] Mon'+firstDate.getMonth());
			eventCalendar.cfg.setProperty("pagedate", (firstDate.getMonth()+1) + "/" + firstDate.getFullYear());
			mapKey = firstDate.getMonth()+1 + "\/" + firstDate.getFullYear();
		}
		//set min /max based on first and last element of list of values from calendarMap[mmyy]
		var selectableCalDates=null;
		if(calMap!= null){
			//alert('configCalendar  >>calMap ok, mapKey'+ mapKey); 
			selectableCalDates = calMap[mapKey];
		}
		if( selectableCalDates!=null ) {
			if(selectableCalDates.length>0){
		
		//	alert('selectableCalDates List'+selectableCalDates.join(" ,") );
			//eventCalendar.cfg.setProperty("mindate",selectableCalDates[0],false);
			//eventCalendar.cfg.setProperty("maxdate",selectableCalDates[selectableCalDates.length-1],false);
			blackOutNonEventDates(selectableCalDates);
		}
		}		
	
	}   catch(e){
     	//console.debug('error in config Cal----'+e.message);
		// alert('error in config Cal----'+e.message);
     }
}
function blackOutNonEventDates(selectableCalDates){
 try{
	 if(selectableCalDates==null){
	 		//alert('cud not black out dates. selectableCalDates is null ');
	 		return;
	 	}
		var fullMonthDates=getDateStringsForAMonth(selectableCalDates[0].split("/")[2] , selectableCalDates[0].split("/")[0]);
		var blockedDates=arrayDifference( fullMonthDates, selectableCalDates);
		if(blockedDates==null) return;
		///alert('blockedDates..'+ blockedDates.join(',') );
		//alert('INSIDE blackOutNonEventDates::selectableCalDates..'+ selectableCalDates.join(',') );
		for (var i = 0; i < blockedDates.length; ++i) { 
	                       var bdate = blockedDates[i]; 
	                       eventCalendar.addRenderer(bdate,eventCalendar.renderBodyCellRestricted); 
	                       
	     } 
    }   catch(e){
     	//console.debug('error in blackOutNonEventDates Cal----'+e.message);
     }         
	
}
function dateToLocaleString(dt, cal) {
    var wStr = cal.cfg.getProperty("WEEKDAYS_LONG")[dt.getDay()];
    var dStr = dt.getDate();
    var mStr = cal.cfg.getProperty("MONTHS_LONG")[dt.getMonth()];
    var yStr = dt.getFullYear();
    return (wStr + ", " + dStr + " " + mStr + " " + yStr);
}






