<?php

  
function sf_theme_widget_youtube($args) {
      
extract($args);
      
$options get_option('sf_theme_widget_youtube');
      if (!
is_home() && $options['onlyhome'])
        return;
      if (!
$options['url'] || !preg_match('|\?v=([^&]+).*?$|'$options['url'], $M))
        return;
      
$title $options['title'];
      
$url htmlspecialchars($options['url']);
      print 
$before_widget $before_title '<a href="' $url '">' $title '</a>' $after_title;
      print 
'<object width="139" height="120"><param name="movie" value="http://www.youtube.com/v/'.$M[1].'"></param><param name="wmode" value="transparent"></param><embed src="http://www.youtube.com/v/'.$M[1].'" type="application/x-shockwave-flash" wmode="transparent" width="139" height="120"></embed></object>';
      print 
$after_widget;
  }

  function 
sf_theme_widget_youtube_control() {
      global 
$wpdb;
      
$options get_option('sf_theme_widget_youtube');
      if ( !
is_array($options) )
          
$options = array('title'=>'Video''url'=>'http://www.youtube.com/watch?v=1nW6yIXMQL8');
      if ( 
$_POST['youtube-submit'] ) {
          
$options['title'] = strip_tags(stripslashes($_POST['youtube-title']));
          
$options['url'] = strip_tags(stripslashes($_POST['youtube-url']));
          
$options['onlyhome'] = isset($_POST['youtube-onlyhome']);
          
update_option('sf_theme_widget_youtube'$options);
      }
      
$title htmlspecialchars($options['title'], ENT_QUOTES);
      
$url htmlspecialchars($options['url'], ENT_QUOTES);
      
$onlyhome $options['onlyhome'] ? 'checked="checked"' '';
      
?>
      <label for="youtube-title" style="line-height:35px;display:block;">Titel</label><input style="width: 200px;" id="youtube-title" name="youtube-title" type="text" value="<?php echo $title?>" />
      <label for="youtube-url" style="line-height:35px;display:block;">URL</label><input style="width: 325px;" id="youtube-url" name="youtube-url" type="text" value="<?php echo $url?>" />
      <p style="text-align:right;margin-right:15px;"><label for="youtube-onlyhome">Vis kun på forsiden? <input class="checkbox" type="checkbox" <?php echo $onlyhome?> id="youtube-onlyhome" name="youtube-onlyhome" /></label></p>
      <input type="hidden" id="youtube-submit" name="youtube-submit" value="1" />
      <?php
    
}

function 
sf_theme_widget_youtube_init() {
    if (!
function_exists('register_sidebar'))
        return;
    
register_sidebar_widget('YouTube''sf_theme_widget_youtube');
    
register_widget_control('YouTube''sf_theme_widget_youtube_control'350200);
}

add_action('init''sf_theme_widget_youtube_init');

?>