(function($) {
	$(document).ready(function(){
		$(".4cqViewType").is4cqViewType();
	});
	// update post mini
	$.fn.is4cqViewType = function () {
		$(this).click(function () {
			var viewType = $(this).val();
			if (viewType == 'history') {
				initialize();
				$(".selectOption select").attr("disabled", "");
			} else if (viewType == 'venues') {
				$(".selectOption select").attr("disabled", true);
				settingClearMap();
				clearMarkers();
				getVenuesView();
				google.maps.event.addListener(map, "dragstart", function(event) {
					clearMarkers();
				});
				google.maps.event.addListener(map, "dragend", function(event) {
					clearMarkers();
					getVenuesView();
				});
				google.maps.event.addListener(map, "dblclick", function(event) {
					clearMarkers();
					getVenuesView();
				});

			} else if (viewType == 'tips') {
				$(".selectOption select").attr("disabled", true);
				settingClearMap();
				clearMarkers();
				getTipsView();

				google.maps.event.addListener(map, "dragstart", function(event) {
					clearMarkers();
				});

				google.maps.event.addListener(map, "dragend", function(event) {
					clearMarkers();
					getTipsView();
				});
				google.maps.event.addListener(map, "dblclick", function(event) {
					clearMarkers();
					getTipsView();
				});
			}
		});
	}

	function clearMarkers(){

		if (markers.length > 0) {
			for(var i = 0; i < markers.length; ++i) markers[i].setMap(null);
		}
//		if (marker) {
//			for (var i=0; i<marker.length; i++) marker[i].setMap(null);
//		}
		if (myLatLng) {
			for (var i=0; i<myLatLng.length; i++) myLatLng[i] = null;
		}
		if (infowindow) {
			for (var i=0; i<infowindow.length; i++) infowindow[i] = null;
		}
		if (infowin) infowin.close();

		markers = [];
		myLatLng = [];
		infowindow = [];
	}

	function getVenuesView() {
		var geoLat = map.getCenter().lat();
		var geoLng = map.getCenter().lng();
		var requestData =  {geolat : geoLat, geolong : geoLng};
		var requestURL = blogURL + "/plugin/getFoursquareVenues.xml";
		var errorMSG = "";
		var htmlContent = "";
		var venues = new Array();
		$.ajax({
			type : "GET", url : requestURL, dataType : "xml", data : requestData,
			beforeSend : function () {
				PM.showMessage("처리 중입니다.", "right", "top");
			},
			success : function (resultXML) {
				errorMSG = $(resultXML).find("error").text();
				if (errorMSG == "1") {
					PM.showErrorMessage("오류가 발생했습니다.", "right", "top");
				} else {
					var num = 0;
					venues = $(resultXML).find("venue").each(function(a){
						var vid = $(this).find("> id").text();
						var permalink = "http://foursquare.com/venue/" + vid;
						var infoTitle = $(this).find("> name").text();
						var geoLat = $(this).find("> geolat").text();
						var geoLng = $(this).find("> geolong").text();
						var pathName = $(this).find("> primarycategory > fullpathname").text();
						var iconUrl = $(this).find("> primarycategory > iconurl").text();
						var iaddress = $(this).find("> address").text();

						htmlContent  = "<div class='infoTitle'><b><a href='" + permalink + "' onclick='window.open(this.href); return false;'>" + infoTitle + "</a></b></div>";
						htmlContent += "<div class='infoContent'>";
						htmlContent += iconUrl == "" ? "" : "<img src='" + iconUrl + "' alt='' border='0' /><br />";
						htmlContent += pathName == "" ? "" : "&bull;" + pathName + "<br />";
						htmlContent += iaddress == "" ? "" : "&bull;" + iaddress + "<br />";
						htmlContent += "</div>";
						htmlContent += "<div class='infoGeo'><b>GEO : " + geoLat + ", " + geoLng + "</b></div>";

						myMarker(num, geoLat, geoLng, map, infoTitle, htmlContent);
						num++;
					});


				}
			},
			error : function () {
				PM.showErrorMessage("오류가 발생했습니다.", "right", "top");
			}
		});
	}

	function getTipsView() {
		var geoLat = map.getCenter().lat();
		var geoLng = map.getCenter().lng();
		var requestData =  {geolat : geoLat, geolong : geoLng};
		var requestURL = blogURL + "/plugin/getFoursquareTips.xml";
		var errorMSG = "";
		var htmlContent = "";
		var venues = new Array();
		$.ajax({
			type : "GET", url : requestURL, dataType : "xml", data : requestData,
			beforeSend : function () {
				PM.showMessage("처리 중입니다.", "right", "top");
			},
			success : function (resultXML) {
				errorMSG = $(resultXML).find("error").text();
				if (errorMSG == "1") {
					PM.showErrorMessage("오류가 발생했습니다.", "right", "top");
				} else {
					var num = 0;
					venues = $(resultXML).find("tip").each(function(a){
						var itemid = $(this).find("> id").text();
						var vid = $(this).find("> id").text();
						var permalink = "http://foursquare.com/item/" + itemid;
						var permalink2 = "http://foursquare.com/venue/" + vid;
						var infoTitle = $(this).find("> venue > name").text();
						var infoContent = $(this).find("> text").text();
						var firstName = $(this).find("> user > firstname").text();
						var lastName = $(this).find("> user > lastname").text();
						var fullName = lastName + " " + firstName;
						var photo = $(this).find("> user > photo").text();
						var gender = $(this).find("> user > gender").text();
						var geoLat = $(this).find("> venue > geolat").text();
						var geoLng = $(this).find("> venue > geolong").text();
						var created = $(this).find("> created").text();
				
						htmlContent  = "<div class='infoTitle'><b><a href='" + permalink2 + "' onclick='window.open(this.href); return false;'>" + infoTitle + "</a></b></div>";
						htmlContent += "<div class='infoContent'>";
						htmlContent += photo == "" ? "" : "<img src='" + photo + "' alt='' width='32' height='32' border='0' /><br />";
						htmlContent += fullName == "" ? "" : "&bull;" + fullName + "(" + gender + ")<br />";
						htmlContent += infoContent == "" ? "" : "&bull;" + infoContent + "<br />";
						htmlContent += "</div>";
						htmlContent += "<div class='infoGeo'><b><a href='" + permalink + "' onclick='window.open(this.href); return false;' class='itemCreated'>" + created + "</a><br/>GEO : " + geoLat + ", " + geoLng + "</b></div>";

						myMarker(num, geoLat, geoLng, map, infoTitle, htmlContent);
						num++;
					});


				}
			},
			error : function () {
				PM.showErrorMessage("오류가 발생했습니다.", "right", "top");
			}
		});
	}


})(jQuery);
