<?php
function gppthemes_more_themes_setup() {

    add_menu_page("gppThemes", "gppThemes", 'edit_themes', 'more-gppthemes', 'gppthemes_more_themes_page', 'http://graphpaperpress.com/favicon.ico');

}

function gppthemes_more_themes_page(){

       //global $options, $themename, $manualurl;
        
        ?>
        
        <style>
        ul.themes {}
        ul.themes li.theme {border-bottom: 1px #ddd solid; padding: 20px 0; display:inline; width: 230px; height: 600px;float:left; margin: 0 2em 2em 0; overflow:hidden}
        ul.themes li.theme img {float: none; display: block; margin: 0 1.5em 1.5em 0; padding: 1em; background: #eee; border: 1px solid #ccc}
        ul.themes li.theme img:hover {background:#ccc; border: 1px solid #999}
        </style>
        
        <div class="wrap">
          <h2>More Themes</h2>
          <div class="theme-info">
          <a href="http://www.graphpaperpress.com/members/member.php">Join Graph Paper Press</a> / <a href="http://www.graphpaperpress.com/category/themes/">Our Themes</a> / <a href="http://www.graphpaperpress.com/showcase/">Theme Showcase</a> / <a href="http://www.graphpaperpress.com/showcase/submit/">Submit your site</a>
          </div>
          
          
          
          <?php // Get RSS Feed(s)
            include_once(ABSPATH . WPINC . '/rss.php');
            $uri = 'http://graphpaperpress.com/category/themes/feed/';
            $feed = fetch_feed($uri);
            
			function returnImage ($text) {
				$text = html_entity_decode($text, ENT_QUOTES, 'UTF-8');
				//echo $text;
				$pattern = "/<img[^>]+\>/i";
				preg_match($pattern, $text, $matches);
				$text = $matches[0];
				return $text;
			}
			
			function scrapeImage($text) {
				$pattern = '/src=[\'"]?([^\'" >]+)[\'" >]/'; 
				preg_match($pattern, $text, $link);
				$link = $link[1];
				$link = urlencode($link);
				return $link;
			}
			
            printf('<ul class="themes">');
            foreach ($feed->get_items() as $item){
            $feedDescription = $item->get_content();
			$imageGot = returnImage($feedDescription);
			$image = scrapeImage($imageGot);
			$imagelink = urldecode($image);
			
            printf('<li class="theme"><h3><a href="%s">%s</a></h3>',$item->get_permalink(), $item->get_title());
            if($imagelink){
            	printf('<a href="%s">',$item->get_permalink());
            	echo '<img src="'.$imagelink.'" width="200" /></a>';
            }
            printf('<p>%s</p>',$item->get_description());
            printf('<p><small>%s</small></p></li>',$item->get_date('j F Y | g:i a'));
            }
			printf('</ul>');
            ?>
            
            </div>
         
         <?php

};

function gppthemes_more_themes_head() { 
         $style = $_REQUEST[style];
     if ($style != '') {
          ?> <link href="<?php bloginfo('template_directory'); ?>/styles/<?php echo $style; ?>.css" rel="stylesheet" type="text/css" /><?php 
     } else { 
          $stylesheet = get_option('gpp_alt_stylesheet');
          if($stylesheet != ''){
               ?><link href="<?php bloginfo('template_directory'); ?>/styles/<?php echo $stylesheet; ?>" rel="stylesheet" type="text/css" /><?php         
          }
     }     
}


?>