<?php
// $Id: websnapr_field.module,v 1.3 2009/09/06 12:14:35 alfaguru Exp $

/**
 * @file
 * Websnapr Field allows links to web pages to be displayed as thumbnails
 *
 */

/**
 * Implements hook_menu()
 *
 */
function websnapr_field_menu() {
  $items['admin/settings/websnapr'] = array(
    'title' => 'Websnapr Account',
    'description' => 'Details of your Websnapr Account.',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('websnapr_field_admin_settings'),
    'access arguments' => array('administer site configuration'),
    'type' => MENU_NORMAL_ITEM,
  );
  return $items;
}

/**
 * Our admin form
 *
 */
function websnapr_field_admin_settings() {
  $form['websnapr_account_key'] = array(
    '#type' => 'textfield',
    '#title' => t('Your account key'),
    '#description' => t('The key given to you by Websnapr.com for access to web thumbnails.'),
    '#default_value' => variable_get('websnapr_account_key', ''),
  );
  return system_settings_form($form);
}

/**
 * Declare our theme functions
 *
 */
function websnapr_field_theme() {
  $theme = array(
    'websnapr_field_formatter_thumbnail' => array(
      'arguments' => array('element' => NULL),
      'function' => 'theme_websnapr_field_formatter',
    ),
    'websnapr_field_formatter_small' => array(
      'arguments' => array('element' => NULL),
      'function' => 'theme_websnapr_field_formatter',
    ),
	'websnapr_field_image' => array(
      'arguments' => array(
        'url' => NULL,
        'size' => 'small',
        'alt' => '',
        'title' => '',
        'attributes' => '',
      ),
    ),
  );
  return $theme;
}

/**
 * Declare our formatters
 *
 */
function websnapr_field_field_formatter_info() {
  $formatters = array();
  $formatters['thumbnail'] = array(
    'label' => t('Websnapr thumbnail'),
    'field types' => array('link'),
  );
  $formatters['small'] = array(
    'label' => t('Websnapr small image'),
    'field types' => array('link'),
  );
  return $formatters;
}

/**
 * Themeable function for field output
 *
 */
function theme_websnapr_field_formatter($element) {
  if ($element['#item']['url']) {
    return websnapr_field_field_formatter($element['#field_name'], $element['#item'], $element['#formatter'], $element['#item']['url'], $element['#node']->title);
  }
}

/**
 * Implementation of hook_field_formatter().
 */
function websnapr_field_field_formatter($field, $item, $formatter, $url, $title) {
  switch ($formatter) {
    default:
    case 'small':
      $size = 'S';
      $height = 152;
      $width = 202;
      break;
    case 'thumbnail':
      $size = 'T';
      $height = 70;
      $width = 92;
      break;
  }
  $key = variable_get('websnapr_account_key', '');
  if ($key) {
    $item_url = $item['query'] ? ($url . '?' . $item['query']) : $url;
    $image = '<script type="text/javascript">';
    $image .= "wsr_snapshot('".urlencode($item_url)."', '".$key."', '".$size."');";
    $image .= '</script>';
    return $image;
  }
}

function theme_websnapr_field_image($url, $size, $alt = '', $title = '', $attributes = NULL) {
  switch ($size) {
    default:
    case 'small':
      $size = 'S';
      $height = 152;
      $width = 202;
      break;
    case 'thumbnail':
      $size = 'T';
      $height = 70;
      $width = 92;
      break;
  }
  $key = variable_get('websnapr_account_key', '');
  if ($key) {
    $image = '<script type="text/javascript">';
    $image .= "wsr_snapshot('".urlencode($url)."', '".$key."', '".$size."');";
    $image .= '</script>';
    return $image;
  }
}
function websnapr_field_init() {
  // load the js once
  // TODO: We should try to load this on event only
  // We even should switch to late jQuery load to make the page load not delay if websnapr has some roundtrip/response time or even once is unavailable (timeout delay).
  drupal_set_html_head('<script type="text/javascript" src="http://www.websnapr.com/js/websnapr.js"></script>');
}