﻿try {
	if(typeof(CFWI)==='undefined'||!CFWI){
		throw "LoadException";
	}else{
		//document.write('<script type=\"text/javascript\" src=\"http://dev.virtualearth.net/mapcontrol/mapcontrol.ashx?v=6.2\"></script>');
		CFWI.Controls.Map=function(id,w,h,css,m,s,db,sb,p){
			var m_mapelement=(typeof(id)==='string')?$get(id)?$get(id):document.getElementById(id):(typeof(id)==='object')?id:null;
			var m_this=null,m_map=null,m_width=0,m_height=0,m_cssclass='',m_mode='',m_style='',m_dashboard='',m_scalebar=null,m_path='',m_data=null,m_firstrun=true;
			var defined=CFWI.Controls.Common.IsDefined,m_usedefaultcenterandzoom=false,m_defaultcenterpoint=null,m_defaultzoom=null,m_usequerystringview=false,m_querystringcenter=null,m_querystringzoom=null;
			if(defined(w)){if(typeof(w)==='string'){m_width=(!isNaN(parseInt(w)))?w:400;}else if(typeof(w)==='number'){m_width=w;}}else{m_width=400;}
			if(defined(h)){if(typeof(h)==='string'){m_height=(!isNaN(parseInt(h)))?h:300;}else if(typeof(h)==='number'){m_height=h;}}else{m_height=300;}
			if(defined(css)){if(typeof(css)==='string'){m_cssclass = css;}else{m_cssclass='cfwi-vemap-element';}}else{m_cssclass='cfwi-vemap-element';}
			if(defined(m)){if(typeof(m)==='string'){m_mode=m.toLowerCase();}else{m_mode='mode2d';}}else{m_mode='mode2d';}
			if(defined(s)){if(typeof(s)==='string'){m_style=s.toLowerCase();}else{m_style='hybrid';}}else{m_style='hybrid';}
			if(defined(db)){if(typeof(db)==='string'){m_dashboard=db.toLowerCase();}else{m_dashboard='normal';}}else{m_dashboard='normal';}
			if(defined(sb)){if(typeof(sb)==='boolean'){m_scalebar=sb;}else{m_scalebar=false;}}else{m_scalebar=false;}
			if(defined(p)){if(typeof(p)==='string'&&p.length>0){m_path=p;}else{m_path='/idxThemes/corporate/content/images/icons/';}}else{m_path='/idxThemes/corporate/content/images/icons/';}
			return {
				GetElement:function(){
					return m_mapelement;
				},
				IsFirstRun:function(v){
					if(defined(v)&&typeof(v)==='boolean'){
						m_firstrun = v;
					}else{
						return m_firstrun;
					}
				},
				GetMap:function(){
					return m_map;
				},
				SetMap:function(id){
					var ri=false;
					if(typeof(id)==='string'){
						m_map = new VEMap(id);
						ri=true;
					}
					return ri;
				},
				GetControlReference:function(){
					return m_this;
				},
				SetControlReference:function(){
					m_this = this;
				},
				GetData:function(){
					return m_data;
				},
				SetData:function(dataset,serialize){
					var oJSS = CFWI.Controls.Common,D=null;
					if(defined(oJSS)){
						if(typeof(serialize)==='boolean'&&serialize===true){
							D = oJSS.Deserialize(dataset);
						}else{
							D = dataset;
						}
						if(D.hasOwnProperty('d')){
							m_data = D.d;
						}else{
							m_data = D;
						}
					}
				},
				SetDefaultCenterAndZoom:function(lat,lng,zoom){
					if(typeof(lat)==='number'&&typeof(lng)==='number'&&typeof(zoom)==='number'){
						m_defaultcenterpoint = new VELatLong(lat,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_querystringcenter = new VELatLong(lat,lng);
						m_querystringzoom = zoom;
					}
				},
				UseQueryStringCenterAndZoom:function(v){
					if(defined(v)&&typeof(v)==='boolean'){
						m_usequerystringview = v;	
					}else{
						return m_usequerystringview;
					}
				},
				AddRemoveHandler:function(eventname,handler,action){
					var ri=false;
					if(defined(eventname)&&defined(handler)&&defined(action)){
						var allowed ='onkeypress|onkeydown|onkeyup|onclick|ondoubleclick|onmousemove|onmousedown|onmouseup|onmouseover|onmouseout|onmousewheel|';
							allowed+='onchangemapstyle|onchangeview|onendpan|onendzoom|onerror|oninitmode|onmodenotavailable|onobliquechange|onobliqueenter|onobliqueleave|onresize|onstartpan|onstartzoom|ontokenerror|ontokenexpire';
						if(m_map.hasOwnProperty('mapelement')){
							var found = (allowed.indexOf(eventname.toLowerCase())>-1);
							if(found){
								if(action.toLowerCase()==='add'){
									m_map.AttachEvent(eventname.toLowerCase(),handler);
									ri = true;
								}else if(action.toLowerCase()==='remove'){
									m_map.DetachEvent(eventname.toLowerCase(),handler);
									ri = true;
								}
							}
						}
					}
					return ri;
				},
				AddHandler:function(eventname,handler){
					if(typeof(eventname)==='string'&&typeof(handler)==='function'){
						return this.AddRemoveHandler(eventname,handler,'add');
					}
				},
				RemoveHandler:function(eventname,handler){
					if(typeof(eventname)==='string'&&typeof(handler)==='function'){
						return this.AddRemoveHandler(eventname,handler,'remove');
					}
				},
				Init:function(){
					this.SetControlReference();
					var m=this.GetElement();
					var oDE=Sys.UI.DomElement;
					if(defined(m)){
						m.style.width=m_width+'px';
						m.style.height=m_height+'px';
						if(defined(oDE)){
							if(!(oDE.containsCssClass(m,m_cssclass))){
								oDE.addCssClass(m,m_cssclass);
							}
						}
						this.SetMap(m.id);
						this.Load();
					}
				},
				Load:function(){
					var d=this.GetData(),mode=(m_mode.toLowerCase()==='mode3d')?VEMapMode.Mode3D:VEMapMode.Mode2D;
					var dashboard=(m_dashboard.toLowerCase()==='small')?VEDashboardSize.Small:(m_dashboard.toLowerCase()==='tiny')?VEDashboardSize.Tiny:VEDashboardSize.Normal;
					try {
						m_map.SetDashboardSize(dashboard);
						if(m_this.UseDefaultCenterAndZoom()){
							m_map.LoadMap(m_defaultcenterpoint,m_defaultzoom);
						}else if(m_this.UseQueryStringCenterAndZoom()){
							m_map.LoadMap(m_querystringcenter,m_querystringzoom);
						}else{
							m_map.LoadMap();
						}
						if(m_scalebar!==null&&m_scalebar===true){
							m_map.HideScalebar();
						}
						m_map.SetMapMode(mode);
						switch(m_style){
							case 'road':
								m_map.SetMapStyle(VEMapStyle.Road);
								break;
							case 'hybrid':
								m_map.SetMapStyle(VEMapStyle.Hybrid);
								break;
							case 'aerial':
								m_map.SetMapStyle(VEMapStyle.Aerial);
								break;
							default:
								m_map.SetMapStyle(VEMapStyle.Hybrid);
								break;
						}
						this.Populate(d,false);
					}
					catch(ex){
						throw new Error(ex.message);
					}
				},
				Update:function(addNewLayer){
					var ri=false,d=this.GetData(),L=(typeof(addNewLayer)==='boolean');
					try {
						if(defined(m_map)&&m_map.hasOwnProperty('mapelement')){
							//m_map.Clear();
							m_map.DeleteAllShapes();
							m_map.DeleteRoute();
							m_map.DeleteAllShapeLayers();
						}
						if(L){
							ri = this.Populate(d,addNewLayer);
						}else{
							ri = this.Populate(d,false);
						}
					}
					catch(ex){
						alert(ex.message);
					}
					finally{
						return ri;
					}
				},
				Populate:function(data,addnewlayer){
					if(!defined(m_map)){
						throw new Error('Map Not Provided');
					}else{
						
						var ri=false,b=false,d=data?(typeof(data)==='object'&&data!==null)?data:null:null,m_item=null,m_layer=null,m_point=null,m_points=[];
						if(d!==null){
							if(defined(addnewlayer)){
								if(typeof(addnewlayer)==='boolean'&&addnewlayer===true){
									m_layer = new VEShapeLayer();
									m_map.AddShapeLayer(m_layer);
									b=true;
								}
							}
							if(d!==null&&d.length>0){
								m_map.DeleteAllShapeLayers();
								for(var i=0,L=d.length;i<L;i++){
									m_item=d[i];
									m_point=this.AddPoint(m_item);
									if(m_point!==null){
										if(b){
											m_layer.AddShape(m_point);
										}else{
											m_map.AddShape(m_point);
										}
										m_points.push(new VELatLong(m_point.GetPoints()[0].Latitude,m_point.GetPoints()[0].Longitude));
									}
								}
							}
							if(m_points.length>0){
								ri=true;
								if(m_firstrun){
									if(!m_this.UseDefaultCenterAndZoom()&&!m_this.UseQueryStringCenterAndZoom()){
										m_map.SetMapView(m_points);
									}
									m_firstrun=false;
								}
							}
						}
					}
					return ri;
				},
				AddPoint:function(p){
					/* 
						p is a custom 'point' object containing the following properties
						p.Latitude = decimal Latitude
						p.Longitude = decimal Longitude
						p.Title = string Title
						p.ImageFile = string ImageFileUrl
						p.Description = string Description
						p.Url = string MoreInfoUrl
						p.IconFile = string IconFileName
						p.IconCssClass = string IconCssClass
						p.IconZIndex = int ZIndex
						p.ID = int BusinessID
					*/
					var ri=null,m_shape=null,m_temp=null,msg='';
					var m_css=(typeof(m_cssclass)==='string'&&m_cssclass.length>0)?' cfwi-'+m_cssclass:'';
					var m_html='<div class="cfwi-mapdialog-container{1}">{0}</div>';
					var m_title='<h5 class="cfwi-mapdialog-title">{0}</h5>';
					var m_imagefile='<div class="cfwi-mapdialog-photo"><a target="_blank" href="http://{2}" onclick="return Business_GetURL({1},this)"><img src="/DesktopModules/WaterPlayUSAMap/Components/ImageResize.aspx?id={0}&width=70" /></a></div>';
					var m_description='<div class="cfwi-mapdialog-description">{0}</div>';
					var m_url='<a target="_blank" class="cfwi-mapdialog-moreinfolink" href="http://{0}" onclick="return Business_GetURL({1},this)">more info</a>';
					var m_icon='<img src="{0}{1}" class="cfwi-mapicon{2}" />';
					if((p.hasOwnProperty('Latitude')&&p.Latitude.toString().length>0)&&(p.hasOwnProperty('Longitude')&&p.Longitude.toString().length>0)){
						//Create Shape
						m_shape=new VEShape(VEShapeType.Pushpin,new VELatLong(p.Latitude,p.Longitude));
						//Populate Title & Set Image Alt Property
						if(p.hasOwnProperty('Title')&&p.Title.length>0){
							m_shape.SetTitle(m_title.replace('{0}',p.Title));
							//m_imagefile=m_imagefile.replace('{1}',p.Title);
						}else{
							//m_imagefile=m_imagefile.replace('{1}','');
						}
						
						//Populate More Info Url & Set Image Link Url Property
						if(p.hasOwnProperty('URL')&&p.URL.length>0){
							m_url = m_url.replace('{0}',p.URL).replace('{1}',p.ID);
							m_imagefile = m_imagefile.replace('{2}',p.URL).replace('{1}',p.ID);
						}else{
							m_url = '';
							m_imagefile = m_imagefile.replace('{2}','#').replace('{1}',p.ID);
						}
						
						//Populate Image File Path
						if(p.hasOwnProperty('Logo')&&p.Logo.length>0){
							m_imagefile = m_imagefile.replace('{0}',p.Logo);
						}else{
							m_imagefile = '';
						}
						
						//Populate Description
						if(p.hasOwnProperty('Description')&&p.Description.length>0){
							m_description = m_description.replace('{0}',p.Description);
						}else{
							m_description = m_description.replace('{0}','');
						}
						
						//Populate Icon File (custom pushpin icon)
						if(p.hasOwnProperty('IconFile')&&p.IconFile.length>0){
							m_icon = m_icon.replace('{0}',m_path).replace('{1}',p.IconFile);
							if(p.hasOwnProperty('IconCssClass')&&p.IconCssClass.length>0){
								m_icon=m_icon.replace('{2}',' '+p.IconCssClass);
							}else{
								m_icon=m_icon.replace('{2}','');
							}
						}
						
						//Build Total Dialog Data & Populate Shape's Description Property
						m_html = m_html.replace('{0}',m_url+m_imagefile+m_description).replace('{1}',m_css);
						m_shape.SetDescription(m_html);
						
						//Set Custom Icon
						m_shape.SetCustomIcon(m_icon);
						if(p.hasOwnProperty('IconZIndex')&&p.IconZIndex.toString().length>0){
							m_shape.SetZIndex(p.IconZIndex);	
						}
						
						//Set Return Item = Current Shape
						ri=m_shape;
					}
					return ri;
				}
			}
		}
	}
}
catch(ex){
	var exmsg = '';
	switch(ex){
		case 'LoadException':
			exmsg = 'Missing Dependency {CFWI} - Control Not Loaded';
			break;
		default:
			exmsg = ex.message;
			break;
	}
	alert(exmsg);
}                             