<?php
/**
 * MyArcadePlugin Pro Theme Options helps theme developers to create MyArcadePlugin Pro compatible theme backends.
 * @package MyArcadePlugin Pro Theme Options
 * @author Onedin Ibrocevic & Daniel Bakovic
 * @version 2.00
 */

/** Init Admin Menu **/
add_action( 'admin_menu', 'myabp_admin_menu_add' );

/** Activate some theme features **/
add_action('after_setup_theme', 'fungames_setup' );

/**
 * Sets up theme defaults and registers support for various WordPress features.
 */ 
function fungames_setup() {
  global $options;
    
  // Make theme available for translation
  // Translations can be filed in the /lang/ directory
  load_theme_textdomain('fungames', TEMPLATEPATH . '/lang');
  
  // This theme uses wp_nav_menu() in two locations.
  register_nav_menus( array(
    'primary' => __( 'Primary Navigation', 'fungames' ),
    'footer' => __( 'Footer Navigation', 'fungames' ),
    ) 
  );
  
  // This theme allows users to set a custom background
  add_custom_background();
  
  // Your changeable header business starts here
  define( 'HEADER_TEXTCOLOR', '000000' );

  // The height and width of your custom header. You can hook into the theme's own filters to change these values.
  // Add a filter to fungames_header_image_width and fungames_header_image_height to change these values.
  define( 'HEADER_IMAGE_WIDTH', apply_filters( 'fungames_header_image_width', 1000 ) );
  define( 'HEADER_IMAGE_HEIGHT', apply_filters( 'fungames_header_image_height', 118 ) );
  
  // Don't support text inside the header image.
  define('NO_HEADER_TEXT', false); 
  
  // Add a way for the custom header to be styled in the admin panel that controls
  // custom headers. See fungames_admin_header_style(), below.
  add_custom_image_header( '', 'fungames_admin_header_style' );
  
  // Check the settings
  if ( !get_option("fungames_color_scheme") ) {
    // Set Default Settings
    foreach ($options as $value) {
      if ( isset($value['id']) && isset($value['std']) ) { 
        add_option($value['id'], $value['std']); 
      }
    }
  }
  
	// This theme uses post thumbnails
	add_theme_support( 'post-thumbnails' );  
}

/**
 * Styles the header image displayed on the Appearance > Header admin panel.
 * Referenced via add_custom_image_header() in fungames_setup().
 */
function fungames_admin_header_style() {
?>
<style type="text/css">
/* Shows the same border as on front end */
#headimg {
  border-bottom: 1px solid #000;
  border-top: 4px solid #000;
}
#headimg #name {
  position:relative;
  top:65px;
  left:38px;
  width:852px;
  font:bold 28px "Trebuchet MS";
  text-decoration:none;
}  
#headimg #desc {
    color:#000;
    border-bottom:none;
    position:relative;
    top:50px;
    width:852px;
    left:38px;
    font:18px arial;
}
</style>
<?php
}


/**
 * Sets the various customised styling according to the options set for the theme
 */ 
function fungames_custom_style() {
  $background  = get_theme_mod('background_image', false);
  $bgcolor     = get_theme_mod('background_color', false);
  
  if (!$background && $bgcolor) {
    echo '<style type="text/css">body { background-image:none; }</style>';   
  }
}


function myabp_admin_menu_add() {
    $basename = basename( 'myabp_themeoptions.php' );
    if (function_exists('add_theme_page')) {
		  /* Create the theme  menu */
      $page = add_theme_page( __( 'Theme Options' ), __( 'Theme Options' ), 'manage_options', basename(__FILE__), 'myabp_admin_menu_theme');
		  /* Using registered $page handle to hook script load */
      add_action('admin_print_scripts-' . $page, 'myabp_admin_plugin_scripts');
	}
}

/** Enqueue needed script only when theme admin page is called **/
function myabp_admin_plugin_scripts() {
	wp_enqueue_script('jquery'); 
}

/** Create Admin Menu **/
function myabp_admin_menu_theme() {
  	global $shortname, $options;

  	if ( isset($_REQUEST['action']) && ('save' == $_REQUEST['action']) ) {
      	foreach ($options as $value) {
          if ( isset($value['id']) ) {
          if ( isset($_REQUEST[$value['id']]) && is_array ( $_REQUEST[$value['id']] ) ) :
            update_option($value['id'], implode(",", $_REQUEST[$value['id']]) );
          else :
            if ( isset( $_REQUEST[ $value['id'] ] ) ) {
                update_option($value['id'], trim($_REQUEST[$value['id']]) ); 
            } else { 
                delete_option( $value['id'] ); 
            }
          endif;
          }
      	}
      	echo '<div id="message" class="updated fade"><p><strong>'.MYAPB_THEMENAME.' settings saved.</strong></p></div>';
    } else if( isset($_REQUEST['action']) &&  ('reset' == $_REQUEST['action']) ) {
      foreach ($options as $value) {
        delete_option( $value['id'] ); 
        update_option( $value['id'], trim ($value['std']) );
      }
      echo '<div id="message" class="updated fade"><p><strong>'.MYAPB_THEMENAME.' settings reset.</strong></p></div>';
    }
  	?>

<script type="text/javascript">
	jQuery(document).ready(function(){
		jQuery(".toggle_container").hide();
		jQuery("h2.trigger").click(function(){
			jQuery(this).toggleClass("active").next().slideToggle("slow");
		});
	});
	
    function check_reset() {
		if (document.resetform.reset_chk.checked == false) {
        	alert('Are you sure? Check the box to load default theme settings.');
          	return false;
        }
        return true;
      }
</script>

<style type="text/css">
	.wrap 			{ width: 500px;	margin: 0px; }
	.clear 			{ clear: both; }
	h1 				{ font: 4em normal Georgia, 'Times New Roman', Times, serif; text-align:center; padding: 20px 0;	color: #aaa; }
	h1 span 		{ color: #666; }
	h1 small		{ font: 0.3em normal Verdana, Arial, Helvetica, sans-serif; text-transform:uppercase; letter-spacing: 1.5em; display: block; color: #666; }
	h2.trigger 		{ padding: 0 0 0 50px; margin: 0 0 5px 0; background: url(<?php echo get_template_directory_uri() . '/inc/'; ?>h2_trigger_a.gif) no-repeat; height: 46px; line-height: 46px; width: 450px; font-size: 2em; font-weight: normal; float: left; }
	h2.trigger a 	{ color: #fff; text-decoration: none; display: block; }
	h2.trigger a:hover { color: #ccc; }
	h2.active 		{ background-position: left bottom; }
	
	.toggle_container 			{ margin: 0px 0px 10px 0px; padding: 0; border-top: 1px solid #d6d6d6; background: #f0f0f0 url(<?php echo get_template_directory_uri() . '/inc/'; ?>toggle_block_stretch.gif) repeat-y left top; overflow: hidden; font-size: 1.2em; width: 500px; clear: both; }
	.toggle_container .block 	{ padding: 20px; background: url(<?php echo get_template_directory_uri() . '/inc/'; ?>toggle_block_btm.gif) no-repeat left bottom; }
	.toggle_container .block p 	{ padding: 5px 0; margin: 5px 0; }
	.toggle_container h3 		{ font: 2.5em normal Georgia, "Times New Roman", Times, serif; margin: 5px 0px; padding: 0 0 0px 0; border-bottom: 1px dashed #ccc; }
	.toggle_container small		{ width: 100%; margin: 3px 0px; padding: 0px; }
  .submit input.button-primary     {margin-top: 15px;}
</style>
  
<div class="wrap">
    <h2><strong><?php echo MYAPB_THEMENAME; ?> <?php echo __( 'Theme Options' ); ?></strong></h2>
    
    <form method="post">    
      <?php foreach ($options as $value) { ?>
        <?php
        switch ( $value['type'] ) { 
          case "open": ?>
            <h2 class="trigger"><a href="#"> 
          <?php break; ?>

          <?php case "close": ?>
							</table>
						</div>
					</div>
					<div class="clear"></div>
        <?php break; ?>

        <?php	case "savebutton": ?>
					<tr>
					    <td style="text-align: right;">
                <span class="submit"><input name="Save" type="submit" value="Save changes" class="button-primary" /></span>
					    </td>
					</tr>
        <?php break; ?>
          
        <?php case "plugincheck": ?>
					<tr>
					    <td>
					    <?php
							if (function_exists( $value['function'] )) { $imgsrc='yes.png'; } else { $imgsrc='no.png'; } ?>
					    	<h3><img alt="no" src="<?php bloginfo('template_directory'); ?>/inc/<?php echo $imgsrc; ?>" /> - <?php echo $value['name']; ?></h3>
					    </td>
					</tr>
        <?php break; ?>

        <?php case "title": ?>
					<?php echo $value['name']; ?></a></h2>
					<div class="toggle_container">
						<div class="block">
							<table class="optiontable" width="100%">
							<tr>
							    <td>
							    	<i><?php echo $value['desc']; ?></i>
							    </td>
							</tr>
        <?php break; ?>

        <?php case 'text':?>
					<tr>
					    <td>
					    	<h3><?php echo $value['name']; ?></h3>
					    	<small><?php echo $value['desc']; ?></small>
					    	<input style="width:100%;" name="<?php echo $value['id']; ?>" id="<?php echo $value['id']; ?>" type="<?php echo $value['type']; ?>" value="<?php if ( get_option( $value['id'] ) != "") { echo stripslashes(get_option( $value['id'] )); } else { echo stripslashes($value['std']); } ?>" />
					    </td>
					</tr>
        <?php break;?>

        <?php case 'textarea': ?>
					<tr>
					    <td>
						    <h3><?php echo $value['name']; ?></h3>
							<small><?php echo $value['desc']; ?></small>
              <textarea name="<?php echo $value['id']; ?>" style="width:100%; height:200px;" cols="" rows=""><?php if ( get_option( $value['id'] ) != "") { echo stripslashes(get_option( $value['id'] )); } else { echo stripslashes($value['std']); } ?></textarea>
					    </td>
					</tr>
        <?php break; ?> 

        <?php case 'select': ?>
					<tr>
					    <td>
					    	<h3><?php echo $value['name']; ?></h3>
					    	<small><?php echo $value['desc']; ?></small><br />
					    	<select style="width:240px;" name="<?php echo $value['id']; ?>" id="<?php echo $value['id']; ?>">
					    		<?php foreach ($value['options'] as $option) { ?>
					    			<option<?php if ( get_option( $value['id'] ) == $option) { echo ' selected="selected"'; } ?>>
					    				<?php echo $option; ?>
					    			</option><?php } ?>
					    	</select>
					    </td>
					</tr>
        <?php break; ?>

        <?php case "checkbox": ?>
				    <tr>
					    <td>
					    	<h3><?php echo $value['name']; ?></h3>
							<small><?php echo $value['desc']; ?></small><br />
				        	<? if(get_option($value['id'])){ $checked = "checked=\"checked\""; }else{ $checked = ""; } ?>
							<input type="checkbox" name="<?php echo $value['id']; ?>" id="<?php echo $value['id']; ?>" value="true" <?php echo $checked; ?> />
				    	</td>
				    </tr>
        	<?php break; ?>
  
        <?php case "exclude_categories": ?>
				    <tr>
					    <td>
					    	<h3><?php echo $value['name']; ?></h3>
							<small><?php echo $value['desc']; ?></small><br />
              <?php
              // Get excluded categories
              $excluded_cats = explode(',', get_option($value['id']));                
              ?>
              <?php foreach ($value['options'] as $cat_id => $option) { 
                foreach ($excluded_cats as $exclude_cat) {
                  if( $exclude_cat == $cat_id ){ $checked = ' checked="checked"'; break;} else { $checked = ""; }
                }
                ?>                
                <input type="checkbox" name="<?php echo $value['id'].'[]'; ?>" value="<?php echo $cat_id; ?>"<?php echo $checked; ?> /> <?php echo $option; ?><br />
					    <?php } ?>				      
				    	</td>
				    </tr>
        <?php break; ?>
            
	<?php 	} // End Switch?>
<?php } ?>

     <p class="submit">
        <input name="save" type="submit" value="Save changes" />    
        <input type="hidden" name="action" value="save" />
      </p>
    </form>
	
	<div style="text-align: right">
	    <form method="post" name="resetform" onsubmit="return check_reset(this.order)">
	      <p class="submit">
	      	<input name="reset_chk" type="checkbox" /> Yes, I want to load the default settings.<br />
	        <input name="reset" type="submit" value="Load default theme settings" class="button-primary" />
	        <input type="hidden" name="action" value="reset" />
	      </p>
	    </form>
	    <p>Do you need support? Visit <a href="http://myarcadeblogthemes.com" title="MyArcadeBlogThemes">MyArcadeBlogThemes</a></p>
        <strong><?php echo MYAPB_THEMENAME; ?> v<?php echo MYAPB_THEMEVERSION;?></strong>
    </div>
</div>  <?php // end wrap ?>
<?php
}
?>