<?php
if ( function_exists('register_sidebar') ) {
    register_sidebar(array(
		'name' => 'Sidebar',
		'before_widget' => '<div class="block %1$s %2$s">',
		'after_widget' => '</div>',
		'before_title' => '<h3>',
		'after_title' => '</h3>',
    ));
}


//GPL image grab code by Justin Tadlock of http://justintadlock.com

function get_the_image($args = array()) {

	$defaults = array(
		'custom_key' => array('Thumbnail','thumbnail'),
		'post_id' => false, // Build functionality in later
		'attachment' => true,
		'default_size' => 'thumbnail',
		'default_image' => false,
		'order_of_image' => 1,
		'link_to_post' => true,
		'image_class' => false,
		'image_scan' => false,
		'width' => false,
		'height' => false,
		'echo' => true
	);

	$args = apply_filters('get_the_image_args', $args);

	$args = wp_parse_args($args, $defaults);

	extract($args);

	if(!is_array($custom_key)) :
		$custom_key = str_replace(' ', '', $custom_key);
		$custom_key = str_replace(array('+'), ',', $custom_key);
		$custom_key = explode(',', $custom_key);
		$args['custom_key'] = $custom_key;
	endif;

	if($custom_key && $custom_key !== 'false' && $custom_key !== '0') $image = image_by_custom_field($args);

	if(!$image && $attachment && $attachment !== 'false' && $attachment !=='0') $image = image_by_attachment($args);

	if(!$image && $image_scan) $image = image_by_scan($args);

	if(!$image && $default_image) $image = image_by_default($args);

	if($image)
		$image = display_the_image($args, $image);

	else
		$image = '<!-- No images were added to this post. -->';

	if($echo && $echo !== 'false' && $echo !== '0')
		echo $image;
	else
		return $image;
}

function image_by_custom_field($args = array()) {

	extract($args);

	if(!$post_id)
		global $post;

	if(isset($custom_key)) :
		foreach($custom_key as $custom) :
			$image = get_post_meta($post->ID, $custom, true);
			if($image) :
				break;
			endif;
		endforeach;
		if(!$image) :
			return false;
		endif;
	endif;

	return array('image' => $image);
}

function image_by_attachment($args = array()) {

	extract($args);

	if(!$post_id)
		global $post;
	if(function_exists('wp_enqueue_style')) :
		$attachments = get_children(array('post_parent' => $post->ID, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => 'ASC', 'orderby' => 'menu_order ID'));
	else :
		$attachments = get_children("post_parent=$post->ID&post_type=attachment&post_mime_type=image&orderby=\"menu_order ASC, ID ASC\"");

	endif;

	if($attachments) :
		foreach($attachments as $id => $attachment) :
			$i++;
			if($i == $order_of_image) :
				$image = wp_get_attachment_image_src($id, $default_size);
				$image = $image[0];
				break;
			endif;
		endforeach;
	endif;

	return array('image' => $image);
}
function image_by_scan($args = array()) {

	if(!$post_id)
		global $post;

	preg_match_all('|<img.*?src=[\'"](.*?)[\'"].*?>|i', $post->post_content, $matches);

	if(isset($matches)) $image = $matches[1][0];

	if($matches)
		return array('image' => $image);
	else
		return false;
}
function image_by_default($args = array()) {

	extract($args);

	$image = $default_image;

	return array('image' => $image);
}

function display_the_image($args = array(), $arr = false) {
	global $post;

	extract($arr);

	if(!$image)
		return;

	extract($args);

	if($width) $width = ' width="' . $width . '"';
	if($height) $height = ' height="' . $height . '"';

	return $image;
}

$themename = "Smooth Real Estate";
$shortname = "sre"; 

$options = array (
				array(	"name" => "Color Hex For Menus",
						"desc" => "This is where you enter the main color hex for the menu. You must enter a hex in this field for any of the color hexes to work.",
			    		"id" => $shortname."_color_hex",
			    		"std" => "Enter your color hex.",
			    		"type" => "text"),
				array(	"name" => "Color Hex For Sub Menus",
						"desc" => "This is where you enter a color hex for the sub menu.",
			    		"id" => $shortname."_color_hex_sub",
			    		"std" => "Enter your color hex.",
			    		"type" => "text"),
				array(	"name" => "Link Color Hex",
						"desc" => "This is where you enter a color hex for the link colors.",
			    		"id" => $shortname."_link_color_hex",
			    		"std" => "Enter your color hex.",
			    		"type" => "text"),
		  );
		
function mytheme_add_admin() {

    global $themename, $shortname, $options;

    if ( $_GET['page'] == basename(__FILE__) ) {
    
        if ( 'save' == $_REQUEST['action'] ) {

                foreach ($options as $value) {
					if($value['type'] != 'multicheck'){
                    	update_option( $value['id'], $_REQUEST[ $value['id'] ] ); 
					}else{
						foreach($value['options'] as $mc_key => $mc_value){
							$up_opt = $value['id'].'_'.$mc_key;
							update_option($up_opt, $_REQUEST[$up_opt] );
						}
					}
				}

                foreach ($options as $value) {
					if($value['type'] != 'multicheck'){
                    	if( isset( $_REQUEST[ $value['id'] ] ) ) { update_option( $value['id'], $_REQUEST[ $value['id'] ]  ); } else { delete_option( $value['id'] ); } 
					}else{
						foreach($value['options'] as $mc_key => $mc_value){
							$up_opt = $value['id'].'_'.$mc_key;						
							if( isset( $_REQUEST[ $up_opt ] ) ) { update_option( $up_opt, $_REQUEST[ $up_opt ]  ); } else { delete_option( $up_opt ); } 
						}
					}
				}
                header("Location: themes.php?page=functions.php&saved=true");
                die;

        } else if( 'reset' == $_REQUEST['action'] ) {

            foreach ($options as $value) {
				if($value['type'] != 'multicheck'){
                	delete_option( $value['id'] ); 
				}else{
					foreach($value['options'] as $mc_key => $mc_value){
						$del_opt = $value['id'].'_'.$mc_key;
						delete_option($del_opt);
					}
				}
			}
            header("Location: themes.php?page=functions.php&reset=true");
            die;

        }
    }

    add_theme_page($themename." Options", "$themename Options", 'edit_themes', basename(__FILE__), 'mytheme_admin');

}

function mytheme_admin() {

    global $themename, $shortname, $options;

    if ( $_REQUEST['saved'] ) echo '<div id="message" class="updated fade"><p><strong>'.$themename.' settings saved.</strong></p></div>';
    if ( $_REQUEST['reset'] ) echo '<div id="message" class="updated fade"><p><strong>'.$themename.' settings reset.</strong></p></div>';
    
?>
<div class="wrap">
<h2><?php echo $themename; ?> options</h2>

<form method="post">

<table class="optiontable">

<?php foreach ($options as $value) { 
	
	switch ( $value['type'] ) {
		case 'text':
		option_wrapper_header($value);
		?>
		        <input style="width:400px;" name="<?php echo $value['id']; ?>" id="<?php echo $value['id']; ?>" type="<?php echo $value['type']; ?>" value="<?php if ( get_settings( $value['id'] ) != "") { echo get_settings( $value['id'] ); } else { echo $value['std']; } ?>" />
		<?php
		option_wrapper_footer($value);
		break;
		
		case 'select':
		option_wrapper_header($value);
		?>
	            <select style="width:240px;" name="<?php echo $value['id']; ?>" id="<?php echo $value['id']; ?>">
	                <?php foreach ($value['options'] as $option) { ?>
	                <option<?php if ( get_settings( $value['id'] ) == $option) { echo ' selected="selected"'; } elseif ($option == $value['std']) { echo ' selected="selected"'; } ?>><?php echo $option; ?></option>
	                <?php } ?>
	            </select>
		<?php
		option_wrapper_footer($value);
		break;
		
		case 'textarea':
		$ta_options = $value['options'];
		option_wrapper_header($value);
		?>
				<textarea name="<?php echo $value['id']; ?>" id="<?php echo $value['id']; ?>" style="width:400px;height:100px;"><?php 
				if( get_settings($value['id']) != "") {
						echo stripslashes(get_settings($value['id']));
					}else{
						echo $value['std'];
				}?></textarea>
		<?php
		option_wrapper_footer($value);
		break;

		case "radio":
		option_wrapper_header($value);
		
 		foreach ($value['options'] as $key=>$option) { 
				$radio_setting = get_settings($value['id']);
				if($radio_setting != ''){
		    		if ($key == get_settings($value['id']) ) {
						$checked = "checked=\"checked\"";
						} else {
							$checked = "";
						}
				}else{
					if($key == $value['std']){
						$checked = "checked=\"checked\"";
					}else{
						$checked = "";
					}
				}?>
	            <input type="radio" name="<?php echo $value['id']; ?>" value="<?php echo $key; ?>" <?php echo $checked; ?> /><?php echo $option; ?><br />
		<?php 
		}
		 
		option_wrapper_footer($value);
		break;
		
		case "checkbox":
		option_wrapper_header($value);
						if( get_settings($value['id']) == true ){
							$checked = "checked=\"checked\"";
						}else{
							$checked = "";
						}
					?>
		            <input type="checkbox" name="<?php echo $value['id']; ?>" id="<?php echo $value['id']; ?>" value="true" <?php echo $checked; ?> /><small>&nbsp;&nbsp;<&nbsp;<?php echo $value['desc']; ?></small>
		<?php
		break;

		case "multicheck":
		option_wrapper_header($value);
		
 		foreach ($value['options'] as $key=>$option) {
	 			$pn_key = $value['id'] . '_' . $key;
				$checkbox_setting = get_settings($pn_key);
				if($checkbox_setting != ''){
		    		if (get_settings($pn_key) ) {
						$checked = "checked=\"checked\"";
						} else {
							$checked = "";
						}
				}else{
					if($key == $value['std']){
						$checked = "checked=\"checked\"";
					}else{
						$checked = "";
					}
				}?>
	            <input type="checkbox" name="<?php echo $pn_key; ?>" id="<?php echo $pn_key; ?>" value="true" <?php echo $checked; ?> /><label for="<?php echo $pn_key; ?>"><?php echo $option; ?></label><br />
		<?php 
		}
		 
		option_wrapper_footer($value);
		break;
		
		case "heading":
		?>
		<tr valign="top"> 
		    <td colspan="2" style="text-align: center;"><h4><?php echo $value['name']; ?></h4></td>
		</tr>
		<?php
		break;
		
		case "bigheading":
		?>
		<tr valign="top"> 
		    <td colspan="2" style="text-align: center;"><h3><?php echo $value['name']; ?></h3></td>
		</tr>
		<?php
		break;
		
		case "break":
		option_wrapper_footer($value);
		break;
		
		default:

		break;
	}
}
?>

</table>

<p class="submit">
<input name="save" type="submit" value="Save changes" />    
<input type="hidden" name="action" value="save" />
</p>
</form>
<form method="post">
<p class="submit">
<input name="reset" type="submit" value="Reset" />
<input type="hidden" name="action" value="reset" />
</p>
</form>

<?php
}

function option_wrapper_header($values){
	?>
	<tr valign="top"> 
	    <th scope="row"><?php echo $values['name']; ?>:</th>
	    <td>
	<?php
}

function option_wrapper_footer($values){
	?>
	    </td>
	</tr>
	<tr valign="top">
		<td>&nbsp;</td><td><small><?php echo $values['desc']; ?></small></td>
	</tr>
	<?php 
}

add_action('admin_menu', 'mytheme_add_admin'); 

?>