﻿
jQuery(document).ready(function() {
	//When page loads...
	jQuery(".tab_content").hide(); //Hide all content
	jQuery("div.tabhead li:first").addClass("active").show(); //Activate first tab
	jQuery(".tab_content:first").show(); //Show first tab content

	// bind favorites and subscription handlers
	bindAddToFavorite();
	jQuery('#subscribeToBuilding').submit(submitSubscription);

	//On Click Event
	jQuery("div.tabhead li").click(function() {

		jQuery("div.tabhead li").removeClass("active"); //Remove any "active" class
		jQuery(this).addClass("active"); //Add "active" class to selected tab
		jQuery(".tab_content").hide(); //Hide all tab content

		var activeTab = jQuery(this).find("a").attr("href"); //Find the href attribute value to identify the active tab + content
		//jQuery(activeTab).fadeIn(); //Fade in the active ID content

		if ( jQuery(activeTab + " div") != null && jQuery(activeTab + " div").attr("id") != null)
		{
			jQuery(activeTab).show();
			switch (jQuery(activeTab + " div").attr("id"))
			{
				case "map_canvas":
					break;
				case "streetview_canvas":
					initializeStreetView();
					break;
				case "birdseye_canvas":
					initializeBirdsEye();
					break;
			}
			//jQuery(activeTab).show();
		}
		else if ( jQuery(activeTab + " iframe") != null && jQuery(activeTab + " iframe").attr("id") != null)
		{
			jQuery(activeTab).show();
			switch (jQuery(activeTab + " iframe").attr("id"))
			{
				case "map_canvas":
					break;
				case "streetview_canvas":
					initializeStreetView();
					break;
				case "birdseye_canvas":
					initializeBirdsEye();
					break;
			}
		}
		else jQuery(activeTab).fadeIn(); //Fade in the active ID content

		return false;
	});

	initializeMap();
	
	// attach sorting functions to any listings tables that may be on the page
    jQuery('.featureheaderrow th.sortable').click(function(event){onResultsHeaderClick(event);});
    jQuery('.featureheaderrow th.sortable').hover(function() {this.style.cursor = 'pointer';}, function() {this.style.cursor = '';});

	// ---- Send to a Friend Form dialog ----
	jQuery('#sendFriendDiv').dialog({
		bgiframe: true,
		modal: true,
		autoOpen: false,
		width: 600
		/*buttons: { "Cancel" : function() { jQuery(this).dialog('close'); jQuery('#sendtofriend_form').resetForm(); },
					"Send" : function() { jQuery('#sendtofriend_form').submit(); }
		}*/
	});

	jQuery('#sendBtn').bind("click", function(event) {
		jQuery('#sendtofriend_wait').hide();
		jQuery('#sendtofriend_closediv').hide();
		jQuery('#sendtofriend_msg').hide();
		jQuery('#sendtofriend_form').show();
		jQuery('#sendFriendDiv').dialog('open'); 
		event.preventDefault(); 
	});
	
	jQuery('#sendtofriend_close').bind("click", function() {jQuery('#sendFriendDiv').dialog('close'); });
	

	// ---- Report Inaccuracy Form dialog ----
	jQuery('#reportInaccuracyDiv').dialog({
		bgiframe: true,
		modal: true,
		autoOpen: false,
		width: 575
	});

	jQuery('#reportInaccuracyLink').bind("click", function(event) {
		jQuery('#reportInaccuracy_wait').hide();
		jQuery('#reportInaccuracy_closediv').hide();
		jQuery('#reportInaccuracy_msg').hide();
		jQuery('#reportInaccuracy_form').show();
		jQuery('#reportInaccuracyDiv').dialog('open'); 
		event.preventDefault(); 
	});
	
	jQuery('#reportInaccuracy_close').bind("click", function() {jQuery('#reportInaccuracyDiv').dialog('close'); });
});


function onSendToFriend() {
	
	jQuery.ajax({
		type: "POST",
		url: "/sendtofriend.php",
		data: jQuery('#sendtofriend_form').serialize(),
		dataType: "text",
		success: function(data) {jQuery('#sendtofriend_msg').text(data); },
		complete: function() {jQuery('#sendtofriend_wait').hide(); jQuery('#sendtofriend_closediv').show(); jQuery('#sendtofriend_msg').show();  }
	});
	
	jQuery('.clear').val('');
	jQuery('#sendtofriend_form').hide();
	jQuery('#sendtofriend_wait').show();
}

function onReportInaccuracy() {
	
	jQuery.ajax({
		type: "POST",
		url: "/report_inaccuracy.php",
		data: jQuery('#reportInaccuracy_form').serialize(),
		dataType: "text",
		success: function(data) {jQuery('#reportInaccuracy_msg').text(data); },
		complete: function() {jQuery('#reportInaccuracy_wait').hide(); jQuery('#reportInaccuracy_closediv').show(); jQuery('#reportInaccuracy_msg').show();  }
	});
	
	jQuery('.clear').val('');
	jQuery('#reportInaccuracy_form').hide();
	jQuery('#reportInaccuracy_wait').show();
}


function initializeMap()
{
	if (GBrowserIsCompatible())
	{
//alert("google compatible");
//alert("lat: " + lat);
		if (lat != 0 && lat != -1 && lon != 0 && lon != -1)
		{
//alert("canvas: " + document.getElementById("map_canvas"));
			var mapLatLon = new GLatLng(parseFloat(lat), parseFloat(lon));
			map = new GMap2(document.getElementById("map_canvas"));
			map.setCenter(mapLatLon, 15);
			
			var mapUIOptions = map.getDefaultUI();
			mapUIOptions.maptypes.physical = false;
			mapUIOptions.controls.maptypecontrol = true;
			map.setUI(mapUIOptions);
			
			map.addOverlay(new GMarker(mapLatLon));
		}
		map.disableScrollWheelZoom();
	}
	else
	{
		alert("google not compatible");
	}

}

function initializeStreetView()
{
	if (GBrowserIsCompatible())
	{
		// StreetviewSettings
		if (typeof(StreetviewSettings) != "undefined")
		{
			if (streetview == null)
			{
				//var fenwayPark = new GLatLng(parseFloat(lat), parseFloat(lon));
				var fenwayPark = new GLatLng(StreetviewSettings["lat"], StreetviewSettings["lon"]);
				var panoramaOptions = null;
			
				if (StreetviewSettings["found"])
				{	// not setting zoom for the time being
					panoramaOptions = { latlng: fenwayPark, pov: {yaw: StreetviewSettings["yaw"], pitch: StreetviewSettings["pitch"]}};
				}
				else
				{
					panoramaOptions = { latlng: fenwayPark};
				}
		
				streetview = new GStreetviewPanorama(document.getElementById("streetview_canvas"), panoramaOptions);
				GEvent.addListener(streetview, "error", handleNoFlash);
			}

		}
		else
		{
			if (lat != 0 && lat != -1 && lon != 0 && lon != -1)
			{
				if (streetview == null)
				{
					var fenwayPark = new GLatLng(parseFloat(lat), parseFloat(lon));
					var panoramaOptions = { latlng: fenwayPark};
					streetview = new GStreetviewPanorama(document.getElementById("streetview_canvas"), panoramaOptions);
					GEvent.addListener(streetview, "error", handleNoFlash);
				}
			}
		}
	}
}

function handleNoFlash(errorCode)
{
	switch(errorCode)
	{
		case 600:
			document.getElementById("streetview_canvas").innerHTML = '<div style="font-weight:bold;text-align:center;padding-top:25px;">No Panorama data was found</div>';
			break;	
		case 601:
			document.getElementById("streetview_canvas").innerHTML = '<div style="font-weight:bold;text-align:center;padding-top:25px;">Flash viewer not available</div>';
			break;
		default:
			document.getElementById("streetview_canvas").innerHTML = '<div style="font-weight:bold;text-align:center;padding-top:25px;">Unknown Flash error: ' + errorCode + '</div>';
			break;
	}
}

function initializeBirdsEye()
{
	if (lat != 0 && lat != -1 && lon != 0 && lon != -1)
	{
		if (jQuery("#birdseye_canvas").attr("src") == "")
		{
			jQuery("#birdseye_canvas").attr("src", "/birdseye.php?lat=" + lat + "&lon=" + lon);
		}
	}
}

function OpenCondoAdvisory(url)
{
	newWindow = window.open(url,'condoadvisory','toolbar=no,scrollbars=yes,height=700,width=950,top=5,left=25');
	newWindow.focus();
}



