﻿/*

 This include requires jQuery and the following javascript functions must be
 defined in the including page:

	$(document).ready(function()
		{	// bind the javascript event handlers
			bindAddToFavorite();
		}
	);

	function sharedImageDir()
	{
		return '<?php echo "/shared/icons/".$site_type;?>';
	}

	function getInMyFavoritesImg()
	{
		return "<?php echo inmytourImg();?>";
	}

*/


function bindAddToFavorite()
{
	$(".addfavorite").bind("click", addFavoriteListing);
}

function addFavoriteListing()
{
	// get the existing URL before we clear the value
	var urlstring = $(this).attr("href").substr(($(this).attr("href").indexOf("?") + 1));

	// display the wait gif
	//$(".addfavorite").attr({href: "" });
	var waitImg = "<img class=\"addtourwait\" src=\""+sharedImageDir()+"/Wait.gif\" />";
	$(".addfavorite").html(waitImg);
	$(".addfavorite").unbind("click", addFavoriteListing);

	$.ajax({
	    url: "/favorites_update.php?return=count&" + urlstring,
	    type: 'GET',
	    dataType: 'xml',
	    error: function(){ alert('Error adding favorite'); },
	    success: refreshFavorites
	});


	// return false to prevent the default action of the anchor tag	
	return false;

}

function refreshFavorites(xml)
{
	var totalListings	= 0;
	var totalBuildings	= 0;
	var totalFavorites	= 0;

	if (xml)
	{
		totalListings  = parseInt($(xml).find('listings > count').text(), 10);
		totalBuildings = parseInt($(xml).find('buildings > count').text(), 10);
		totalFavorites = totalListings + totalBuildings;
				
		totalFavoritesLabel = "My Favorites";
		if (totalFavorites > 0) { totalFavoritesLabel += " (" + totalFavorites + ")";}
	
		$("#topNavFavorites").html(totalFavoritesLabel);

	}

	// get the mode from the original querystring parms
	var urlstring = $(".addfavorite").attr("href").substr(($(".addfavorite").attr("href").indexOf("?") + 1));
	var params = urlstring.split("&");
	var mode = "";
	for (i=0; i < params.length; i++)
	{
		if (params[i].indexOf("mode") == 0)
		{
			mode = params[i];
		}
	}

	// update the TourNav icon and link 
	$(".addfavorite").attr({href: "/favorites.php?"+mode });
	$(".addfavorite").html(getInMyFavoritesImg());
	$(".addfavorite").unbind("click", addFavoriteListing);

	if (mode.indexOf("buildings") >= 0) {
		// since buildings automatically get added as a subscription we need to prompt
		// Facebook users for Email permission if they haven't already give it.
		FB.Facebook.apiClient.users_hasAppPermission("email",
													function(result) {
													        // prompt for email permission
													        if (result == 0) {
													            // render the permission dialog
													            FB.Connect.showPermissionDialog('email', cbFbEmailPermissionFav);
													        } 
													    });
	}

}

function cbFbEmailPermissionFav(permissionGranted) {

	if (permissionGranted) {
		// get the email address that the user has chosen to share with us
		$.ajax({
		    url: "facebook_update_email.php",
		    type: 'GET',
		    dataType: 'xml',
		    error: function(){ },
		    success: function () { }
		});
	}
	
}

/*
function shareFavorite() {

	if (isFBuser()) {
		var attachment = getFBshareAttachment('added this building as a favorite');
		
		FB.Connect.streamPublish('added a favorite building', attachment, null, null, 'Share your subscribed building with your friends');
	}

}
*/


