<?php
////////////////////////////////////////////////////////
//                                                    //
//          by: Kevin A. Hoogheem                     //
//                   (kevin@hoogheem.net)             //
//                                                    //
// You can redistribute this software under the terms //
// of the GNU General Public License as published by  //
// the Free Software Foundation; either version 2 of  //
// the License, or (at your option) any later         //
// version.                                           //
//                                                    //
// You should have received a copy of the GNU General //
// Public License along with this program; if not,    //
// write to the Free Software Foundation, Inc., 59    //
// Temple Place, Suite 330, Boston, MA 02111-1307 USA //
//                                                    //
//     Copyright 2004 by Kevin A. Hoogheem            //
//   Please keep this copyright information intact.   //
////////////////////////////////////////////////////////

/** 
 * Implementation of hook_help(). 
 */ 
function snap_help($section) { 
  switch ($section) { 
    case 'admin/help#leech':
    case 'admin/modules#description': 
      // This description is shown in the listing at admin/modules. 
      return t('Implementation of the snap site preview.'); 
  } 
} 


/** Access snap key. */
function snap_get_key() {
  return variable_get('snap_key', '');
}


/**
 * Implementation of hook_menu().
 */
function snap_menu($may_cache) {
  $items = array();
  
  if (!$may_cache && ('img_assist' != arg(0)) && user_access('view snap preview')){
	$key = snap_get_key();
  
	if ('' != $key) {
	  $snap_js = snap_javascript($key);
	  drupal_set_html_head($snap_js);
	}
  }
  
  return $items;
}


/** 
 * Implementation of hook_perm(). 
 */ 
function snap_perm() { 
  return array('view snap preview'); 
} 


/** 
 * Implementation of hook_settings(). 
 */ 
function snap_settings() {
  
  $form['snap_key'] = array( 
    '#type' => 'textfield', 
    '#title' => t('Snap Key'), 
    '#required' => TRUE, 
    '#default_value' => snap_get_key(),
    '#description' => t('Copy your key here.  <a class="snap_nopreview" href="http://www.snap.com/about/shots.php" target="_new">Get free key here</a>'),
  ); 
 
   $form['snap_preview_internal'] = array(
		'#type' => 'checkbox',
		'#title' => t('Preview internal links'),
		'#default_value' => variable_get('snap_preview_internal', 0),
		'#description' => t('Preview internal links (we do not recommend turning this on).'),
	);
   
  $form['snap_searchbox'] = array(
		'#type' => 'checkbox',
		'#title' => t('Display Snap Search Box'),
    '#default_value' => variable_get('snap_searchbox', 0),
		'#description' => t('Include a Search box in preview window.'),
	);
	
  $form['snap_logo'] = array(
		'#type' => 'checkbox',
		'#title' => t('Display Customized Logo'),
    '#default_value' => variable_get('snap_logo', 0),
		'#description' => t('Enable customized logo in snap preview popup, uploaded during snap sign up.'),
	);

  $form['snap_linktype'] = array(
    '#type' => 'select',
    '#title' => t('Show link with'),
    '#default_value' => variable_get('snap_linktype', 0),
    '#options' => array(0 => t('Link Only'), 
                        1 => t('Icon and Link (both)'), 
                        2 => t('Icon and Link (icon only)')),
    '#description' => t('Set preview to show on both a icon and link, icon only, or link only'),
  );

  $form['snap_theme'] = array(
    '#type' => 'select',
    '#title' => t('Theme'),
    '#default_value' => variable_get('snap_theme', 'silver'),
    '#options' => array('silver' => t('Silver (default)'), 
                        'ice' => t('Ice (light blue)'), 
                        'green' => t('Green (light green)'), 
                        'linen' => t('Linen'),
                        'orange' => t('Orange'),
                        'pink' => t('Pink'),
                        'purple' => t('Purple'),
                        'asphalt' => t('Asphalt'),
                        ),
    '#description' => t('Set popup with a new theme'),
  );

  return $form;
}


function snap_javascript($key) {
  global $base_url;
  
  $output = '<script type="text/javascript" defer="defer" src="http://shots.snap.com/snap_shots.js?cl='.variable_get('snap_logo', 0).'&amp;po=0&amp;df=0&amp;lang=en-us&amp;ap=1&amp;si='.variable_get('snap_preview_internal', 0).'&amp;key='.$key.'&amp;sb='.variable_get('snap_searchbox', 0).'&amp;oi=0&amp;';
  switch(variable_get('snap_linktype', 0)){
    case 0:
    break;
    case 1:
      $output .= 'link_icon=on&amp;shots_trigger=both';
    break;
    case 2:
      $output .= 'link_icon=on&amp;shots_trigger=icon';
    break;
  }
  $output .= '&amp;th='.variable_get('snap_theme', 'silver').'&amp;domain='.$base_url.'/"></script>';
  return $output;   
}

?>
