﻿$(document).ready(function () {
    $.ga.load('UA-11347170-1');
	var gLocalSearch;
	var geocoder;
	var gMap;
	var gBounds;
	var gInfoWindow;
	var gSelectedResults = [];
	var gCurrentResults = [];
	var gSearchForm;
	var gSearchTerm;
	var gMarkerListing;
	var gPoiIcon;
	var gPoiIconSel;
	var gPoiShadow;
    fullScreen();
	InitMap();
	InitSendMessage();
	$("#divFloorPlan").miniZoomPan({
		sW: 988,
		sH: 550,
		lW: 1500,
		lH: 1300
	});
	$('li a.group').click(function(){
		$('[id^="tblDetail_"]:visible').hide();
		$('#imgDetails:hidden').fadeIn('fast');
		var parent = $(this).parents('li');
		if (parent.hasClass('active')) {
			$('#h3Details').html($('#hidH3').val());
			parent.find('ul').slideUp('fast', function(){
				parent.removeClass('active');
			});
		}
		else {
			$('#h3Details').html($(this).attr('title'));
			$('li.group.active').find('ul').slideUp('fast', function(){
				$('li.group.active').removeClass('active');	
			});
			parent.find('ul').slideDown('fast', function(){
				parent.addClass('active');
			});
		}
	});
	$('li a.detail').click(function(){
		$('#h3Details').html($(this).attr('title'));
		var detailId = $(this).attr('id').split('aDetail_')[1];
		$('#imgDetails:visible').hide();
		$('[id^="tblDetail_"]:visible').hide();
		$('#tblDetail_' + detailId).fadeIn('fast');
	});
	$('[id^="aFloor_"]').click(function(){
		var floor = $(this).attr('id').split('aFloor_')[1];
		var imgSrc = $("#imgFloorPlan").attr("src");
		var imgBase = imgSrc.slice(0, imgSrc.length - 7)
		$("#imgFloorPlan").attr("src", imgBase + floor + "_s.jpg");
	});
    $("div.marquisItem").hoverIntent(function () {
        $(this).find("img.marquisImage").animate({
            top: "295px"
        }, {
            queue: false,
            duration: 250
        });
    }, function () {
        $(this).find("img.marquisImage").animate({
            top: "0px"
        }, {
            queue: false,
            duration: 400
        });
    });
    $("#txtEmailFriend").blur(function () {
        if ($(this).val().toLowerCase() == "") {
            $(this).val("Email address...");
        }
    });
    $("#txtEmailFriend").focus(function () {
        if ($(this).val().toLowerCase() == "email address...") {
            $(this).val("");
        }
    });
    $("#aEmailFriend").click(function(){
		if ($(this).hasClass('active')) {
			$('#divEmailFriend').slideUp('fast');
			$("#txtEmailFriend").val("Email address...");
			$(this).removeClass('active');
		}
		else {
			if ($("#txtEmailFriend").val().toLowerCase() == "email address...") {
				$("#txtFriend_Email").val("");
			}
			else {
				$("#txtFriend_Email").val($("#txtEmailFriend").val());
			}
			$('#divEmailFriend').slideDown('fast');
			$(this).addClass('active');
		}
		return false;
	});
	    $("#aSubmitEmailFriend").click(function(){
			EmailFriend($(this));
			return false;
		});
    $('[id^="aPhotoGroup_"]').click(function (){
        var album = $(this).attr("id").split("aPhotoGroup_")[1];
        sspLoadAlbum(album);
        return false;
    });
});
function InitSendMessage(){
	if ($('#aSendMessage').length > 0) {
		BindSendMessage();
	}
}
function BindSendMessage(){
	$("#aSendMessage").bind('click', function(){
		$("#spaSendYourMessageError").hide();
		var sendData = new Object();
		sendData.__type = "ContactSendFields:#WCF";
		sendData.Subject = "Info Request from Ultra Contact Page";
		sendData.FirstName = $("#txtContact_Fname").val();
		sendData.LastName = $("#txtContact_Lname").val();
		sendData.Email = $("#txtContact_Email").val();
		sendData.Phone = $("#txtContact_Phone").val();
		sendData.Comments = $("#txtContact_Comments").val();
		var data = new Object();
		data.sendData = sendData;
		var sdata = JSON.stringify(data);
		$.ajax({
			url: "/SiteService.svc/SendContactMessage",
			type: "POST",
			data: sdata,
			dataType: "json",
			contentType: "application/json; charset=utf-8",
			error: function(xhr, desc, exception){
				alert(xhr.responseText);
			},
			success: function(obj){
				obj = obj.d;
				if (obj.Status.toLowerCase() == "invalid") {
					$('#spaSendYourMessageError').html(obj.Message).fadeIn('fast');
				}
				else 
					if (obj.Status.toLowerCase() == "success") {
						$("#txtContact_Fname").val('');
						$("#txtContact_Lname").val('');
						$("#txtContact_Email").val('');
						$("#txtContact_Phone").val('');
						$("#txtContact_Comments").val('Please contact me regarding this property ASAP.');
						$('#spaSendYourMessageError').html(obj.Message).addClass('success').fadeIn('fast');
						setTimeout(function(){
							$('#spaSendYourMessageError').fadeOut(5000, function(){
								$(this).removeClass('success');
							});
						}, 2000);
					}
			}
		});
		return false;
	});
}
function InitMap(){
	if ($('#divMap').length > 0) {
		BindMap();
		BindMapAmenities();
	}
}
function BindMap(){
	var fLat = parseFloat($('#hidLati').val());
	var fLon = parseFloat($('#hidLongi').val());
	var loc = $('#hidLoca').val();
	var marker;
	var latlng;
	var gHouseIcon = new google.maps.MarkerImage("http://www.orlareal.com/images/mapHouse.png", new google.maps.Size(40, 48), new google.maps.Point(0, 0), new google.maps.Point(20, 48));
	var gHouseShadow = new google.maps.MarkerImage("http://www.orlareal.com/images/shadow-mapHouse.png", new google.maps.Size(65, 48), new google.maps.Point(0, 0), new google.maps.Point(20, 48));
	if ((fLat == 0) && (fLon == 0)) {
		geocoder = new google.maps.Geocoder();
		geocoder.geocode({
			'address': loc
		}, function(results, status){
			if (status == google.maps.GeocoderStatus.OK) {
				var myOptions = {
					zoom: 9,
					center: results[0].geometry.location,
					mapTypeControl: true,
					mapTypeControlOptions: {
						style: google.maps.MapTypeControlStyle.DROPDOWN_MENU
					},
					navigationControl: true,
					navigationControlOptions: {
						style: google.maps.NavigationControlStyle.SMALL
					},
					mapTypeId: google.maps.MapTypeId.ROADMAP
				}
				gMap = new google.maps.Map(document.getElementById('divMap'), myOptions);
				gMarkerListing = new google.maps.Marker({
					icon: gHouseIcon,
					shadow: gHouseShadow,
					map: gMap,
					position: results[0].geometry.location,
					title: loc
				});
			}
			else {
				alert("Geocode was not successful for the following reason: " + status);
			}
		});
	}
	else {
		latlng = new google.maps.LatLng(fLat, fLon);
		var myOptions = {
			zoom: 9,
			center: latlng,
			mapTypeControl: true,
			mapTypeControlOptions: {
				style: google.maps.MapTypeControlStyle.DROPDOWN_MENU
			},
			navigationControl: true,
			navigationControlOptions: {
				style: google.maps.NavigationControlStyle.SMALL
			},
			mapTypeId: google.maps.MapTypeId.ROADMAP
		}
		gMap = new google.maps.Map(document.getElementById('divMap'), myOptions);
		gMarkerListing = new google.maps.Marker({
			icon: gHouseIcon,
			shadow: gHouseShadow,
			map: gMap,
			position: latlng,
			title: loc
		});
	}
	return false;
}
function BindMapAmenities(){
	var fLat = parseFloat($('#hidLati').val());
	var fLon = parseFloat($('#hidLongi').val());
	var loc = $('#hidLoca').val();
	var latlng;
	$('a.amenities').bind('click', function(){
		var searchTerm = $(this).html();
		var poiImage = "http://www.orlareal.com/images/poi-" + searchTerm.replace(/ /g, "-") + ".png";
		gPoiIcon = new google.maps.MarkerImage(poiImage, new google.maps.Size(32, 37), new google.maps.Point(0, 0), new google.maps.Point(16, 37));
		gPoiIconSel = new google.maps.MarkerImage("http://www.orlareal.com/images/poi-selected.png", new google.maps.Size(32, 37), new google.maps.Point(0, 0), new google.maps.Point(16, 37));
		//gPoiShadow = new google.maps.MarkerImage("http://labs.google.com/ridefinder/images/mm_20_shadow.png", new google.maps.Size(22, 20), new google.maps.Point(0, 0), new google.maps.Point(6, 20));
		
		if (typeof(gLocalSearch) == "undefined") {
			gLocalSearch = new GlocalSearch();
		}
		gBounds = new google.maps.LatLngBounds();
		gLocalSearch.setSearchCompleteCallback(null, OnLocalSearch);
		gLocalSearch.setResultSetSize(google.search.Search.LARGE_RESULTSET);
		if ((fLat == 0) && (fLon == 0)) {
			if (typeof(geocoder) == "undefined") {
				geocoder = new google.maps.Geocoder();
			}
			geocoder.geocode({
				'address': loc
			}, function(results, status){
				if (status == google.maps.GeocoderStatus.OK) {
					gLocalSearch.setCenterPoint(results[0].geometry.location);
					gBounds.extend(results[0].geometry.location);
				}
				else {
					alert("Geocode was not successful for the following reason: " + status);
				}
			});
		}
		else {
			latlng = new google.maps.LatLng(fLat, fLon);
			gLocalSearch.setCenterPoint(latlng);
			gBounds.extend(latlng);
		}
		gLocalSearch.execute(searchTerm);
		if (typeof(gInfoWindow) != "undefined") {
			if (gInfoWindow) {
				gInfoWindow.close();
			}
		}
		gInfoWindow = new google.maps.InfoWindow;
		google.maps.event.addListener(gInfoWindow, 'closeclick', function(){
			unselectMarkers();
		});
		return false;
	});
}
function unselectMarkers(){
	for (var i = 0; i < gCurrentResults.length; i++) {
		gCurrentResults[i].unselect();
	}
}
function OnLocalSearch(){
	if (!gLocalSearch.results) {
		return;
	}
	if (typeof(gInfoWindow) != "undefined") {
		if (gInfoWindow) {
			gInfoWindow.close();
		}
	}
	if (typeof(gCurrentResults) != "undefined") {
		for (var i = 0; i < gCurrentResults.length; i++) {
			gCurrentResults[i].marker().setMap(null);
		}
	}
	gCurrentResults = [];
	for (var i = 0; i < gLocalSearch.results.length; i++) {
		gCurrentResults.push(new LocalResult(gLocalSearch.results[i]));
		latlng=new google.maps.LatLng(parseFloat(gLocalSearch.results[i].lat), parseFloat(gLocalSearch.results[i].lng)); 
		gBounds.extend(latlng);
	}
	gMap.fitBounds(gBounds);	
}
function LocalResult(result) {// A class representing a single Local Search result returned by the Google AJAX Search API.
    var me = this;
    me.result_ = result;
    me.resultNode_ = me.node();
    me.marker_ = me.marker();
    google.maps.event.addDomListener(me.resultNode_, 'mouseover', function() {// Highlight the marker and result icon when the result is mouseovered.  Do not remove any other highlighting at this time.
        me.highlight(true);
    });
    google.maps.event.addDomListener(me.resultNode_, 'mouseout', function() {// Remove highlighting unless this marker is selected (the info window is open).  
        if (!me.selected_) me.highlight(false);
    });
    google.maps.event.addDomListener(me.resultNode_, 'click', function() {
        me.select();
    });
}

LocalResult.prototype.node = function() {
    if (this.resultNode_) return this.resultNode_;
    return this.html();
};
LocalResult.prototype.marker = function() {// Returns the GMap marker for this result, creating it with the given icon if it has not already been created.
    var me = this;
    if (me.marker_) return me.marker_;
    var marker = me.marker_ = new google.maps.Marker({
        position: new google.maps.LatLng(parseFloat(me.result_.lat),
                                 parseFloat(me.result_.lng)),
        icon: gPoiIcon, map: gMap
    });
    google.maps.event.addListener(marker, "click", function() {
        me.select();
    });
    return marker;
};
LocalResult.prototype.select = function() {// Unselect any selected markers and then highlight this result and display the info window on it.
    unselectMarkers();
    this.selected_ = true;
    this.highlight(true);
    gInfoWindow.setContent(this.html(true));
    gInfoWindow.open(gMap, this.marker());
};
LocalResult.prototype.unselect = function() {// Remove any highlighting on this result.
    this.selected_ = false;
    this.highlight(false);
};
LocalResult.prototype.html = function() {// Returns the HTML we display for a result before it has been "saved"
    var me = this;
    var container = document.createElement("div");
    container.className = "infoContent";
	container.style.width = "200px";
    container.style.height = "70px";
    container.appendChild(me.result_.html.cloneNode(true));
    return container;
}
LocalResult.prototype.highlight = function(highlight){
	this.marker().setOptions({
		icon: highlight ? gPoiIconSel : gPoiIcon
	});
	this.node().className = "unselected" + (highlight ? " red" : "");
}
function sspLoadAlbum(album) {
    thisMovie("ssp").sspLoadAlbum(album);
}
function thisMovie(movieName) {
    return swfobject.getObjectById(movieName);
}
function fullScreen() {
    this.moveTo(0, 0);
    this.resizeTo(screen.availWidth, screen.availHeight)
}
function EmailFriend(e){
	$("#spaEmailFriendError").hide();
	var sendData = new Object();
	sendData.__type = "ContactSendFields:#WCF";
	sendData.FirstName = $("#txtYour_Fname").val();
	sendData.LastName = $("#txtYour_Lname").val();
	sendData.Email = $("#txtYour_Email").val();
	sendData.FriendFirstName = $("#txtFriend_Fname").val();
	sendData.FriendLastName = $("#txtFriend_Lname").val();
	sendData.FriendEmail = $("#txtFriend_Email").val();
	sendData.Comments = $("#txtY_Message").val();
	var data = new Object();
	data.sendData = sendData;
	var sdata = JSON.stringify(data);
	$.ajax({
		url: "/SiteService.svc/EmailFriend",
		type: "POST",
		data: sdata,
		dataType: "json",
		contentType: "application/json; charset=utf-8",
		error: function(xhr, desc, exception){
			alert(xhr.responseText);
		},
		success: function(obj){
			obj = obj.d;
			if (obj.Status.toLowerCase() == "invalid") {
				$('#spaEmailFriendError').html(obj.Message).fadeIn('fast');
			}
			else 
				if (obj.Status.toLowerCase() == "success") {
					$("#txtYour_Fname").val('');
					$("#txtYour_Lname").val('');
					$("#txtYour_Email").val('');
					$("#txtFriend_Fname").val('');
					$("#txtFriend_Lname").val('');
					$("#txtFriend_Email").val('');
					$("#txtY_Message").val('I found this property, and thought you might like to see it.');
					$('#spaEmailFriendError').html(obj.Message).addClass('success').fadeIn('fast');
					setTimeout(function(){
						$('#spaEmailFriendError').fadeOut(5000, function(){
							$(this).removeClass('success');
							$('#divEmailFriend').slideUp('fast');
							$("#txtEmailFriend").val("Email address...");
							$("#aEmailFriend").removeClass('active');
						});
					}, 2000);
				}
		}
	});
}
