<?php

if ( function_exists('register_sidebar') )
    register_sidebars(4,array(
        'before_widget' => '<div class="widget">',
        'after_widget' => '</div><!-- close widget-->',
        'before_title' => '<h2>',
        'after_title' => '</h2>',
    ));



	function LimitText($Text,$Min,$Max,$MinAddChar)
	{
	    if (strlen($Text) < $Min)
	    {
	        $Limit = $Min-strlen($Text);
	        $Text .= $MinAddChar;
	    }
	    elseif (strlen($Text) >= $Max)
	    {
	        $words = explode(" ", $Text);
	        $check=1;
	        while (strlen($Text) >= $Max)
	        {
	            $c=count($words)-$check;
	            $Text=substr($Text,0,(strlen($words[$c])+1)*(-1));
	            $check++;
	        }
	    }

	    return $Text;
}


function bdw_get_images($postID,$imgwidth,$imgheight)
{

    // Get the post ID
    $iPostID = $postID;

    // Get images for this post
    $arrImages =& get_children('post_type=attachment&post_mime_type=image&post_parent=' . $iPostID );

    // If images exist for this page
    if($arrImages)
    {

        // Get array keys representing attached image numbers
        $arrKeys = array_keys($arrImages);

		/******BEGIN BUBBLE SORT BY MENU ORDER************
		// Put all image objects into new array with standard numeric keys (new array only needed while we sort the keys)
		foreach($arrImages as $oImage)
		{
			$arrNewImages[] = $oImage;
		}

		// Bubble sort image object array by menu_order TODO: Turn this into std "sort-by" function in functions.php
		for($i = 0; $i < sizeof($arrNewImages) - 1; $i++)
		{
			for($j = 0; $j < sizeof($arrNewImages) - 1; $j++)
			{
				if((int)$arrNewImages[$j]->menu_order > (int)$arrNewImages[$j + 1]->menu_order)
				{
					$oTemp = $arrNewImages[$j];
					$arrNewImages[$j] = $arrNewImages[$j + 1];
					$arrNewImages[$j + 1] = $oTemp;
				}
			}
		}

		// Reset arrKeys array
		$arrKeys = array();

		// Replace arrKeys with newly sorted object ids
		foreach($arrNewImages as $oNewImage)
		{
			$arrKeys[] = $oNewImage->ID;
		}
		******END BUBBLE SORT BY MENU ORDER**************/

        // Get the first image attachment
        $iNum = $arrKeys[0];

        // Get the thumbnail url for the attachment
        $sThumbUrl = wp_get_attachment_thumb_url($iNum);

        // UNCOMMENT THIS IF YOU WANT THE FULL SIZE IMAGE INSTEAD OF THE THUMBNAIL
        //$sImageUrl = wp_get_attachment_url($iNum);

        // Build the <img> string
        $sImgString = '<a href="' . get_permalink() . '">' .
                            '<img src="' . $sThumbUrl . '" width="' . $imgwidth . '" height="' . $imgheight . '" alt="' . get_the_title($iPostID) . '" title="' . get_the_title($iPostID) . '" border="0"/>' .
                        '</a>';

        // Print the image
        echo $sImgString;
	}
}

function get_image_for_crop($postID)
{

    // Get the post ID
    $iPostID = $postID;

    // Get images for this post
    $arrImages =& get_children('post_type=attachment&post_mime_type=image&post_parent=' . $iPostID );

    // If images exist for this page
    if($arrImages)
    {

        // Get array keys representing attached image numbers
        $arrKeys = array_keys($arrImages);

		/******BEGIN BUBBLE SORT BY MENU ORDER************
		// Put all image objects into new array with standard numeric keys (new array only needed while we sort the keys)
		foreach($arrImages as $oImage)
		{
			$arrNewImages[] = $oImage;

		}

		// Bubble sort image object array by menu_order TODO: Turn this into std "sort-by" function in functions.php
		for($i = 0; $i < sizeof($arrNewImages) - 1; $i++)
		{
			for($j = 0; $j < sizeof($arrNewImages) - 1; $j++)
			{
				if((int)$arrNewImages[$j]->menu_order > (int)$arrNewImages[$j + 1]->menu_order)
				{
					$oTemp = $arrNewImages[$j];
					$arrNewImages[$j] = $arrNewImages[$j + 1];
					$arrNewImages[$j + 1] = $oTemp;
				}
			}
		}

		// Reset arrKeys array
		$arrKeys = array();

		// Replace arrKeys with newly sorted object ids
		foreach($arrNewImages as $oNewImage)
		{
			$arrKeys[] = $oNewImage->ID;
		}
		******END BUBBLE SORT BY MENU ORDER**************/

        // Get the first image attachment
        $iNum = $arrKeys[0];

        $sImageUrl = wp_get_attachment_url($iNum);

       return $sImageUrl;
	}
}


// Handle Theme Options


// VARIABLES


$this_theme = "Saturday 658";
$themeoptionsprefix = "6609";
$options = array();
$arrnumposts=array('4','5','6','7','8','9','10');
$numfeaturedarr=array('3','4','5','6','7','8','9','10');
$styleschemeoptions=get_style_scheme_options();


$thecatlist_A = get_categories('hide_empty=0');
$catdlist = array();

foreach ($thecatlist_A as $catforlist)
{
	$catdlist[$catforlist->cat_ID] = $catforlist->cat_name;
}

$categories_tmp = array_unshift($catdlist, "Select a category:");

$pathtothemetemplate = get_bloginfo('template_directory');

$menuico6609=TEMPLATEPATH . '/images/6609ico.png';

$options = array (

array("optionname" => "General Settings",
"optiontype" => "heading"),

array("optionname" => "Site Logo",
"optiondescription" => "<p>You can replace the text title with a logo by entering the logo URL here Note:include http://</p>",
"optionid" => $themeoptionsprefix."_sitelogo",
"selectoptiondef" => "",
"optiontype" => "text"),

array("optionname" => "Style scheme to use",
"optiondescription" => "<p>You can change betweeen style schemes by selecting the scheme of choice.</p>",
"optionid" => $themeoptionsprefix."_stylescheme",
"selectoptiondef" => "Select a scheme:",
"optiontype" => "select",
"options" => $styleschemeoptions),

array("optionname" => "Site Tracking Code Example: Google Analytics",
"optiondescription" => "<p>Using Goolge Analytics? You can enter your code below.</p>",
"optionid" => $themeoptionsprefix."_sitetrackingcode",
"selectoptiondef" => "",
"optiontype" => "textarea"),

array("optionname" => "Header Banner",
"optiondescription" => "<p>You can display a banner in the header of your page to the far right of your title/logo by entering the complete banner code</p>",
"optionid" => $themeoptionsprefix."_bannerad",
"selectoptiondef" => "",
"optiontype" => "textarea"),

array("optionname" => "Sidebar Ad Code",
"optiondescription" => "<p>You can display adds in your sidebar. For best results use 250 pixels wide ad or 280 pixels wide ad</p>",
"optionid" => $themeoptionsprefix."_sidebarad",
"selectoptiondef" => "",
"optiontype" => "textarea"),


array("optionname" => "Feedburner RSS URL",
"optiondescription" => "<p>Your Feedburner URL</p>",
"optionid" => $themeoptionsprefix."_feedburnerurl",
"selectoptiondef" => "",
"optiontype" => "text"),

array("optionname" => "Feedburner ID",
"optiondescription" => "<p>Your Feedburner ID here.</p>",
"optionid" => $themeoptionsprefix."_feedburnerid",
"selectoptiondef" => "",
"optiontype" => "text"),

array("optionname" => "Header for Sidebar Subscribe Block",
"optiondescription" => "<p>The text to display as the header above the subscribe links in the sidebar.</p>",
"optionid" => $themeoptionsprefix."_rsssubscribeblockh2",
"selectoptiondef" => "",
"optiontype" => "text"),

array("optionname" => "Subtext for rss subscribe block",
"optiondescription" => "<p>The text to display as a short description for why to subscribe to your feed.</p>",
"optionid" => $themeoptionsprefix."_rsssubscribeblocksubtext",
"selectoptiondef" => "",
"optiontype" => "text"),

array("optionname" => "Subscribe Link Text",
"optiondescription" => "<p>The text to use for your subscribe link.</p>",
"optionid" => $themeoptionsprefix."_rsssubscribeblocklinktext",
"selectoptiondef" => "",
"optiontype" => "text"),

array("optionname" => "Featured Category for slideshow",
"optiondescription" => "<p>Select the category from which your featured content will be pulled for the featured slideshow.</p>",
"optionid" => $themeoptionsprefix."_featuredcat",
"selectoptiondef" => "Select Category:",
"optiontype" => "select",
"options" => $catdlist),


array("optionname" => "Number of Featured Posts to show",
"optiondescription" => "<p>You can set the number of featured posts to show in the featured slideshow.</p>",
"optionid" => $themeoptionsprefix."_numpostsfeatured",
"selectoptiondef" => "Select a Number:",
"optiontype" => "select",
"options" => $numfeaturedarr),

array("optionname" => "Number of Posts on Home Page",
"optiondescription" => "<p>You can set the number of posts to show on the homepage.</p>",
"optionid" => $themeoptionsprefix."_numpostshome",
"selectoptiondef" => "Select a Number:",
"optiontype" => "select",
"options" => $arrnumposts),

);

function do_options_update_theme()
{
	global $this_theme, $options;

	if ( $_GET['page'] == basename(__FILE__) )
	{
		if ( 'updateoptions' == $_REQUEST['action'] )
		{

			foreach ($options as $optionvalue)
			{
				update_option( $optionvalue['optionid'], $_REQUEST[ $optionvalue['optionid'] ] );
			}

			foreach ($options as $optionvalue)
			{

					if( isset( $_REQUEST[ $optionvalue['optionid'] ] ) )
					{
						update_option( $optionvalue['optionid'], $_REQUEST[ $optionvalue['optionid'] ]  );
					}
					else
					{
						delete_option( $optionvalue['optionid'] );
					}

			}

			header("Location: admin.php?page=functions.php&optionsupdated=true");

			die;

		}
	}

	add_menu_page($this_theme." Options", $this_theme." Options", 'edit_themes', basename(__FILE__), 'do_page_6609', $menuico6609);
}

function get_style_scheme_options()
{

	$styleschemeoptionsarr=array();
	$stylespath=TEMPLATEPATH . '/styles';

		if (is_dir($stylespath))
		{

		  $stylesavailable=opendir($stylespath);

		   if ($stylesavailable)
		   {
			   while( $thestyledir = readdir($stylesavailable) )
			   {

					if( is_valid_style_dir($thestyledir) )
					{

						$styleschemeoptionsarr[]=$thestyledir;

					}
				}
			}

			closedir($stylesavailable);

		}

		return $styleschemeoptionsarr;

}

function is_valid_style_dir( $thestyledir )
{

	if( $thestyledir == '.' || $thestyledir == '..'  )
	{
		$isvalidstyledir=0;
	}
	else
	{
		$isvalidstyledir=1;
	}

	return $isvalidstyledir;
}

function do_page_6609 ()
{

	global $options, $this_theme;

?>

	<div class="wrap">

    <form action="<?php echo $_SERVER['REQUEST_URI']; ?>" method="post">

	<h2><?php echo $this_theme; ?> Options</h2>

	<?php if ( $_REQUEST['optionsupdated'] ) { ?><?php echo "The options for $this_theme were saved";  } ?>

	<table>

<?php

		foreach ($options as $optionvalue)
		{

			if ( $optionvalue['optiontype'] <> "heading" )
			{

?>
				<tr>
				<td><?php echo $optionvalue['optionname']; ?></td>
				<td>
<?php
			}

				switch ( $optionvalue['optiontype'] )
				{
					case 'text':

?>
					<input name="<?php echo $optionvalue['optionid']; ?>" id="<?php echo $optionvalue['optionid']; ?>" type="<?php echo $optionvalue['optiontype']; ?>" value="<?php if ( get_settings( $optionvalue['optionid'] ) != "") { echo get_settings($optionvalue['optionid']); } else { echo $optionvalue['optionselectoptiondef']; } ?>" />

<?php

					break;
					case 'select':
?>
						<select name="<?php echo $optionvalue['optionid']; ?>" id="<?php echo $optionvalue['optionid']; ?>">
<?php

					foreach ($optionvalue['options'] as $option)
					{

?>
						<option<?php if ( get_settings( $optionvalue['optionid'] ) == $option) { echo ' selected="selected"'; } elseif ($option == $optionvalue['optionselectoptiondef']) { echo ' selected="selected"'; } ?>><?php echo $option; ?></option>

<?php
					}

?>
						</select>

<?php
					break;
					case 'textarea':
					$textareainput = $optionvalue['options'];

?>
						<textarea name="<?php echo $optionvalue['optionid']; ?>" id="<?php echo $optionvalue['optionid']; ?>" cols="100" rows="8"><?php  if( get_settings($optionvalue['optionid']) != "") { echo stripslashes(get_settings($optionvalue['optionid'])); } else { echo $optionvalue['optionselectoptiondef']; } ?></textarea>
<?php

					break;
					case "heading":

?>
						</table>
   						<h3 class="title"><?php echo $optionvalue['optionname']; ?></h3>
						<table>

<?php

					break;
					default:
					break;

				}

?>

<?php

				if ( $optionvalue['optiontype'] <> "heading" )
				{

?>
					<span><?php echo $optionvalue['optiondescription']; ?></span>
					</td></tr>

<?php

				}

?>

<?php

			}

?>

					</table>

					<p class="submit">
					<input name="updateoptions" type="submit" value="Update Options" />
					<input type="hidden" name="action" value="updateoptions" />
					</p></form>
					</div>
<?php

}

add_action('admin_menu', 'do_options_update_theme');



?>