/*******************************************************
/	expand and collapse dashboard mediations
/
/******************************************************/
	var name = "asc";
	var stars = "desc";
	var price = "asc";
	var date = "asc";
	var dir = "asc";
	var MONTH_NAMES = new Array("January","February","March","April","May","June","July","August","September","October","November","December");

	function sortquery(start, total, order) {
		if(order == null){
			if(YAHOO.util.Dom.hasClass("stars", "selected")){
				order = "stars";
			} else if(YAHOO.util.Dom.hasClass("price", "selected")){
				order = "price";
			} else {
				order = "name";
			}
		}
		var date = Dom.get("currentWeek").innerHTML;

		var category = Dom.get("MediatorCategories").value;
		
		queryDB(tz, start, total, category, date, order, dir);
	}
	
	function dateChange(newdate) {
		if(newdate == "next") { // next week
			currentWeek = nextWeek;
		} else if(newdate == "prev") { // previous week
			currentWeek = prevWeek;
		} else {
			currentWeek = newdate;
		}
		
		var myDate = new Date(currentWeek);

		myDate.setTime(myDate.getTime() + 604800000);
		nextWeek = MONTH_NAMES[myDate.getMonth()] + " " + myDate.getDate() + ", " + myDate.getFullYear();

		myDate.setTime(myDate.getTime() - 1209600000);
		prevWeek = MONTH_NAMES[myDate.getMonth()] + " " + myDate.getDate() + ", " + myDate.getFullYear();
		
		var ajaxDisplay = getElement("currentWeek");
		ajaxDisplay.innerHTML = currentWeek;
		
		$('div#current-week').show();
		
		sortquery(1, 10);

	}

	function direction(sortBy) {
		if(sortBy == "stars"){
			YAHOO.util.Dom.removeClass("price", "selected");
		} else {
			YAHOO.util.Dom.removeClass("stars", "selected");
		}
		if(YAHOO.util.Dom.hasClass(sortBy, "ascending")){
			if(YAHOO.util.Dom.hasClass(sortBy, "selected")){
				YAHOO.util.Dom.removeClass(sortBy, "ascending");
				YAHOO.util.Dom.addClass(sortBy, "descending");
				dir = "desc";
			} else {
				YAHOO.util.Dom.addClass(sortBy, "selected");
				dir = "asc";
			}
		} else {
			if(YAHOO.util.Dom.hasClass(sortBy, "selected")){
				YAHOO.util.Dom.removeClass(sortBy, "descending");
				YAHOO.util.Dom.addClass(sortBy, "ascending");
				dir = "asc";
			} else {
				YAHOO.util.Dom.addClass(sortBy, "selected");
				dir = "desc";
			}
		}
		
		sortquery(1, 10, sortBy);
	}
	
	function queryAllMediators(){
		$('div#current-week').hide();
		$("a#all-mediators").addClass("selected");
		sortquery(1, 10);
	}
	
    function createCal(id){
	
		var Event = YAHOO.util.Event, Dom = YAHOO.util.Dom, dialog, calendar;
		
		var showBtn = Dom.get("show" + id);
	
		Event.on(showBtn, "click", function(e){
			var elTarget = YAHOO.util.Event.getTarget(e);
			var cal = "cal"+elTarget.id;
			var container = "container"+elTarget.id; 
			
			// Lazy Dialog Creation - Wait to create the Dialog, and setup document click listeners, until the first time the button is clicked.
			if (!dialog) {
			
				// Hide Calendar if we click anywhere in the document other than the calendar
				Event.on(document, "click", function(e){
					var el = Event.getTarget(e);
					var dialogEl = dialog.element;
					if (el != dialogEl && !Dom.isAncestor(dialogEl, el) && el != showBtn && !Dom.isAncestor(showBtn, el)) {
						dialog.hide();
					}
				});
				
				function resetHandler(){
					// Reset the current calendar page to the select date, or 
					// to today if nothing is selected.
					var selDates = calendar.getSelectedDates();
					var resetDate;
					
					if (selDates.length > 0) {
						resetDate = selDates[0];
					}
					else {
						resetDate = calendar.today;
					}
					
					calendar.cfg.setProperty("pagedate", resetDate);
					calendar.render();
				}
				
				function closeHandler(){
					dialog.hide();
				}
				dialog = new YAHOO.widget.Dialog(container, {
					visible: false,
					context: ["show", "tl", "bl"],
					buttons: [{
						text: "Reset",
						handler: resetHandler,
						isDefault: true
					}, {
						text: "Close",
						handler: closeHandler
					}],
					draggable: false,
					close: true
				});
				dialog.setHeader("Pick A Date");
				dialog.setBody("<div id=\'"+cal+"\'></div>");
				dialog.render("calButton" + id);
				
				dialog.showEvent.subscribe(function(){
					if (YAHOO.env.ua.ie) {
						// Since we\'re hiding the table using yui-overlay-hidden, we 
						// want to let the dialog know that the content size has changed, when
						// shown
						dialog.fireEvent("changeContent");
					}
				});
			}
			
			// Lazy Calendar Creation - Wait to create the Calendar until the first time the button is clicked.
			if (!calendar) {
			
			
				calendar = new YAHOO.widget.Calendar(cal, {
					iframe: false, // Turn iframe off, since container has iframe support.
					hide_blank_weeks: true // Enable, to demonstrate how we handle changing height, using changeContent
				});
				calendar.render();
				
				calendar.selectEvent.subscribe(function(){
					if (calendar.getSelectedDates().length > 0) {
					
						var selDate = calendar.getSelectedDates()[0];
						
						// Pretty Date Output, using Calendar\'s Locale values: Friday, 8 February 2008
						var wStr = calendar.cfg.getProperty("WEEKDAYS_LONG")[selDate.getDay()];
						var dStr = selDate.getDate();
						var mStr = calendar.cfg.getProperty("MONTHS_LONG")[selDate.getMonth()];
						var yStr = selDate.getFullYear();
						
						Dom.get("date"+id).value = mStr + " " + dStr + ", " + yStr;
						$('#mmFormRequestTimesbutton'+id).removeAttr("disabled");
					}
					else {
						Dom.get("date"+id).value = "";
					}
					dialog.hide();
				});
				
				calendar.renderEvent.subscribe(function(){
					// Tell Dialog it\'s contents have changed, which allows 
					// container to redraw the underlay (for IE6/Safari2)
					dialog.fireEvent("changeContent");
				});
			}
			
			var seldate = calendar.getSelectedDates();
			
			if (seldate.length > 0) {
				// Set the pagedate to show the selected date if it exists
				calendar.cfg.setProperty("pagedate", seldate[0]);
				calendar.render();
			}
			
			dialog.show();
			
		});
	}

