//=================================
//		wrapBuildList
//=================================
function wrapBuildList(form, sort, page, size, mode) {
	msg_search_buildList(form, sort, page, size, mode);
}

//		ucFirst 
String.prototype.ucFirst = function () {
   return this.substr(0,1).toUpperCase() + this.substr(1,this.length);
}

//	==============================
//		Autocompletion
//	==============================
AutoCompleteElements = {};
var markQueryString = function(oResultData, sQuery, sResultMatch) {
	sQuery = sQuery.ucFirst();
	var result = sResultMatch.replace(sQuery, '<span class="mark" style="font-weight: bold;">'+sQuery+'</span>');
	result = result.replace(sQuery, '<span class="mark" style="font-weight: bold;">'+sQuery.toLowerCase()+'</span>');
	return result + " " + oResultData[3];
};

var setCityUid	= function(id)	{
	var control = document.getElementById('msg_search_city_uid');
	control.value = id;
}

function initAutoComplete(p_sId, mConfig, fFormat) {
	id = p_sId;
	if (AutoCompleteElements[id] == null) {
		// Process configuration
		var type = mConfig.type;
		var onselect = mConfig.onselect;
		var numResults = parseInt(mConfig.numresults);
		var queryLength = parseInt(mConfig.minquerylength);
		var iso = mConfig.iso;
		
		// Create DataSource
		var oDS = new YAHOO.util.XHRDataSource("index.php?id=1&eID=msg_search&extkey=msg_search&a=autocomplete&t="+ type + "&iso=" + iso + "&");
		oDS.responseType = YAHOO.util.XHRDataSource.TYPE_JSON;
		
		oDS.responseSchema = {
			resultsList : "ResultSet.Results",
			fields : [ "Label", "Uid", "Zipcode", "Countrycode" ]
		};

		// Create AutoComplete
		var oAC = new YAHOO.widget.AutoComplete(id, id + "Container", oDS);
		oAC.maxResultsDisplayed = numResults;
		oAC.minQueryLength = queryLength;
		oAC.queryQuestionMark = false;
		oAC.formatResult = fFormat;

		if(onselect != null)	{
			var defaultHandler = function(sType, aArgs) {
				eval(onselect+"("+aArgs[2][1]+")");
			}
	
			oAC.itemSelectEvent.subscribe(defaultHandler);
		}
		AutoCompleteElements[p_sId] = oAC;
	}
}


//	=================================
//			Panels
//	=================================
function getPosition(e) {
    e = e || window.event;
    var cursor = {x:0, y:0};
    if (e.pageX || e.pageY) {
        cursor.x = e.pageX;
        cursor.y = e.pageY;
    } 
    else {
        var de = document.documentElement;
        var b = document.body;
        cursor.x = e.clientX + 
            (de.scrollLeft || b.scrollLeft) - (de.clientLeft || 0);
        cursor.y = e.clientY + 
            (de.scrollTop || b.scrollTop) - (de.clientTop || 0);
    }
    return cursor;
}


function MjtPanel() {
	this.panels = new Array();
	this.manager = new YAHOO.widget.OverlayManager();

	this.NewPanel = function _NewPanel(id, renderid, top, left, width, height) {
		var properties = {
			xy : [ top, left ],
			visible : false,
			draggable : true,
			width : width + "px",
			height : height + "px",
			autofillheight : "body",
			constraintoviewport : true,
			dragOnly : true,
			autofillheight : "body",
			strings: { close: "." }
		};
		
		this.panels[id] = new YAHOO.widget.Panel(id, properties);
		this.panels[id].setHeader("<span id='" + id + "Title'></span>");
		this.panels[id].setBody("<div id='" + id
				+ "Body'></div>");
		this.panels[id].render(renderid);
		document.getElementById(renderid).className='yui-skin-sam';
		
		this.manager.register(this.panels[id]);

		// Workaround to set the panel-container width
		var obj = document.getElementById(id);
		newHeight = parseInt(height) + 35;
		obj.style.height = newHeight + "px";		
	}

	this.ShowPanel = function _ShowPanel(id) {
		this.panels[id].show();
		this.panels[id].focus();
	}
	
	this.GetCursorPosition = function _GetCursorPosition(e)	{
		var posx = 0;
		var posy = 0;
		if (!e) var e = window.event;
		if (e.pageX || e.pageY) 	{
			posx = e.pageX;
			posy = e.pageY;
		}
		else if (e.clientX || e.clientY) 	{
			posx = e.clientX + document.body.scrollLeft
				+ document.documentElement.scrollLeft;
			posy = e.clientY + document.body.scrollTop
				+ document.documentElement.scrollTop;
		}
		
		var cursor = {x:posx,y:posy};
		return cursor;
	}
	
	this.HidePanel = function _HidePanel(id) {
		this.panels[id].hide();
	}
}	

function showTherapistInformation(therapistId, obj, e)	{
	var panelId = 'therapist-information-'+therapistId;
	el = document.getElementById(panelId);
	el.style.display = 'inline';
	el.style.left = 10;
	el.style.top = 10;
	
	searchDialog = document.getElementById('msg_search_dialog_c');
	el.style.zIndex = searchDialog.style.zIndex + 1;
}

function hideTherapistInformation(therapistId){
	var panelId = 'therapist-information-'+therapistId;
	el = document.getElementById(panelId);
	el.style.display = 'none';
}

function Init() {
	MjtPanel = new MjtPanel();
	MjtPanel.NewPanel('msg_search_dialog', 'content', '200', '-20px', '1000', '435');
	MjtPanel.NewPanel('msg_search_therapist_detail', 'content', '-180px', '-20px', '500', '265');
}
YAHOO.util.Event.onDOMReady(Init);