<?php 
 if ( ! defined( 'WP_CONTENT_URL' ) )
       define( 'WP_CONTENT_URL', get_option( 'siteurl' ) . '/wp-content' );
 if ( ! defined( 'WP_CONTENT_DIR' ) )
       define( 'WP_CONTENT_DIR', ABSPATH . 'wp-content' );
 if ( ! defined( 'WP_PLUGIN_URL' ) )
       define( 'WP_PLUGIN_URL', WP_CONTENT_URL. '/plugins' );
 if ( ! defined( 'WP_PLUGIN_DIR' ) )
       define( 'WP_PLUGIN_DIR', WP_CONTENT_DIR . '/plugins' );
if ( !defined('XMAS') )
	define('XMAS',WP_CONTENT_URL.'/themes/thischristmas');
add_editor_style();
add_custom_image_header('','');
add_custom_background(); 
// get_my_image is a bypass of wordpress image funcationlity to get content
// images.  Requires input of content. Outputs a img tag ready to go
function get_my_image($content){
	
	$post_image = null;
	$myimage 	= null;
	$src 		= null;
	$gothere	= null;
	$result 	= array();
	$img_regex 	= '/<img[^>]+src[\\s=\'"]';
	$img_regex .= '+([^"\'>\\s]+)/is';

	@preg_match_all($img_regex,$content,$result,PREG_PATTERN_ORDER);
	$result = $result[0];
	foreach($result as $k=>$v):
		$src = substr($v,9);
		if(strstr($src,'http://')):
			$post_image =  parse_url($src);
			$myimage = $src;
		endif;
	endforeach;
	if(!strstr($myimage,'src=')) :
		$gothere = 'http:'.substr($post_image['host'],8).$post_image['path'].'?'.$post_image['query'];
		if(strstr($gothere,'wp-o-matic')):
			// cache images look break too much default this
			$img = '<img src="'.XMAS.'/images/xmastree.gif" width="75" height="75" align="left" border=0>';		
		elseif($gothere != 'http:?'):
			// remote url is probably okay to use
			$img = '<img src="'.$gothere.'" width="75" height="75" align="left" border=0>';
		else :
			// default image
			$img = '<img src="'.XMAS.'/images/xmastree.gif" width="75" height="75" align="left" border=0>';		
		endif;
	elseif(strlen($myimage)< 3):
		// no images should be this small, default image
		$img = '<img src="'.XMAS.'/images/xmastree.gif" width="75" height="75" align="left" border=0>';
	else :
		// local url should be ok.
		$img = '<img '.$myimage.'" width="75" height="75" align="left" border=0>';
	endif;
	
	return $img;
}				
if ( function_exists('register_sidebar') ) :
	register_sidebar(array(
        'before_widget' => '<div class="widgetbox">',
        'after_widget' => '</div>',
        'before_title' => '<div class="boxheading"><span>',
        'after_title' => '</span></div>',
		'name' => '1'
    ));
	register_sidebar(array(
        'before_widget' => '<div class="widgetbox2">',
        'after_widget' => '</div>',
        'before_title' => '<div class="boxheading"><span>',
        'after_title' => '</span></div>',
		'name' => '2'
    ));
endif;
	if ( function_exists( 'register_nav_menu' ) ) {
		register_nav_menu( 'pluginbuddy_mobile', 'PluginBuddy Mobile Navigation Menu' );
	}
	add_theme_support( 'post-thumbnails' );
    add_theme_support('automatic-feed-links');

// Popularposts pull from technical-speech, added get_my_image
function popularPosts($options='') {

		global $tid;
		global $wpdb;  
		
		// these settings should remaing 3 and content_col
		// if you change it the feature boxes may bonk
		$ns_options = array(
						"count" => "3",
						"comments" => "0",
						"before"  => "<div class=\"featuredposts\">",
						"after" => "</div>",
						"show" => true,
						"place" => "content_col"
					   );
		$special_where = '1=1'; // this is to make your own criteria see (rel.term_taxonomy_id != 6)
		$options = explode("&",$options);
		$popular = '';
		$img = null;
		foreach ($options as $option) {
			$parts = explode("=",$option);
			if(isset($parts[1]))
				$options[$parts[0]] = $parts[1];
		
		}
		
		if (isset($options['count'])) {$ns_options['count'] = $options['count'];}
		if (isset($options['comments'])) {$ns_options['comments'] = $options['comments'];}
		if (isset($options['before'])) {$ns_options['before'] = $options['before'];}
		if (isset($options['after'])) {$ns_options['after'] = $options['after'];}
		if (isset($options['show'])) {$ns_options['show'] = $options['show'];}
		if (isset($options['place'])) {$ns_options['place'] = $options['place'];}
		
		
	
		if (!is_category()) : 
		$sql = "SELECT * FROM ".$wpdb->posts." as post
	JOIN ".$wpdb->prefix."term_relationships as rel ON post.ID = rel.object_ID
	JOIN ".$wpdb->prefix."term_taxonomy as ttax ON rel.term_taxonomy_id = ttax.term_taxonomy_id
	JOIN ".$wpdb->prefix."terms as terms ON ttax.term_id = terms.term_id
	WHERE (".$special_where.") AND post_type='post' ORDER BY RAND() LIMIT 0,3";
		else:
			$myc = get_the_category();
			$tid = $myc[0]->cat_ID;
			$sql = "SELECT * FROM ".$wpdb->posts." as post
	JOIN ".$wpdb->prefix."term_relationships as rel ON post.ID = rel.object_ID
	JOIN ".$wpdb->prefix."term_taxonomy as ttax ON rel.term_taxonomy_id = ttax.term_taxonomy_id
	JOIN ".$wpdb->prefix."terms as terms ON ttax.term_id = terms.term_id
	WHERE (rel.term_taxonomy_id = ".$tid.") AND post_type='post' ORDER BY RAND() LIMIT 0,3";		
		endif;
		$posts = $wpdb->get_results($sql);  
		if(count($posts)<3):
			$sql = "SELECT * FROM ".$wpdb->posts." as post
	JOIN ".$wpdb->prefix."term_relationships as rel ON post.ID = rel.object_ID
	JOIN ".$wpdb->prefix."term_taxonomy as ttax ON rel.term_taxonomy_id = ttax.term_taxonomy_id
	JOIN ".$wpdb->prefix."terms as terms ON ttax.term_id = terms.term_id
	WHERE (".$special_where.") AND post_type='post' ORDER BY RAND() LIMIT 0,3";
			$posts = $wpdb->get_results($sql);  
		endif;

		foreach ($posts as $post) {  
			setup_postdata($post);  
			$id = $post->ID;  
			$title = substr($post->post_title,0,20);  
			$excerpt = $post->post_excerpt;			
			$content = $post->post_content;
			$pthumb = get_the_post_thumbnail($id, 'thumbnail');
			//if($pthumb == '') $pthumb = home_url( '/wp-content/themes/thischristmas/');
			if (!$excerpt){
				$s = 1;
				if (strlen($content) > 180){
					$excerpt = substr(strip_tags($content),0,180);
				}else{
					$excerpt = strip_tags($content);
				}	
			}
						
			$img = get_my_image($content);
			if ($ns_options['place'] == 'content_col') {
				$popular.= $ns_options['before'].
						'<div class="featuredinner">
						<h6><a href="' . get_permalink($id) . '" title="' . $title . '">' . $title . '</a></h6>
						<a href="' . get_permalink($id) . '" title="' . $title . '">'.$img.'</a><p>'.$excerpt.'</p>	
						<a href="'.get_permalink($id).'" class="readmorelink"><img src="'.XMAS.'/images/more.png" border=0></a>
						</div>'.$ns_options['after']; 
			 	
			}elseif ($ns_options['place'] == 'content_row'){ 
				
				$popular .= '<div class="search_post"><div class="search_postdata"><a href="' . get_permalink($id) . '" title="' . $title . '">' . $title . '</a></div><p>'.$excerpt.'</p><div class="morelink" style="margin-bottom:5px;"><a href="'.get_permalink($id).'">Read More</a></div></div>'; 
				 
			}elseif ($ns_options['place'] == 'sidebar'){
				
			   $popular .='<li><a href="'.get_permalink($id).'" title="'.$title.'">'.$title.'</a></li>';
			}
			
		}  
	
		if ($ns_options['show']==1) {echo $popular;} else {return $popular;}
	}
 if ( ! isset( $content_width ) ) $content_width = 606; 

function drop_tags()
{
    echo "<select onChange=\"document.location.href=this.options[this.selectedIndex].value;\">";
    echo "<option>Tags</option>\n";
    foreach (get_the_tags() as $tag)
    {
        echo "<option value=\"";
        echo get_tag_link($tag->term_id);
        echo "\">".$tag->name."</option>\n";
    }
    echo "</select>";
}
?>