<?php 
if(function_exists('register_sidebar')){
	register_sidebar(array(
		'name'				=> 'Sidebar - Standard',			
		'before_widget' 	=> '<div class="right_col sidebar">',
    	'after_widget' 		=> '</div>',
    	'before_title' 		=> '<h5>',
        'after_title' 		=> '</h5>' )
	);} 
?>
<?php
if(function_exists('register_sidebar')){
	register_sidebar(array(
		'name'          	=> 'Sidebar - Categories',
		'before_widget' 	=> '<div class="right_col sidebar">',
		'after_widget'  	=> '</div>',
		'before_title'  	=> '<h5>',
		'after_title'   	=> '</h5>' )
	);} 
?>
<?php
if(function_exists('register_sidebar')){
	register_sidebar(array(
		'name'          	=> 'Top-Translator'
		 )
	);} 
?>
<?php
	add_theme_support( 'post-thumbnails' );
	set_post_thumbnail_size( 80, 80, true );
	add_image_size( 'single-post-thumbnail', 610, 9000 );
?>
<?php
/*Start of Theme Options*/

$categories = get_categories('hide_empty=0&orderby=name&parent=0');
$wp_cats = array();
foreach ($categories as $category_list ) {
       $wp_cats[$category_list->cat_ID] = $category_list->cat_name;
}
array_unshift($wp_cats, "Choose a category"); 

$themename = "Global Press";
$shortname = "gp";
 
$options = array (
 
array( "name" => $themename." Options",
	"type" => "title"),
 
array( "name" => "General",
	"type" => "section"),
array( "type" => "open"),
 	
array( "name" => "Custom CSS",
	"desc" => "Want to add any custom CSS code? Put in here, and the rest is taken care of. This overrides any other stylesheets. eg: a.button{color:green}",
	"id" => $shortname."_custom_css",
	"type" => "textarea",
	"std" => ""),		
	
array( "type" => "close"),
array( "name" => "Homepage",
	"type" => "section"),
array( "type" => "open"),
	
array( "name" => "Number of Featured Articles",
	"desc" => "Set the number of latest news to be displayed in the featured section on the frontpage.",
	"id" => $shortname."_featured_articles",
	"type" => "text",
	"std" => "5"),

array( "name" => "Left Column Categorie",
	"desc" => "Choose the category to be displayed in the left column at the bottom.",
	"id" => $shortname."_leftcol_cat",
	"type" => "select",
	"options" => $wp_cats,
	"std" => "Choose a category"),

array( "name" => "Center Column Categorie",
	"desc" => "Choose the category to be displayed in the centered column at the bottom.",
	"id" => $shortname."_centercol_cat",
	"type" => "select",
	"options" => $wp_cats,
	"std" => "Choose a category"),

array( "name" => "Right Column Categorie",
	"desc" => "Choose the category to be displayed in the right column at the bottom.",
	"id" => $shortname."_rightcol_cat",
	"type" => "select",
	"options" => $wp_cats,
	"std" => "Choose a category"),


array( "type" => "close"),
array( "name" => "Articles",
	"type" => "section"),
array( "type" => "open"),

array( "name" => "Display Retweet Button ?",
	"desc" => "Check this if you want to display the Retweet Button in each Articles.",
	"id" => $shortname."_retweet_article",
	"type" => "checkbox",
	"std" => "true"),

array( "name" => "Retweet @username",
	"desc" => "By default the retweet button will be in the format of “RT @your_username”. You have to change the button to retweet your user account.",
	"id" => $shortname."_retweet_username",
	"type" => "text",
	"std" => "@your_username"),

array( "type" => "close"),
array( "name" => "Archives",
	"type" => "section"),
array( "type" => "open"),

array( "name" => "Number of Latest Articles",
	"desc" => "Set the number of latest articles to be displayed in the Archives page on the left column.",
	"id" => $shortname."_archives_articles",
	"type" => "text",
	"std" => "10"),

array( "type" => "close"),
array( "name" => "Footer",
	"type" => "section"),
array( "type" => "open"),

array( "name" => "Copyright Text",
	"desc" => "Enter the copyright text which appears in the footer eg. Copyright &copy; Your Company, 2009",
	"id" => $shortname."_footer_text",
	"type" => "textarea",
	"std" => "Copyright &copy; 2010 Global Press. All rights reserved. \nSite design by <a href='http://www.simonbouchard.com' title='Simon Bouchard'>Simon Bouchard</a>"),

array( "type" => "close"),
array( "name" => "Miscellaneous",
	"type" => "section"),
array( "type" => "open"),
	
array( "name" => "Google Analytics Code",
	"desc" => "You can paste your Google Analytics or other tracking code in this box. This will be automatically added to the footer.",
	"id" => $shortname."_ga_code",
	"type" => "textarea",
	"std" => ""),	
	
array( "name" => "Custom Favicon",
	"desc" => "A favicon is a 16x16 pixel icon that represents your site; paste the URL to a .ico image that you want to use as the image",
	"id" => $shortname."_favicon",
	"type" => "text",
	"std" => get_bloginfo('url') ."/favicon.ico"),	
	
array( "name" => "Feedburner URL",
	"desc" => "Feedburner is a Google service that takes care of your RSS feed. Paste your Feedburner URL here to let readers see it in your website",
	"id" => $shortname."_feedburner",
	"type" => "text",
	"std" => get_bloginfo('rss2_url')),

array( "type" => "close")
 
);
 
 
 
function mytheme_add_init() {

$file_dir=get_bloginfo('template_directory');
wp_enqueue_style("functions", $file_dir."/functions/functions.css", false, "1.0", "all");
wp_enqueue_script("rm_script", $file_dir."/functions/rm_script.js", false, "1.0");
}
 
function mytheme_add_admin() {
 
global $themename, $shortname, $options;
 
if ( $_GET['page'] == basename(__FILE__) ) {
 
if ( 'save' == $_REQUEST['action'] ) {
 
foreach ($options as $value) {
update_option( $value['id'], $_REQUEST[ $value['id'] ] ); }
 
foreach ($options as $value) {
if( isset( $_REQUEST[ $value['id'] ] ) ) { update_option( $value['id'], $_REQUEST[ $value['id'] ]  ); } else { delete_option( $value['id'] ); } }
 
header("Location: admin.php?page=functions.php&saved=true");
die;
 
} else if( 'reset' == $_REQUEST['action'] ) {
 
foreach ($options as $value) {
delete_option( $value['id'] ); }
 
header("Location: admin.php?page=functions.php&reset=true");
die;
 
}
}
 
add_menu_page($themename, $themename, 'administrator', basename(__FILE__), 'mytheme_admin');
}
 
function mytheme_admin() {
 
global $themename, $shortname, $options;
$i=0;
 
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 rm_wrap">
<h2><?php echo $themename; ?> Settings</h2>
 
<div class="rm_opts">
<form method="post">
 
<?php foreach ($options as $value) {
switch ( $value['type'] ) {
 
case "open":
?>
 
<?php break;
 
case "close":
?>
 
</div>
</div>
<br />

 
<?php break;
 
case "title":
?>
<p>To easily use the <?php echo $themename;?> theme, you can use the menu below.</p>

 
<?php break;
 
case 'text':
?>

<div class="rm_input rm_text">
	<label for="<?php echo $value['id']; ?>"><?php echo $value['name']; ?></label>
 	<input name="<?php echo $value['id']; ?>" id="<?php echo $value['id']; ?>" type="<?php echo $value['type']; ?>" value="<?php if ( get_settings( $value['id'] ) != "") { echo stripslashes(get_settings( $value['id'])  ); } else { echo $value['std']; } ?>" />
 <small><?php echo $value['desc']; ?></small><div class="clearfix"></div>
 
 </div>
<?php
break;
 
case 'textarea':
?>

<div class="rm_input rm_textarea">
	<label for="<?php echo $value['id']; ?>"><?php echo $value['name']; ?></label>
 	<textarea name="<?php echo $value['id']; ?>" type="<?php echo $value['type']; ?>" cols="" rows=""><?php if ( get_settings( $value['id'] ) != "") { echo stripslashes(get_settings( $value['id']) ); } else { echo $value['std']; } ?></textarea>
 <small><?php echo $value['desc']; ?></small><div class="clearfix"></div>
 
 </div>
  
<?php
break;
 
case 'select':
?>

<div class="rm_input rm_select">
	<label for="<?php echo $value['id']; ?>"><?php echo $value['name']; ?></label>
	
<select 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"'; } ?>><?php echo $option; ?></option><?php } ?>
</select>

	<small><?php echo $value['desc']; ?></small><div class="clearfix"></div>
</div>
<?php
break;
 
case "checkbox":
?>

<div class="rm_input rm_checkbox">
	<label for="<?php echo $value['id']; ?>"><?php echo $value['name']; ?></label>
	
<?php 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; ?> />


	<small><?php echo $value['desc']; ?></small><div class="clearfix"></div>
 </div>
<?php break; 

case "section":

$i++;

?>

<div class="rm_section">
<div class="rm_title"><h3><img src="<?php bloginfo('template_directory')?>/functions/images/trans.gif" class="inactive" alt="""><?php echo $value['name']; ?></h3><span class="submit"><input name="save<?php echo $i; ?>" type="submit" value="Save changes" />
</span><div class="clearfix"></div></div>
<div class="rm_options">




 
<?php break;
 
}
}
?>
 
<input type="hidden" name="action" value="save" />
</form>
<form method="post">
<p class="submit">
<input name="reset" type="submit" value="Reset" />
<input type="hidden" name="action" value="reset" />
</p>
</form>
<div style="font-size:9px; margin-bottom:10px;">Icons: <a href="http://www.woothemes.com/2009/09/woofunction/">WooFunction</a></div>
 </div>
 
 

<?php
}

?><?php
/* Custom Write Panel */
$key = $themename;
$meta_boxes = array(

"image" => array(
"name" => "laptopimage",
"title" => "Image",
"type" => "text",
"description" => "<strong>(Only for posts, not pages)</strong>Upload an image to represent the post here. Please make the image 315x189 pixels or a (rough) multiple"),

"pageheading" => array(
"name" => "pageheading",
"title" => "Page Title",
"type" => "text",
"description" => "<strong>(Only for pages, not posts)</strong>Add a heading for the page that shows up near the top."),

"pagecaption" => array(
"name" => "pagecaption",
"title" => "Page Caption",
"type" => "text",
"description" => "<strong>(For all posts and pages)</strong>Enter a small punchline or caption for the page, displayed under the heading(see above)."),



"visitlink" => array(
"name" => "visitlink",
"title" => "'Visit Site URL",
"type" => "text",
"description" => "<strong>(Only for posts in the portfolio or child categories)</strong>Enter the URL to view the project (eg: http://www.google.com). Leave it blank for no link."),


"visittext" => array(
"name" => "visittext",
"title" => "'Visit Site text",
"type" => "text",
"description" => "<strong>(Only for posts in the portfolio or child categories)</strong>Enter the text for the 'visit site' link. eg: 'Visit Site' or 'Read More'")
);

;


/*-----START FUNCTIONS-----*/

function create_meta_box() {
global $key;

if( function_exists( 'add_meta_box' ) ) {
add_meta_box( 'new-meta-boxes', ucfirst( $key ) . ' Options Panel', 'display_meta_box', 'post', 'normal', 'high' );
add_meta_box( 'new-meta-boxes', ucfirst( $key ) . ' Options Panel', 'display_meta_box', 'page', 'normal', 'high' );
}
}

function display_meta_box() {
global $post, $meta_boxes, $key;
?>

<div class="form-wrap">

<?php
wp_nonce_field( plugin_basename( __FILE__ ), $key . '_wpnonce', false, true );

foreach($meta_boxes as $meta_box) {
$data = get_post_meta($post->ID, $key, true);
?>

<div class="form-field form-required">
<label for="<?php echo $meta_box[ 'name' ]; ?>"><strong><?php echo $meta_box[ 'title' ]; ?></strong></label>

<?php if($meta_box['type']=="text") :?>
<input type="text" name="<?php echo $meta_box[ 'name' ]; ?>" value="<?php echo htmlspecialchars( $data[ $meta_box[ 'name' ] ] ); ?>" />
<?php endif; ?>
<p><?php echo $meta_box[ 'description' ]; ?></p>
</div>

<?php } ?>

</div>
<?php
}

function save_meta_box( $post_id ) {
global $post, $meta_boxes, $key;

foreach( $meta_boxes as $meta_box ) {
$data[ $meta_box[ 'name' ] ] = $_POST[ $meta_box[ 'name' ] ];
}

if ( !wp_verify_nonce( $_POST[ $key . '_wpnonce' ], plugin_basename(__FILE__) ) )
return $post_id;

if ( !current_user_can( 'edit_post', $post_id ))
return $post_id;

update_post_meta( $post_id, $key, $data );
}

?>
<?php
/**
 * Tests if any of a post's assigned categories are descendants of target categories
 *
 * @param int|array $cats The target categories. Integer ID or array of integer IDs
 * @param int|object $_post The post. Omit to test the current post in the Loop or main query
 * @return bool True if at least 1 of the post's categories is a descendant of any of the target categories
 * @see get_term_by() You can get a category by name or slug, then pass ID to this function
 * @uses get_term_children() Passes $cats
 * @uses in_category() Passes $_post (can be empty)
 * @version 2.7
 * @link http://codex.wordpress.org/Function_Reference/in_category#Testing_if_a_post_is_in_a_descendant_category
 */
function post_is_in_descendant_category( $cats, $_post = null )
{
	foreach ( (array) $cats as $cat ) {
		// get_term_children() accepts integer ID only
		$descendants = get_term_children( (int) $cat, 'category');
		if ( $descendants && in_category( $descendants, $_post ) )
			return true;
	}
	return false;
}
?>
<?php
add_action('admin_init', 'mytheme_add_init');
add_action('admin_menu', 'mytheme_add_admin');
#add_action('admin_menu', 'create_meta_box');
#add_action('save_post', 'save_meta_box');
?>