<?php

define('VL_WALLPAPER_DIR', 'wallpapers/');
define('VL_THUMBNAIL_DIR', VL_WALLPAPER_DIR.'thumbs/');

function buildWallpaperArray() {
    $wallpapers=array();
	$wallpapers[ 'tiled' ] = array();
    $d=dirname(__FILE__).'/'.VL_WALLPAPER_DIR;
    $dir = opendir($d);
     while ($f = readdir($dir)) { 
      $matches = null;
      if (eregi("(.*)\.jpg",$f,$matches)) { 
       $wallpapers[ 'tiled' ][$matches[1]] = VL_WALLPAPER_DIR . $f; 
      }
    }
	closedir($dir);
	
	$wallpapers[ 'bottom-left' ] = array();
	$d=dirname(__FILE__).'/'.VL_WALLPAPER_DIR.'bottom-left/';
    $dir = opendir($d);
     while ($f = readdir($dir)) { 
      $matches = null;
      if (eregi("(.*)\.jpg",$f,$matches)) { 
       $wallpapers[ 'bottom-left' ][$matches[1]] = VL_WALLPAPER_DIR . 'bottom-left/'. $f; 
      }
    }
    return $wallpapers;
}	

function buildThumbnailArray( $wallpapers )
{
    $d=dirname(__FILE__).'/'.VL_THUMBNAIL_DIR;
    
    // To create our thumbnail array, we first merge the wallpaper array
    // and then we set the values to null
    // this is to keep the order the same
    // while allowing us to only shown thumbails
    // for wallpapers that actually have thumbnails
    // I should use a class with data members that point
    // to the wallpaper and corresponding thumbnail
    // but that can wait till later.
    $all_wallpapers=array_merge( $wallpapers[ 'tiled' ], $wallpapers[ 'bottom-left' ]  );
    $dir = opendir($d);
    $thumbnails=$all_wallpapers;
    $keys = array_keys($thumbnails);
    for ($index = 0; $index < sizeof($keys); $index++) {
		$thumbnails[$keys[$index]] = null;
	}
	foreach( $thumbnails as $key => $value ) {
		$value = null;
	}
    while ($f = readdir($dir)) { 
		$matches = null;
		if (eregi("(.*)\.jpg",$f,$matches)) { 
			if (array_key_exists($matches[1],$wallpapers['tiled'])
				|| array_key_exists($matches[1],$wallpapers['bottom-left']) )
			$thumbnails[$matches[1]]=VL_THUMBNAIL_DIR.$f;
		}
    }
	foreach( $thumbnails as $key => $value ) {
		if( $value == null ) {
			unset( $thumbnails[ $key ] );
			if( array_key_exists($key,$wallpapers['tiled'] ) ) 
				unset( $wallpapers['tiled'][ $key ] );
			if( array_key_exists($key,$wallpapers['bottom-left'] ) ) 
				unset( $wallpapers['bottom-left'][ $key ] );
		}
	}
    
    
	$thumbnails[-1]=array_rand( $thumbnails );
    return $thumbnails; 
}

$vl_wallpapers = buildWallpaperArray();
$vl_thumbnails = buildThumbnailArray( $vl_wallpapers );

function switch_wallpaper() {
	session_start();
	global $vl_wallpapers;
	global $vl_thumbnails;
	for( $i = 0; $i < count( $vl_thumbnails ); ++$i ) {
		if( isset( $_GET[ 'thumbnail'.$i ] ) ) {
			$_SESSION[ 'vl_wallpaper'] = $_GET[ 'thumbnail'.$i ];
			setcookie('vl_wallpaper', $_GET[ 'thumbnail'.$i ], time() + 7000 * 24 * 60 * 60, "/");
			session_write_close();
			
			wp_redirect( $_SERVER[ 'HTTP_REFERER'] );
			exit;
		}
	}
	if( isset( $_COOKIE[ 'vl_wallpaper' ] ) ) {
		$_SESSION[ 'vl_wallpaper'] = $_COOKIE[ 'vl_wallpaper' ];
	}
	session_write_close();
}
if( function_exists('add_action') ) {
	add_action( 'init', 'switch_wallpaper' );
}

function spitOutWallpaperThumbs() {
    global $vl_wallpapers;
    global $vl_thumbnails;
    global $vl_thumbpos;
	$countwallpapers = '0';
	
	?><div id="thumbs" class="<?php
	if( $vl_thumbpos == "right" )
		echo 'thumbright ';
	else if( $vl_thumbpos == "sidebar" )
		echo 'sidebar ';
	else
		echo 'thumbleft ';
	if( vl_get_theme_option('framedthumbs') == 'image' )
		echo ' framed';
	?>"><?php
?>
<form action="<?php echo wp_specialchars($_SERVER[ 'REQUEST_URI'], 1); ?>" method="get">
<fieldset>
<?php
	foreach($vl_thumbnails as $key => $thumbnail) {
		if( $key == -1 ) {
			if( vl_get_theme_option( 'randomthumb' ) != "show" ) {
				continue;
			}
			$id = "-1";
			$title = 'Random';
			$src = 'images/random.png';
		}
		else {
			$id = $countwallpapers;
			$title = basename( $thumbnail, ".jpg" );
			$src = $thumbnail;
		}
?><input type="image" <?php
	?>onclick="changebackground(this, '<?php echo $id; ?>'); return false;" <?php
	?>id="thumbnail_<?php echo $id; ?>" value="<?php echo $id; ?>" <?php
	?>src="<?php echo get_stylesheet_directory_uri() . '/' . $src; ?>" <?php
	?>title="<?php echo $title; ?>" <?php
	if( wallpaper_selection() == $id ) { 
		?>class="selected"<?php
	}
?>/><?php
	$countwallpapers++;
	}
?>
</fieldset></form>
<?php
	echo '</div>';
}


?>