<?php

function sf_theme_widget_flickr_header() {
    echo 
'
<style type="text/css">
.flickr_badge_image { float: left; padding: 0; margin: 0; overflow: visible; position: relative; top: 0; left: 0; height: 38px; width: 38px }
.flickr_badge_image img { padding: 0; margin: 0; border: none; position: relative; top: 0px; left: 0px }
</style>

<script type="text/javascript">

var small = 37;
var large = small * 2 + 1;

var image = null;
    
function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != "function") {
    window.onload = func;
  } else {
    window.onload = function() {
      if (oldonload) {
        oldonload();
      }
      func();
    }
  }
}

function flickr_animate_init() {
    if (!(wrapper=document.getElementById("flickr_badge_uber_wrapper")))
        return;
    wrapper.style.width = (small * 3 + 3) + "px";
  for (i=1; i!=10; i++) {
        if (!(div=document.getElementById("flickr_badge_image" + i)))
            return;
        div.style.width = small + 1;
        div.style.height = small + 1;
        div.style.zIndex = 1;
        if (!(img = div.firstChild.firstChild))
            return;
        img.width = small;
        img.height = small;
    }
  setTimeout(flickr_animate_begin, 1000);
}

function flickr_animate_begin() {
    // Select a random image
    i = Math.ceil(Math.random() * 9);
    if (!(div=document.getElementById("flickr_badge_image" + i)))
        return;
    if (!(img = div.firstChild.firstChild))
        return;
    // Bring div to foreground, make larger...
    div.style.zIndex = 2;
    image = i;
    size = small;
  setTimeout(flickr_animate_larger, 1);
}

function flickr_animate_larger() {
    if (large > size) {
        size = size + 1;
        flickr_animate_set();
      setTimeout(flickr_animate_larger, 40);
        return;
    }
    size = large;
    flickr_animate_set();
  setTimeout(flickr_animate_smaller, 500);
}

function flickr_animate_smaller() {
    if (size > small + 3) {
        size = size - 3;
        flickr_animate_set();
      setTimeout(flickr_animate_smaller, 0);
        return;
    }
    div.style.zIndex = 1;
    size = small;
    flickr_animate_set();
  setTimeout(flickr_animate_begin, 1000);
}

function flickr_animate_set() {
    if (!image)
        return;
    if (!(div=document.getElementById("flickr_badge_image" + image)))
        return;
    if (!(img = div.firstChild.firstChild))
        return;
    if (image==3 || image==6 || image==9) {
        img.style.left = (0 - size + small) + "px";
    }
    if (image >= 7) {
        img.style.top = (0 - size + small) + "px";
    }
    img.width = size;
    img.height = size;
}

addLoadEvent(flickr_animate_init);
</script>    
'
;
}

function 
sf_theme_widget_flickr($args) {
    
extract($args);
    
$options get_option('sf_theme_widget_flickr');
    
$title $options['title'];
    
$tags join('+'$options['tags']);
    
$tagstitle join(' & '$options['tags']);
    if (empty(
$tags))
        return;
    echo 
$before_widget $before_title $title $after_title;
    echo 
'<div id="flickr_badge_uber_wrapper">
<div id="flickr_badge_wrapper">
    <script type="text/javascript" src="http://www.flickr.com/badge_code_v2.gne?show_name=1&count=9&display=random&size=s&layout=x&source=all_tag&tag='
.$tags.'">
    </script>
</div>
</div>'
;
    echo 
$after_widget;
}

function 
sf_theme_widget_flickr_control() {
    
$options get_option('sf_theme_widget_flickr');
    if ( !
is_array($options) )
        
$options = array('title'=>'flickr!''tags'=>array('sfpolitik''landsmøde'));
    if ( 
$_POST['sf_theme_widget_flickr-submit'] ) {
        
$options['title'] = strip_tags(stripslashes($_POST['sf_theme_widget_flickr-title']));
        
$options['tags'] = array_map(trimpreg_split('/\s*,\s*/'strip_tags(stripslashes($_POST['sf_theme_widget_flickr-tags'])), -1PREG_SPLIT_NO_EMPTY));
        
update_option('sf_theme_widget_flickr'$options);
    }
    
$title htmlspecialchars($options['title'], ENT_QUOTES);
    
$tags htmlspecialchars(join(', '$options['tags']), ENT_QUOTES);
    
?>
    <label for="sf_theme_widget_flickr-title" style="line-height:35px;display:block;">Titel</label><input style="width: 200px;" id="sf_theme_widget_flickr-title" name="sf_theme_widget_flickr-title" type="text" value="<?php echo $title?>" />
    <label for="sf_theme_widget_flickr-tags" style="line-height:35px;display:block;">Tags (adskilt med komma)</label><input style="width: 200px;" id="sf_theme_widget_flickr-tags" name="sf_theme_widget_flickr-tags" type="text" value="<?php echo $tags?>" />
    <input type="hidden" id="sf_theme_widget_flickr-submit" name="sf_theme_widget_flickr-submit" value="1" />
    <?php
}

function 
sf_theme_widget_flickr_init() {
    if (!
function_exists('register_sidebar'))
        return;
    
register_sidebar_widget('flickr!''sf_theme_widget_flickr');
    
register_widget_control('flickr!''sf_theme_widget_flickr_control'300160);
    if (
is_active_widget('sf_theme_widget_flickr'))
      
add_action('wp_head''sf_theme_widget_flickr_header');
}

add_action('init''sf_theme_widget_flickr_init');

?>