<?php
//sidebar functionality
if ( function_exists('register_sidebar') ){
    register_sidebar('name=Sidebar&before_widget=<div class="zenboxsolid cool">&after_widget=</div>&before_title=<h3>&after_title=</h3>');
    register_sidebar('name=Footer&before_widget=&after_widget=&before_title=<h3>&after_title=</h3>');
    register_sidebar('name=Optional Footer&before_widget=<div class="footer_col">&after_widget=</div>&before_title=<h3>&after_title=</h3>');
}

// Turns the links into the top navigation menu.
// A link Categories' description becomes it's URL if it's valid.
function unl_menu_bookmarks(){
	$output = "";
	$cats = get_terms('link_category');
	foreach ( (array) $cats as $cat ) {
		$bookmarks = get_bookmarks('category='.$cat->term_id);
		if ( empty($bookmarks) )
			continue;
		if(filter_var($cat->description, FILTER_VALIDATE_URL)){
			$output .= '<li><a href="'. $cat->description. '" title="' . $cat->name . '">' . $cat->name . '</a><ul>';
		}else{
			$output .= '<li>' . $cat->name . '<ul>';
		}
		foreach($bookmarks as $bookmark){
			$output .= '<li><a href="' . $bookmark->link_url . '" title="' . $bookmark->link_name . '">' . $bookmark->link_name . '</a></li>';
		}
		$output .= '</ul></li>';
	}
	return $output;
}

function unl_footer_widgets($index = 1, $template_regions, $page) {
	global $wp_registered_sidebars, $wp_registered_widgets;

	if ( is_int($index) ) {
		$index = "sidebar-$index";
	} else {
		$index = sanitize_title($index);
		foreach ( (array) $wp_registered_sidebars as $key => $value ) {
			if ( sanitize_title($value['name']) == $index ) {
				$index = $key;
				break;
			}
		}
	}

	$sidebars_widgets = wp_get_sidebars_widgets();

	if ( empty($wp_registered_sidebars[$index]) || !array_key_exists($index, $sidebars_widgets) || !is_array($sidebars_widgets[$index]) || empty($sidebars_widgets[$index]) )
		return false;

	$sidebar = $wp_registered_sidebars[$index];

	$did_one = false;
	$x = 0;
	foreach ( (array) $sidebars_widgets[$index] as $id ) {
		if ( !isset($wp_registered_widgets[$id]) ) continue;

		$params = array_merge(
			array( array_merge( $sidebar, array('widget_id' => $id, 'widget_name' => $wp_registered_widgets[$id]['name']) ) ),
			(array) $wp_registered_widgets[$id]['params']
		);

		// Substitute HTML id and class attributes into before_widget
		$classname_ = '';
		foreach ( (array) $wp_registered_widgets[$id]['classname'] as $cn ) {
			if ( is_string($cn) )
				$classname_ .= '_' . $cn;
			elseif ( is_object($cn) )
				$classname_ .= '_' . get_class($cn);
		}
		$classname_ = ltrim($classname_, '_');
		$params[0]['before_widget'] = sprintf($params[0]['before_widget'], $id, $classname_);

		$params = apply_filters( 'dynamic_sidebar_params', $params );

		$callback = $wp_registered_widgets[$id]['callback'];
		if($x > sizeof($template_regions)-1) continue; //UNL Template only has 4 footer columns. only 2 of which are normally changeable
		if ( is_callable($callback) ) {
			ob_start();
			call_user_func_array($callback, $params);
			$page->$template_regions[$x] = ob_get_clean();
			$did_one = true;
			$x++;
		}
	}

	return $did_one;
}


/**
 * UNL_Feedback_Widget
 */
class UNL_Feedback_Widget extends WP_Widget {
    /** constructor */
    function UNL_Feedback_Widget() {
        parent::WP_Widget(false, $name = 'UNL Feedback');	
    }

    /** @see WP_Widget::widget */
    function widget($args, $instance) {
		extract( $args );
    	echo $before_widget;
?>
<h3>Your Feedback</h3>
<form action="http://www1.unl.edu/comments/" method="post" id="wdn_feedback" title="WDN Feedback Form:4" class="rating">
    <fieldset><legend>Please rate this page</legend>
    <ol>
        <li><label for="r1">Your Rating:</label> 
            <select id="r1" name="rating">
                <option value="1">1</option>
                <option value="2">2</option>
                <option value="3">3</option>
                <option value="4">4</option>
                <option value="5">5</option>
            </select>
        </li>
    </ol>
    <input type="submit" value="Submit" name="submit" /></fieldset>
</form>
<form action="http://www1.unl.edu/comments/" method="post" id="wdn_feedback_comments" title="WDN Feedback Form" class="comments">
    <fieldset><legend>Comments for this page</legend>
    <ol>
        <li><label>Comments</label>
          <textarea rows="2" cols="20" name="comment"></textarea>
        </li>
    </ol>
    <input type="submit" value="Submit" name="submit" /></fieldset>
</form>
<?php
		echo $after_widget;
    }

    /** @see WP_Widget::update */
    function update($new_instance, $old_instance) {				
        return $new_instance;
    }

    /** @see WP_Widget::form */
    function form($instance) {
    }

} // class UNL_Feedback_Widget

/**
 * UNL_Socialsite_Widget
 */
class UNL_Socialsite_Widget extends WP_Widget {
    /** constructor */
    function UNL_Socialsite_Widget() {
        parent::WP_Widget(false, $name = 'UNL Share This Page');	
    }

    /** @see WP_Widget::widget */
    function widget($args, $instance) {
		extract( $args );
    	echo $before_widget;
?>
<h3>Share This Page</h3>
<ul class="socialmedia">
    <li><a class="external tooltip" id="wdn_emailthis" href="mailto:" title="Email This Page To A Friend">Email This Page</a></li>
    <li><a class="external tooltip" id="wdn_facebook" href="http://www.facebook.com/" title="Share This Page On Facebook">Facebook</a></li>
    <li><a class="external tooltip" id="wdn_linkedin" href="http://www.linkedin.com/" title="Share This Page On LinkedIn">Linked In</a></li>
    <li><a class="external tooltip" id="wdn_myspace" href="http://www.myspace.com/" title="Share This Page On Myspace">Myspace</a></li>    
    <li><a class="external tooltip" id="wdn_twitter" href="http://www.twitter.com/" title="Share This Page On Twitter">Twitter</a></li>
    <li><a class="external tooltip" id="wdn_plurk" href="http://www.plurk.com/" title="Share This Page On Plurk">Plurk</a></li>
</ul>
<ul class="socialmedia">
    <li><a class="external tooltip" id="wdn_digg" href="http://www.digg.com/" title="Digg This Page">Digg</a></li>
    <li><a class="external tooltip" id="wdn_delicious" href="http://del.icio.us/" title="Share This Page On Del.icio.us">Delicious</a></li>
    <li><a class="external tooltip" id="wdn_reddit" href="http://www.reddit.com/" title="Share This Page On Reddit">Reddit</a></li>
    <li><a class="external tooltip" id="wdn_stumbleupon" href="http://www.stumbleupon.com/" title="Share This Page On Stumbleupon">StumbleUpon</a></li>
    <li><a class="external tooltip" id="wdn_newsvine" href="http://www.newsvine.com/" title="Share This Page On Newsvine">Newsvine</a></li>
    <li><a class="external tooltip" id="wdn_googlebookmark" href="http://www.google.com/" title="Share This As A Google Bookmark">Google Bookmark</a></li>
</ul>
<?php
		echo $after_widget;
    }

    /** @see WP_Widget::update */
    function update($new_instance, $old_instance) {				
        return $new_instance;
    }

    /** @see WP_Widget::form */
    function form($instance) {
    }

} // class UNL_Socialsite_Widget

register_widget('UNL_Feedback_Widget');
register_widget('UNL_Socialsite_Widget');

add_action('admin_menu', 'unl_template_menu');
add_action('admin_init', 'unl_template_options_reg');

function unl_template_options_reg(){
	register_setting('unl_template_options', 'sharedcode_path');
	register_setting('unl_template_options', 'template_dependants_path');
	register_setting('unl_template_options', 'pages_in_nav');
	register_setting('unl_template_options', 'links_in_nav');
	register_setting('unl_template_options', 'default_sidebar_on');
  register_setting('unl_template_options', 'template_style');
  register_setting('unl_template_options', 'footer_copyright');
}

function unl_template_menu() {
	add_theme_page('UNL Template Options', 'UNL Template Options', 'edit_themes', basename(__FILE__), 'unl_template_options');
}

function unl_template_options() {
?>
<div class="wrap">
<h2>UNL Template Options</h2>

<form method="post" action="options.php">
<?php wp_nonce_field('update-options'); ?>

<table class="form-table">

<tr valign="top">
<th scope="row">Shared Code Path</th>
<td><input type="text" name="sharedcode_path" value="<?php echo get_option('sharedcode_path'); ?>" /></td>
</tr>
 
<tr valign="top">
<th scope="row">Template Dependants Path</th>
<td><input type="text" name="template_dependants_path" value="<?php echo get_option('template_dependants_path'); ?>" /></td>
</tr>

<tr valign="top">
<th scope="row">Show in the Navigation Menu</th>
<td>
	Links <input type="checkbox" name="links_in_nav" value="yes" <?php echo (get_option('links_in_nav')=='yes') ? 'checked="checked"':''; ?> /><br />
	Pages <input type="checkbox" name="pages_in_nav" value="yes" <?php echo (get_option('pages_in_nav')=='yes') ? 'checked="checked"':''; ?> />
</td>
</tr>

<tr valign="top">
<th scope="row">Default Sidebar</th>
<td>Enable <input type="checkbox" name="default_sidebar_on" value="yes" <?php echo (get_option('default_sidebar_on')=='yes') ? 'checked="checked"':''; ?>" /></td>
</tr>

<tr valign="top"
<th scope="row">Template Style</th>
<td><select name="template_style">
<option value="Document" <?php echo (get_option('template_style')=='Document') ? 'selected="selected"':''; ?>>Document</option>
<option value="Fixed" <?php echo (get_option('template_style')=='Fixed') ? 'selected="selected"':''; ?>>Fixed</option>
</select>
</td>
</tr>

<tr valign="top"
<th scope="row">Copyright</th>
<td>
  <textarea name="footer_copyright"><?php echo get_option('footer_copyright'); ?></textarea>
</td>
</tr>

</table>

<input type="hidden" name="action" value="update" />
<?php settings_fields('unl_template_options'); ?>
<p class="submit">
<input type="submit" class="button-primary" value="<?php _e('Save Changes') ?>" />
</p>

</form>
</div>
<?php
}