<?php
if ( ! isset( $content_width ) )
	$content_width = 480;

if ( function_exists('register_sidebar') )
		register_sidebars(2);



add_action( 'after_setup_theme', 'rehtse_setup' );

if ( ! function_exists( 'rehtse_setup' ) ):
function rehtse_setup() {

	add_editor_style();

	add_theme_support( 'post-formats', array( 'aside', 'gallery' ) );

	add_theme_support( 'post-thumbnails' );

	add_theme_support( 'automatic-feed-links' );

	//load_theme_textdomain( 'twentyten', TEMPLATEPATH . '/languages' );

	$locale = get_locale();
	$locale_file = TEMPLATEPATH . "/languages/$locale.php";
	if ( is_readable( $locale_file ) )
		require_once( $locale_file );

	// This theme uses wp_nav_menu() in one location.
	register_nav_menus( array(
		'primary' => __( 'Primary Navigation', 'rehtse' ),
	) );

	// This theme allows users to set a custom background
	//add_custom_background();

	// Your changeable header business starts here
	define( 'HEADER_TEXTCOLOR', '' );
	// No CSS, just IMG call. The %s is a placeholder for the theme template directory URI.
	define('HEADER_IMAGE', '%s/images/default.jpg');

	// The height and width of your custom header. You can hook into the theme's own filters to change these values.
	define('HEADER_IMAGE_WIDTH', 401);
	define('HEADER_IMAGE_HEIGHT', 285);


	// We'll be using post thumbnails for custom header images on posts and pages.
	// We want them to be 940 pixels wide by 198 pixels tall.
	// Larger images will be auto-cropped to fit, smaller ones will be ignored. See header.php.
	set_post_thumbnail_size( HEADER_IMAGE_WIDTH, HEADER_IMAGE_HEIGHT, true );

	// Don't support text inside the header image.
	define( 'NO_HEADER_TEXT', true );

	// Add a way for the custom header to be styled in the admin panel that controls
	// custom headers. See twentyten_admin_header_style(), below.
	add_custom_image_header( '', 'hope_admin_header_style' );

}
endif;

if ( ! function_exists( 'hope_admin_header_style' ) ) :

function hope_admin_header_style() {
?>
<style type="text/css">
#headimg{
	background: url(<?php header_image() ?>) no-repeat;
	background-repeat: no-repeat;
	height: <?php echo HEADER_IMAGE_HEIGHT; ?>px;
	width:<?php echo HEADER_IMAGE_WIDTH; ?>px;
}

#headimg h1, #headimg #desc {
	display: none;
}
</style>
<?php
}

endif;

function header_style() {
?>
<style type="text/css">
#rotating {background: url(<?php header_image() ?>) no-repeat center;}
</style>
<?php
}

//add_custom_image_header('', 'hope_admin_header_style');


function hope_admin_menu(){
	if ( current_user_can('edit_themes') )
		add_theme_page( __( 'Theme Options'), __( 'Theme Options' ), 'edit_theme_options', 'theme-setting', 'hope_admin' );

}

function hope_flickr() {
	if ( !get_option('flickr') )
		return;

	if( file_exists( ABSPATH . WPINC . '/rss.php') ) {
		require_once(ABSPATH . WPINC . '/rss.php');
	} else {
		require_once(ABSPATH . WPINC . '/rss-functions.php');
	}

	$items = get_option( 'rssnum' );
	if ( empty($items) || $items < 1 || $items > 50 )
		$items = 2;

	$flickr = get_option( 'flickr' );

	if ( empty( $flickr ) ){
		$flickr = "http://api.flickr.com/services/feeds/photos_public.gne?id=90827185@N00&lang=en-us&format=rss_200";
	}

	$rss = fetch_rss( $flickr );
	if( is_array( $rss->items ) ) {
		//$out = '';
		$out = wp_cache_get('hopeii');
		if ( false == $out ) {
			$items = array_slice( $rss->items, 0, $items );
			while( list( $key, $photo ) = each( $items ) ) {
				preg_match_all("/<IMG.+?SRC=[\"']([^\"']+)/si",$photo[ 'description' ],$sub,PREG_SET_ORDER);
				$photo_url = str_replace( "_m.jpg", "_m.jpg", $sub[0][1] );
				//$photo_url = str_replace( "_m.jpg", ".jpg?v=0", $sub[0][1] );

				if (strlen($out)>0)
					$out .= ",\n";

				$out .= '["' . $photo_url . '", "' . $photo['link'] . '", "_new", "'. esc_html( $photo[ 'title' ], true ). '"]';
			}

		wp_cache_set('hopeii', $out, 'theme', 21600); //cache for 6 hours
		} //end cache
	}
	?>

<script type="text/javascript">
var mygallery=new simpleGallery({
	wrapperid: "simplegallery",
	dimensions: [402, 285],
	imagearray: [
<?php
if ( !get_option('flickr') ) {?>
		["<?php header_image() ?>", "", "_new", " "],
<?php }
?>
		<?php echo $out; ?>
	],
	autoplay: [false, 2500, 2],
	persist: true,
	fadeduration: 500,
	oninit:function(){
		//Keyword "this": references current gallery instance (ie: try this.navigate("play/pause"))
	},
	onslide:function(curslide, i){
		//Keyword "this": references current gallery instance
		//curslide: returns DOM reference to current slide's DIV (ie: try alert(curslide.innerHTML)
		//i: integer reflecting current image within collection being shown (0=1st image, 1=2nd etc)
	}
})

</script>
<?php
}

function hope_admin(){
	$twit	= esc_html( get_option('twit') );
	$flickr	= esc_html( get_option('flickr') );
	$items	= esc_html( get_option( 'rssnum' ) );
	if ( empty($items) || $items < 1 ) $items = 5;

	if ( $_FILES ) {
		check_admin_referer('custom-background');
		$overrides = array('test_form' => false);
		$file = wp_handle_upload($_FILES['import'], $overrides);

		if ( isset($file['error']) )
			wp_die( $file['error'] );

		$url = $file['url'];
		$type = $file['type'];
		$file = $file['file'];
		$filename = basename($file);
		$object = array(
			'post_title' => $filename,
			'post_content' => $url,
			'post_mime_type' => $type,
			'guid' => $url
		);
		$id = wp_insert_attachment($object, $file);
		wp_update_attachment_metadata( $id, wp_generate_attachment_metadata( $id, $file ) );

		update_option('background_image', esc_url($url));
	}


		if ( isset( $_POST[ 'action' ] ) && esc_attr( $_POST[ 'action' ] ) == 'update' ) {
		if ( isset($_POST['twit']) ) {
			$twit = strip_tags(stripslashes($_POST["twit"]));
				update_option( 'twit', $twit );

		}

		if ( isset($_POST['flickr']) ) {
			$flickr = strip_tags(stripslashes($_POST["flickr"]));
			$items = strip_tags(stripslashes($_POST["rssnum"]));
				update_option( 'flickr', $flickr );
				update_option( 'rssnum', $items );
		} else {
				delete_option( 'flickr' );
				delete_option( 'rssnum' );
		}

		echo '<div id="message" class="updated fade"><p>Options saved successfully.</p></div>';
	}


		if ( isset( $_POST[ 'action' ] ) && esc_attr( $_POST[ 'action' ] ) == 'reset' ) {
		check_admin_referer('custom-background');
		delete_option('background_image');

		echo '<div id="message" class="updated fade"><p>The background has reset successfully.</p></div>';
	}

?>
<div class="wrap">
<div id="icon-themes" class="icon32"><br /></div>
<h2><?php _e('Rehtse Evoli Theme Settings')?></h2>

<div style='float:right;background: #ffc; border: 1px solid #333; margin: 2px; padding: 5px;width:200px;'>
<h3 align='center'>Support Rehtse Evoli Theme</h3>
<p>I am happy that you get a coffee to me, Thanks! Patrick</p>
<div align='center'>
	<a href="https://www.paypal.com/cgi-bin/webscr?cmd=_donations&business=mypatricks@gmail.com&item_name=Donate%20to%20HoPE%20Theme&item_number=1244379463&amount=10.00&no_shipping=0&no_note=1&tax=0&currency_code=USD&bn=PP%2dDonationsBF&charset=UTF%2d8&return=http://patrick.bloggles.info"><img src="https://www.paypal.com/en_GB/i/btn/btn_donate_SM.gif" /></a>
</div>
<p>Get theme support at <a href="http://www.facebook.com/pages/Rehtse-Evoli/120572287997737">Facebook</a></p>
<p>You may look me at my <a href="http://twitter.com/mypatricks">twitter</a>, <a href="http://penote.com/patrick/">penote</a></p>
</div>

<p><strong><?php _e('These are general extras that you can enable for your entire blog themes.')?></strong></p>

<form enctype="multipart/form-data" name="form1" method="post" action="<?php echo esc_attr( str_replace( '%7E', '~', $_SERVER['REQUEST_URI'] ) ); ?>">

<table class="form-table">

<tbody><tr>
	<th><label for="twitter"><?php _e('Twitter Username');?></label></th>
	<td><input name="twit" id="twit" value="<?php echo get_option('twit');?>" size="45" type="text" class="regular-text" /></td>
</tr>

<tr>
	<th><label for="flickr"><?php _e('flickr/PicasaWeb RSS');?></label></th>
	<td><input name="flickr" id="flickr" value="<?php echo get_option('flickr');?>" size="45" type="text" class="regular-text" /></td>
</tr>

<tr>
	<th><label for="rssnum"><?php _e('Number Photos'); ?></th>
	<td><select id="rssnum" name="rssnum"><?php for ( $i = 5; $i <= 100; ++$i ) echo "<option value='$i' ".($items==$i ? "selected='selected'" : '').">$i</option>"; ?></select></td>
</tr>
<tr>	<th><label for="help"><?php _e('Themes Help'); ?></th>
	<td><p>Note: Your RSS feed can be found on your Flickr homepage or your PicasaWeb RSS. Scroll down to the bottom of the page until you see the <em>Feed</em> link or <em>RSS</em> link and copy that into the box above.<br /></p>

<p><strong>flickr Feed Link:</strong><br />
<code>http://api.flickr.com/services/feeds/photos_public.gne?tags=rose&amp;lang=en-us&amp;format=rss_200</code><br />
<strong>Picasa RSS Link:</strong><br />
<code>http://picasaweb.google.com/data/feed/base/user/wael.tabba?alt=rss&amp;kind=album&amp;hl=en_US&amp;access=public</code>
</p>

<p class="submit">
	<input value="Save Theme Setting" type="submit" />
	<input name="action" value="update" type="hidden" /></p>
</td></form>
</tr>

<tr valign="top">
<th scope="row"><?php _e('Upload Background Image'); ?></th>
<td><form enctype="multipart/form-data" id="uploadForm" method="POST" action="">
<input type="file" id="upload" name="import" /><br />Images must be smaller than 800k. GIF, JPG, PNG.
<input type="hidden" name="action" value="save" />
<?php wp_nonce_field('custom-background') ?>
<p class="submit"><input type="submit" value="<?php esc_attr_e('Upload'); ?>" /></p>
</form>
</td>
</tr>
<?php if ( get_option('background_image') ) { ?>
<form enctype="multipart/form-data" name="form1" method="post" action="<?php echo esc_attr( str_replace( '%7E', '~', $_SERVER['REQUEST_URI'] ) ); ?>">
<?php wp_nonce_field('background') ?>
<tr valign="top"><td>Your current image: </td><td><img src="<?php echo get_option('background_image'); ?>" class="imgshot"></td>
</tr>
<tr><td colspan="2">
<p><?php _e('This will remove background image. You will not be able to retrieve any customizations.') ?></p>
<form method="post" action="<?php echo esc_attr( str_replace( '%7E', '~', $_SERVER['REQUEST_URI'] ) ); ?>">
<?php wp_nonce_field('custom-background'); ?>
	<input name="action" value="resset" type="hidden" />
<input type="submit" class="button" name="remove-background" value="<?php esc_attr_e('Remove Background'); ?>" />
</form></td>
</tr><?php } ?>

</tbody>
</table>
</div>
<?php
}

if ( !get_option('flickr') && !isset($_POST['update']) ) {
	function hope_tips() {
		if( substr( $_SERVER[ 'PHP_SELF' ], -19 ) != '/wp-admin/index.php' )
			return;

		echo "<div id='hope-tips' class='updated fade'><p>".__('Tips: You may custom your blog theme header and gallery with your flickr.') ."</p><p>". sprintf(__('You must <a href="%1$s">enter</a> the flickr/PicasWeb feed url for it to work.'), "themes.php?page=theme-setting")."</p></div>";
	}
	add_action('admin_notices', 'hope_tips');
}

add_action( 'admin_menu', 'hope_admin_menu' );
add_action( 'wp_footer', 'hope_flickr' );

?>