<?php

/*
------- sidebar -------
*/

if (function_exists('register_sidebars')) {
    register_sidebars(1);
}



/*
------- theme options -------
*/

if (!get_option ('photon_custom_stylesheet_name')) {
    add_option ('photon_custom_stylesheet_name', 'default.css');
}

add_action ('admin_menu', 'photon_add_menu');




function photon_clear_options () {
    delete_option ('photon_custom_stylesheet_name');
}

function photon_add_menu () {
    add_submenu_page ('themes.php', 'Photon Options', 'Photon Options', 5, __FILE__, 'photon_menu');
}

function photon_menu () {
    if ($_REQUEST ['submit']) {
        photon_update_options ();
    }

    echo '<div class="wrap">';
    echo '<h2>Photon Options</h2>';
    photon_options_form ();
    echo '</div>';
}

function photon_update_options () {

    $updated = false;

    if ($_REQUEST ['stylesheet']) {
        update_option ('photon_custom_stylesheet_name', $_REQUEST ['stylesheet']);
        $updated = true;
    }

    if ($updated) {
		echo '<div id="message" class="updated fade">';
		echo '<p>Options updated</p>';
		echo '</div>';
    }

}

function photon_options_form () {

    $files = photon_get_custom_styles ();
    $currentname = get_option ('photon_custom_stylesheet_name');

    echo '<form method="post">' . "\n";
    echo '<p>Select a stylesheet ';
    echo "(currently $currentname):</p>";
    echo '<p><select name="stylesheet">' . "\n";

    foreach ($files as $file) {
        echo "<option value=\"$file\">$file</option>\n";
    }

    echo '</select></p>' . "\n";
    echo '<p><input type="submit" name="submit" value="Update Options" /></p>' . "\n";
    echo '</form>' . "\n\n";

}

function photon_get_custom_styles () {

    $dir = '../wp-content/themes/photon/styles';

    if (is_dir($dir)) { 
        if ($dh = opendir($dir)) {
            while (($file=readdir($dh)) != false) {
                if($file[0] == '.')
                    continue;
                $files[] = $file;
            }
            closedir($dh);
        }
    }

    return $files;

}

?>
