<?php // Widgetized Sidebar.
if (function_exists('register_sidebar')){
	register_sidebar(array(
		'before_widget' => '<ul><li id="%1$s" class="widget %2$s">',
    'after_widget' => '</li></ul>',
    'before_title' => '<h3 class="widgettitle">',
    'after_title' => '</h3>',
	));
}
// Recent Comments.
function get_wpkitcomments($no_comments = 5, $before = '<li> ', $after = '</li>', $show_pass_post = false){
	global $wpdb, $tablecomments, $tableposts;
	$request = "SELECT ID, comment_ID, comment_content, comment_author FROM $tableposts, $tablecomments WHERE $tableposts.ID=$tablecomments.comment_post_ID AND (post_status = 'publish' OR post_status = 'static')";
if(!$show_pass_post){ $request .= "AND post_password ='' "; }
    $request .= "AND comment_approved = '1' ORDER BY $tablecomments.comment_date DESC LIMIT 
$no_comments";
    $comments = $wpdb->get_results($request);
    $output = '';
    foreach ($comments as $comment){
		 $comment_author = stripslashes($comment->comment_author);
		 $comment_content = strip_tags($comment->comment_content);
		 $comment_content = stripslashes($comment_content);
		 $comment_excerpt =substr($comment_content,0,50);
		 $comment_excerpt = wptutf8trim($comment_excerpt);
		 $permalink = get_permalink($comment->ID)."#comment-".$comment->comment_ID;
		 $output .= $before . '<a href="' . $permalink . '" title="View the entire comment by ' . $comment_author . '">' . $comment_author . '</a>: ' . $comment_excerpt . '...' . $after;
		 }
		 echo $output;
}
function wptutf8trim($str){
	$len = strlen($str);
	for ($i=strlen($str)-1; $i>=0; $i-=1){
		$hex .= ' '.ord($str[$i]);
		$ch = ord($str[$i]);
        if (($ch & 128)==0) return(substr($str,0,$i));
		if (($ch & 192)==192) return(substr($str,0,$i));
	}
	return($str.$hex);
}
// Custom Style. Themes Options Page.
define("themename", "iStudio");
class iStudioOptions{
	function getOptions(){
		$options = get_option('iStudio_Options');
		if (!is_array($options)){
			$options['imageLogo'] = false;
			$options['SearchKey'] = false;
			$options['SearchKeyword'] = '';
			$options['GrowlSwitch'] = false;
			$options['GrowlInfo'] = '';
			$options['googleSearch'] = false;
			$options['googleId'] = '';
			$options['ctrlentry'] = false;
			$options['studiowidget'] = false;
			$options['widgetInfo'] = '';
			update_option('iStudio_Options', $options);
		}
		return $options;
	}
	function addOptions(){
		if(isset($_POST['save_submit'])){
			$options = iStudioOptions::getOptions();
			if ($_POST['imageLogo']){ 
				$options['imageLogo'] = (bool)true;
			}	else { 
				$options['imageLogo'] = (bool)false; 
			}
			if ($_POST['SearchKey']){ 
				$options['SearchKey'] = (bool)true;
			}	else { 
				$options['SearchKey'] = (bool)false; 
			}
			$options['SearchKeyword'] = stripslashes($_POST['SearchKeyword']);
			if ($_POST['GrowlSwitch']){ 
				$options['GrowlSwitch'] = (bool)true;
			}	else { 
				$options['GrowlSwitch'] = (bool)false; 
			}
			$options['GrowlInfo'] = stripslashes($_POST['GrowlInfo']);
			// google custom search engine
			if ($_POST['googleSearch']){
				$options['googleSearch'] = (bool)true;
			} else {
				$options['googleSearch'] = (bool)false;
			}
			$options['googleId'] = stripslashes($_POST['googleId']);
			// custom show widgets
			if ($_POST['studioWidget']){
				$options['studioWidget'] = (bool)true;
			} else {
				$options['studioWidget'] = (bool)false;
			}
			$options['widgetInfo'] = stripslashes($_POST['widgetInfo']);
			if ($_POST['ctrlentry']){
				$options['ctrlentry'] = (bool)true;
			} else {
				$options['ctrlentry'] = (bool)false;
			}
			update_option('iStudio_Options', $options);
			// Give an updated message
			echo "<div id='message' class='updated fade'><p><strong>".__('Settings saved.')."</strong></p></div>";
		} else {
			iStudioOptions::getOptions();
		}	
		add_theme_page(__(themename.' Options'), __(themename.' Options'), 'edit_themes', basename(__FILE__), array('iStudioOptions', 'OptionsPage')); 
	}
	function OptionsPage(){
		$options = iStudioOptions::getOptions();?>
<div class="wrap" style="padding:10px;">
  <div id="icon-options-general" class="icon32"><br></div>
  <h2><?php echo themename; ?> <?php _e('Options', 'iStudio');?></h2>
  <p><?php _e('Thank you for the use of the theme iStudio Theme.', 'iStudio');?></p>
  <p><?php _e('iStudio Theme Designed by <a href="http://xuhel.cn">Xu.hel</a>.', 'iStudio');?></p>
  <p><?php _e('Theme URL:', 'iStudio');?> <?php bloginfo('template_directory'); ?></p>
  <h3><?php _e('Custom Shortcode', 'iStudio');?></h3>
  <div style="padding-left:1em;">
  	<p><?php _e('Shortcode format:', 'iStudio');?></p>
  	<p><?php _e('Insert FLV Video: [flv width="160" height="10"]FLV URL[/flv]', 'iStudio');?></p>
  	<p><?php _e('Insert MP3 Music: [mp3 auto="1" replay="0"]MP3 URL[/mp3]', 'iStudio');?></p>
  </div>
  <h3><?php _e('Options', 'iStudio');?></h3>
  <div>
    <form action="#" method="post" enctype="multipart/form-data" name="options_form" id="options_form">
      <p><label><input name="imageLogo" type="checkbox" value="checkbox" <?php if($options['imageLogo']) echo "checked='checked'"; ?> > <?php _e('Use the Picture Logo.', 'iStudio');?></label></p>
      <p><label><input name="SearchKey" type="checkbox" value="checkbox" <?php if($options['SearchKey']) echo "checked='checked'"; ?> > <?php _e('Enable the search keywords', 'iStudio');?></label></p>
      <div style="padding-left:1em;">
        <p><?php _e('Search keyword values fill in the following input box. Each keyword with "," separated.', 'iStudio');?></p>
        <input name="SearchKeyword" type="text" value="<?php echo($options['SearchKeyword']); ?>" size="80" >
      </div>
      <p><label><input name="GrowlSwitch" type="checkbox" value="checkbox" <?php if($options['GrowlSwitch']) echo "checked='checked'"; ?> > <?php _e('Open GrowlBox pop-up tips', 'iStudio');?></label></p>
      <div style="padding-left:1em;">
        <p><?php _e('GrowlBox the content of the pop-up tips(If you use HTML code, then the code needs to be in the " replace \")：', 'iStudio');?></p>
        <textarea name="GrowlInfo" cols="80" rows="3"><?php echo($options['GrowlInfo']); ?></textarea>
      </div>
      <p><label><input name="googleSearch" type="checkbox" value="checkbox" <?php if($options['googleSearch']) echo "checked='checked'"; ?> > <?php _e('Use google custom search engine', 'iStudio');?></label></p>
      <div style="padding-left:1em;">
        <p><?php _e('Find <code>name="cx"</code> in the <strong>Search box code</strong> of <a href="http://www.google.com/coop/cse/">Google Custom Search Engine</a>, and type the <code>value</code> here.<br/>For example: <code>014782006753236413342:1ltfrybsbz4</code>', 'iStudio'); ?></p>
        <input name="googleId" type="text" value="<?php echo($options['googleId']); ?>" size="80" >
      </div>
      <p><label><input name="studioWidget" type="checkbox" value="checkbox" <?php if($options['studioWidget']) echo "checked='checked'"; ?> > <?php _e('Open the article Recommended Widgets', 'iStudio');?></label></p>
      <div style="padding-left:1em;">
        <p><?php _e('Recommended articles to be linked with the HTML code written in the input box below:','iStudio');?></p>
        <textarea name="widgetInfo" cols="80" rows="3"><?php echo($options['widgetInfo']); ?></textarea>
      </div>
      <p><label><input name="ctrlentry" type="checkbox" value="checkbox" <?php if($options['ctrlentry']) echo "checked='checked'"; ?> /><?php _e('Use Ctrl+Enter Reply Comments', 'iStudio');?></label></p>
      <p class="submit">
        <input type="submit" name="save_submit" class="button-primary" value="<?php _e('Save Changes', 'iStudio'); ?>" />
      </p>
    </form>
  </div>
  <hr size="1" />
  <h3>Donation</h3>
  <div>
  	<p>If you find my work useful and you want to encourage the development of more free resources, you can do it by donating...</p>
  	<form action="https://www.paypal.com/cgi-bin/webscr" method="post">
      <input type="hidden" name="cmd" value="_s-xclick">
      <input type="hidden" name="hosted_button_id" value="4839415">
      <input type="image" src="https://www.paypal.com/en_US/i/btn/btn_donate_SM.gif" border="0" name="submit" alt="PayPal - The safer, easier way to pay online!">
      <img alt="" border="0" src="https://www.paypal.com/zh_XC/i/scr/pixel.gif" width="1" height="1">
    </form>
  </div>
</div>
<?php }
}
add_action('admin_menu', array('iStudioOptions', 'addOptions'));
// Custom Comments List.
if (function_exists('wp_list_comments')){
	function comment_count( $commentcount ){
		global $id;
		$_commnets = get_comments('status=approve&post_id=' . $id);
		$comments_by_type = &separate_comments($_commnets);
		return count($comments_by_type['comment']);
	}
}
function custom_comments($comment, $args, $depth){
	$GLOBALS['comment'] = $comment;
	global $commentcount;
	if(!$commentcount){
		$commentcount = 0;
	} ?>
<li <?php comment_class(); ?> id="comment-<?php comment_ID(); ?>">
  <div class="list">
    <?php if ($comment->comment_author_email == get_the_author_email()){ ?>
    <table class="inc" border="0" cellspacing="0" cellpadding="0">
      <tr><td align="right" valign="bottom"><table border="0" cellpadding="0" cellspacing="0">
          <tr><td class="topleft"></td><td class="top"></td><td class="topright"></td></tr>
          <tr><td class="left"></td>
            <td align="left" class="conmts"><?php comment_text(); ?></td>
            <td class="right"></td></tr>
          <tr><td class="bottomleft"></td><td class="bottom"></td><td class="bottomright"></td></tr>
        </table></td>
        <td class="icontd" align="right" valign="bottom"><?php if (function_exists('get_avatar')){ ?><div class="gravatar2"><?php echo get_avatar( $comment, 32 ); ?></div><?php } ?></td></tr>
    </table>
    <div class="comment_textr">
      <cite><?php comment_author_link() ?></cite> at <a href="#comment-<?php comment_ID() ?>" title=""><?php comment_date() ?> <?php comment_time() ?></a><?php if ($comment->comment_approved == '0') : ?><em>Your comment is awaiting moderation.</em><?php endif; ?>
      <small class="commentmetadata"><?php edit_comment_link('Edit','&nbsp;&nbsp;',' | '); ?> <a onclick='Studio.goReply("<?php comment_ID() ?>", "<?php comment_author();?>")' href="#respond" style="cursor:pointer;"/><?php _e('Reply'); ?></a></small>
    </div>
		<?php } else { ?>
    <table class="out" border="0" cellspacing="0" cellpadding="0">
      <tr><td class="icontd" align="left" valign="bottom"><?php if (function_exists('get_avatar')){ ?><div class="gravatar"><?php echo get_avatar( $comment, 32 ); ?></div><?php } ?></td>
        <td align="left" valign="bottom"><table border="0" cellspacing="0" cellpadding="0">
          <tr><td class="topleft"></td><td class="top"></td><td class="topright"></td></tr>
          <tr><td class="left"></td>
            <td class="conmts"><?php comment_text(); ?></td>
          	<td class="right"></td></tr>
          <tr><td class="bottomleft"></td><td class="bottom"></td><td class="bottomright"></td></tr>
        </table></td></tr>
    </table>
    <div class="comment_text">
      <cite><?php comment_author_link() ?></cite> at <a href="#comment-<?php comment_ID() ?>" title=""><?php comment_date() ?> <?php comment_time() ?></a><?php if ($comment->comment_approved == '0') : ?><em>Your comment is awaiting moderation.</em><?php endif; ?>
      <small class="commentmetadata"><?php edit_comment_link('Edit','&nbsp;&nbsp;',' | '); ?> <a onclick='Studio.goReply("<?php comment_ID() ?>", "<?php comment_author();?>")' href="#respond" style="cursor:pointer;"/><?php _e('Reply'); ?></a></small>    
    </div>
    <?php }?>
  </div>
</li>
<?php }
// Custom Trackbacks List.
function custom_pings($comment, $args, $depth){ 
$GLOBALS['comment'] = $comment; ?>
<li <?php echo $oddcomment; ?>id="comment-<?php comment_ID() ?>">
	<div class="list">    
    <table class="out" border="0" cellspacing="0" cellpadding="0">
    	<tr><td class="topleft"></td><td class="top"></td><td class="topright"></td></tr><tr><td class="left"></td><td class="conmts">
      	<cite><?php comment_author_link() ?></cite> 
      	<small>(<?php echo $pingtype; ?>,<?php if ($comment->comment_approved == '0') : ?><em>Your comment is awaiting moderation.</em><?php endif; ?><?php comment_date() ?></small>)
      	<?php comment_text() ?>
      </td><td class="right"></td>
      </tr><tr><td class="bottomleft"></td><td class="bottom"></td><td class="bottomright"></td></tr>
    </table>  
  </div>
</li>
<?php }
// Page Nav.
function iStudiopagenav_check_num($num){
  return ($num%2) ? true : false; 
}
function iStudiopagenav_prevpage($paged, $max_page, $prevpage){
	if( $max_page > 1 && $paged > 1 )
		$pagingString = '<a class="prevpage" href="'.get_pagenum_link($paged-1). '" title="'.$prevpage.'">'.$prevpage.'</a>';
	return $pagingString;
}
function iStudiopagenav_left_side($max_page, $limit_pages, $paged, $pagingString){
	$pagingString = "";
	$page_check_max = false;
	$page_check_min = false;
	if($max_page > 1){
		for($i=1; $i<($max_page+1); $i++){
			if( $i <= $limit_pages ){
				if ($paged == $i || ($paged == "" && $i == 1))
					$pagingString .= '<a class="activepage" href="'.get_pagenum_link($i). '" title="'.$i.'">'.$i.'</a>'."\n";
				else
					$pagingString .= '<a href="'.get_pagenum_link($i). '" title="'.$i.'">'.$i.'</a>'."\n";
				if ($i == 1) $page_check_min = true;
				if ($max_page == $i) $page_check_max = true; }
		}
		return array($pagingString, $page_check_max, $page_check_min);
	}
}
function iStudiopagenav_middle_side($max_page, $paged, $limit_pages_left, $limit_pages_right){
	$pagingString = "";
	$page_check_max = false;
	$page_check_min = false;
	for($i=1; $i<($max_page+1); $i++){
		if($paged-$i <= $limit_pages_left && $paged+$limit_pages_right >= $i){
			if ($paged == $i)
				$pagingString .= '<a class="activepage" href="'.get_pagenum_link($i). '" title="'.$i.'">'.$i.'</a>'."\n";
			else
				$pagingString .= '<a href="'.get_pagenum_link($i). '" title="'.$i.'">'.$i.'</a>'."\n";
			if ($i == 1) $page_check_min = true;
			if ($max_page == $i) $page_check_max = true; }
	}
	return array($pagingString, $page_check_max, $page_check_min);
}
function iStudiopagenav_right_side($max_page, $limit_pages, $paged, $pagingString){
	$pagingString = "";
	$page_check_max = false;
	$page_check_min = false;
	for($i=1; $i<($max_page+1); $i++){
		if( ($max_page + 1 - $i) <= $limit_pages ){
			if ($paged == $i)
				$pagingString .= '<a class="activepage" href="'.get_pagenum_link($i). '" title="' .$i. '">'.$i.'</a>'."\n";
			else
				$pagingString .= '<a href="'.get_pagenum_link($i). '" title="' .$i. '">'.$i.'</a>'."\n";
			if ($i == 1) $page_check_min = true; }
		if ($max_page == $i) $page_check_max = true;
	}
	return array($pagingString, $page_check_max, $page_check_min);
}
function iStudiopagenav_nextpage($paged, $max_page, $nextpage){
	if( $paged != "" && $paged < $max_page)
		$pagingString = '<a class="nextpage" href="'.get_pagenum_link($paged+1). '" title="'.$nextpage.'">'.$nextpage.'</a>'."\n";
	return $pagingString;
}
function iStudiopagenav(){
	global $wp_query;
	global $max_page;
	global $paged;
	if (!$max_page){ $max_page = $wp_query->max_num_pages; }
	if (!$paged){ $paged = 1; }
	$nextpage = "&gt;"; // Settings.
	$prevpage = "&lt;"; // Settings.
	$startspace = "..."; // Settings.
	$endspace = "..."; // Settings.
	$limit_pages = "10"; // Settings.
	if(iStudiopagenav_check_num($limit_pages) == true){
		$limit_pages_left = ($limit_pages-1)/2;
		$limit_pages_right = ($limit_pages-1)/2;
	} else {
		$limit_pages_left = $limit_pages/2;
		$limit_pages_right = ($limit_pages/2)-1; }
	if( $max_page <= $limit_pages ) { $limit_pages = $max_page; }
	$pagingString = "<div class='iStudiopagenav'>\n";	
	if( ($paged) <= $limit_pages_left ){
		list ($value1, $value2, $page_check_min) = iStudiopagenav_left_side($max_page, $limit_pages, $paged, $pagingString);
		$pagingMiddleString .= $value1;
	} elseif(($max_page+1 - $paged) <= $limit_pages_right){
		list ($value1, $value2, $page_check_min) = iStudiopagenav_right_side($max_page, $limit_pages, $paged, $pagingString);
		$pagingMiddleString .= $value1;
	} else {
		list ($value1, $value2, $page_check_min) = iStudiopagenav_middle_side($max_page, $paged, $limit_pages_left, $limit_pages_right);
		$pagingMiddleString .= $value1; }
	$pagingString .= iStudiopagenav_prevpage($paged, $max_page, $prevpage); // Prev Page.
		if ($page_check_min == false && $show_start_end_numbers != "no"){
			$pagingString .= "<a class=\"lastpage\" href=\"" . get_pagenum_link(1) . "\" title=\""."1"."\">1</a>\n";
			$pagingString .= "<span class=\"space\">".$startspace."</span>\n";
		}
	$pagingString .= $pagingMiddleString;
	if ($value2 == false && $show_start_end_numbers != "no"){
		$pagingString .= "<span class=\"space\">".$endspace."</span>\n";
		$pagingString .= "<a class=\"lastpage\" href=\"".get_pagenum_link($max_page)."\" title=\"".$max_page."\">" . $max_page."</a>\n";
	}
	$pagingString .= iStudiopagenav_nextpage($paged, $max_page, $nextpage); // Next Page.
	$pagingString .= "</div>\n";
	if($max_page != 1)
		echo $pagingString;
}
// Custom languages.
function theme_init(){
	load_theme_textdomain('iStudio', get_template_directory() . '/resources/languages');
}
add_action ('init', 'theme_init');
// Custom Login Style.
function custom_login(){
echo '<link rel="stylesheet" tyssspe="text/css" href="' . get_bloginfo('template_directory') . '/resources/custom-login/custom-login.css" />'; }
add_action('login_head', 'custom_login');
// Custom Shortcode.
function flvlink($atts, $content = null){
	extract(shortcode_atts(array("width" => '160',"height" => '10',), $atts));
	return '<embed src="'.get_bloginfo("template_url").'/resources/flvplayer.swf?vcastr_file='.$content.'" quality="high" width="'.$width.'" height="'.$height.'" align="middle" allowScriptAccess="sameDomain" type="application/x-shockwave-flash"></embed>';
}
function mp3link($atts, $content = null){
	extract(shortcode_atts(array("auto" => '0',"replay" => '0',), $atts));	
	return '<embed src="'.get_bloginfo("template_url").'/resources/dewplayer.swf?mp3='.$content.'&amp;autostart='.$auto.'&amp;autoreplay='.$replay.'" wmode="transparent" height="20" width="200" type="application/x-shockwave-flash"></embed>';
}
add_shortcode('flv', 'flvlink');
add_shortcode('mp3', 'mp3link');
// WordPress Hack.
remove_action('wp_head','wp_generator');?>