function MyServiceClass(site_code) {

  this.facebox_for_homepage = function() {
    var param_string = window.location.search.substring(1);
    if (param_string.length) {
      var path = param_string.match(/my_service_path=(.*?)(&|$)/)[1];
      get_script(path)
    }
  };

  this.render_ad = function(selector, params) {
    var params = jQuery.param({ 'selector': selector })
    var path = '/ad.js?' + params;
    get_script(path);
  };

  // TODO:  rename
  this.recently_viewed_and_render_save_link = function(listing_id) {
    var params = saved_listing_params(listing_id, '');
    var path = "/snippets/recently_viewed_and_render_save_link.js?" + params;
    get_script(path);
  };

  this.refresh = function() {
    jQuery(document).trigger("refresh.my_service");
  };
  
  this.update_last_search = function(last_search_geodata, nodes) {
    var path = "/users/update_last_search.js?";
    path += jQuery.param({'geodata' : last_search_geodata, 'nodes' : nodes});
    get_script(path);
  };

  this.render_save_links = function() {
    var listing_ids = [];
    jQuery(".my_service__save_property_link").each(function() {
      var matches = jQuery(this).attr('id').match(/__listing_(\w+)$/);
      listing_ids.push(matches[matches.length - 1]);
    });

    var params = jQuery.param({ 'listing_ids[]': listing_ids });
    var path = "/snippets/save_links.js?" + params;
    get_script(path);
  };

  // TODO:  rename
  this.render_batch_profile = function() {
    get_script('/snippets/profile.js');
  };

  // TODO:  rename
  this.render_header_links = function(profile_label, sign_in_label, sign_up_label, sign_out_label) {
    var path = '/snippets/header.js?';
    profile_label = null_to_empty(profile_label);
    sign_in_label = null_to_empty(sign_in_label);
    sign_up_label = null_to_empty(sign_up_label);
    sign_out_label = null_to_empty(sign_out_label);
    path += jQuery.param({'profile_label' : profile_label, 'sign_in_label' : sign_in_label, 'sign_up_label' : sign_up_label, 'sign_out_label' : sign_out_label});
    get_script(path);
  };
  
  this.render_properties_map = function() {
    get_script('/map/properties.js');
  };

  this.render_saved_listing_community = function(saved_listing_id) {
    var path = '/saved_listings/' + saved_listing_id + '/community.js';
    get_script(path);
  }

  this.render_saved_searches = function() {
    get_script('/saved_searches.js');
  };

  this.render_save_search_button = function() {
    var querystring_params = ['saved_search[site]=' + site_code];

    var search_term = jQuery('#search_term').text();
    search_term = jQuery.trim(search_term);

    if (search_term.length) {
      querystring_params.push('saved_search[search_label]=' + search_term);
    }

    var querystring = querystring_params.join('&');
    querystring = encodeURI(querystring);
    
    querystring += encodeURI('&saved_search[search_url]=') + encodeURIComponent(document.location.href);

    var button = "<a href='/saved_searches/new.js?" + querystring + "' class='remote'>Save Search</a>";
    jQuery("#my_service__save_search_button").html(button);
  };

  this.render_school = function(school_id) {
    var path = '/snippets/school.js?id=' + school_id;
    get_script(path);
  };

  this.render_communities_and_schools_my_profile = function() {
    get_script('/my/profile/communities_and_schools.js');
  };

  this.save_listing = function(listing_id) {
    params = saved_listing_params(listing_id, 'saved');
    path = '/saved_listings/create.js?' + params;
    get_script(path);
  };
  
  this.save_contacted_listing = function(listing_id) {
    var params = saved_listing_params(listing_id, 'contacted');
    var path = '/saved_listings/contacted.js?' + params;
    get_script(path);
  };
  
  this.save_shared_listing = function(listing_id) {
    var params = saved_listing_params(listing_id, 'shared');
    var path = '/saved_listings/share.js?' + params;
    get_script(path);
  };

  this.share_this_link = function(listing_id, share_link, listing_url, options) {
    share_link.each(function() {
      SHARETHIS.addEntry(options, {
        url:listing_url,
        button: false,
        onclick: function() {
          MyService.save_shared_listing(listing_id);
        }
      }).attachButton(this);
    });
  };

  // private //////////////////////////////////////////////////////////////////

  var button_to_remote = function(selector) {
    jQuery(selector).unbind('click.my_service').bind('click.my_service', function() {
      var form = jQuery(selector);
      var path = form.attr('action');
      get_script(path);
      return false;
    });
  }
  
  var null_to_empty = function(variable) {
    if (variable == null) {
      return "";
    } else {
      return variable;
    };
  };

  var form_for_remote = function(selector) {
    var submit = jQuery(selector + ' input[type=submit]');

    submit.unbind('click.my_service').bind('click.my_service', function() {
      var form = jQuery(this).parents('form');
      var path = form.attr('action') + '?' + jQuery(form).serialize();
      get_script(path);
      return false;
    });
  }

  var form_for_remote_old = function(selector) {
    var form = jQuery(selector);
    var submit = form.find('input[type=submit]');

    submit.unbind('click.my_service').bind('click.my_service', function() {
      var path = form.attr('action') + '?' + jQuery(form).serialize();
      get_script(path);
      return false;
    });
  }

  var link_to_remote = function(selector) {
    jQuery(selector).unbind('click.my_service').bind('click.my_service', function() {
      var path = jQuery(this).attr('href');
      get_script(path);
      return false;
    });
  }

  var after_get_script = function() {
    button_to_remote('.my_service form.button-to');
    form_for_remote('.my_service form.remote');
    link_to_remote('.my_service a.remote');
  };

  var get_script = function(path) {
    path = path.replace(protocol_with_host(), '');
    path += (path.indexOf('?') >= 0) ? '&' : '?';
    path += jQuery.param({ 'site': site_code });
    if(path.charAt(0) != '/'){ path = '/' + path; };
    var url = MY_SERVICE_URL + path;
    jQuery.getScript(url, after_get_script);
  };

  var protocol_with_host = function() {
    return window.location.protocol + "//" + window.location.host
  }

  var saved_listing_params = function(listing_id, label) {
    var params = {
      'saved_listing[listing_id]': listing_id,
      'saved_listing[label]': label,
      'saved_listing[site]': site_code
    };

    var querystring = jQuery.param(params);
    return querystring;
  };
}

var MyService = null;

if (typeof(MY_SERVICE_URL) == 'undefined') {
  alert("var MY_SERVICE_URL = 'http://some_url/'; must be defined in client app's global JavaScript scope, before this file is included.");
}
else if (typeof(MY_SERVICE_SITE_CODE) == 'undefined') {
  alert("var MY_SERVICE_SITE_CODE = 'xyz'; must be defined in client app's global JavaScript scope, before this file is included.");
}
else {
  MyService = new MyServiceClass(MY_SERVICE_SITE_CODE);
  jQuery(document).trigger("loaded.my_service");
  jQuery(document).bind('beforeReveal.facebox', function() { jQuery('#facebox').bgiframe(); });
}
