﻿try {
	if(typeof(CFWI)==='undefined'||!CFWI){
		throw new Error("Missing Dependency {CFWI} - Control Not Loaded");
	}else{
		CFWI.Controls.IDX.MapSearch=function(){
			if(typeof(CFWI.Controls.Map)==='undefined'||!CFWI.Controls.Map){
				throw new Error("Missing Dependency {CFWI.Controls.Map} - Control Not Loaded");
			}else{
				var m_this=null,m_map=null,m_form=null,m_webservice=null,m_data=null,defined=CFWI.Controls.Common.IsDefined,m_currentelementvalue='',m_searchhandler=null,m_loadctrl=null;
				var m_mapid=null,m_mapwidth=null,m_mapheight=null,m_mapcss=null,m_mapmode=null,m_mapstyle=null,m_dashboardsize=null,m_hidescalebar=null,m_mapiconpath=null,m_usedefaultcenterandzoom=false,m_defaultcenterlat=null,m_defaultcenterlng=null,m_defaultzoom=null,m_querystringcenterlat=null,m_querystringcenterlng=null,m_querystringzoom=null,m_usequerystringview=false;
				var m_formid=null,m_formcss=null,m_grid=null,m_gridcontainer=null,m_grid_onpopulatestart=null,m_grid_onpopulatecomplete=null,m_grid_onclearstart=null,m_grid_onclearcomplete=null;
				var m_inputs=null,m_selects=null,m_textareas=null,m_linkctrl=null,m_legend=null,m_legendcssclass='';
				var S=CFWI.Controls.Search,QS=new CFWI.Controls.QueryString(),oDE=Sys.UI.DomElement;
				function m_GetInitialValue(){ return m_currentelementvalue; }
				function m_SetInitialValue(e){ var target=e?e.target:null; if(target!==null){ m_currentelementvalue = target.value; }}
				function m_ToggleLoadingMessage(mode){
					var ri=false;
					if(defined(mode)&&typeof(mode)==='string'){
						var oDE=Sys.UI.DomElement,x=0,y=0,w=0,h=0,top=0,left=0;
						if(m_loadctrl!==null){
							if(m_loadctrl.style.position!=='absolute'){
								m_loadctrl.style.position='absolute';
							}
							if(mode==='show'){
								if(m_map.GetMap().hasOwnProperty('mapelement')){
									m_loadctrl.style.display='block';
									x=m_map.GetMap().GetLeft()+(m_map.GetMap().LatLongToPixel(m_map.GetMap().GetCenter()).x);
									y=m_map.GetMap().GetTop()+(m_map.GetMap().LatLongToPixel(m_map.GetMap().GetCenter()).y);
									w=oDE.getBounds(m_loadctrl).width;
									h=oDE.getBounds(m_loadctrl).height;
									m_loadctrl.style.left=(x-(w/2))+'px';
									m_loadctrl.style.top=(y-(h/2))+'px';
									m_loadctrl.style.zIndex='2000';
									oDE.removeCssClass(m_loadctrl,'hide');
									ri = true;
								}
							}else if(mode==='hide'){
								oDE.addCssClass(m_loadctrl,'hide');
								m_loadctrl.style.display='none';
								ri = true;
							}
						}	
					}
					return ri;
				}
				return {
					Map:{
						GetInstance:function(){
							return m_map;
						},
						SetWidth:function(v){
							if(defined(v)&&v.toString().length>0){
								var w = v;
								if(typeof(w)==='string'){
									m_mapwidth=(!isNaN(parseInt(w)))?w:400;
								}else if(typeof(w)==='number'){
									m_mapwidth=w;
								}
							}else{
								m_mapwidth=400;
							}
						},
						SetHeight:function(v){
							if(defined(v)&&v.toString().length>0){
								var h = v;
								if(typeof(h)==='string'){
									m_mapheight=(!isNaN(parseInt(h)))?h:400;
								}else if(typeof(h)==='number'){
									m_mapheight=h;
								}
							}else{
								m_mapheight=300;
							}
						},
						SetCssClass:function(v){
							if(defined(v)&&v.length>0){
								var css=v;
								if(typeof(css)==='string'){
									m_mapcss = css;
								}else{
									m_mapcss='cfwi-vemap-element';
								}
							}else{
								m_mapcss='cfwi-vemap-element';
							}
						},
						SetMapMode:function(v){
							if(defined(v)&&v.length>0){
								var m = v;
								if(typeof(m)==='string'){
									m_mapmode=m.toLowerCase();
								}else{
									m_mapmode='mode2d';
								}
							}else{
								m_mapmode='mode2d';
							}
						},
						SetStyle:function(v){
							if(defined(v)&&v.length>0){
								var s = v;
								if(typeof(s)==='string'){
									m_mapstyle=s.toLowerCase();
								}else{
									m_mapstyle='hybrid';
								}
							}else{
								m_mapstyle='hybrid';
							}
						},
						SetDashboardSize:function(v){
							if(defined(v)&&(typeof(v)==='string'&&v.length>0)){
								m_dashboardsize=v.toLowerCase();
							}else{
								m_dashboardsize='normal';
							}
						},
						HideScaleBar:function(v){
							if(defined(v)&&typeof(v)==='boolean'){
								m_hidescalebar=v;
							}else{
								m_hidescalebar=false;
							}
						},
						SetIconPath:function(v){
							if(defined(v)&&v.length>0){
								var p = v;
								if(typeof(p)==='string'&&p.length>0){
									m_mapiconpath=p;
								}else{
									m_mapiconpath='/idxThemes/corporate/content/images/icons/';
								}
							}else{
								m_mapiconpath='/idxThemes/corporate/content/images/icons/';
							}
						},
						SetLoadingMessageElement:function(id){ m_loadctrl=(typeof(id)==='string')?$get(id):(typeof(id)==='object'&&id!==null)?id:null },
						ShowLoadingMessage:function(){ return m_ToggleLoadingMessage('show'); },
						HideLoadingMessage:function(){ return m_ToggleLoadingMessage('hide'); },
						SetDefaultCenterAndZoom:function(lat,lng,zoom){
							if(typeof(lat)==='number'&&typeof(lng)==='number'&&typeof(zoom)==='number'){
								m_defaultcenterlat = lat;
								m_defaultcenterlng = lng;
								m_defaultzoom = zoom;
							}
						},
						UseDefaultCenterAndZoom:function(v){
							if(defined(v)&&typeof(v)==='boolean'){
								m_usedefaultcenterandzoom = v;	
							}else{
								return m_usedefaultcenterandzoom;
							}
						},
						SetQueryStringCenterAndZoom:function(lat,lng,zoom){
							if(typeof(lat)==='number'&&typeof(lng)==='number'&&typeof(zoom)==='number'){
								m_querystringcenterlat = lat;
								m_querystringcenterlng = lng;
								m_querystringzoom = zoom;
							}
						},
						UseQueryStringCenterAndZoom:function(v){
							if(defined(v)&&typeof(v)==='boolean'){
								m_usequerystringview = v;
							}else{
								return m_usequerystringview;
							}
						},
						SetLegenedElement:function(link,element,expandedcssclass){
							function m_legendclick(e){
								var sender=e?e.target?e.target:null:null;
								if(sender===null){sender=m_linkctrl;}
								if(oDE.containsCssClass(sender,m_legendcssclass)){
									oDE.removeCssClass(sender,m_legendcssclass);
									oDE.removeCssClass(m_legend,m_legendcssclass);
								}else{
									oDE.addCssClass(sender,m_legendcssclass);
									oDE.addCssClass(m_legend,m_legendcssclass);
								}
								if(e){
									if(e.preventDefault){ e.preventDefault(); }
									if(e.returnValue){ e.returnValue=false; }
								}
								return false;
							}
							if(defined(link)&&defined(element)&&(defined(expandedcssclass)&&typeof(expandedcssclass)==='string')){
								m_linkctrl=(typeof(link)==='string')?$get(link):(typeof(link)==='object')?link:null;
								m_legend=(typeof(element)==='string')?$get(element):(typeof(element)==='object')?element:null;
								m_legendcssclass = expandedcssclass;
								if(m_linkctrl!==null && m_legend!==null){
									setTimeout(function(){
										if(oDE.containsCssClass(m_linkctrl,m_legendcssclass)){ oDE.removeCssClass(m_linkctrl,m_legendcssclass); }
										if(oDE.containsCssClass(m_legend,m_legendcssclass)){ oDE.removeCssClass(m_legend,m_legendcssclass); }
										$addHandler(m_linkctrl,'click',m_legendclick);
									},3000);
								}
							}
						},
						Create:function(id){
							if(defined(id)&&id.length>0){
								m_mapid=id;
							}else{
								throw new Error('Map Creation Failed: HTMLDomElement ID of Map Container Not Specified.');
							}
							if(m_mapid===null){ 
								throw new Error('Map Creation Failed: HTMLDomElement ID of Map Container Not Specified.'); 
							}else{
								if(m_mapwidth===null){ m_this.Map.SetMapWidth(); }
								if(m_mapheight===null){ m_this.Map.SetMapHeight(); }
								if(m_mapcss===null){ m_this.Map.SetCssClass(); }
								if(m_mapmode===null){ m_this.Map.SetMapMode(); }
								if(m_mapstyle===null){ m_this.Map.SetStyle(); }
								if(m_dashboardsize===null){ m_this.Map.SetDashboardSize(); }
								if(m_hidescalebar===null){ m_this.Map.HideScaleBar(); }
								if(m_mapiconpath===null){ m_this.Map.SetIconPath(); }
								m_map = new CFWI.Controls.Map(m_mapid,m_mapwidth,m_mapheight,m_mapcss,m_mapmode,m_mapstyle,m_dashboardsize,m_hidescalebar,m_mapiconpath);
								if(m_this.Map.UseQueryStringCenterAndZoom()){
									m_map.UseQueryStringCenterAndZoom(true);
									m_map.SetQueryStringCenterAndZoom(m_querystringcenterlat,m_querystringcenterlng,m_querystringzoom);
								}else if(m_this.Map.UseDefaultCenterAndZoom()){
									m_map.UseDefaultCenterAndZoom(true);
									m_map.SetDefaultCenterAndZoom(m_defaultcenterlat,m_defaultcenterlng,m_defaultzoom);
								}
								m_map.Init();
							}
						},
						Populated:false
					},
					Form:{
						GetInstance:function(){
							return m_form;
						},
						AddRemoveHandler:function(events,collection,action){
							var ri=false;
							if(defined(events)&&defined(collection)&&defined(action)){
								try{
									if(collection.length>0){
										for(var i=0,L=collection.length;i<L;i++){
											for(var j=0,X=events.length;j<X;j++){
												var eventname=events[j].event;
												var handler=events[j].handler;
												if((typeof(eventname)==='string'&&eventname.length>0)&&typeof(handler)==='function'){
													if(action==='add'){
														$addHandler(collection[i],eventname,handler);
														ri=true;
													}else if(action==='remove'){
														$removeHandler(collection[i],eventname,handler);
														ri=true;
													}
												}
											}
										}
									}
								}
								catch(ex){
									throw new Error(ex.message);
								}
							}
							return ri;
						},
						AddHandler:function(args){
							/*
								args is an object literal containing the following properties:
								{
									'collection':HTMLCollection|[Array of HTMLElements] collection
									'events':[{'event':'name','handler':function},{'event':'name','handler':function}]
								}
							*/
							var events=null,collection=null;
							if(defined(args)){
								if(args.hasOwnProperty('events')){
									if(defined(args.events)&&args.events.length>0){
										events=args.events;
									}
								}
								if(events!==null){
									if(args.hasOwnProperty('collection')){
										collection=args.collection;
									}else{
										collection=[m_form.GetElement()];
									}
									ri = this.AddRemoveHandler(events,collection,'add');
								}								
							}
							return ri;
						},
						RemoveHandler:function(args){
							/*
								args is an object literal containing the following properties:
								{
									'collection':HTMLCollection|[Array of HTMLElements] collection
									'events':[{'event':'name','handler':function},{'event':'name','handler':function}]
								}
							*/
							var events=null,collection=null;
							if(defined(args)){
								if(args.hasOwnProperty('events')){
									if(defined(args.events)&&args.events.length>0){
										events=args.events;
									}
								}
								if(events!==null){
									if(args.hasOwnProperty('collection')){
										collection=args.collection;
									}else{
										collection=[m_form.GetElement()];
									}
									ri = this.AddRemoveHandler(events,collection,'remove');
								}								
							}
							return ri;
						},
						SetCssClass:function(v){
							if(defined(v)&&v.length>0){
								var css=v;
								if(typeof(css)==='string'){
									m_formcss = css;
								}else{
									m_formcss='cfwi-mapsearch-formelement';
								}
							}else{
								m_formcss='cfwi-mapsearch-formelement';
							}
						},
						Create:function(id){
							var oDE = Sys.UI.DomElement;
							if(defined(id)&&id.length>0){
								m_form = new CFWI.Controls.Form(id);
								if(m_formcss===null){ m_this.Form.SetCssClass(); }
								if(defined(oDE)){ oDE.addCssClass(m_form.GetElement(),m_formcss); }
								m_form.Init();
							}else{
								throw new Error('Form Creation Failed: HTMLDomElement ID of Form Container Not Specified.');
							}
						}
					},
					QueryString:{
						GetInstance:function(){
							return QS;
						},
						GetQueryString:function(){
							return QS.GetQueryString();
						},
						GetQueryStringAsList:function(){
							return QS.GetQueryStringAsList();
						},
						SetQueryString:function(qs){
							if(typeof(qs)==='string'){
								QS.SetQueryString(S.ResetQueryString());
								QS.SetQueryString(qs);
							}
						},
						UpdateQueryString:function(element){
							QS.SetQueryString(S.ResetQueryString());
							if(defined(element)){
								if(element.type==='text'||element.type==='textarea'){
									if(e.target.value===m_GetInitialValue()){
										return false;
									}
								}
							}
							S.BuildQueryStringFromForm(m_this.GetUpdateParameters('input'));
							S.BuildQueryStringFromForm(m_this.GetUpdateParameters('select'));
							S.BuildQueryStringFromForm(m_this.GetUpdateParameters('textarea'));
							QS.SetQueryString(S.GetQueryString());
							return true;
						},
						AppendToQueryString:function(s){
							if(typeof(s)==='string'){
								if(QS.GetQueryString().length>0){
									QS.SetQueryString(QS.GetQueryString()+'&'+s);
								}else{
									QS.SetQueryString(s);
								}
							}
						}
					},
					WebService:{
						GetInstance:function(){
							return m_webservice;
						},
						Create:function(fn){
							var ri=false;
							if(defined(fn)&&typeof(fn)==='function'){
								m_webservice = new CFWI.Controls.WebService();
								m_webservice.Reference(fn);
								ri=true;
							}
							return ri;
						}
					},
					GetData:function(){
						return m_data;
					},
					SetData:function(dataset){
						var oJSS = Sys.Serialization.JavaScriptSerializer,oData;
						if(defined(oJSS)){
							oData = oJSS.deserialize(dataset);
							if(D.hasOwnProperty('d')){
								m_data = oData.d;
							}else{
								m_data = oData;
							}
						}
					},
					GetUpdateParameters:function(t){
						if(typeof(t)==='string'){
							switch(t.toLowerCase()){
								case 'input':
									return m_inputs;
									break;
								case 'select':
									return m_selects;
									break;
								case 'textarea':
									return m_textareas;
									break;
								default:
									return null;
							}
						}
					},
					SetUpdateParameters:function(t,params){
						if(typeof(t)==='string'&&typeof(params)==='object'&&params.hasOwnProperty('type')){
							switch(t.toLowerCase()){
								case 'input':
									m_inputs=params;
									break;
								case 'select':
									m_selects=params;
									break;
								case 'textarea':
									m_textareas=params;
									break;
								default:
									null;
							}
						}					
					},
					SetSearchMethod:function(method){
						if(typeof(method)==='function'){
							m_searchhandler = method;
						}
					},
					Search:function(e){
						var m_element=e?e.target?e.target:null:null;
						var m_onstart = m_webservice.OnStartEventHandler();
						var oWS = m_webservice.Reference();
						if(oWS){
							if(typeof(m_onstart)==='function'){ m_onstart(m_webservice); }
							if(!m_webservice.Cancel()){
								var m_updated=m_this.QueryString.UpdateQueryString(m_element);
								if(m_updated){
									if(typeof(m_searchhandler)==='function'){
										m_searchhandler(m_webservice);
									}else{
										throw new Error("Search Aborted! No search method was provided.  Set the search method using (CFWI.Controls.IDX.MapSearch)Instance.SetSearchMethod(Method);");
									}
								}
							}else{
								m_webservice.Cancel(false);
							}	
						}
					},
					GetControlReference:function(){
						return m_this;
					},
					SetControlReference:function(){
						m_this = this;
					},
					Init:function(){
						this.SetControlReference();
						
					}
				}
			}
		}
	}
}
catch(ex){
	
}