<?php 
if ( function_exists('register_sidebars') ) {
    register_sidebars(1);
}
/*this function allows for the auto-creation of post excerpts*/
function truncate_post($amount,$echo=true) {
	global $post;
	
	$truncate = $post->post_content;
	if ( strlen($truncate) <= $amount ) $echo_out = ''; else $echo_out = '...';
	$truncate = apply_filters('the_content', $truncate);
	$truncate = preg_replace('@<script[^>]*?>.*?</script>@si', '', $truncate);
	$truncate = preg_replace('@<style[^>]*?>.*?</style>@si', '', $truncate);
	$truncate = strip_tags($truncate);
	if ($echo_out == '...') $truncate = substr($truncate, 0, strrpos(substr($truncate, 0, $amount), ' '));
	else $truncate = substr($truncate, 0, $amount);

	if ($echo) echo $truncate,$echo_out;
	else return ($truncate . $echo_out);
}
/*this function truncates titles to create preview excerpts*/
function truncate_title($amount,$echo=true) {
	$truncate = get_the_title(); 
	if ( strlen($truncate) <= $amount ) $echo_out = ''; else $echo_out = '...';
	$truncate = mb_substr( $truncate, 0, $amount, 'UTF-8' );
	if ($echo) {
		echo $truncate;
		echo $echo_out;
	}
	else { return ($truncate . $echo_out); }
}
/* this function gets thumbnail from Post Thumbnail or Custom field or First post image */
function get_thumbnail($width=100, $height=100, $class='', $alttext='', $titletext='', $fullpath=false, $custom_field='')
{
	global $post, $shortname, $posts;
	
	$thumb_array['thumb'] = '';
	$thumb_array['use_timthumb'] = true;
	if ($fullpath) $thumb_array['fullpath'] = ''; //full image url for lightbox
	
	if ( function_exists('has_post_thumbnail') ) {
		if ( has_post_thumbnail() ) {
			$thumb_array['use_timthumb'] = false;
			
			$args='';
			if ($class <> '') $args['class'] = $class;
			if ($alttext <> '') $args['alt'] = $alttext;
			if ($titletext <> '') $args['title'] = $titletext;
			
			$thumb_array['thumb'] = get_the_post_thumbnail( $post->ID, array($width,$height), $args );
			
			if ($fullpath) {
				$thumb_array['fullpath'] = get_the_post_thumbnail( $post->ID );
				$thumb_array['fullpath'] = preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $thumb_array['fullpath'], $matches);
				$thumb_array['fullpath'] = $matches[1][0];
			};
		}
	};
	
	if ($thumb_array['thumb'] == '') {
		if ($custom_field == '') $thumb_array['thumb'] = get_post_meta($post->ID, 'Thumbnail', $single = true);
		else { 
			$thumb_array['thumb'] = get_post_meta($post->ID, $custom_field, $single = true);
			if ($thumb_array['thumb'] == '') $thumb_array['thumb'] = get_post_meta($post->ID, 'Thumbnail', $single = true);
		}
		
		if (($thumb_array['thumb'] == '') && ((get_option($shortname.'_grab_image')) == 'on')) $thumb_array['thumb'] = first_image();
		
		if ($fullpath) $thumb_array['fullpath'] = $thumb_array['thumb'];
	};
			
	return $thumb_array;
}
/* this function prints thumbnail from Post Thumbnail or Custom field or First post image */
function print_thumbnail($thumbnail = '', $use_timthumb = true, $alttext = '', $width = 100, $height = 100, $class = '', $echoout = true, $forstyle = false) {

	$output = '';

	if ($forstyle === false) {
	
		if ($use_timthumb === false) {
			$output = $thumbnail;
		} else { 
			$thumbnail = '/images/sample_image.gif';
			$output = '<img src="'.get_bloginfo('stylesheet_directory').'/timthumb.php?src='.$thumbnail.'&amp;h='. $height .'&amp;w='. $width .'&amp;zc=1"';
		
			if ($class <> '') $output .= " class='$class' ";

			$output .= " alt='$alttext' width='$width' height='$height' />";
		};
	
	} else {
		
		$output = $thumbnail;
		
		if ($use_timthumb === false) {
			$output = preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $output, $matches);
			$output = $matches[1][0];
		} else {
			
			$output = get_bloginfo('stylesheet_directory').'/timthumb.php?src='.$output.'&amp;h='.$height.'&amp;w='.$width.'&amp;q=80&amp;zc=1';
		}
		
	};
	
	if ($echoout) echo $output;
	else return $output;
	
}
function content($num) {  
$theContent = get_the_content();  
$theContent .= " ";
$limit = $num+1;  
$content = explode(' ', $theContent, $limit);  
array_pop($content);
if(sizeof($content)>=100)  
$content = implode(" ",$content)."...";  
else
$content = implode(" ",$content);
echo $content;  
}
function string_limit_words($string, $word_limit)
{
$words = explode(' ', $string, ($word_limit + 1));
if(count($words) > $word_limit)
array_pop($words);
return implode(' ', $words);
}
add_theme_support( 'post-thumbnails' );
/* Adds jquery 1.3.2 for wordpress < 2.8 */
function jquery_script(){
	if ((substr($GLOBALS['wp_version'],0,3)) >= 2.8) { wp_enqueue_script('jquery'); }
	else { wp_deregister_script('jquery');
	   wp_register_script('jquery', 'http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js', false, '1.3.2'); }
};
add_action('wp_print_scripts', 'jquery_script',8);
/* --------------------------------------------- */

class ControlPanel {
	var $default_settings = array(
		'contentSliderCatId' => '1',
		'aboutusPageId' => '2',
		'collumnSetting' => 'three',
		'firstCatId' => '2',
		'secondCatId' => '3',
		'thirdCatId' => '4',
		'footerRight' => '5',
		'footerLeft' => '6',
		);
	var $options;
	function ControlPanel() {
		add_action('admin_menu', array(&$this, 'add_menu'));
 		add_action('admin_head', array(&$this, 'admin_head'));
		if (!is_array(get_option('Corporate Identity')))
 		add_option('Corporate Identity', $this->default_settings);
 
 		$this->options = get_option('Corporate Identity');
	}
	
	function add_menu() {
		add_theme_page('Theme Settings', 'Blue - Corporate - Hyve Theme Settings','edit_themes', 'Corporate Identity', array(&$this,'optionsmenu'));
	}
	function admin_head() {
		echo '
		<script type="text/javascript" src="' .get_template_directory_uri(). '/js/main.js"></script>
		<style type="text/css" media="screen">@import url( ' .get_template_directory_uri(). '/controlpanel.css );</style>';
	}		
	function optionsmenu() {
		if($_POST['ss_action'] == 'save') {
			$this->options['contentSliderCatId'] = $_POST['content_slider_catid'];
			$this->options['aboutusPageId'] = $_POST['aboutus_page_id'];
			$this->options['collumnSetting'] = $_POST['collumns'];
			$this->options['firstCatId'] = $_POST['first_cat_id'];
			$this->options['secondCatId'] = $_POST['second_cat_id'];
			$this->options['thirdCatId'] = $_POST['third_cat_id'];
			$this->options['footerLeft'] = $_POST['footer_left'];
			$this->options['footerRight'] = $_POST['footer_right'];
			
			update_option('Corporate Identity', $this->options);
			echo '<div class="updated fade" id="message" style="background-color: rgb(255, 251, 204); width: 500px; margin-left: 50px"><p>Corporate Identity settings <strong>saved</strong>.</p></div>';
		}
		 if($this->options['collumnSetting']=="three") {
		 	$checked = 'checked="checked"';
			$checked1 = '';
			$style = 'display:block';
			}
		 else {
		 	$checked = '';
			$checked1 = 'checked="checked"';
			$style = 'display:none"';
			}
		 
		echo '
			<form action="" method="post" class="themeform">
				<fieldset>
					<input type="hidden" name="ss_action" id="ss_action" value="save">
					<h1 style="font-size: 20px; font-family: Arial, Helvetica, sans-serif; color: #000; font-weight: normal;">Theme Settings</h1>
					<div style="padding-top: 8px;"> <h2 style="padding: 0px; margin: 0px; font-family: Arial, Helvetica, sans-serif; color: #114668; font-weight: normal; font-size: 18px;">Content Slider </h2>
					<input type="text" style="width:30px" name="content_slider_catid" id="content_slider_catid" value="'.$this->options['contentSliderCatId'].'" /> <label>Enter the <strong>Category ID</strong> for Slide Show </label> </div><br />
					
					<div style="padding-top: 8px;"> <h2 style="padding: 0px; margin: 0px; font-family: Arial, Helvetica, sans-serif; color: #114668; font-weight: normal; font-size: 18px;">Welcome Text</h2>
					<input type="text" style="width:30px" name="aboutus_page_id" id="aboutus_page_id" value="'.$this->options['aboutusPageId'].'" /><label>Enter the <strong>Page ID</strong> to display some introduction text.</label> </div> <br />
					
					<div style="padding-top: 8px;"> <h2 style="padding: 0px; margin: 0px; font-family: Arial, Helvetica, sans-serif; color: #114668; font-weight: normal; font-size: 18px;">Column Settings </h2>
					<div style="padding-top: 7px;"><input type="radio" name="collumns" id="collumns_2" value="two"  onclick="collumn(2)"'.$checked1.' /> Two Column &nbsp; &nbsp; &nbsp;
					<input type="radio" name="collumns" id="collumns_3" value="three"  onclick="collumn(3)"'. $checked.' />  <label>Three Collumn Setting </label></div> </div>
					
					<input type="text" style="width:30px" name="first_cat_id" id="first_cat_id" value="'.$this->options['firstCatId'].'"><label>Enter the First Category Id.</label><br />
					
					<input type="text" style="width:30px" name="second_cat_id" id="second_cat_id" value="'.$this->options['secondCatId'].'"><label>Enter the Second Category Id.</label><br />
					
					<div id="third_collumn" style="'.$style.'" >
					<input type="text" style="width:30px" name="third_cat_id" id="third_cat_id" value="'.$this->options['thirdCatId'].'"><label>Enter the Third Category Id.</label><br /></div> <br />
					
					<div style="padding-top: 8px;"> <h2 style="padding: 0px; margin: 0px; ffont-family: Arial, Helvetica, sans-serif; color: #114668; font-weight: normal; font-size: 18px;">Footer Columns </h2>
					<input type="text" style="width:30px" name="footer_left" id="footer_left" value="'.$this->options['footerLeft'].'"><label>Enter the Footer left column category id.</label></div>
					<input type="text" style="width:30px" name="footer_right" id="footer_right" value="'.$this->options['footerRight'].'"><label>Enter the Footer right column Page id.</label>
					<p class="submit"><input type="submit" value="Save Changes" name="cp_save" /></p>
				</fieldset>
			</form>';
	}
}
$cpanel = new ControlPanel();
$theme_options = get_option('Corporate Identity');
?>
