/**
 *
 * Draw the options page
 *
 */
function cws_flickr_options_page()
{
    // global $get_page;
    $hook = 'cws_flickr';
    if (!current_user_can('manage_options')) {
        wp_die(__('You do not have sufficient permissions to access this page.'));
    }
    ?>
		<div class="wrap">
			<?php 
    screen_icon();
    ?>
			<h2>Flickr Settings Page</h2>
			<?php 
    $options = get_option('cws_flickr_options');
    $WPFlickr = new CWS_WPFlickr();
    // Check if we need to authenticate before displaying options form...
    $cws_FlickrAdmin = new CWS_FlickrAdmin($WPFlickr->preflight_errors);
    if (!$WPFlickr->preflight_errors) {
        if (isset($_REQUEST['cws_oauth_return'])) {
            if ($cws_FlickrAdmin->debug) {
                error_log('Returned from callback');
            }
            if (!isset($_REQUEST['page']) || $_REQUEST['page'] != $hook) {
                return;
            }
            // Make sure we play nicely with other OAUth peeps
            // Save access token and run $cws_FlickrAdmin->is_authenticated() again...
            try {
                if ($cws_FlickrAdmin->debug) {
                    error_log('Storing Access Token');
                }
                $access_token = serialize($cws_FlickrAdmin->consumer->getAccessToken($_GET, unserialize($cws_FlickrAdmin->request_token)));
                add_option('CWS-FLICKR-ACCESS_TOKEN', $access_token);
                delete_option('CWS_FLICKR_REQUEST_TOKEN');
                // no longer need this token so delete it.
                header("Location: " . CWS_WPFlickr::cws_get_admin_url('/options-general.php') . "?page=cws_flickr");
            } catch (Zend_Oauth_Exception $ex) {
                // Nuke request token...
                delete_option('CWS_FLICKR_REQUEST_TOKEN');
                error_log('ERROR: ' . $ex);
                header("Location: " . CWS_WPFlickr::cws_get_admin_url('/options-general.php') . "?page=cws_flickr");
                die;
            }
        } else {
            // If user is authenticated display options form
            if ($cws_FlickrAdmin->is_authenticated()) {
                ?>
							
						<form method="post" action="options.php">
							<?php 
                if (function_exists('settings_fields')) {
                    settings_fields('cws_flickr_options');
                }
                //
                if (function_exists('do_settings_sections')) {
                    do_settings_sections('cws_flickr');
                }
                cws_flickr_setting_input();
                // Grab the form
                cws_flickr_meta_box_feedback();
                // Grab the meta boxes
                // cws_flickr_meta_box_links();		// Grab the links meta boxes
                ?>
						</form>								
						<?php 
            } else {
                ?>
					<p>
						<?php 
                _e('This is the preferred method of authenticating your Flickr account.', 'cws_flickr');
                ?>
 <br/>
						<?php 
                _e("All authentication is taken place on FLickr's secure servers.", 'cws_flickr');
                ?>
<br/>
					</p>
					<p>
                		<?php 
                _e('Clicking the "Start the Login Process" link will redirect you to a login page at Flickr.com.', 'cws_flickr');
                ?>
<br/>
                		<?php 
                _e('After accepting the login there you will be returned here.', 'cws_flickr');
                ?>
                	</p>
					<?php 
                echo $cws_FlickrAdmin->get_grant_link();
            }
        }
    } else {
        $WPFlickr->showAdminMessages($WPFlickr->preflight_errors);
    }
    ?>
		
		</div>
<?php 
}