package com.usai.apex;

//
//import com.google.android.gms.maps.GoogleMap;
//import com.google.android.gms.maps.MapFragment;
//import com.google.android.gms.maps.SupportMapFragment;
//import com.google.android.gms.maps.model.LatLng;
//import com.google.android.gms.maps.model.MarkerOptions;

import java.util.HashMap;
import java.util.Map;

import org.json.JSONException;
import org.json.JSONObject;

import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.GoogleMap.InfoWindowAdapter;
import com.google.android.gms.maps.GoogleMap.OnInfoWindowClickListener;
import com.google.android.gms.maps.GoogleMap.OnMarkerClickListener;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.CameraPosition;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.Marker;
import com.google.android.gms.maps.model.MarkerOptions;
import com.usai.apex.AnnouncementActivity.SearchTask;
import com.usai.util.Network;

import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
//import android.app.Fragment;
//import android.app.FragmentManager;
//import android.app.FragmentManager;
//import android.app.Fragment;
//import android.app.FragmentManager;
//import android.app.FragmentManager;
import android.os.Bundle;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.util.Log;
import android.view.Gravity;
//import android.support.v4.app.FragmentManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

public class ServiceLocationFragment extends Fragment implements
		OnMarkerClickListener, OnInfoWindowClickListener
{
	private SearchTask				m_task				= null;
	String							fragment_content	= null;
	HashMap<String, LocationDetail>	marker_detail		= new HashMap<String, LocationDetail>();
	static final CameraPosition		US					= new CameraPosition.Builder()
																.target(new LatLng(
																		39.833333,
																		-98.3583333))
																.zoom(2.5f)
																.bearing(0)
																.tilt(0)
																.build();
	private SupportMapFragment		fragment;
	private GoogleMap				map;

//	String							locations			= "Apex Maritime Co. (ORD) Inc.';'Chicago, IL\nUnited States';'42.051169,-88.0317029';'1900 E. Golf Road, Suite# 1105 Schaumburg, IL 60173';'Tel:+1 630 227-9818';'Fax:+1 630 227-9808';'Contact:\nSteve Yeh\nAileen Hsieh';'Email:\nsteve.yeh@apexshipping.com\naileen.hsieh@apexshipping.com';'Apex Maritime Co. (Hou) Inc.';'Houston, TX\nUnited States';'29.797022,-95.528827';'9610 Long Point Rd., Suite# 320 Houston, TX 77055';'Tel:+1 832 649-8198';'Fax:+1 832 649-8194';'Contact:\nAndy Pan\nTong Tong';'Email:\nandy.pan@apexshipping.com\ntong.tong@apexshipping.com';'Apex Maritime Co. (LAX) Inc.';'Los Angeles, CA\nUnited States';'34.0092709,-117.8463627';'565 Brea Canyon Road, Suite D Walnut, CA 91789';'Tel:+1 909 594-8828';'Fax:+1 909 594-9199';'Contact:\nVincent Luo\nSteven Chow';'Email:\nvincent.luo@apexshipping.com\nsteven.chow@apexshipping.com';'Apex Shipping Co., (NYC) Inc.';'New York, NY\nUnited States';'40.668404,-73.708329';'20 North Central Ave., 2nd FL, Suite# D Valley Stream, NY 11580';'Tel:516-200-9500';'Fax:516-200-9510';'Contact:\nWei Sun\nIrene Tseng';'Email:\nwei.sun@apexshipping.com\nirene.tseng@apexshipping.com';'Apex Maritime Co., Inc.';'San Francisco, CA\nUnited States';'37.646374,-122.398847';'206 Utah Ave. So. San Francisco, CA 94080';'Tel:650-589-2575';'Fax:650-589-8861';'Contact:\nJames Chu\nEsther Hui';'Email:\njames.chu@apexshipping.com\nesther.hui@apexshipping.com';'Apex Maritime Co., (PNW) Inc.';'Seattle, WA\nUnited States';'47.467763,-122.2321';'1107 S. W. Grady Way, Building B, Suite #110 Renton, WA 98055';'Tel:206-241-0771';'Fax:206-241-0823';'Contact:\nMike Langley\nRobert Bleck';'Email:\nmike.langley@apexshipping.com\nrobert.bleck@apexshipping.com';'Apex Maritime (Thailand) Co., Ltd.';'Bangkok, Thailand\nSoutheast Asia';'13.7308333,100.5241667';'138 Boonmitr Bldg., 12th Fl. Room B3, Silom Rd., Suriyawongse, Bangrak, Bangkok 10500';'Tel:+66 2 2634-3899 (Auto)';'Fax:+66 2 2634-3055\n66 2 2634-3056';'Contact:\nThikarn\nJaruwan\nSantana';'Email:\nthikarn@apexmaritime.co.th\njaruwan@apexmaritime.co.th\nsantana@apexmaritime.co.th';'PT. Apex Maritim Indonesia';'Cirebon, Indonesia\nSoutheast Asia';'-6.7166667,108.5666667';'HOTEL BUMI ASIH 2 ND FLOOR # 209 JL. WAHIDIN NO. 52 CIREBON';'Tel:+62 231 207278';'Fax:+62 231 207287';'Contact:\nSOEGIHARTO';'Email:\napex_cirebon@apex-indonesia.com';'Apex Vietnam';'Da Nang, Vietnam\nSoutheast Asia';'16.0641802,108.1873407';'3 FL, 39-41 Quang Trung Street Hai Chau Dist., Danang City, Vietnam';'Tel:+84 511 3896403';'Fax:+84 511 3823563';'Contact:\nMs. LAI';'Email:\ndanang@apexshipping.com.vn';'Apex Vietnam';'Haiphong, Vietnam\nSoutheast Asia';'20.861361,106.679802';'6th FL, 452 Le Thanh Tong Street Ngo Quyen District, Haiphong City, Vietnam';'Tel:+84 31 3750152';'Fax:+84 31 3836796';'Contact:\nMr. Nguyen Nhat Tan\nMr. Nguyen Nhat Huy';'Email:\nhaiphong@apexshipping.com.vn';'Apex Vietnam';'Hanoi, Vietnam\nSoutheast Asia';'21.0333333,105.85';'2nd FL. 108 Lo Duc Street. Hai Ba Trung District. Hanoi City, Vietnam';'Tel:+84 4 3971-9128';'Fax:+84 4 3971-9129';'Contact:\nMs. Le Thu Phuong';'Email:\nhanoi@apexshipping.com.vn';'Maritime Bank Tower ';'Ho Chi Minh City, Vietnam\nSoutheast Asia';'10.7685558,106.7012357';'Maritime Bank Tower 22th floor, room 02 180-192 Nguyen Cong Tru St., District 1 Hochiminh City, Vietnam';'Tel:+84 8 3914 2224';'Fax:+84 8 3914 3228\n3229';'Contact:\nThomas Phong';'Email:\nthomas@apexshipping.com.vn';'PT. Apex Shipping Indonesia';'Jakarta, Indonesia\nSoutheast Asia';'-6.225246,106.597423';'JIn. Bulevar Gajah Mada No. 2087 Lippo Cyberpark, Lippo Karawaci Tangerang 15811, Indonesia';'Tel:+62 21 558-2427';'Fax:+62 21 5577-1362';'Contact:\nNigolas Sjahputra\nUmi';'Email:\nnigolas@apex-indonesia.com\numi@apex-indonesia.com';'Apex Container Line (M) SDN BHD';'Malaysia\nSoutheast Asia';'1.3814563,103.4800838';'Suite 23A-05, level 23A, City Square, Jln Wong Ah Fook, 80000 Johor Bahru, Hohor, West Malaysia.';'Tel:(60) 7 222-2248 ( 6 lines)';'Fax:(60) 7 222-2278 (2 lines)';'Contact:\nFong Yee Chong';'Email:\nchloe.chong@apexshipping.com.my';'PT. Apex Maritim Indonesia';'Semarang, Indonesia\nSoutheast Asia';'-7.0016889,110.4331614';'RUKO METRO PLAZA JL. MT. HARYONO 970 BLOK C-7';'Tel:+62 24 844-7555';'Fax:+62 24 844-7666';'Contact:\nM. AYUB';'Email:\nm_ayub@apex-indonesia.com';'PT. Apex Maritim Indonesia';'Surabaya, Indonesia\nSoutheast Asia';'-7.289166,112.734398';'RUKO PERMATA BINTORO JL. TAMAN KETAMPON NO. 87 SURABAYA �C INDONESIA';'Tel:+62 31 566-1144';'Fax:+62 31 566-8289\n+62 31 566-9933';'Contact:\nIKA';'Email:\nika_setiawan@apex-indonesia.com';'Apex Maritime (Dalian) Co., Ltd.';'Dalian, China\nNorthern China';'38.9218515,121.6561693';'Room 201 No.1-3 No.112 Zhigong Street, Zhongshan Dis. Dalian, China';'Tel:+86 411 88866116 (Main)';'Fax:+86 411 82717900 (OP Dep.)';'Contact:\nWillis Yan �C +86 411 88866118\nJulia Guan �C +86 411 88866136';'Email:\nwillis.yan@apexshipping.com.cn\njulia.guan@apexshipping.com.cn';'Apex Maritime (Lianyungang) Co., Ltd.';'Lianyungang, China\nNorthern China';'34.596544,119.221282';'Room 715, No. 1 Street Building. Wangtian Haicheng Square. Xugou Lianyungang, Jiangsu, China';'Tel:+86 518 8223-3218\n+86 518 8223-3219';'Fax:+86 518 8223-3220';'Contact:\nZena Zheng';'Email:\nzena.zheng@apexshipping.com.cn';'Qingdao Apex Shipping Co., Ltd.';'Qingdao, China\nNorthern China';'36.0565576,120.3709705';'Suite 1903, No. 5 Donghai West Road Qingdao City, 266071, Shandong Province, China';'Tel:+86 532 85708982, 85708595, 85708109, 81979902';'Fax:+86 532 85708110';'Contact:\nFrank Song\nJennifer Liu';'Email:\nfrank.song@apexshipping.com.cn\njennifer.liu@apexshipping.com.cn';'Apex Maritime (Tianjin) Co., Ltd.';'Tianjin, China\nNorthern China';'39.1186127,117.1973693';'Room 3506, The Exchange North Tower, 189 Nanjin Rd. Tianjin, China';'Tel:+86 22 5886 3000';'Fax:+86 22 5886 3006\n5886 3008';'Contact:\nLucy Diao\nMandy Miao';'Email:\nlucy.diao@apexshipping.com.cn\nmandy.miao@apexshipping.com.cn';'Apex Maritime (Xian) Co., Ltd.';'Xian, China\nNorthern China';'34.2368419,108.8988572';'Room 912, Building C, Fortune Center II Gao Xin Third Rd., Hi-Tech Development Zone Xian City, Shaanxi Province,China 710075';'Tel:+86 29 65676809\n65676810\n65676811\n65676812';'Fax:+86 29 65676805';'Contact:\nKaren Huang';'Email:\nkaren.huang@apexshipping.com.cn';'Apex Maritime (Yantai) Co., Ltd.';'Yantai, China\nNorthern China';'37.5422926,121.3947602';'Room 2301, Huaxia Media Mansion, No. 53 North Ave. Yantai City,Shandong Province,China 264001';'Tel:+86 535 623-1345';'Fax:+86 535 623-4630';'Contact:\nJoey Yan';'Email:\njoey.yan@apexshipping.com.cn';'Apex Maritime (Chongqing) Co., Ltd.';'Chongqing, China\nEastern China';'29.5716184,106.531413';'Room 6, Floor 23, South Tower, Shijiyinghuang Building No. 38, North Jianxin Rd, Jiangbei District Chongqing 400020, China';'Tel:+86 23 8819 2009';'Fax:+86 23 8819 2036';'Contact:\nCindy Zeng\nLisa Xie';'Email:\ncindy.zeng@apexshipping.com.cn\nlisa.xie@apexshipping.com.cn';'Apex Maritime (Nanjing) Co., Ltd.';'Nanjing, China\nEastern China';'32.043114,118.789098';'Room D2, 18th Floor No.89 Hanzhong Road, Nanjing 210029, Jiangsu, China';'Tel:+86 25 84845871';'Fax:+86 25 84845879';'Contact:\nWinter Zhu';'Email:\nwinter.zhu@apexshipping.com.cn';'Apex Maritime (Ningbo) Co., Ltd.';'Ningbo, China\nEastern China';'29.8651695,121.5573647';'Rm 902, China Life Building No. 777 Lingqiao Road Ningbo 315010, China';'Tel:+86 574 8768 6310';'Fax:+86 574 8768 6314';'Contact:\nMr. Jason Sun';'Email:\njason.sun@apexshipping.com.cn';'Apex Maritime (Shanghai) Co., Ltd.';'Shanghai, China\nEastern China';'31.1668495,121.5430195';'RM 1211, WangJiao Plaza, 175, East Yianan Road, Shanghai 200002, China';'Tel:+86 21 6336 2020';'Fax:+86 21 53930630, 63563585, 63269809';'Contact:\nHali Huang';'Email:\nhali.huang@apexshipping.com.cn\njennifer.yu@apexshipping.com.cn';'Apex Maritime (Wuhan) Co., Ltd.';'Wuhan, China\nEastern China';'30.5952142,114.2977709';'Rm 1201, Truroll Plaza Wusheng Road Wuhan 430030, China.';'Tel:+86 27 8551 8817';'Fax:+86 27 8551 8882';'Contact:\nKatherine Lee';'Email:\nkatherine.lee@apexshipping.com.cn';'Apex Maritime (Fuzhou) Co., Ltd.';'Fuzhou, China\nSouthern China';'26.085417,119.294304';'Room A, 10\nF, Block No. 2, Hongyangxincheng Building No. 118 East Yangqiao Road Fuzhou 350001, China';'Tel:+86 591 87275810';'Fax:+86 591 87275890';'Contact:\nHelen Zhang';'Email:\nhelen.zhang@apexshipping.com.cn';'Apex Maritime (Guangzhou)';'Guangzhou, China\nSouthern China';'23.1345288,113.3079438';'Room 13B, Tower A Guangdong International Building No. 339 Huanshi Dong Road, Yuexiu District Guangzhou City, China';'Tel:+86-20-2237-8671';'Fax:+86-20-2237-8677';'Contact:\nBondy Zhang';'Email:\nbondy.zhang@apexshipping.com.cn';'Apex Maritime (HK) Co., Ltd.';'Hong Kong\nSouthern China';'22.281076,114.175695';'Room 1407-08, Harbour Centre 25 Harbour Rd. Wanchai, Hong Kong';'Tel:852-3188-9666';'Fax:852-3188-9555';'Contact:\nMartin Siu\nAndy Wong';'Email:\nmartin.siu@apexshipping.com.hk\nandy.wong@apexshipping.com.hk';'Apex Maritime (Shenzhen) Co., Ltd.';'Shenzhen, China\nSouthern China';'22.5427078,114.0710992';'Room 802, 8\nF., New China Insurance Edifice Mintian Rd., Futian District Shenzhen City, China 518026';'Tel:+86-755-8826-4001';'Fax:+86-755-8826-4014';'Contact:\nJames Zhuo\nShirley Xia';'Email:\njames.zhuo@apexshipping.com.cn\nshirley.xia@apexshipping.com.cn';'Apex Maritime (XMN) Co., Inc.';'Xiamen, China\nSouthern China';'24.462809,118.075456';'24H, International Plaza No. 8 Lujiang Rd. Xiamen 361001, China';'Tel:+86-592-261-8066';'Fax:+86-592-261-8070';'Contact:\nHelen Zhang';'Email:\nhelen.zhang@apexshipping.com.cn';'Apex Maritime (HK) Co., Ltd. (Zhongshan Branch)';'Zhongshan, China\nSouthern China';'22.501821,113.397235';'Room 07-08, 10\nF., Da Dong Yu Commerce Center Boai 5 Road, Zhongshan City, PRC';'Tel:+86 760 2 3889966\n+86 760 2 3889961';'Fax:+86 760 2 3889969';'Contact:\nAndy Shan';'Email:\nandy.shan@apexshipping.com.cn';'Apex Maritime India Pvt. Ltd.';'New Delhi\nIndia';'28.6244285,77.1349161';'CB 385 B First Floor, Ring Road Naraina, New Delhi 110028';'Tel:+91 98 11 123333';'Fax:';'Contact:\n';'Email:\ngt@apexshipping.in';'Apex Shipping B.V.';'Rotterdam, Netherlands\nEurope';'51.8637994,4.4348681';'Achterdijk 51-55 3161 EB Rhoon The Netherlands';'Tel:+31 10 8208801';'Fax:+31 10 8208802';'Contact:\nRon Reuver\nJohn Born';'Email:\nron.reuver@apexshipping.nl\njohn.born@apexshipping.nl";

	void AddMarker()
	{
		
		if (map == null)
		{

			Log.d("onResume", "get map from fragment");
			map = fragment.getMap();
			if (map == null)
				return;
		}


		try
		{
			JSONObject jsobj = new JSONObject(fragment_content);

			int count = jsobj.getInt("total");

			JSONObject objrecords = jsobj.getJSONObject("records");
			


			for (int i = 0; i < count; i++)
			{
				// offset++;
				
				
				
				JSONObject objrec = objrecords.getJSONObject("record" + i);
				String area = objrec.getString("area");
				String company = objrec.getString("company");
				String city = objrec.getString("city");
				String longitude = objrec.getString("longitude");
				String latitude = objrec.getString("latitude");
				String address = objrec.getString("address");
				String telephone = objrec.getString("telephone");
				String fax = objrec.getString("fax");
				String contact = objrec.getString("contact");
				String email = objrec.getString("email");

//				String markertext = company;
//				String city = parts[i + 1];
//				String postext = parts[i + 2];
//				String[] point = postext.split(",");
//				String address = parts[i + 3];
//				String tel = parts[i + 4];
//				String fax = parts[i + 5];
//				String contact = parts[i + 6];
//				String email = parts[i + 7];
				LocationDetail detail = new LocationDetail(company, address,
						telephone, fax, contact, email);
				// detail.Name=markertext;
				// detail.Addr=address;
				// detail.Tel=tel;
				// detail.Fax=fax;
				// detail.Contact=contact;
				// detail.Email=email;
				marker_detail.put(company, detail);
				// BitmapDescriptor
				// bd=BitmapDescriptorFactory.fromResource(R.drawable.ic_launcher);
				// Drawable da=getResources().getDrawable(R.drawable.ic_launcher);
				// Bitmap bmp=BitmapFactory.decodeResource(getResources(),
				// R.drawable.ic_map);
				// Log.d("markersize",bmp.getWidth()+","+bmp.getHeight());
				map.addMarker(new MarkerOptions()
						.icon(BitmapDescriptorFactory
								.fromResource(R.drawable.ic_marker))
						.position(
								new LatLng(Double.parseDouble(latitude), Double
										.parseDouble(longitude))).title(company)
						.snippet(city+"\n"+area));

			}
		}
		catch (JSONException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public void requestloactions()
	{

		if (m_task != null)
		{
			return;
		}

		if (fragment_content != null)
		{
			// LayoutInflater inflater = (LayoutInflater)
			// ApexTrackingApplication
			// .get_instance().getSystemService(
			// Context.LAYOUT_INFLATER_SERVICE);
			// init(fragment_content,inflater);
			return;
		}
		// mStatusMessageView.setText(R.string.str_Loading);
		// showProgress(true);
		m_task = new SearchTask();

		// TextView text_page = (TextView) view_page_footer
		// .findViewById(R.id.text_page);
		// text_page.setText("Loading...");
		// text_page.setEnabled(false);
		m_task.execute();

	}

	class SearchTask extends AsyncTask<Void, Void, Boolean>
	{
		int	errorcode;

		// boolean bfinish = false;

		@Override
		protected Boolean doInBackground(Void... params)
		{
			Log.d("SearchTask", "doInBackground");
			if (!Network.NetworkIsAvailable())

			{
				errorcode = Network.RESULT_NET_NOTAVAILABLE;
				return false;
			}
			String jstr = Network.get_servicelocation();

			if (jstr == null || jstr.length() <= 0)
			{
				// Log.d(TAG, "json is wrong");

				errorcode = Network.RESULT_NET_ERROR;
				return false;
			}

			JSONObject jsobj;
			//
			// array = new JSONArray(json);
			try
			{
				jsobj = new JSONObject(jstr);
				// if (searchresult.get_fieldscount() == 0)
				// {
				// JSONObject objfields = jsobj.getJSONObject("fields");
				// if (objfields != null)
				// searchresult.init_fields(objfields.toString());
				// }
				int count = jsobj.getInt("total");
				// if (count < limit)
				// bfinish = true;
				JSONObject objrecords = jsobj.getJSONObject("records");
				if (objrecords != null)
				{
					fragment_content = jstr;
					errorcode = Network.RESULT_TRUE;

					return true;
				}
			}
			catch (JSONException e)
			{
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			errorcode = Network.RESULT_NET_ERROR;
			return false;
		}

		@Override
		protected void onPostExecute(Boolean success)
		{
			Log.d("onPostExecute", "entry");
			m_task = null;
			// showProgress(false);

			switch (errorcode)
			{
				case Network.RESULT_NET_NOTAVAILABLE:
				{
					Toast toast = Toast.makeText(
							ApexTrackingApplication.get_instance(),
							getText(R.string.msg_connection_none),
							Toast.LENGTH_LONG);
					toast.setGravity(Gravity.CENTER, 0, 0);
					toast.show();
					break;
				}
				case Network.RESULT_NET_ERROR:
				{
					Toast toast = Toast.makeText(
							ApexTrackingApplication.get_instance(),
							getText(R.string.msg_net_error), Toast.LENGTH_LONG);
					toast.setGravity(Gravity.CENTER, 0, 0);
					toast.show();
					break;
				}
				case Network.RESULT_ERROR:
				// case Network.RESULT_RESPONSE_NULL:
				{
					Toast toast = Toast.makeText(
							ApexTrackingApplication.get_instance(),
							getText(R.string.msg_net_resulterror),
							Toast.LENGTH_LONG);
					toast.setGravity(Gravity.CENTER, 0, 0);
					toast.show();
					break;
				}

				default:
					break;
			}

			if (success)
			{
				AddMarker();

			}

			super.onPostExecute(success);
		}

		@Override
		protected void onCancelled()
		{
			m_task = null;
			// showProgress(false);
		}
	}

	class CustomWindowAdapter implements InfoWindowAdapter
	{

		View	mContents;

		CustomWindowAdapter()
		{
			mContents = getActivity().getLayoutInflater().inflate(
					R.layout.marker_info, null);
		}

		@Override
		public View getInfoContents(Marker marker)
		{

			String title = marker.getTitle();
			TextView titleUi = ((TextView) mContents
					.findViewById(R.id.tv_title));
			if (title != null)
			{
				// Spannable string allows us to edit the formatting of the
				// text.
				// SpannableString titleText = new SpannableString(title);
				// titleText.setSpan(new ForegroundColorSpan(Color.RED), 0,
				// titleText.length(), 0);
				titleUi.setText(title);
			}
			else
			{
				titleUi.setText("");
			}

			String snippet = marker.getSnippet();
			TextView snippetUi = ((TextView) mContents
					.findViewById(R.id.tv_snippet));
			if (snippet != null && snippet.length() > 12)
			{
				// SpannableString snippetText = new SpannableString(snippet);
				// snippetText.setSpan(new ForegroundColorSpan(Color.MAGENTA),
				// 0, 10, 0);
				// snippetText.setSpan(new ForegroundColorSpan(Color.BLUE), 12,
				// snippet.length(), 0);
				snippetUi.setText(snippet);
			}
			else
			{
				snippetUi.setText("");
			}
			return mContents;
		}

		@Override
		public View getInfoWindow(Marker marker)
		{

			return null;
		}

	}

	// InfoWindowAdapter adpter = new ();

	//

	@Override
	public void onDestroy()
	{
		if (m_task != null)
			m_task.cancel(false);
		super.onDestroy();
	}

	@Override
	public void onDetach()
	{
		if (m_task != null)
			m_task.cancel(false);
		super.onDetach();
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState)
	{
		requestloactions();
		return inflater.inflate(R.layout.fragment_service_location, container,
				false);
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState)
	{
		super.onActivityCreated(savedInstanceState);
		FragmentManager fm = getChildFragmentManager();
		fragment = (SupportMapFragment) fm.findFragmentById(R.id.map);
		if (fragment == null)
		{

			Log.d("onActivityCreated", "create support map fragment");
			fragment = new SupportMapFragment();// .newInstance();
			fm.beginTransaction().replace(R.id.map, fragment).commit();
		}
	}

	@Override
	public void onResume()
	{
		super.onResume();
		if (map == null)
		{

			Log.d("onResume", "get map from fragment");
			map = fragment.getMap();
			if (map != null)
				setUpMap();
		}

	}

	private void setUpMap()
	{
		Log.d("setup map", "setup map");
		// // Hide the zoom controls as the button panel will cover it.
		// map.getUiSettings().setZoomControlsEnabled(false);
		//
		// // Add lots of markers to the map.
		// addMarkersToMap();
		//
		// // Setting an info window adapter allows us to change the both the
		// contents and look of the
		// // info window.
		// map.setInfoWindowAdapter(new CustomInfoWindowAdapter());

		// Set listeners for marker events. See the bottom of this class for
		// their behavior.
//		AddMarker(fragment_content);

//		String[] parts = locations.split("';'");
//
//		for (int i = 0; i < parts.length; i += 8)
//		{
//			String markertext = parts[i];
//			String city = parts[i + 1];
//			String postext = parts[i + 2];
//			String[] point = postext.split(",");
//			String address = parts[i + 3];
//			String tel = parts[i + 4];
//			String fax = parts[i + 5];
//			String contact = parts[i + 6];
//			String email = parts[i + 7];
//			LocationDetail detail = new LocationDetail(markertext, address,
//					tel, fax, contact, email);
//			// detail.Name=markertext;
//			// detail.Addr=address;
//			// detail.Tel=tel;
//			// detail.Fax=fax;
//			// detail.Contact=contact;
//			// detail.Email=email;
//			marker_detail.put(markertext, detail);
//			// BitmapDescriptor
//			// bd=BitmapDescriptorFactory.fromResource(R.drawable.ic_launcher);
//			// Drawable da=getResources().getDrawable(R.drawable.ic_launcher);
//			// Bitmap bmp=BitmapFactory.decodeResource(getResources(),
//			// R.drawable.ic_map);
//			// Log.d("markersize",bmp.getWidth()+","+bmp.getHeight());
//			map.addMarker(new MarkerOptions()
//					.icon(BitmapDescriptorFactory
//							.fromResource(R.drawable.ic_marker))
//					.position(
//							new LatLng(Double.parseDouble(point[0]), Double
//									.parseDouble(point[1]))).title(markertext)
//					.snippet(city));
//		}

		map.setInfoWindowAdapter(new CustomWindowAdapter());
		map.moveCamera(CameraUpdateFactory.newCameraPosition(US));
		map.setOnMarkerClickListener(this);
		map.setOnInfoWindowClickListener(this);
		// map.setOnMarkerDragListener(this);

		// Pan to see all markers in view.
		// Cannot zoom to bounds until the map has a size.
		// final View mapView = getChildFragmentManager().findFragmentById(
		// R.id.map).getView();
		// if (mapView.getViewTreeObserver().isAlive())
		// {
		// mapView.getViewTreeObserver().addOnGlobalLayoutListener(
		// new OnGlobalLayoutListener()
		// {
		// @SuppressWarnings("deprecation")
		// // We use the new method when supported
		// @SuppressLint("NewApi")
		// // We check which build version we are using.
		// @Override
		// public void onGlobalLayout()
		// {
		// LatLngBounds bounds = new LatLngBounds.Builder()
		// .include(PERTH).include(SYDNEY)
		// .include(ADELAIDE).include(BRISBANE)
		// .include(MELBOURNE).build();
		// if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN)
		// {
		// mapView.getViewTreeObserver()
		// .removeGlobalOnLayoutListener(this);
		// }
		// else
		// {
		// mapView.getViewTreeObserver()
		// .removeOnGlobalLayoutListener(this);
		// }
		// map.moveCamera(CameraUpdateFactory.newLatLngBounds(
		// bounds, 50));
		// }
		// });
		// }
	}

	@Override
	public void onInfoWindowClick(Marker marker)
	{
		// Toast.makeText(getActivity(), "Click Info Window",
		// Toast.LENGTH_SHORT).show();
		Intent intent = new Intent();
		intent.setClass(getActivity(), LocationDetailActivity.class);
		String markertext = marker.getTitle();

		intent.putExtra("detail", marker_detail.get(markertext));
		// // intent.putExtra("password", password);
		// intent.putExtra("function_name", function_name);
		// intent.putExtra("behavior", Network.BEHAVIOR_SEARCH);
		startActivity(intent);

	}

	@Override
	public boolean onMarkerClick(Marker arg0)
	{
		// TODO Auto-generated method stub
		return false;
	}
}
