var map = null;

function createMarker(latlng, html) {
	var tinyIcon = new GIcon();
	tinyIcon.image = 'images/marker-blue.png';
//	tinyIcon.image = 'images/blue.png';
//	tinyIcon.iconSize = new GSize(25, 30);
	tinyIcon.iconSize = new GSize(57, 57);
	tinyIcon.iconAnchor = new GPoint(28, 57);
	tinyIcon.infoWindowAnchor = new GPoint(28, 28);
	var markerOptions = { icon:tinyIcon };
	var marker = new GMarker(latlng, markerOptions);
//	var marker = new GMarker(latlng);
	
	GEvent.addListener(marker, 'click', function() {
		var markerHTML = html;
		marker.openInfoWindowHtml(markerHTML);
	});
	return marker;
}

function limitText(e){
	var limitNum = 140;
	//cross browser handling of events
	if (window.event) {
		trgSrc = window.event.srcElement;//ie
	} else if (e) {
		trgSrc = e.target;
 	}	
	var totLen = document.getElementById(trgSrc.id).value.length;
	if(totLen > limitNum){
		document.getElementById(trgSrc.id).value = document.getElementById(trgSrc.id).value.substring(0, limitNum)
	}
	totLen = document.getElementById(trgSrc.id).value.length;
//	document.getElementById('countdown').innerHTML = limitNum - totLen;//Changing the countdown element
	document.getElementById('countdown').innerHTML = limitNum - totLen;
}

/*function select_all(checkbox_name){
	$("input[@name='" + checkbox_name + "'][type='checkbox']").attr('checked', true);
}
function deselect_all(checkbox_name){
	$("input[@name='" + checkbox_name + "'][type='checkbox']").attr('checked', false);
}*/
/*function selectAccount(){
//	showHideDiv("follower_list");
	$("#follower_list").html("");
//	var checked = $('.accChk:checked');
	var checked = $('input[name=accChkBox]:checked');
	if(checked.length != 0){
		var qStr = "";
		for (i=0;i<checked.length;i++)
		{    
			qStr = qStr + jQuery.trim(checked[i].id.substring(6)) + ",";
		}
		$.getJSON("ajax_functions.php?twitter_id=" + qStr + "&get_follower=yes" , function(data) {
			var followerHtml = "";
			followerHtml += "<input type='checkbox' name='followerChkBoxSelectAll' id='chk_all_follower_acc' class='followerChk' onclick='select_all(\"followerChkBox\", \"chk_all_follower_acc\")' /><p class='chkTxt'  style='font-weight:bold'>Check/Uncheck All</p><br class='spacer' />";
			for(key in data){
				followerHtml += "<input type='checkbox' name='followerChkBox' id='chk_f_" + data[key]["follower"] + "' class='followerChk' /><p class='chkTxt'>" + data[key]["follower"] + "</p><br class='spacer' />";
			}
			$("#follower_list").html(followerHtml);
//			showHideDiv("follower_list");
		});
	}else{
		alert("Please Select an Account to Fetch Followers");
	}
}
function postTweet(){
//	showHideDiv("follower_list");
	var post_type = $('input:radio[name=post_type]:checked').val();
//	var accChecked = $('.accChk:checked');
	var accChecked = $('input[name=accChkBox]:checked');
	if(accChecked.length != 0){
		var qStr = "";
		for (i=0;i<accChecked.length;i++)
		{    
			qStr = qStr + jQuery.trim(accChecked[i].id.substring(6)) + ",";
		}
		var checked = $('input[name=followerChkBox]:checked');
		if((checked.length != 0) || (post_type != "dm")){
			var fStr = "";
			for (i=0;i<checked.length;i++)
			{    
				fStr = fStr + "'" + jQuery.trim(checked[i].id.substring(6)) + "',";
			}
//			alert("ajax_functions.php?twitter_id=" + qStr + "&follower_scr_name=" + fStr + "&post_type=" + post_type + "&status=" + $.URLEncode($("#tweet_text").val()));
			$.ajax({
				type: "POST",
				dataType: "json",
				url: "ajax_functions.php",
				data: "twitter_id=" + qStr + "&follower_scr_name=" + fStr + "&post_type=" + post_type + "&status=" + $("#tweet_text").val(),
				success: function(data){
					if(data["error_message"] === undefined){
						$("#tweet_text").val("");
					}else{
						alert(data["error_message"]);
					}
//					$('#countdown').html(140);
				},
				error: function(){
					$("#tweet_text").val("");
				}
			});
		}else{
			alert("Please Select a Follower to Send Direct Message");
		}
	}else{
		alert("Please Select an Account to Tweet/DM");
	}
}*/

function markOnMap(){
	$.ajax({
		type: "POST",
		dataType: "json",
		url: "ajax_functions.php",
		data: "reviewFormNeighbourhood=" + $("#reviewFormNeighbourhood").val() + "&reviewFormCity=" + $("#reviewFormCity").val() + "&reviewFormState=" + $("#reviewFormState").val() + "&reviewFormCountry=" + $("#reviewFormCountry").val() + "&post_review=true",
		success: function(data){
			if(data["error_message"] == ""){
				map = new GMap2(document.getElementById("map_canvas"));
				map.addControl(new GSmallMapControl());
				map.addControl(new GMapTypeControl());
				
				$("#review_loc_lat").val(data["latitude"]);
				$("#review_loc_long").val(data["longitude"]);

				var latlng = new GLatLng(parseFloat(data["latitude"]),parseFloat(data["longitude"]));
				var marker = new GMarker(latlng, {draggable:true});
				GEvent.addListener(marker, "dragend", function() {
					$("#review_loc_lat").val(marker.getPoint().lat());
					$("#review_loc_long").val(marker.getPoint().lng());
				});
				map.addOverlay(marker);
				map.setCenter(latlng, 5);
			}else{
				alert(data["error_message"]);
			}
//					$('#countdown').html(140);
		},
		error: function(){
			$("#tweet_text").val("");
		}
	});
}

function changeCompareList(type,property_id){
	var checkedType = false;
	if(type == "list"){
		if($("#chk_cmp_" + property_id).is(":checked")){
			$("#chk_gcmp_" + property_id).attr("checked", true);
			checkedType = true;
		}else{
			$("#chk_gcmp_" + property_id).attr("checked", false);
		}
	}else{
		if($("#chk_gcmp_" + property_id).is(":checked")){
			$("#chk_cmp_" + property_id).attr("checked", true);
			checkedType = true;
		}else{
			$("#chk_cmp_" + property_id).attr("checked", false);
		}
	}
//	alert("ajax_functions.php?compare_property_id=" + property_id + "&add_to_list=" + checkedType);
	$.ajax({
		type: "POST",
		dataType: "json",
		url: "ajax_functions.php",
		data: "compare_property_id=" + property_id + "&add_to_list=" + checkedType,
		success: function(data){
		},
	});
}

function removeFromCompareList(property_id){
	$.ajax({
		type: "POST",
		dataType: "json",
		url: "ajax_functions.php",
		data: "compare_property_id=" + property_id + "&add_to_list=false",
		success: function(data){
			window.open('compare.php', target='_self');
		},
		error: function(){
			window.open('compare.php', target='_self');
		}
	});
}

function view_listing(listing_id){
	window.open('view-detail.php?ref=' + listing_id, target='_self');
}
function update_visit_time(){
	$.ajax({
		type: "POST",
		url: "ajax_functions.php",
		data: "update_visit_time=true",
		success: function(data){
			setTimeout(update_visit_time, 180000);
		},
		error: function(){
		}
	});
}
function sendMessage(){
//	alert("ajax_functions.php?receiving_user=" + $("#receiving_user").val() +  "&message_body=" + $("#message_body").val());
	$.ajax({
		type: "POST",
		url: "ajax_functions.php",
		data: "receiving_user=" + $("#receiving_user").val() +  "&message_body=" + $("#message_body").val(),
		success: function(data){
			alert("Your message has been sent");
			$.modal.close();
		},
		error: function(){
			alert("There was some problem sending your message. Please try after some time.");
		}
	});
}
function sendMessage2(){
	$.ajax({
		type: "POST",
		url: "inc.mail-view-details.php",
		data: "receiving_user=" + $("#receiving_user").val() +  "&message_body=" + $("#message_body").val(),
		success: function(data){
			alert($("#receiving_user").val()+'and the msg is'+$("#message_body").val());
			$.modal.close();
		},
		error: function(){
			alert("There was some problem sending your message. Please try after some time.");
		}
	});
}
function submitReview(){
	$.ajax({
		type: "POST",
		url: "ajax_functions.php",
		data: "listing_id=" + $("#listing_id").val() +  "&review_text=" + $("#review_text").val(),
		success: function(data){
			alert("Thank you for your review");
			$.modal.close();
			document.frm_review.submit();
		},
		error: function(){
			alert("There was some problem posting your review. Please try after some time.");
		}
	});
}

function contact_us(){
	$.ajax({
		type: "POST",
		url: "contact_us.php",
		data: "message=" + $("#listing_id").val() +  "&review_text=" + $("#review_text").val(),
		success: function(data){
			alert("Thank you for contacting us. We will contact you shortly.");
			$.modal.close();
			document.frm_review.submit();// post the form here
		},
		error: function(){
			alert("There was some problem posting your message. Please try after some time.");
		}
	});
}
