<?php
// $Id: fb_social.module,v 1.2.2.8 2010/10/04 23:54:40 ferdi Exp $

/**
 * @file
 * Common settings for all fb social plugins
 */

/**
 * Implementation of hook_menu
 */
function fb_social_menu(){
  $items = array();
  $items['admin/settings/fb/social'] = array(
    'type' => MENU_NORMAL_ITEM,
    'title' => 'Facebook social',
    'description' => 'Settings for facebook social plugins.',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('fb_social_settings_form'),
    'file' => 'fb_social.admin.inc',
    'access callback' => 'user_access',
    'access arguments' => array('administer site configuration'),
  );
  
  $items['admin/settings/fb/social/settings'] = array(
    'type' => MENU_DEFAULT_LOCAL_TASK,
    'title' => 'Settings',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('fb_social_settings_form'),
    'file' => 'fb_social.admin.inc',
    'access callback' => 'user_access',
    'access arguments' => array('administer site configuration'),
    'weight' => -10,
  );  
  
  return $items;
}


/**
 * Implementation of hook_footer
 */
function fb_social_footer($main = 0) {
  global $language;

  $languages = _map_active_languages();

  if (fb_social_auto_language()) {
    if (array_key_exists($language->language, $languages)) {
      $fb_locale = $languages[$language->language];
    } else {
      drupal_set_message("There is no mapping for the current language. Using the default locale.");
    }
  } else {
    $fb_locale = variable_get('fb_social_locale', 'en_US');
  }

  
  $appid = variable_get('fb_social_appid', '');

  $output = '<div id="fb-root"></div>';
  $output .=  
  "<script type=\"text/javascript\">
     window.fbAsyncInit = function() {
       FB.init({
         appId: " . drupal_to_js($appid) .",
         status: true, 
         cookie: true,
         xfbml: true});
       
         jQuery.event.trigger('fb:init');    
     };
     (function() {
       var e = document.createElement('script'); 
       e.async = true;
       e.src = document.location.protocol + '//connect.facebook.net/" . $fb_locale  . "/all.js';
       document.getElementById('fb-root').appendChild(e);
     }());
  </script>";
  
  return $output;
}

function fb_social_auto_language() {
  return variable_get('fb_social_locale_auto', 0);
}

function _map_active_languages() {
  $languages = language_list();
  $mapped = array();
  foreach ($languages as $key => $language) {
    $mapped[$language->language] = variable_get('fb_social_language_' . $language->language, '');
  }
  return $mapped;
}

// API functions

/**
 *  Based on the user settings return the aliased / unaliased version
 *  of a given $url
 */
function fb_social_url($url = NULL){
  $aliased = variable_get('fb_social_urls_mode', 0);
  return url($url, array('absolute' => TRUE, 'alias' => $aliased ));
}
