<?php

global 	$tab_feature_b1,
        $tab_feature_b2,
        $tab_count_b1,
        $tab_count_b2,
	        $next_page,
	        $next_page_txt,
        $seo_meta_tags,
        $geourl_meta_tags,
        $screen_width,
        $js_status,
        $theme_version;
        
global  $tab_b1_titles,
				$tab_b2_titles;


////////////////////////////////////////////////////////////////////////////////
// register/unregister bottom bars
////////////////////////////////////////////////////////////////////////////////

function register_bottom_bar_b1()
{
	if ( function_exists('register_sidebar') )
		register_sidebar(array(
			'name' => 'BottomBar-1',
			'id' => 'BottomBar-1',
			'before_title' => '<h2>',
			'after_title' => '</h2>',
			));
}

function register_bottom_bar_b2()
{
	if ( function_exists('register_sidebar') )
		register_sidebar(array(
			'name' => 'BottomBar-2',
			'id' => 'BottomBar-2',
			'before_title' => '<h2>',
			'after_title' => '</h2>',
			));
}

function unregister_bottom_bar_b1()
{
	if ( function_exists('unregister_sidebar') )
	  unregister_sidebar('BottomBar-1');
}

function unregister_bottom_bar_b2()
{
	if ( function_exists('unregister_sidebar') )
	  unregister_sidebar('BottomBar-2');
}



////////////////////////////////////////////////////////////////////////////////
// register/unregister bottom tabs
////////////////////////////////////////////////////////////////////////////////
function register_b1_tabs()
{
	global 	$tab_feature_b1,
	        $tab_feature_b2,
	        $tab_count_b1,
	        $tab_count_b2;

	if ( function_exists('register_sidebar') )
	{
		for ( $i = 1; $i <= $tab_count_b1; $i++ )
		{
		  $name = 'BottomBar-1 Tab-' . $i;
		  
			register_sidebar(array(
				'name' => $name,
				'id' => $name,
				'before_title' => '<h2>',
				'after_title' => '</h2>',
				));
		}
	}
}

function register_b2_tabs()
{
	global 	$tab_feature_b1,
	        $tab_feature_b2,
	        $tab_count_b1,
	        $tab_count_b2;

	if ( function_exists('register_sidebar') )
	{
		for ( $i = 1; $i <= $tab_count_b2; $i++ )
		{
		  $name = 'BottomBar-2 Tab-' . $i;

			register_sidebar(array(
				'name' => $name,
				'id' => $name,
				'before_title' => '<h2>',
				'after_title' => '</h2>',
				));
		}
	}
}


////////////////////////////////////////////////////////////////////////////////
//	add tab titles options
////////////////////////////////////////////////////////////////////////////////
function	add_tab_b1_titles()
{
	global  $tab_b1_titles,
	        $tab_count_b1;

	$def_titles = '';
	for ( $i = 1; $i <= $tab_count_b1; $i++ )
	{
	  $def_titles[ $i ] = 'Tab' . $i;
	}

	add_option( 'gn_tab_b1_titles', $def_titles );
}


function	add_tab_b2_titles()
{
	global  $tab_b2_titles,
	        $tab_count_b2;

	$def_titles = '';
	for ( $i = 1; $i <= $tab_count_b2; $i++ )
	{
	  $def_titles[ $i ] = 'Tab' . $i;
	}

	add_option( 'gn_tab_b2_titles', $def_titles );
}


////////////////////////////////////////////////////////////////////////////////
//	get titles
////////////////////////////////////////////////////////////////////////////////
function  get_tab_b1_titles()
{
	global  $tab_b1_titles;
	
	$tab_b1_titles 	= get_option('gn_tab_b1_titles');
}

function  get_tab_b2_titles()
{
	global  $tab_b2_titles;

	$tab_b2_titles 	= get_option('gn_tab_b2_titles');
}


////////////////////////////////////////////////////////////////////////////////
// load theme wp-admin css
////////////////////////////////////////////////////////////////////////////////
function gn_load_css()
{
	$theme_option_css_url = get_template_directory_uri();
	
	echo 	"\n\n";
	echo 	'<!-- German NewsPaper - Theme Option CSS -->' . "\n";
	echo 	'<!--[if gte IE 6]>' . "\n" . '<link rel="stylesheet" href="' . $theme_option_css_url . '/css/theme-option.css" type="text/css" />' . "\n" . '<![endif]-->' . "\n";
	echo 	'<link rel="stylesheet" type="text/css" href="' . $theme_option_css_url .
				'/css/theme-option.css" />';
	echo 	"\n\n";
}


////////////////////////////////////////////////////////////////////////////////
// get posts with most comments
////////////////////////////////////////////////////////////////////////////////
function get_mostcommented($limit = 5)
{
	global $wpdb, $post;

	$mostcommenteds = $wpdb->get_results("SELECT  $wpdb->posts.ID, post_title, post_name, post_date, COUNT($wpdb->comments.comment_post_ID) AS 'comment_total' FROM $wpdb->posts LEFT JOIN $wpdb->comments ON $wpdb->posts.ID = $wpdb->comments.comment_post_ID WHERE comment_approved = '1' AND post_date_gmt < '".gmdate("Y-m-d H:i:s")."' AND post_status = 'publish' AND post_password = '' GROUP BY $wpdb->comments.comment_post_ID ORDER  BY comment_total DESC LIMIT $limit");

	foreach ($mostcommenteds as $post)
	{
		$post_title = htmlspecialchars(stripslashes($post->post_title));
		$comment_total = (int) $post->comment_total;
		echo "<li><a href=\"".get_permalink()."\">$post_title&nbsp;<strong>($comment_total)</strong></a></li>";
	}
}



////////////////////////////////////////////////////////////////////////////////
//	check screen width
//
////////////////////////////////////////////////////////////////////////////////
function  check_screen_width()
{
	global  $screen_width;
	
	$screen_width = get_option('gn_screen_width');
}

////////////////////////////////////////////////////////////////////////////////
//	check tab feature
//	if necessary register tabs
////////////////////////////////////////////////////////////////////////////////
function  check_tab_feature()
{
	global 	$tab_feature_b1,
	        $tab_feature_b2,
	        $tab_count_b1,
	        $tab_count_b2;

	$tab_feature_b1 = get_option('gn_tab_feature_b1');
	$tab_feature_b2 = get_option('gn_tab_feature_b2');
	$tab_count_b1 	= get_option('gn_tab_count_b1');
	$tab_count_b2 	= get_option('gn_tab_count_b2');

	if ( $tab_feature_b1 == 'off' )
	{
  	register_bottom_bar_b1();
	}
	else
	{
	  register_b1_tabs();
	}
	
	if ( $tab_feature_b2 == 'off' )
	{
		register_bottom_bar_b2();
	}
	else
	{
	  register_b2_tabs();
	}

  add_tab_b1_titles();
  add_tab_b2_titles();
}




////////////////////////////////////////////////////////////////////////////////
//	write header css
////////////////////////////////////////////////////////////////////////////////
function  write_body_css()
{
  global  $gn_body_color,         
		      $gn_body_background,    
		      $gn_wrapper_background; 


	$save_dir  = getcwd();
	$file_dir  = TEMPLATEPATH;


	if ( !chdir( $file_dir ) )
	{
		_e("	<div class=\"error\">
					<p><strong>Can't change file directory !!!</strong></p>
				</div>\n");
	}

	$myFile = $file_dir . '/css/body.css';

	if ( !chmod($myFile, 0755) )
	{
		_e("	<div class=\"error\">
					<p><strong>Can't change file permissions !!!</strong></p>
				</div>\n");
	}

	$fh = fopen($myFile, 'w+');

	if ( $fh )
	{
    $stringData = ".body_color" . "\n" .
                  "{" . "\n" .
                  "    color:      " . $gn_body_color . ";" . "\n" . 
                  "    background: " . $gn_body_background . ";" . "\n" .
                  "}" . "\n\n" .
                  ".wrapper_color" . "\n" .
                  "{" . "\n" .
                  "  background:   " . $gn_wrapper_background . ";" . "\n" .
                  "}" . "\n";

		fwrite($fh, $stringData);
	}
	else
	{
		_e("	<div class=\"error\">
					<p><strong>Can't open file: body.css!</strong></p>
				</div>\n");
	}

	if ( !rewind($fh) )
	{
		_e("	<div class=\"error\">
					<p><strong>Can't set file handler back!</strong></p>
				</div>\n");
	}
	if ( !chdir( $save_dir ) )
	{
		_e("	<div class=\"error\">
					<p><strong>Can't set directory back!</strong></p>
				</div>\n");
	}

	fclose($fh);
}



////////////////////////////////////////////////////////////////////////////////
//	write header css
////////////////////////////////////////////////////////////////////////////////
function  write_header_css()
{
	global $header_img,
				 $img_background;

	$save_dir  = getcwd();
	$file_dir  = TEMPLATEPATH;

  $show_header  = get_option("gn_show_header");
  $header_img   = get_option("gn_header_img");

	if ( !chdir( $file_dir ) )
	{
		_e("	<div class=\"error\">
					<p><strong>Can't change file directory !!!</strong></p>
				</div>\n");
	}

	$myFile = $file_dir . '/css/header.css';

	if ( !chmod($myFile, 0755) )
	{
		_e("	<div class=\"error\">
					<p><strong>Can't change file permissions !!!</strong></p>
				</div>\n");
	}

	$fh = fopen($myFile, 'w+');

	if ( $fh )
	{
		$stringData = 	"#header\n" .
				"{\n" .
				"	height:		auto;\n" .
				"	text-align:	center;\n";
				
    if ( $show_header == 'off' )
    {
  		$stringData .= "	background:	transparent url(../" . $header_img . ") no-repeat 25px 10px;\n";
    }
    
		$stringData .=
				"	border-bottom:	3px solid #000;\n" .
				"	font-family:	Georgia;\n" .
				"	position: 	relative;\n" .
				"	float: 	left;\n" .
				"	width:  	100%;\n" .
				"}\n" .
				"\n" .
				"img\n" .
				"{\n" .
				" background: " . $img_background . ";\n" .
				"}\n";

		fwrite($fh, $stringData);
	}
	else
	{
		_e("	<div class=\"error\">
					<p><strong>Can't open file: header.css!</strong></p>
				</div>\n");
	}

	if ( !rewind($fh) )
	{
		_e("	<div class=\"error\">
					<p><strong>Can't set file handler back!</strong></p>
				</div>\n");
	}
	if ( !chdir( $save_dir ) )
	{
		_e("	<div class=\"error\">
					<p><strong>Can't set directory back!</strong></p>
				</div>\n");
	}

	fclose($fh);
}



////////////////////////////////////////////////////////////////////////////////
//	update theme options
////////////////////////////////////////////////////////////////////////////////
function  update_gn_options()
{
	global 	$tab_feature_b1,
	        $tab_feature_b2,
	        $tab_count_b1,
	        $tab_count_b2,
	        $next_page,
	        $next_page_txt,
					$header_img,
					$show_header,
					$img_background,
					$seo_meta_tags,
					$geourl_meta_tags,
	        $screen_width;
  global  $gn_body_color,         
		      $gn_body_background,    
		      $gn_wrapper_background; 

  $new_gn_tab_b1_titles = array();
  $new_gn_tab_b2_titles = array();

	//  check if remove all widgets is set
	if ( isset( $_POST['new_gn_rem_widgets'] ) )
	{
	  gn_remove_all_widgets();
	}
	
	//  check if screen width checkbox is set --> 800px
	if (!isset( $_POST['new_gn_screen_width'] ))
	{
	  $_POST['new_gn_screen_width'] = 'off';
	  $screen_width = 'off';
	}
	else
	{
	  $_POST['new_gn_screen_width'] = 'on';
	  $screen_width = 'on';
	}

	
	//  check if show header option checkbox is set 
	if (!isset( $_POST['new_gn_show_header'] ))
	{
	  $_POST['new_gn_show_header'] = 'off';
	  $show_header = 'off';
	}
	else
	{
	  $_POST['new_gn_show_header'] = 'on';
	  $show_header = 'on';
	}
  

  //  check if Next Page String is set
	if (!isset( $_POST['new_gn_next_page'] ))
	{
	  $_POST['new_gn_next_page'] = 'off';
	  $next_page = 'off';
	}
	else
	{
	  $_POST['new_gn_next_page'] = 'on';
	  $next_page = 'on';
	}
   

	
	//  check which meta tags checkbox is active
	if (!isset( $_POST['new_gn_seo_meta_tags'] ))
	{
	  $_POST['new_gn_seo_meta_tags'] = 'off';
	  $seo_meta_tags = 'off';
	}
	else
	{
	  $_POST['new_gn_seo_meta_tags'] = 'on';
	  $seo_meta_tags = 'on';
	}

	//  check which meta tags checkbox is active
	if (!isset( $_POST['new_gn_geourl_meta_tags'] ))
	{
	  $_POST['new_gn_geourl_meta_tags'] = 'off';
	  $geourl_meta_tags = 'off';
	}
	else
	{
	  $_POST['new_gn_geourl_meta_tags'] = 'on';
	  $geourl_meta_tags = 'on';
	}


	
	//  check if tab feature has changed and
	//  register or unregister sidebars accordingly
	if (!isset( $_POST['new_gn_tab_feature_b1'] ))
	{
	  $_POST['new_gn_tab_feature_b1'] = 'off';
	  
	  if ( get_option('gn_tab_feature_b1') == 'on' )
	  {
	    //  unregister tabs
	    if ( function_exists('unregister_sidebar') )
	    {
	      $tab_b1_old_count = get_option('gn_tab_count_b1');

	      for ( $i = 1; $i <= $tab_b1_old_count; $i++ )
	      {
				  $name = 'BottomBar-1 Tab-' . $i;
	        unregister_sidebar($name);
	      }
	    }
	    //  and register bottom_bar_b1
	    register_bottom_bar_b1();
	  }
	  else
	  {
	    //  tab feature was off ==> nothing to do
	  }
    //  and set varaible to off
    $tab_feature_b1 = 'off';
	}
	else
	{
	  $_POST['new_gn_tab_feature_b1'] = 'on';
	  
	  if ( get_option('gn_tab_feature_b1') == 'off')
	  {
	    //  first unregister bottom_menu_b1
	    unregister_bottom_bar_b1();
	    
	    //  and then register tabs
			if ( function_exists('register_sidebar') )
			{
				$tab_count_b1 = (int)$_POST['new_gn_tab_count_b1'];
				for ( $i=1; $i <= $tab_count_b1; $i++ )
				{
				  $name = 'BottomBar-1 Tab-' . $i;
					register_sidebar(array(
						'name' => $name,
						'id' => $name,
						'before_title' => '<h2>',
						'after_title' => '</h2>',
						));
				}
			}
	  }
	  else
	  {
	    //  check if count has changed
			$tab_count_b1 = (int)$_POST['new_gn_tab_count_b1'];
      $tab_b1_old_count = get_option('gn_tab_count_b1');

			if ( $tab_b1_old_count != $tab_count_b1 )
			{
			  if ( $tab_b1_old_count > $tab_count_b1 )
			  {
			    //  unregister sidebars
			    for ( $i = $tab_count_b1 + 1; $i <= $tab_b1_old_count; $i++)
			    {
					  $name = 'BottomBar-1 Tab-' . $i;
       	    if ( function_exists('unregister_sidebar') )
       	      unregister_sidebar( $name );
			    }
			  }
			  else
			  {
			    //  register sidebars
			    for ( $i = $tab_b1_old_count + 1; $i <= $tab_count_b1; $i++)
			    {
					  $name = 'BottomBar-1 Tab-' . $i;
			      if ( function_exists('register_sidebars') )
			      {
							register_sidebar(array(
								'name' => $name,
								'id' => $name,
								'before_title' => '<h2>',
								'after_title' => '</h2>',
								));
    	      }
			    }
			  }
			}
	  }
		//  and set variable to on
		$tab_feature_b1 = 'on';
	}

	if (!isset( $_POST['new_gn_tab_feature_b2'] ))
	{
	  $_POST['new_gn_tab_feature_b2'] = 'off';

	  if ( get_option('gn_tab_feature_b2') == 'on' )
	  {
	    //  unregister tabs
      $tab_b2_old_count = get_option('gn_tab_count_b2');

      for ( $i = 1; $i <= $tab_b2_old_count; $i++ )
      {
			  $name = 'BottomBar-2 Tab-' . $i;
        unregister_sidebar($name);
      }
      
      //  register Bottom Bar 2
      register_bottom_bar_b2();
	  }
	  else
	  {
	    //  tab feature was off ==> nothing to do
	  }
    //  and set varaible to off
    $tab_feature_b2 = 'off';
	}
	else
	{
	  $_POST['new_gn_tab_feature_b2'] = 'on';

	  if ( get_option('gn_tab_feature_b2') == 'off')
	  {
	    //  first unregister bottom_menu_b2
	    unregister_bottom_bar_b2();

	    //  and then register tabs
			if ( function_exists('register_sidebar') )
			{
				$tab_count_b2 = (int)$_POST['new_gn_tab_count_b2'];
				for ( $i=1; $i <= $tab_count_b2; $i++ )
				{
				  $name = 'BottomBar-2 Tab-' . $i;
					register_sidebar(array(
						'name' => $name,
						'id' => $name,
						'before_title' => '<h2>',
						'after_title' => '</h2>',
						));
				}
			}
	  }
	  else
	  {
	    //  check if count has changed
			$tab_count_b2 = (int)$_POST['new_gn_tab_count_b2'];
      $tab_b2_old_count = get_option('gn_tab_count_b2');

			if ( $tab_b2_old_count != $tab_count_b2 )
			{
			  if ( $tab_b2_old_count > $tab_count_b2 )
			  {
			    //  unregister sidebars
			    for ( $i = $tab_count_b2 + 1; $i <= $tab_b2_old_count; $i++)
			    {
					  $name = 'BottomBar-2 Tab-' . $i;
       	    if ( function_exists('unregister_sidebar') )
       	      unregister_sidebar( $name );
			    }
			  }
			  else
			  {
			    //  register sidebars
			    for ( $i = $tab_b2_old_count + 1; $i <= $tab_count_b2; $i++)
			    {
					  $name = 'BottomBar-2 Tab-' . $i;
			      if ( function_exists('register_sidebars') )
			      {
							register_sidebar(array(
								'name' => $name,
								'id' => $name,
								'before_title' => '<h2>',
								'after_title' => '</h2>',
								));
    	      }
			    }
			  }
			}
	  }
		//  and set variable to on
		$tab_feature_b2 = 'on';
	}

	//  build
	//  gn_tab_b1_titles      and
	//  gn_tab_b2_titles
	for ( $i = 1; $i <= $tab_count_b1; $i++)
	{
	  $field_name = 'new_gn_b1_tab_' . $i . '_title';
		$new_gn_tab_b1_titles[ $i ] = $_POST[$field_name];
	}
	for ( $i = 1; $i <= $tab_count_b2; $i++)
	{
	  $field_name = 'new_gn_b2_tab_' . $i . '_title';
		$new_gn_tab_b2_titles[ $i ] = $_POST[$field_name];
	}

	//  write header css if changed
	if  ( $_POST['new_gn_header_img'] != get_option( 'gn_header_img' ) )
	{
		$header_img = $_POST['new_gn_header_img'];
		write_header_css();
	}

	//  write next_page_txt if changed
	if  ( $_POST['new_gn_next_page_txt'] != get_option( 'gn_next_page_txt' ) )
	{
		$next_page_txt = $_POST['new_gn_next_page_txt'];
	}

	//  write image background if changed
	if  ( $_POST['new_gn_img_background'] != get_option( 'gn_img_background' ) )
	{
		$img_background = $_POST['new_gn_img_background'];
	}

	//  write body css class if changed
  if ( ($_POST['new_gn_body_color'] != get_option( 'gn_body_color' )) OR 
       ($_POST['new_gn_body_background'] != get_option( 'gn_body_background' )) OR 
       ($_POST['new_gn_wrapper_background'] != get_option( 'gn_wrapper_background' )) ) 
  {
		$gn_body_color          = $_POST['new_gn_body_color'];
		$gn_body_background     = $_POST['new_gn_body_background'];
		$gn_wrapper_background  = $_POST['new_gn_wrapper_background'];
		write_body_css();
  } 

	update_option( 'gn_show_header', $_POST['new_gn_show_header'] );
	update_option( 'gn_header_img', $_POST['new_gn_header_img'] );
	update_option( 'gn_img_background', $_POST['new_gn_img_background'] );
	update_option( 'gn_left_img', $_POST['new_gn_left_img'] );
	update_option( 'gn_right_img', $_POST['new_gn_right_img'] );
	update_option( 'gn_left_img_atxt', $_POST['new_gn_left_img_atxt'] );
	update_option( 'gn_right_img_atxt', $_POST['new_gn_right_img_atxt'] );
	update_option( 'gn_left_img_lnk', $_POST['new_gn_left_img_lnk'] );
	update_option( 'gn_right_img_lnk', $_POST['new_gn_right_img_lnk'] );
	update_option( 'gn_next_page', $_POST['new_gn_next_page'] );
	update_option( 'gn_next_page_txt', $_POST['new_gn_next_page_txt'] );
	update_option( 'gn_page_exclude', $_POST['new_gn_page_exclude'] );
	update_option( 'gn_body_color', $_POST['new_gn_body_color'] );
	update_option( 'gn_body_background', $_POST['new_gn_body_background'] );
	update_option( 'gn_wrapper_background', $_POST['new_gn_wrapper_background'] );
	update_option( 'gn_seo_meta_author', $_POST['new_gn_seo_meta_author'] );
	update_option( 'gn_seo_meta_desc', $_POST['new_gn_seo_meta_desc'] );
	update_option( 'gn_seo_meta_keyw', $_POST['new_gn_seo_meta_keyw'] );
	update_option( 'gn_geourl_loc', $_POST['new_gn_geourl_loc'] );
	update_option( 'gn_geourl_meta_tags', $_POST['new_gn_geourl_meta_tags'] );
	update_option( 'gn_geourl_loc', $_POST['new_gn_geourl_loc'] );
	update_option( 'gn_geourl_title', $_POST['new_gn_geourl_title'] );
	update_option( 'gn_screen_width', $_POST['new_gn_screen_width'] );
	update_option( 'gn_tab_feature_b1', $_POST['new_gn_tab_feature_b1'] );
	update_option( 'gn_tab_feature_b2', $_POST['new_gn_tab_feature_b2'] );
	update_option( 'gn_tab_count_b1', $_POST['new_gn_tab_count_b1'] );
	update_option( 'gn_tab_count_b2', $_POST['new_gn_tab_count_b2'] );
	update_option( 'gn_tab_b1_titles', $new_gn_tab_b1_titles );
	update_option( 'gn_tab_b2_titles', $new_gn_tab_b2_titles );

	echo "	<div class=\"updated\">" . "\n"; 
  echo "    <p><strong>Options updated!</strong></p>" . "\n";
  echo "	</div>" . "\n\n";
				
  //  print info in status bar				
  echo '<script language="JavaScript" type="text/javascript">' . "\n";
  echo '//<!-- hide for old Browser' . "\n";
  echo '//<![CDATA[' . "\n";
  echo 'window.status = "German Newspaper - Theme Options Updated !"' . "\n";
  echo '//]]> */' . "\n";
  echo '// End hide -->' . "\n";
  echo '</script>' . "\n";				


				
	if ( isset( $_POST['new_gn_rem_widgets'] ) )
	{
		_e("	<div class=\"updated\">
						<p><strong>All registered widgets deleted !!!</strong></p>
					</div>\n");
	}
	
  //  write the header.css file new  
  write_header_css();
	
}


////////////////////////////////////////////////////////////////////////////////
// remove all widgets from all sidebars
////////////////////////////////////////////////////////////////////////////////
function gn_remove_all_widgets()
{
	global $wpdb;

	$sql_stmt = "DELETE FROM $wpdb->options WHERE option_name = 'sidebars_widgets'";

	$sql_rc = $wpdb->query( $sql_stmt );
}




?>