﻿/*

 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 bindAddToFavorite() {
	jQuery(".addfavorite").bind("click", addFavoriteListing);
}

function addFavoriteListing(event) {

	// get the existing URL before we clear the value
	var urlstring = jQuery(this).attr("href").substr((jQuery(this).attr("href").indexOf("?") + 1));
	
	var waitImg = "";
	if (jQuery(this).hasClass("oneline")) {
		waitImg = '<img class="addtourwait" src="/images/Wait.gif" width="12px" height="12px" />';
	} else {
		waitImg = '<img class="addtourwait" src="/images/Wait.gif" />';
	}
	
	jQuery(this).html(waitImg);
	
	jQuery(this).unbind("click", addFavoriteListing);

	jQuery.ajax({
	    url: "/favorites_update.php?" + urlstring,
	    type: 'GET',
	    dataType: 'xml',
	    error: function(){ alert('Error adding favorite'); },
	    success: refreshFavorites
	});


	// return false to prevent the default action of the anchor tag	
	event.preventDefault();  // stop the default href action

}


function refreshFavorites(xml) {

	var totalListings	= 0;
	var totalBuildings	= 0;
	var totalCatchments = 0;
	var totalSubareas   = 0;
	var totalFavorites	= 0;

	if (xml)
	{
		var favlink = jQuery("#"+jQuery(xml).find('favorites').attr("id"));
		
		totalListings  = parseInt(jQuery(xml).find('listings > count').text(), 10);
		totalBuildings = parseInt(jQuery(xml).find('buildings > count').text(), 10);
		totalCatchments = parseInt(jQuery(xml).find('catchments > count').text(), 10);
		totalSubareas = parseInt(jQuery(xml).find('subareas > count').text(), 10);
		totalFavorites = totalListings + totalBuildings + totalCatchments + totalSubareas;
				
		var totalFavoritesLabel = "Favorites";
		if (totalFavorites > 0) { totalFavoritesLabel += " (" + totalFavorites + ")";}
	
		jQuery("#favnav").html(totalFavoritesLabel);
	}

	// get the mode from the original querystring parms
	var urlstring = jQuery(".addfavorite").attr("href").substr((jQuery(".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];
		}
	}

	if (favlink.hasClass('oneline')) {
		favlink.parent().html('<img src="/images/checkmark.gif" alt="In my favorites">');
	} else {
		// update the TourNav icon and link 
		favlink.attr({href: "/favorites.php?"+mode });
		favlink.html('<img class="addtour" src="/images/inmyfav.gif" />');
		favlink.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.login(function(response) {
		  if (response.session) {
		    if (response.perms) {
		      // user is logged in and granted some permissions.
		      // perms is a comma separated list of granted permissions
		      	alert('granted perms: '+response.perms);
				jQuery.ajax({
				    url: "facebook_update_email.php",
				    type: 'GET',
				    dataType: 'xml',
				    error: function(){ },
				    success: function () { }
				});
		    } else {
		      // user is logged in, but did not grant any permissions
		    }
		  } else {
		    // user is not logged in
		  }
		}, {perms:'email'});
		*/	
	}

}

function cbFbEmailPermissionFav(permissionGranted) {

	if (permissionGranted) {
		// get the email address that the user has chosen to share with us
		jQuery.ajax({
		    url: "facebook_update_email.php",
		    type: 'GET',
		    dataType: 'xml',
		    error: function(){ },
		    success: function () { }
		});
	}
	
}


