<?php
/**
 * Core functions file for the theme. Includes other key files.
 *
 * @package Suffusion
 * @subpackage Functions
 */
$locale = get_locale();
load_textdomain('suf_theme', locate_template(array("translation/{$locale}.mo", "{$locale}.mo")));

if (!isset($content_width)) $content_width = 725;
$themename = "Suffusion";
$shortname = "suf";
$suffusion_reevaluate_styles = false;
// Option names
$color_scheme = $shortname."_color_scheme";
$sidebar_header = $shortname."_sidebar_header";
$show_shadows = $shortname."_show_shadows";
$body_style_setting = $shortname."_body_style_setting";
$body_font_style_setting = $shortname."_body_font_style_setting";
$header_style_setting = $shortname."_header_style_setting";

$font_faces = array (
	"Arial, Helvetica, sans-serif" => "<font face=\"Arial, Helvetica, sans-serif\">Arial, <span class='mac'>Arial, Helvetica,</span> <i>sans-serif</i></font>",
	"'Arial Black', Gadget, sans-serif" => "<font face=\"'Arial Black', Gadget, sans-serif\">Arial Black, <span class='mac'>Arial Black, Gadget,</span> <i>sans-serif</i></font>",
	"'Comic Sans MS', cursive" => "<font face=\"'Comic Sans MS', cursive\">Comic Sans MS, <span class='mac'>Comic Sans MS,</span> <i>cursive</i></font>",
	"'Courier New', Courier, monospace " => "<font face=\"'Courier New', Courier, monospace\">Courier New, <span class='mac'>Courier New, Courier,</span> <i>monospace</i></font>",
	"Georgia, serif" => "<font face=\"Georgia, serif\">Georgia, <span class='mac'>Georgia,</span> <i>serif</i></font>",
	"Impact, Charcoal, sans-serif" => "<font face=\"Impact, Charcoal, sans-serif\">Impact, <span class='mac'>Impact, Charcoal,</span> <i>sans-serif</i></font>",
	"'Lucida Console', Monaco, monospace" => "<font face=\"'Lucida Console', Monaco, monospace\">Lucida Console, <span class='mac'>Monaco,</span> <i>monospace</i></font>",
	"'Lucida Sans Unicode', 'Lucida Grande', sans-serif" => "<font face=\"'Lucida Sans Unicode', 'Lucida Grande', sans-serif\">Lucida Sans Unicode, <span class='mac'>Lucida Grande,</span> <i>sans-serif</i></font>",
	"'Palatino Linotype', 'Book Antiqua', Palatino, serif" => "<font face=\"'Palatino Linotype', 'Book Antiqua', Palatino, serif\">Palatino Linotype, Book Antiqua, <span class='mac'>Palatino,</span> <i>serif</i></font>",
	"Tahoma, Geneva, sans-serif" => "<font face=\"Tahoma, Geneva, sans-serif\">Tahoma, <span class='mac'>Geneva,</span> <i>sans-serif</i></font>",
	"'Times New Roman', Times, serif" => "<font face=\"'Times New Roman', Times, serif\">Times New Roman, <span class='mac'>Times,</span> <i>serif</i></font>",
	"'Trebuchet MS', Helvetica, sans-serif" => "<font face=\"'Trebuchet MS', Helvetica, sans-serif\">Trebuchet MS, <span class='mac'>Helvetica,</span> <i>sans-serif</i></font>",
	"Verdana, Geneva, sans-serif" => "<font face=\"Verdana, Geneva, sans-serif\">Verdana, <span class='mac'>Verdana, Geneva,</span> <i>sans-serif</i></font>",
	"Symbol" => "<font face=\"Symbol\">Symbol, <span class='mac'>Symbol</span></font> (\"Symbol\" works in IE and Chrome on Windows and in Safari on Mac)",
	"Webdings" => "<font face=\"Webdings\">Webdings, <span class='mac'>Webdings</span></font> (\"Webdings\" works in IE and Chrome on Windows and in Safari on Mac)",
	"Wingdings, 'Zapf Dingbats'" => "<font face=\"Wingdings, 'Zapf Dingbats'\">Wingdings, <span class='mac'>Zapf Dingbats</span></font> (\"Wingdings\" works in IE and Chrome on Windows and in Safari on Mac)",
	"'MS Sans Serif', Geneva, sans-serif" => "<font face=\"'MS Sans Serif', Geneva, sans-serif\">MS Sans Serif, <span class='mac'>Geneva,</span> <i>sans-serif</i></font>",
	"'MS Serif', 'New York', serif" => "<font face=\"'MS Serif', 'New York', serif\">MS Serif, <span class='mac'>New York,</span> <i>serif</i></font>",
);

$suffusion_options = get_option('suffusion_options');
$theme_name = suffusion_get_theme_name();
$default_theme_name = "dark-theme-green";
$suffusion_rtl_layout = false;

$all_page_ids = get_all_page_ids();
$all_page_ids = implode(',',$all_page_ids);
$all_category_ids = get_all_category_ids();
$all_category_ids = implode(',',$all_category_ids);

//Essential for multi-selects. Do not delete!!! And do not move it below the next statement!!!
$spawned_options = array();
$page_array = null;
$category_array = null;

// Global variables
$SUFFUSION_COMMENT_TYPES = array('comment' => __('Comments', 'suf_theme'), 'trackback' => __('Trackbacks', 'suf_theme'), 'pingback' => __('Pingbacks', 'suf_theme'));

$sidebar_tabs = array(
	'archives' => array('title' => __('Archives', 'suf_theme')),
	'categories' => array('title' => __('Categories', 'suf_theme')),
	'links' => array('title' => __('Links', 'suf_theme')),
	'meta' => array('title' => __('Meta', 'suf_theme')),
	'pages' => array('title' => __('Pages', 'suf_theme')),
	'recent_comments' => array('title' => __('Recent Comments', 'suf_theme')),
	'recent_posts' => array('title' => __('Recent Posts', 'suf_theme')),
	'search' => array('title' => __('Search', 'suf_theme')),
	'tag_cloud' => array('title' => __('Tag Cloud', 'suf_theme')),
	'custom_tab_1' => array('title' => __('Custom Tab 1', 'suf_theme')),
	'custom_tab_2' => array('title' => __('Custom Tab 2', 'suf_theme')),
	'custom_tab_3' => array('title' => __('Custom Tab 3', 'suf_theme')),
	'custom_tab_4' => array('title' => __('Custom Tab 4', 'suf_theme')),
	'custom_tab_5' => array('title' => __('Custom Tab 5', 'suf_theme')),
	'custom_tab_6' => array('title' => __('Custom Tab 6', 'suf_theme')),
	'custom_tab_7' => array('title' => __('Custom Tab 7', 'suf_theme')),
	'custom_tab_8' => array('title' => __('Custom Tab 8', 'suf_theme')),
	'custom_tab_9' => array('title' => __('Custom Tab 9', 'suf_theme')),
	'custom_tab_10' => array('title' => __('Custom Tab 10', 'suf_theme')),
);

$suffusion_404_title =  __("Error 404 - Not Found", "suf_theme");
$suffusion_404_content = __("Sorry, the page that you are looking for does not exist.", "suf_theme");

$suffusion_comment_label_name = __('Name', "suf_theme");
$suffusion_comment_label_req = __('(required)', "suf_theme");
$suffusion_comment_label_email = __('E-mail', "suf_theme");
$suffusion_comment_label_uri = __('URI', "suf_theme");
$suffusion_comment_label_your_comment = __('Your Comment', "suf_theme");

$social_networks = array('twitter' => 'Twitter',
                'facebook' => 'Facebook',
                'technorati' => 'Technorati',
                'linkedin' => "LinkedIn",
                'flickr' => 'Flickr',
                'delicious' => 'Delicious',
                'digg' => 'Digg',
                'stumbleupon' => 'StumbleUpon',
                'reddit' => "Reddit");

$suffusion_sitemap_entities = array(
	'pages' => array('title' => 'Pages', 'opt' => '_pages'),
	'categories' => array('title' => 'Categories', 'opt' => '_categories'),
	'authors' => array('title' => 'Authors', 'opt' => '_authors'),
	'years' => array('title' => 'Yearly Archives', 'opt' => '_yarchives'),
	'months' => array('title' => 'Monthly Archives', 'opt' => '_marchives'),
	'weeks' => array('title' => 'Weekly Archives', 'opt' => '_warchives'),
	'days' => array('title' => 'Daily Archives', 'opt' => '_darchives'),
	'tag-cloud' => array('title' => 'Tags', 'opt' => '_tags'),
	'posts' => array('title' => 'Blog Posts', 'opt' => '_posts'),
);

$all_sitemap_entities = array_keys($suffusion_sitemap_entities);
$all_sitemap_entities = implode(',', $all_sitemap_entities);

include_once (TEMPLATEPATH . "/admin/theme-definitions.php");
if (is_admin()) {
	include_once (TEMPLATEPATH . "/admin/theme-options.php");
}
$suffusion_unified_options = suffusion_get_unified_options(true);

$post_type_options = array(
	array('name' => 'post_type', 'type' => 'text', 'desc' => 'Post Type (e.g. book)', 'std' => '', 'reqd' => true),
	array('name' => 'style_inherit', 'type' => 'select', 'desc' => 'Inherit styles from:', 'std' => 'post',
		'options' => array('post' => 'Post - will get styles for Posts', 'page' => 'Page - will get styles for Pages', 'custom' => 'Custom - define your own styles')),
);

$post_type_labels = array(
	array('name' => 'name', 'type' => 'text', 'desc' => 'Name (e.g. Books)', 'std' => '', 'reqd' => true),
	array('name' => 'singular_name', 'type' => 'text', 'desc' => 'Singular Name (e.g. Book)', 'std' => '', 'reqd' => true),
	array('name' => 'add_new', 'type' => 'text', 'desc' => 'Text for "Add New" (e.g. Add New)', 'std' => ''),
	array('name' => 'add_new_item', 'type' => 'text', 'desc' => 'Text for "Add New Item" (e.g. Add New Book)', 'std' => ''),
	array('name' => 'edit_item', 'type' => 'text', 'desc' => 'Text for "Edit Item" (e.g. Edit Book)', 'std' => ''),
	array('name' => 'new_item', 'type' => 'text', 'desc' => 'Text for "New Item" (e.g. New Book)', 'std' => ''),
	array('name' => 'view_item', 'type' => 'text', 'desc' => 'Text for "View Item" (e.g. View Book)', 'std' => ''),
	array('name' => 'search_items', 'type' => 'text', 'desc' => 'Text for "Search Items" (e.g. Search Books)', 'std' => ''),
	array('name' => 'not_found', 'type' => 'text', 'desc' => 'Text for "Not found" (e.g. No Books Found)', 'std' => ''),
	array('name' => 'not_found_in_trash', 'type' => 'text', 'desc' => 'Text for "Not found in Trash" (e.g. No Books Found in Trash)', 'std' => ''),
	array('name' => 'parent_item_colon', 'type' => 'text', 'desc' => 'Parent Text with a colon (e.g. Book Series:)', 'std' => ''),
);

$post_type_args = array(
	array('name' => 'public', 'desc' => 'Public', 'type' => 'checkbox', 'default' => true),
	array('name' => 'publicly_queryable', 'desc' => 'Publicly Queriable', 'type' => 'checkbox', 'default' => true),
	array('name' => 'show_ui', 'desc' => 'Show UI', 'type' => 'checkbox', 'default' => true),
	array('name' => 'query_var', 'desc' => 'Query Variable', 'type' => 'checkbox', 'default' => true),
	array('name' => 'rewrite', 'desc' => 'Rewrite', 'type' => 'checkbox', 'default' => true),
	array('name' => 'hierarchical', 'desc' => 'Hierarchical', 'type' => 'checkbox', 'default' => true),
	array('name' => 'exclude_from_search', 'desc' => 'Exclude from Search', 'type' => 'checkbox', 'default' => true),
	array('name' => 'show_in_nav_menus', 'desc' => 'Show in Navigation menus', 'type' => 'checkbox', 'default' => true),
	array('name' => 'menu_position', 'desc' => 'Menu Position', 'type' => 'text', 'default' => null),
);

$post_type_supports = array(
	array('name' => 'title', 'desc' => 'Title', 'type' => 'checkbox', 'default' => false),
	array('name' => 'editor', 'desc' => 'Editor', 'type' => 'checkbox', 'default' => false),
	array('name' => 'author', 'desc' => 'Author', 'type' => 'checkbox', 'default' => false),
	array('name' => 'thumbnail', 'desc' => 'Thumbnail', 'type' => 'checkbox', 'default' => false),
	array('name' => 'excerpt', 'desc' => 'Excerpt', 'type' => 'checkbox', 'default' => false),
	array('name' => 'trackbacks', 'desc' => 'Trackbacks', 'type' => 'checkbox', 'default' => false),
	array('name' => 'custom-fields', 'desc' => 'Custom Fields', 'type' => 'checkbox', 'default' => false),
	array('name' => 'comments', 'desc' => 'Comments', 'type' => 'checkbox', 'default' => false),
	array('name' => 'revisions', 'desc' => 'Revisions', 'type' => 'checkbox', 'default' => false),
	array('name' => 'page-attributes', 'desc' => 'Page Attributes', 'type' => 'checkbox', 'default' => false),
);

$taxonomy_options = array(
	array('name' => 'taxonomy', 'type' => 'text', 'desc' => 'Taxonomy (e.g. genres)', 'std' => '', 'reqd' => true),
	array('name' => 'object_type', 'type' => 'text', 'desc' => 'Applicable to post types (comma-separated list e.g. book, movie)', 'std' => '', 'reqd' => true),
);

$taxonomy_labels = array(
	array('name' => 'name', 'type' => 'text', 'desc' => 'Name (e.g. Genres)', 'std' => '', 'reqd' => true),
	array('name' => 'singular_name', 'type' => 'text', 'desc' => 'Singular Name (e.g. Genre)', 'std' => '', 'reqd' => true),
	array('name' => 'search_items', 'type' => 'text', 'desc' => 'Text for "Search Items" (e.g. Search Genres)', 'std' => ''),
	array('name' => 'popular_items', 'type' => 'text', 'desc' => 'Text for "Popular Items" (e.g. Popular Genres)', 'std' => ''),
	array('name' => 'all_items', 'type' => 'text', 'desc' => 'Text for "All Items" (e.g. All Genres)', 'std' => ''),
	array('name' => 'parent_item', 'type' => 'text', 'desc' => 'Parent Item (e.g. Parent Genre)', 'std' => ''),
	array('name' => 'parent_item_colon', 'type' => 'text', 'desc' => 'Parent Item Colon (e.g. Parent Genre:)', 'std' => ''),
	array('name' => 'edit_item', 'type' => 'text', 'desc' => 'Text for "Edit Item" (e.g. Edit Genre)', 'std' => ''),
	array('name' => 'update_item', 'type' => 'text', 'desc' => 'Text for "Update Item" (e.g. Update Genre)', 'std' => ''),
	array('name' => 'add_new_item', 'type' => 'text', 'desc' => 'Text for "Add New Item" (e.g. Add New Genre)', 'std' => ''),
	array('name' => 'new_item_name', 'type' => 'text', 'desc' => 'Text for "New Item Name" (e.g. New Genre Name)', 'std' => ''),
);

$taxonomy_args = array(
	array('name' => 'public', 'desc' => 'Public', 'type' => 'checkbox', 'default' => true),
	array('name' => 'show_ui', 'desc' => 'Show UI', 'type' => 'checkbox', 'default' => true),
	array('name' => 'show_tagcloud', 'desc' => 'Show in Tagcloud widget', 'type' => 'checkbox', 'default' => true),
	array('name' => 'hierarchical', 'desc' => 'Hierarchical', 'type' => 'checkbox', 'default' => true),
	array('name' => 'rewrite', 'desc' => 'Rewrite', 'type' => 'checkbox', 'default' => true),
);

if (is_admin()) { // The following don't need to be loaded for non-admin screens
	include_once (TEMPLATEPATH . "/admin/suffusion-options-page.php");
	require_once(TEMPLATEPATH.'/admin/theme-options-renderer.php');
}

function suf_get_formatted_page_array($prefix) {
	global $spawned_options, $page_array;
	$ret = array();
	$pages = get_pages('sort_column=menu_order');
    if ($pages != null) {
        foreach ($pages as $page) {
            if (is_null($page_array)) {
                $ret[$page->ID] = array ("title" => $page->post_title, "depth" => count(get_post_ancestors($page)));
            }
            $spawned_options[count($spawned_options)] = array(  "id" => $prefix."_".$page->ID,
                "type" => "checkbox",
                "parent" => $prefix, "std" => "false");
        }
    }
	if ($page_array == null) {
		$page_array = $ret;
		return $ret;
	}
	else {
		return $page_array;
	}
}

function suf_get_formatted_category_array($prefix, $spawn = true) {
	global $spawned_options, $category_array;
	$ret = array();
	$args = array("type" => "post",
		"orderby" => "name",
		"hide_empty" => false,
	);
	$categories = get_categories($args);
	if ($categories == null) { $categories = array(); }
	foreach ($categories as $category) {
		if ($category_array == null) {
			$ret[$category->cat_ID] = array("title" => $category->cat_name);
		}
		if ($spawn) {
			$spawned_options[count($spawned_options)] = array(  "id" => $prefix."_".$category->cat_ID,
														        "type" => "checkbox",
														        "parent" => $prefix,
																"std" => "false");
		}
	}
	if ($category_array == null) {
		$category_array = $ret;
		return $category_array;
	}
	else {
		return $category_array;
	}
}

function suf_get_allowed_categories($prefix) {
	global $suffusion_options;
	$allowed = array();
	if (isset($suffusion_options[$prefix])) {
		$selected = $suffusion_options[$prefix];
		if ($selected && trim($selected) != '') { $selected_categories = explode(',', $selected); } else { $selected_categories = array(); }
		if ($selected_categories && is_array($selected_categories)) {
			foreach ($selected_categories as $category) {
				$allowed[count($allowed)] = get_category($category);
			}
		}
	}
	return $allowed;
}

if (class_exists("WP_Widget")) {
	include_once (TEMPLATEPATH . '/suffusion-widgets.php');
}
include_once (TEMPLATEPATH . '/suffusion-classes.php');

add_action('admin_menu', 'suffusion_add_page_fields');

function suffusion_add_page_fields() {
	if (function_exists('add_meta_box')) {
		add_meta_box('suffusion-page-box', 'Additional Options for Suffusion', 'suffusion_page_extras', 'page', 'normal', 'high');
		add_meta_box('suffusion-post-box', 'Additional Options for Suffusion', 'suffusion_post_extras', 'post', 'normal', 'high');
	}
}

function suffusion_page_extras() {
	global $post;
?>
	<p>
		<label for="suf_alt_page_title"><?php _e("Page Title in Dropdown Menu", "suf_theme"); ?></label><br/>
		<input type="text" id="suf_alt_page_title" name="suf_alt_page_title"
		value="<?php echo get_post_meta($post->ID, "suf_alt_page_title", true); ?>" /> <?php _e("This text will be shown in the drop-down menus in the navigation bar. If left blank, the title of the page is used.", 'suf_theme'); ?>
	</p>
	<p>
		<label for="suf_nav_unlinked"><?php _e("Do not link to this page in the navigation bars", "suf_theme"); ?></label><br/>
		<input type="checkbox" id="suf_nav_unlinked" name="suf_nav_unlinked"
			<?php if (get_post_meta($post->ID, 'suf_nav_unlinked', true)) { echo " checked='checked' ";} ?> />
			<?php _e('If this box is checked, clicking on this page in the navigation bar will not take you anywhere.', 'suf_theme'); ?>
	</p>
	<p>
		<label for="thumbnail"><?php _e("Thumbnail", "suf_theme"); ?></label><br/>
		<input type="text" id="thumbnail" name="thumbnail"
			value="<?php echo get_post_meta($post->ID, "thumbnail", true); ?>" /> <?php _e("Enter the full URL of the thumbnail image that you would like to use, including http://", "suf_theme"); ?>
	</p>
	<p>
		<label for="featured_image"><?php _e("Featured Image", "suf_theme"); ?></label><br/>
		<input type="text" id="featured_image" name="featured_image"
			value="<?php echo get_post_meta($post->ID, "featured_image", true); ?>" /> <?php _e("Enter the full URL of the featured image that you would like to use, including http://", "suf_theme"); ?>
	</p>
	<p>
		<label for="meta_description"><?php _e("Meta Description", "suf_theme"); ?></label><br/>
		<textarea id="meta_description" name="meta_description" cols='80' rows='5'><?php echo get_post_meta($post->ID, "meta_description", true); ?></textarea>
	</p>
	<p>
		<label for="meta_keywords"><?php _e("Meta Keywords", "suf_theme"); ?></label><br/>
		<input type="text" id="meta_keywords" name="meta_keywords" style='width: 500px;'
			value="<?php echo get_post_meta($post->ID, "meta_keywords", true); ?>" /> <?php _e("Enter a comma-separated list of keywords for this post. This list will be included in the meta tags for this post.", "suf_theme"); ?>
	</p>
	<input type='hidden' id='suffusion_post_meta' name='suffusion_post_meta' value='suffusion_post_meta'/>
<?php
}

function suffusion_post_extras() {
	global $post;
?>
	<p>
		<label for="thumbnail"><?php _e("Thumbnail", "suf_theme"); ?></label><br/>
		<input type="text" id="thumbnail" name="thumbnail"
			value="<?php echo get_post_meta($post->ID, "thumbnail", true); ?>" /> <?php _e("Enter the full URL of the thumbnail image that you would like to use, including http://", "suf_theme"); ?>
	</p>
	<p>
		<label for="featured_image"><?php _e("Featured Image", "suf_theme"); ?></label><br/>
		<input type="text" id="featured_image" name="featured_image"
			value="<?php echo get_post_meta($post->ID, "featured_image", true); ?>" /> <?php _e("Enter the full URL of the featured image that you would like to use, including http://", "suf_theme"); ?>
	</p>
	<p>
		<label for="suf_magazine_headline"><?php _e("Make this post a headline", "suf_theme"); ?></label><br/>
		<input type="checkbox" id="suf_magazine_headline" name="suf_magazine_headline"
			<?php if (get_post_meta($post->ID, 'suf_magazine_headline', true)) { echo " checked='checked' ";} ?> />
			<?php _e('If this box is checked, this post will show up as a headline in the magazine template.', 'suf_theme'); ?>
	</p>
	<p>
		<label for="suf_magazine_excerpt"><?php _e("Make this post an excerpt in the magazine layout", "suf_theme"); ?></label><br/>
		<input type="checkbox" id="suf_magazine_excerpt" name="suf_magazine_excerpt"
			<?php if (get_post_meta($post->ID, 'suf_magazine_excerpt', true)) { echo " checked='checked' ";} ?> />
			<?php _e('If this box is checked, this post will show up as an excerpt in the magazine template.', 'suf_theme'); ?>
	</p>
	<p>
		<label for="meta_description"><?php _e("Meta Description", "suf_theme"); ?></label><br/>
		<textarea id="meta_description" name="meta_description" cols='80' rows='5'><?php echo get_post_meta($post->ID, "meta_description", true); ?></textarea>
	</p>
	<p>
		<label for="meta_keywords"><?php _e("Meta Keywords", "suf_theme"); ?></label><br/>
		<input type="text" id="meta_keywords" name="meta_keywords" style='width: 500px;'
			value="<?php echo get_post_meta($post->ID, "meta_keywords", true); ?>" /> <?php _e("Enter a comma-separated list of keywords for this post. This list will be included in the meta tags for this post.", "suf_theme"); ?>
	</p>
	<input type='hidden' id='suffusion_post_meta' name='suffusion_post_meta' value='suffusion_post_meta'/>
<?php
}

function suffusion_save_post_fields($post_id) {
	$suffusion_post_fields = array('thumbnail', 'featured_image', 'suf_magazine_headline', 'suf_magazine_excerpt', 'suf_alt_page_title', 'meta_description', 'meta_keywords', 'suf_nav_unlinked');
    if (isset($_POST['suffusion_post_meta'])) {
        foreach ($suffusion_post_fields as $post_field) {
            $data = stripslashes($_POST[$post_field]);
            if (get_post_meta($post_id, $post_field) == '') {
                add_post_meta($post_id, $post_field, $data, true);
            }
            else if ($data != get_post_meta($post_id, $post_field, true)) {
                update_post_meta($post_id, $post_field, $data);
            }
            else if ($data == '') {
                delete_post_meta($post_id, $post_field, get_post_meta($post_id, $post_field, true));
            }
        }
    }
}

add_action("save_post", "suffusion_save_post_fields");
add_action("publish_post", "suffusion_save_post_fields");

function suffusion_export_settings() {
	global $options, $suffusion_options;
	$export = array();
	foreach ($options as $value) {
		if ((isset($value['export']) && $value['export'] == 'ne') || !isset($value['id']) || $value['type'] == 'button') {
			continue;
		}
		if (!isset($suffusion_options[$value['id']]) && isset($value['std'])) {
			$export[$value['id']] = $value['std'];
		}
		else {
			$export[$value['id']] = $suffusion_options[$value['id']];
		}
	}
	header('Content-Type: text/plain');
	header('Content-Disposition: attachment; filename="suffusion-options.php"');
	echo "<?php \n";
	echo "/* Suffusion settings exported on ".date('Y-m-d H:i')." */ \n";
	echo '$suffusion_exported_options = ';
	var_export($export);
	echo ";\n ?>";
	die;
}

add_action('wp_head', 'suf_add_header_contents');
function suf_add_header_contents() {
	suf_create_openid_links();
	suf_create_additional_feeds();
}

add_action('wp_footer', 'suf_add_footer_contents');
function suf_add_footer_contents() {
	suf_create_analytics_contents();
}

// OpenID stuff...
function suf_create_openid_links() {
	global $suf_openid_enabled, $suf_openid_server, $suf_openid_delegate;
	if ($suf_openid_enabled == "enabled") {
		echo "<!-- Start OpenID settings -->\n";
		echo "<link rel=\"openid.server\" href=\"".$suf_openid_server."\" />\n";
		echo "<link rel=\"openid.delegate\" href=\"".$suf_openid_delegate."\" />\n";
		echo "<!-- End OpenID settings -->\n";
	}
}
// ... End OpenID stuff

// Analytics ...
function suf_create_analytics_contents() {
	global $suf_analytics_enabled, $suf_custom_analytics_code;
	if ($suf_analytics_enabled == "enabled") {
		if (trim($suf_custom_analytics_code) != "") {
			echo "<!-- Start Google Analytics -->\n";
			echo stripslashes($suf_custom_analytics_code)."\n";
			echo "<!-- End Google Analytics -->\n";
		}
	}
}
// ... End Analytics

// Additional Feeds ...
function suf_create_additional_feeds() {
	global $suffusion_options;
	echo "<!-- Start Additional Feeds -->\n";
	if (isset($suffusion_options['suf_custom_rss_feed_1']) && trim($suffusion_options['suf_custom_rss_feed_1']) != "") {
		echo "<link rel=\"alternate\" type=\"application/rss+xml\" title=\"".esc_attr($suffusion_options['suf_custom_rss_title_1'])."\" href=\"".$suffusion_options['suf_custom_rss_feed_1']."\" />\n";
	}
	if (isset($suffusion_options['suf_custom_rss_feed_2']) && trim($suffusion_options['suf_custom_rss_feed_2']) != "") {
		echo "<link rel=\"alternate\" type=\"application/rss+xml\" title=\"".esc_attr($suffusion_options['suf_custom_rss_title_2'])."\" href=\"".$suffusion_options['suf_custom_rss_feed_2']."\" />\n";
	}
	if (isset($suffusion_options['suf_custom_rss_feed_3']) && trim($suffusion_options['suf_custom_rss_feed_3']) != "") {
		echo "<link rel=\"alternate\" type=\"application/rss+xml\" title=\"".esc_attr($suffusion_options['suf_custom_rss_title_3'])."\" href=\"".$suffusion_options['suf_custom_rss_feed_3']."\" />\n";
	}
	if (isset($suffusion_options['suf_custom_atom_feed_1']) && trim($suffusion_options['suf_custom_atom_feed_1']) != "") {
		echo "<link rel=\"alternate\" type=\"application/atom+xml\" title=\"".esc_attr($suffusion_options['suf_custom_atom_title_1'])."\" href=\"".$suffusion_options['suf_custom_atom_feed_1']."\" />\n";
	}
	if (isset($suffusion_options['suf_custom_atom_feed_2']) && trim($suffusion_options['suf_custom_atom_feed_2']) != "") {
		echo "<link rel=\"alternate\" type=\"application/atom+xml\" title=\"".esc_attr($suffusion_options['suf_custom_atom_title_2'])."\" href=\"".$suffusion_options['suf_custom_atom_feed_2']."\" />\n";
	}
	if (isset($suffusion_options['suf_custom_atom_feed_3']) && trim($suffusion_options['suf_custom_atom_feed_3']) != "") {
		echo "<link rel=\"alternate\" type=\"application/atom+xml\" title=\"".esc_attr($suffusion_options['suf_custom_atom_title_3'])."\" href=\"".$suffusion_options['suf_custom_atom_feed_3']."\" />\n";
	}
	echo "<!-- End Additional Feeds -->\n";
}
// ... End Additional Feeds

function get_excluded_pages($prefix) {
	global $$prefix;
	$inclusions = $$prefix;

	$all_pages = get_all_page_ids();//get_pages('sort_column=menu_order');
	if ($all_pages == null) {
		$all_pages = array();
	}

    if ($inclusions && trim($inclusions) != '') {
        $include = explode(',', $inclusions);
        $translations = wpml_lang_object_ids($include, 'post');
        foreach ($translations as $translation) {
            $include[count($include)] = $translation;
        }
    }
    else {
        $include = array();
    }

	// First we figure out which pages have to be excluded
	$exclude = array();
	foreach ($all_pages as $page) {
		if (!in_array($page, $include)) {
            $exclude[count($exclude)] = $page;
        }
	}
	// Now we need to figure out if these excluded pages are ancestors of any pages on the list. If so, we remove the descendants
	foreach ($all_pages as $page) {
		$ancestors = get_post_ancestors($page);
		foreach ($ancestors as $ancestor) {
			if (in_array($ancestor, $exclude)) {
				$exclude[count($exclude)] = $page;
			}
		}
	}

	$exclusion_list = implode(",", $exclude);
	return $exclusion_list;
}

function suffusion_get_page_structure($id, $title, $link, $depth, $exclusion_list) {
	$page_structure = new Suf_Navigation_Structure($id, $title, $link, $depth, 'page');
	$page_structure->children = array();
	$child_pages = get_pages('sort_column=menu_order&parent='.$id.'&child_of='.$id.'&exclude='.$exclusion_list);
	if ($child_pages == null) { $child_pages = array(); }
	$child_depth = $depth + 1;
	foreach ($child_pages as $child_page) {
		$link_disabled = get_post_meta($child_page->ID, 'suf_nav_unlinked', true);
		if ($link_disabled) {
			$child_page_link = "";
		}
		else {
			$child_page_link = get_page_link($child_page->ID);
		}
		$child = suffusion_get_page_structure($child_page->ID, $child_page->post_title, $child_page_link, $child_depth, $exclusion_list);
		$page_structure->children[count($page_structure->children)] = $child;
	}
	return $page_structure;
}

function suffusion_get_home_link_html($position) {
	global $suffusion_options;
	$retStr = "";
	$suf_show_home = "";
	$option_name = $position == 'top' ? 'suf_navt_show_home' : 'suf_show_home';
	if (!isset($suffusion_options[$option_name])) {
		$suf_show_home = "none";
	}
	else {
		$suf_show_home = $suffusion_options[$option_name];
	}

    $show_on_front = get_option('show_on_front');
	$class = "";
    if (is_front_page()) {
        $class = " class='highlighted' ";
    }
    else if (is_home() && $show_on_front == 'posts') {
        $class = " class='highlighted' ";
    }

	$option_name = $position == 'top' ? 'suf_navt_home_text' : 'suf_home_text';
	$home_link = get_option("home");
	if (function_exists('icl_get_home_url')) {
		$home_link = icl_get_home_url();
	}
	if ($suf_show_home == "text") {
		if ($suffusion_options[$option_name] === FALSE || $suffusion_options[$option_name] == null) {
			$suf_home_text = "Home";
		}
		else if (trim($suffusion_options[$option_name]) == "") {
			$suf_home_text = "Home";
		}
		else {
			$suf_home_text = trim($suffusion_options[$option_name]);
		}
		$retStr .= "\n\t\t\t\t\t"."<li><a $class href='".$home_link."'>".$suf_home_text."</a></li>";
	}
	else if ($suf_show_home == "icon") {
		$retStr .= "\n\t\t\t\t\t"."<li><a $class href='".$home_link."'><img src='".get_bloginfo("template_directory")."/images/home-light.png' alt='Home' class='home-icon'/></a></li>";
	}
	return $retStr;
}

/**
 * This method takes a Suffusion_Nav_Structure object and parses it out to build the navigation menu.
 * The menu is based on Stu Nicholls' famous CSS-only navigation menu: http://www.cssplay.co.uk/menus/final_drop.html. I have made minor modifications to it.
 */
function suffusion_parse_nav_structure($nav_structure, $position = 'main') {
	global $post;
	$retStr = "";
	if ($nav_structure->depth == 0) {
		$home_link = suffusion_get_home_link_html($position);
		if (($nav_structure->children != null && is_array($nav_structure->children) && count($nav_structure->children) > 0) || $home_link) {
			$retStr .= "\n\t\t\t\t\t"."<ul class='sf-menu'>";
			$retStr .= $home_link;
			foreach ($nav_structure->children as $child_page) {
				$retStr .= suffusion_parse_nav_structure($child_page, $position);
			}
			$retStr .= "\n\t\t\t\t\t"."</ul>";
		}
	}
	else if ($nav_structure->depth == 1) {
        $show_on_front = get_option('show_on_front');
        $page_for_posts = get_option('page_for_posts');
        $ancestors = get_post_ancestors($post);
		if (!is_array($ancestors)) {
			$ancestors = array();
		}

		$href = trim($nav_structure->link) == "" ? " href='#' onclick='return false;' " : " href='$nav_structure->link' ";
		if (is_page() && ($post->ID == $nav_structure->ID || in_array($nav_structure->ID, $ancestors)) && $nav_structure->type == 'page' ||
                (($nav_structure->ID == $page_for_posts) && $show_on_front == 'page' && is_home())) {
			$retStr .= "\n\t\t\t\t\t"."<li><a $href class='highlighted'>".$nav_structure->title."</a>";
		}
		else if (is_category() && (single_cat_title('', false) == $nav_structure->title)&& $nav_structure->type == 'cat') {
			$retStr .= "\n\t\t\t\t\t"."<li><a $href class='highlighted'>".$nav_structure->title."</a>";
		}
		else {
			$target = $nav_structure->target ? " target='".$nav_structure->target."' " : ' ';
			$retStr .= "\n\t\t\t\t\t"."<li><a $href $target>".$nav_structure->title."</a>";
		}
		if ($nav_structure->children != null && count($nav_structure->children) != 0) {
			$retStr .= "\n\t\t\t"."<ul>";
			foreach ($nav_structure->children as $child_page) {
				$target = $child_page->target ? " target='".$child_page->target."' " : ' ';
				$child_href = trim($child_page->link) == "" ? " href='#' onclick='return false;' " : " href='$child_page->link' ";
				//$retStr .= "\n\t\t\t\t"."<li><a href='$child_page->link' $target";
				$retStr .= "\n\t\t\t\t"."<li><a $child_href $target";
				$retStr .= suffusion_parse_nav_structure($child_page, $position);
			}
			$retStr .= "\n\t\t\t\t"."</ul>";
		}
        $retStr .= "\n\t\t\t\t"."</li>";
	}
	else {
		if ($nav_structure->children != null && count($nav_structure->children) != 0) {
			$retStr .= " class='drop' title=\"$nav_structure->title\"><!--[if !IE]>--><span class='float-ptr'>&gt;</span><!--<![endif]-->$nav_structure->title<!--[if lte IE 7]>&nbsp;&nbsp;&gt;<![endif]--> <!--[if (!IE)|(gt IE 7)]><span class='float-ptr'>&gt;</span><![endif]--></a>";
			$retStr .= "\n\t\t\t"."<ul>";

			foreach ($nav_structure->children as $child_page) {
				$target = $child_page->target ? " target='".$child_page->target."' " : ' ';
				$child_href = trim($child_page->link) == "" ? " href='#' onclick='return false;' " : " href='$child_page->link' ";
				//$retStr .= "\n\t\t\t\t"."<li><a href='$child_page->link' $target";
				$retStr .= "\n\t\t\t\t"."<li><a $child_href $target";
				$retStr .= suffusion_parse_nav_structure($child_page, $position);
			}
			$retStr .= "\n\t\t\t\t"."</ul>";
			$retStr .= "\n\t\t\t\t"."</li>";
		}
		else {
			$retStr .= " title=\"$nav_structure->title\">$nav_structure->title</a></li>";
		}
	}
	return $retStr;
}

function suf_get_excluded_categories($prefix) {
    global $$prefix;
	$inclusions = $$prefix;
    $inclusions_array = explode(',', $inclusions);

    if (is_array($inclusions_array)) {
        $translations_array = wpml_lang_object_ids($inclusions_array, 'category');
    }
    if (is_array($translations_array)) {
        foreach ($translations_array as $translation) {
            if (!in_array($translation, $inclusions_array)) {
                $inclusions_array[count($inclusions_array)] = $translation;
            }
        }
    }
    if (is_array($inclusions_array)) {
        $inclusions = implode(',', $inclusions_array);
    }

	$args = array(
        "type" => "post",
		"orderby" => "name",
		"exclude" => $inclusions,
	);

	$excluded = get_categories($args);
	return $excluded;
}

function suf_get_included_bookmarks($prefix, $all = false) {
    global $$prefix;
	$inclusions = $$prefix;
    if (function_exists('mylinkorder')) {
        $order_by = 'order';
    }
    else {
        $order_by = 'name';
    }
    $args = array(
        "order" => "ASC",
        "orderby" => $order_by,
    );
    $links = get_bookmarks($args);
	if ($all) {
		return $links;
	}
    $included = array();
    $inclusions_array = explode(',',$inclusions);
    if ($inclusions_array && count($inclusions_array) > 0) {
        foreach ($links as $bookmark) {
            if (in_array($bookmark->link_id, $inclusions_array)) {
                $included[count($included)] = $bookmark;
            }
        }
    }
	return $included;
}

/**
 * Returns the list of included menus. It uses the following logic:
 *   1. Checks the options set in the navigation bar setup screens
 *   2. Checks if the menu is assigned to a "location" through the menu administration screens in WP (Apparently this is needed for theme approvals!!)
 * If a menu is assigned to a location, that takes precedence over the theme's options.
 *
 * @param  $prefix This is the option name in the theme options
 * @param bool $all
 * @param string $position
 * @return array
 */
function suf_get_included_menus($prefix, $all = false, $position = 'main') {
    global $$prefix;
	$inclusions = $$prefix;
    $args = array(
        'sort_column' => 'menu_order'
    );
    if (!function_exists('wp_get_nav_menus')) {
	    return array();
    }

    $menus = wp_get_nav_menus($args);
	if ($all) {
		return $menus;
	}
    $included = array();
    $inclusions_array = explode(',',$inclusions);

    $menu_locations = array();
    if (function_exists('get_nav_menu_locations')) {
	    $menu_locations = get_nav_menu_locations();
    }
    if (isset($menu_locations[$position])) {
	    $menu_in_location = $menu_locations[$position];
    }

    $all_ids = array();
    if ($inclusions_array && count($inclusions_array) > 0) {
        foreach ($menus as $menu) {
	        $all_ids[] = $menu->term_id;
            if (in_array($menu->term_id, $inclusions_array)) {
                $included[] = $menu;
            }
	        else if (isset($menu_in_location) && $menu_in_location == $menu->term_id) {
		        $included[] = $menu;
	        }
        }
    }
	return $included;
}

function create_nav_for_all($position = 'main', $page_option = 'suf_nav_pages', $cats_option = 'suf_nav_cats', $links_option = 'suf_nav_links', $menus_option = 'suf_nav_menus') {
	global $post, $suf_nav_pages_style, $suf_nav_page_tab_title, $suf_nav_page_tab_link, $suf_navt_pages_style, $suf_navt_page_tab_title, $suf_navt_page_tab_link;
	global $suf_nav_cat_style, $suf_nav_cat_tab_title, $suf_nav_cat_tab_link, $suf_navt_cat_style, $suf_navt_cat_tab_title, $suf_navt_cat_tab_link;
	global $suf_nav_links_style, $suf_nav_links_tab_title, $suf_nav_links_tab_link, $suf_navt_links_style, $suf_navt_links_tab_title, $suf_navt_links_tab_link;
	global $suf_nav_entity_order, $suf_navt_entity_order, $suf_nav_pages_all_sel, $suf_nav_cats_all_sel, $suf_nav_links_all_sel, $suf_navt_pages_all_sel, $suf_navt_cats_all_sel, $suf_navt_links_all_sel;
	global $suf_nav_menus_all_sel, $suf_navt_menus_all_sel, $suf_navt_menus_style, $suf_nav_menus_style;

	switch ($position) {
		case 'top':
			$pages_style = $suf_navt_pages_style;
			$page_tab_title = stripslashes($suf_navt_page_tab_title);
			$page_tab_link = $suf_navt_page_tab_link;
			$page_all_sel = $suf_navt_pages_all_sel;
			$cat_style = $suf_navt_cat_style;
			$cat_tab_title = stripslashes($suf_navt_cat_tab_title);
			$cat_tab_link = $suf_navt_cat_tab_link;
			$cat_all_sel = $suf_navt_cats_all_sel;
			$links_style = $suf_navt_links_style;
			$links_tab_title = stripslashes($suf_navt_links_tab_title);
			$links_tab_link = $suf_navt_links_tab_link;
			$link_all_sel = $suf_navt_links_all_sel;
			$menus_all_sel = $suf_navt_menus_all_sel;
			$menus_style = $suf_navt_menus_style;
			$entity_order = $suf_navt_entity_order;
			break;
		default:
			$pages_style = $suf_nav_pages_style;
			$page_tab_title = stripslashes($suf_nav_page_tab_title);
			$page_tab_link = $suf_nav_page_tab_link;
			$page_all_sel = $suf_nav_pages_all_sel;
			$cat_style = $suf_nav_cat_style;
			$cat_tab_title = stripslashes($suf_nav_cat_tab_title);
			$cat_tab_link = $suf_nav_cat_tab_link;
			$cat_all_sel = $suf_nav_cats_all_sel;
			$links_style = $suf_nav_links_style;
			$links_tab_title = stripslashes($suf_nav_links_tab_title);
			$links_tab_link = $suf_nav_links_tab_link;
			$link_all_sel = $suf_nav_links_all_sel;
			$menus_all_sel = $suf_nav_menus_all_sel;
			$menus_style = $suf_nav_menus_style;
			$entity_order = $suf_nav_entity_order;
			break;
	}

	if ($page_all_sel == 'selected') {
		$pages_excluded = get_excluded_pages($page_option);
	}
	else {
		$pages_excluded = array();
	}

	if ($cat_all_sel == 'selected') {
		$categories_excluded = suf_get_excluded_categories($cats_option);
	}
	else {
		$categories_excluded = array();
	}

	if ($link_all_sel == 'selected') {
		$bookmarks_included = suf_get_included_bookmarks($links_option, false);
	}
	else {
		$bookmarks_included = suf_get_included_bookmarks($links_option, true);
	}

	if (function_exists('wp_nav_menu')) {
		if ($menus_all_sel == 'selected') {
			$menus_included = suf_get_included_menus($menus_option, false, $position);
		}
		else {
			$menus_included = suf_get_included_menus($menus_option, true, $position);
		}
	}

	$nav_structure = new Suf_Navigation_Structure(0, 'Root', '', 0);
	$nav_structure->children = array();
	$entity_order = suf_get_entity_order($entity_order, 'nav');
	$entity_order = explode(',', $entity_order);
	$processed_menus = array();
	foreach ($entity_order as $entity) {
		if ($entity == 'pages') {
			if ($pages_style == 'rolled-up') {
				$page_structure = suffusion_get_page_structure(0, $page_tab_title, $page_tab_link, 1, $pages_excluded);
				if ($page_structure->children != null && count($page_structure->children) > 0) {
					$nav_structure->children[count($nav_structure->children)] = $page_structure;
				}
			}
			else if ($pages_style == 'flattened') {
				$page_structure = suffusion_get_page_structure(0, $page_tab_title,  $page_tab_link, 0, $pages_excluded);
				if ($page_structure->children != null && count($page_structure->children) > 0) {
					foreach ($page_structure->children as $child_structure) {
						$nav_structure->children[count($nav_structure->children)] = $child_structure;
					}
				}
			}
		}
		else if ($entity == 'categories') {
			if ($cat_style == 'rolled-up') {
				$category_structure = suffusion_get_category_structure(0, $cat_tab_title, $cat_tab_link, 1, $categories_excluded, 'page');
				if ($category_structure->children != null && count($category_structure->children) > 0) {
					$nav_structure->children[count($nav_structure->children)] = $category_structure;
				}
			}
			else if ($cat_style == 'flattened') {
				$category_structure = suffusion_get_category_structure(0, $cat_tab_title, $cat_tab_link, 0, $categories_excluded, 'cat');
				foreach ($category_structure->children as $child_structure) {
					$nav_structure->children[count($nav_structure->children)] = $child_structure;
				}
			}
		}
		else if ($entity == 'links') {
            if ($links_style == 'rolled-up') {
                $bookmark_structure = suffusion_get_bookmark_structure(0, $links_tab_title, $links_tab_link, 1, $bookmarks_included, 'page');
                if ($bookmark_structure->children != null && count($bookmark_structure->children) > 0) {
                    $nav_structure->children[count($nav_structure->children)] = $bookmark_structure;
                }
            }
            else if ($links_style == 'flattened') {
                $bookmark_structure = suffusion_get_bookmark_structure(0, $links_tab_title, $links_tab_link, 0, $bookmarks_included, 'link');
                foreach ($bookmark_structure->children as $child_structure) {
                    $nav_structure->children[count($nav_structure->children)] = $child_structure;
                }
            }
        }
		else if ((strlen($entity) >= 5 && substr($entity, 0, 5) == 'menu-')) {
			if (function_exists('wp_nav_menu')) {
				if ($menus_style == 'rolled-up') {
					foreach ($menus_included as $menu) {
						if ($menu->term_id == intval(substr($entity, 5)) && !in_array($menu->term_id, $processed_menus)) {
							$processed_menus[] = $menu->term_id;
							$menu_items = wp_get_nav_menu_items($menu->term_id);
							$menu_structure = suffusion_get_menu_structure(0, $menu->name, null, 1, $menu_items, 'menu');
							$nav_structure->children[] = $menu_structure;
						}
					}
				}
				else if ($menus_style == 'flattened') {
					foreach ($menus_included as $menu) {
						if ($menu->term_id == intval(substr($entity, 5)) && !in_array($menu->term_id, $processed_menus)) {
							$processed_menus[] = $menu->term_id;
							$menu_items = wp_get_nav_menu_items($menu->term_id);
							foreach ($menu_items as $menu_item) {
								if ($menu_item->menu_item_parent == 0) {
									$title = $menu_item->attr_title ? $menu_item->attr_title : $menu_item->title;
									$child = suffusion_get_menu_structure($menu_item->ID, esc_html($title), $menu_item->url, 1, $menu_items, 'menu', $menu_item->target);
									$nav_structure->children[] = $child;
								}
							}
						}
					}
				}
			}
		}
	}
	$retStr = suffusion_parse_nav_structure($nav_structure, $position);
	return $retStr;
}

function create_nav($echo = true, $type = "pages", $position = 'main', $page_option = 'suf_nav_pages', $cats_option = 'suf_nav_cats', $links_option = 'suf_nav_links', $menus_option = 'suf_nav_menus') {
	$retStr = "";
	if ($type == "pages") {
		$retStr = create_nav_for_all($position, $page_option, $cats_option, $links_option, $menus_option);
	}
	if ($echo) {
		echo $retStr;
	}
	return $retStr;
}

/**
 * If more than one page exists, return TRUE.
 */
function show_page_nav() {
	global $wp_query;
	return ($wp_query->max_num_pages > 1);
}
/**
 * If more than one post exists, return TRUE.
 */
function suffusion_post_count() {
	$post_type = get_query_var('post_type');
	if (!isset($post_type) || $post_type == null || $post_type == '') {
		$post_type = 'post';
	}
	$post_count = wp_count_posts($post_type);
	return $post_count->publish;
}

function check_integer($val) {
    return ($val !== true) && ((string)abs((int) $val)) === ((string) ltrim($val, '-0'));
}

function get_size_from_field($val, $default) {
	$ret = $default;
	if (substr(trim($val), -2) == "px") {
		$test_str = trim(substr(trim($val), 0, strlen(trim($val)) - 2));
		if (check_integer($test_str)) {
			$ret = $test_str."px";
		}
	}
	else if (check_integer(trim($val))) {
		$ret = trim($val)."px";
	}
	return $ret;
}

function get_numeric_size_from_field($val, $default) {
	$ret = $default;
	if (substr(trim($val), -2) == "px") {
		$test_str = trim(substr(trim($val), 0, strlen(trim($val)) - 2));
		if (check_integer($test_str)) {
			$ret = (int)$test_str;
		}
	}
	else if (check_integer(trim($val))) {
		$ret = (int)(trim($val));
	}
	return $ret;
}

function suffusion_get_formatted_sbtab_array($prefix, $spawn = true) {
	global $sidebar_tabs, $spawned_options;

	foreach ($sidebar_tabs as $tab => $tab_options) {
		$spawned_options[count($spawned_options)] = array('id' => $prefix.'_'.$tab, 'type' => 'checkbox', 'parent' => $prefix, 'std' => 'false');
	}
	return $sidebar_tabs;
}

function suffusion_get_category_structure($id, $title, $link, $depth, $exclusion_list = array(), $type = 'cat') {
	$category_structure = new Suf_Navigation_Structure($id, $title, $link, $depth, $type);
	$category_structure->children = array();
	$exclude_categories = array();
	foreach ($exclusion_list as $category) {
		$exclude_categories[count($exclude_categories)] = $category->cat_ID;
	}
	$exclude = implode(',', $exclude_categories);
	$child_cats = get_categories(array('type' => 'post', 'child_of' => $id, 'parent' => $id, 'exclude' => $exclude));
    $child_cat_ids = array();
    if ($child_cats) {
        foreach ($child_cats as $child_cat) {
            $child_cat_ids[count($child_cat_ids)] = $child_cat->cat_ID;
        }
    }
    $lang_specific_cat_ids = wpml_lang_object_ids($child_cat_ids, 'category');
    if (count($lang_specific_cat_ids) != 0) {
        $include = implode(',',$lang_specific_cat_ids);
        if (function_exists('mycategoryorder')) {
            $order_by = 'order';
        }
        else {
            $order_by = 'name';
        }
        $child_cats = get_categories(array('type' => 'post', 'include' => $include, 'orderby' => $order_by));
        if ($child_cats == null) { $child_cats = array(); }
        $child_depth = $depth + 1;
        foreach ($child_cats as $child_cat) {
            $child_cat_link = get_category_link($child_cat->cat_ID);
            $child = suffusion_get_category_structure($child_cat->cat_ID, $child_cat->cat_name, $child_cat_link, $child_depth, $exclusion_list);
            $category_structure->children[count($category_structure->children)] = $child;
        }
    }
	return $category_structure;
}

function suffusion_get_category_ancestor_count($cat, $counter) {
	if ($cat == null || !$cat) {
		return $counter;
	}
	$count = $counter;
	if ($cat->category_parent) {
		$parent = get_category($cat->category_parent);
		$count = suffusion_get_category_ancestor_count($parent, $count + 1);
	}
	return $count;
}

function suffusion_get_bookmark_structure($id, $title, $link, $depth, $inclusion_list = array(), $type = 'link', $target = false) {
	$bookmark_structure = new Suf_Navigation_Structure($id, $title, $link, $depth, $type, $target);
	$bookmark_structure->children = array();
    $child_depth = $depth + 1;
    foreach ($inclusion_list as $bookmark) {
        $child = suffusion_get_bookmark_structure($bookmark->link_id, $bookmark->link_name, $bookmark->link_url, $child_depth, array(), 'link', $bookmark->link_target);
        $bookmark_structure->children[count($bookmark_structure->children)] = $child;
    }
	return $bookmark_structure;
}

function suffusion_get_menu_structure($id, $title, $link, $depth, $inclusions, $type = 'menu', $target = false) {
	$menu_structure = new Suf_Navigation_Structure($id, $title, $link, $depth, $type, $target);
	$menu_structure->children = array();
    $child_depth = $depth + 1;
	foreach ($inclusions as $menu_item) {
		if ($menu_item->menu_item_parent == $id) {
			$title = $menu_item->attr_title ? $menu_item->attr_title : $menu_item->title;
			$child = suffusion_get_menu_structure($menu_item->ID, esc_html($title), $menu_item->url, $child_depth, $inclusions, 'menu-item', $menu_item->target);
			$menu_structure->children[] = $child;
		}
	}
	return $menu_structure;
}

function suf_get_allowed_pages($prefix) {
	global $suffusion_options;
	$allowed = array();
	if (isset($suffusion_options[$prefix])) {
		$selected = $suffusion_options[$prefix];
		if (!empty($selected)) {
			$selected_pages = explode(',', $selected);
			if (is_array($selected_pages) && count($selected_pages) > 0) {
				foreach ($selected_pages as $page_id) {
					$page = get_page($page_id);
					$allowed[count($allowed)] = $page;
				}
			}
		}
	}
	return $allowed;
}

function suf_get_formatted_link_array($prefix, $spawn = true) {
	global $spawned_options, $link_array;
	$ret = array();
	$args = array(
		"order" => "ASC",
		"orderby" => 'name',
	);
	$links = get_bookmarks($args);
	if ($links == null) {
		$links = array();
	}
	foreach ($links as $link) {
		if ($link_array == null) {
			$ret[$link->link_id] = array("title" => $link->link_name);
		}
		if ($spawn) {
			$spawned_options[count($spawned_options)] = array(  "id" => $prefix."_".$link->link_id,
														        "type" => "checkbox",
														        "parent" => $prefix,
																"std" => "false");
		}
	}
	if ($link_array == null) {
		$link_array = $ret;
		return $link_array;
	}
	else {
		return $link_array;
	}
}

function suf_get_formatted_wp_menu_array($prefix, $spawn = true) {
	global $spawned_options, $menu_array;
	$ret = array();
	if (!function_exists('wp_nav_menu')) {
		return $ret;
	}

	$menus = wp_get_nav_menus();
	if ($menus == null) {
		$menus = array();
	}

	foreach ($menus as $menu) {
		if ($menu_array == null) {
			$ret[$menu->term_id] = array("title" => $menu->name);
		}
		if ($spawn) {
			$spawned_options[count($spawned_options)] = array(  "id" => $prefix."_".$menu->term_id,
														        "type" => "checkbox",
														        "parent" => $prefix,
																"std" => "false");
		}
	}

	if ($menu_array == null) {
		$menu_array = $ret;
		return $menu_array;
	}
	else {
		return $menu_array;
	}
}

/* Functions for WPML compatibility */
function wpml_lang_object_id($id, $type){
    if(function_exists('icl_object_id')) {
        return icl_object_id($id, $type, true);
    } else {
        return $id;
    }
}

function wpml_lang_object_ids($ids_array, $type) {
    if(function_exists('icl_object_id')) {
        $res = array();
        foreach ($ids_array as $id) {
            $xlat = icl_object_id($id, $type, false);
            if (!is_null($xlat) && !in_array($xlat, $res)) {
                $res[] = $xlat;
            }
        }
        return $res;
    }
    else {
        return $ids_array;
    }
}
/* End functions for WPML compatibility */

function suffusion_tab_array_prepositions() {
    global $sidebar_tabs;
    $ret = array();
    foreach ($sidebar_tabs as $key => $value) {
        $ret[count($ret)] = array('key' => $key, 'value' => $value['title']);
    }
    return $ret;
}

function suffusion_nav_entity_prepositions() {
    $ret = array(array('key' => 'pages', 'value' => 'Pages'), array('key' => 'categories', 'value' => 'Categories'), array('key' => 'links', 'value' => 'Links'));
    return $ret;
}

function suf_get_formatted_options_array($prefix, $options_array) {
	global $spawned_options;
	$ret = array();
    foreach ($options_array as $option_key => $option_value) {
        $ret[$option_key] = array('title' => $option_value, 'depth' => 1);
        $spawned_options[] = array('id' => $prefix.'_'.$option_key, 'type' => 'checkbox', 'parent' => $prefix, 'std' => 'false');
    }
    return $ret;
}

function suf_get_entity_order($entity_order, $entity_type='nav') {
    $ret = array();
    if (is_array($entity_order)) {
        foreach ($entity_order as $element) {
            $ret[] = $element['key'];
        }
        $ret = implode(',',$ret);
    }
    else {
        $defaults = suffusion_entity_prepositions($entity_type);
        $ret = explode(',', $entity_order);
        $default_array = array();
        foreach ($defaults as $default) {
            $default_array[] = $default['key'];
            if (!in_array($default['key'], $ret)) {
                $ret[] = $default['key'];
            }
        }
        $ret_array = array();
        foreach ($ret as $ret_entry) {
            if (in_array($ret_entry, $default_array)) {
                $ret_array[] = $ret_entry;
            }
        }
        $ret = implode(',', $ret_array);
    }
    return $ret;
}

function suffusion_update_available($theme) {
    static $themes_update;
    if ( !isset($themes_update) )
        $themes_update = get_transient('update_themes');

    if ( is_object($theme) && isset($theme->stylesheet) )
        $stylesheet = $theme->stylesheet;
    elseif ( is_array($theme) && isset($theme['Stylesheet']) )
        $stylesheet = $theme['Stylesheet'];
    else
        return false; //No valid info passed.

    if (isset($themes_update->response[ $stylesheet ])) {
        return true;
    }
    return false;
}

function suffusion_get_full_content_count() {
	global $suffusion, $suf_category_fc_number, $suf_author_fc_number, $suf_tag_fc_number, $suf_search_fc_number, $suf_archive_fc_number, $suf_index_fc_number, $suf_pop_fc_number;
	$context = $suffusion->get_context();
	$full_post_count = 0;
	if (in_array('category', $context)) {
		$full_post_count = (int)$suf_category_fc_number;
	}
	else if (in_array('author', $context)) {
		$full_post_count = (int)$suf_author_fc_number;
	}
	else if (in_array('tag', $context)) {
		$full_post_count = (int)$suf_tag_fc_number;
	}
	else if (in_array('search', $context)) {
		$full_post_count = (int)$suf_search_fc_number;
	}
	else if (in_array('date', $context)) {
		$full_post_count = (int)$suf_archive_fc_number;
	}
	else if (in_array('home', $context) || in_array('blog', $context)) {
		$full_post_count = (int)$suf_index_fc_number;
	}
	else if (in_array('page', $context)) {
		if (in_array('posts.php', $context)) {
			$full_post_count = (int)$suf_pop_fc_number;
		}
	}
	return $full_post_count;
}

function suffusion_nr_entity_prepositions() {
    $ret = array(array('key' => 'current', 'value' => 'Currently Reading'), array('key' => 'unread', 'value' => 'Not Yet Read'), array('key' => 'completed', 'value' => 'Completed'));
    return $ret;
}

function suffusion_entity_prepositions($entity_type = 'nav') {
	if ($entity_type == 'nav') {
		$ret = array(array('key' => 'pages', 'value' => 'Pages'), array('key' => 'categories', 'value' => 'Categories'), array('key' => 'links', 'value' => 'Links'));
		if (function_exists('wp_get_nav_menus')) {
			$menus = wp_get_nav_menus();
			if ($menus == null) {
				$menus = array();
			}

			foreach ($menus as $menu) {
				$ret[] = array('key' => "menu-".$menu->term_id, "value" => $menu->name);
			}
		}
	}
	else if ($entity_type == 'nr') {
		$ret = array(array('key' => 'current', 'value' => 'Currently Reading'), array('key' => 'unread', 'value' => 'Not Yet Read'), array('key' => 'completed', 'value' => 'Completed'));
	}
	else if ($entity_type == 'mag-layout') {
		$ret = array(array('key' => 'headlines', 'value' => 'Headlines'), array('key' => 'excerpts', 'value' => 'Excerpts'), array('key' => 'categories', 'value' => 'Categories'));
	}
	else if ($entity_type == 'sitemap') {
		global $suffusion_sitemap_entities;
		$ret = array();
		foreach ($suffusion_sitemap_entities as $entity => $entity_options) {
			$ret[] = array('key' => $entity, 'value' => $entity_options['title']);
		}
	}
    return $ret;
}

function suffusion_get_unified_options($update = false, $fetch_from_cache = true) {
	global $options, $suffusion_options, $skin_settings, $theme_name, $default_theme_name, $suf_cache_unified;
	if (!isset($suf_cache_unified) || $suf_cache_unified == 'cache') {
		$unified_options_from_db = get_option('suffusion_unified_options');
		if (isset($unified_options_from_db) && isset($unified_options_from_db['suffusion_options_version']) && $fetch_from_cache) {
			$suffusion_theme = get_current_theme(); // Need this because a child theme might be getting used.
			$suffusion_theme_data = get_theme($suffusion_theme);
			if (isset($suffusion_theme_data['Version'])) {
				$suffusion_theme_version = $suffusion_theme_data['Version'];
			}
			else {
				$suffusion_theme_version = "1.0";
			}
			$options_version = $unified_options_from_db['suffusion_options_version'];
			if ($options_version == $suffusion_theme_version) {
				return $unified_options_from_db;
			}
		}
	}
	include_once (TEMPLATEPATH . "/admin/theme-options.php");
	$theme_name = suffusion_get_theme_name();
	$suffusion_options = get_option('suffusion_options');
	if ($theme_name == 'root') {
		$skin = $default_theme_name;
	}
	else {
		$skin = $theme_name;
	}

	if (file_exists(STYLESHEETPATH."/skins/$skin/settings.php")) {
		include_once(STYLESHEETPATH."/skins/$skin/settings.php");
	}
	else if (file_exists(TEMPLATEPATH."/skins/$skin/settings.php")) {
		include_once(TEMPLATEPATH."/skins/$skin/settings.php");
	}
	$unified_options_array = array();
	foreach ($options as $value) {
		if (isset($value['id'])) {
			if (!isset($suffusion_options[$value['id']])) {
				if (is_array($skin_settings) && isset($skin_settings[$value['id']])) {
					$unified_options_array[$value['id']] = $skin_settings[$value['id']];
				}
				else if (isset($value['std'])) {
					$unified_options_array[$value['id']] = $value['std'];
				}
		    }
			else if (isset($value['std']) && $suffusion_options[$value['id']] == $value['std']) {
				if (is_array($skin_settings) && isset($skin_settings[$value['id']])) {
					$unified_options_array[$value['id']] = $skin_settings[$value['id']];
				}
				else {
					$unified_options_array[$value['id']] = $suffusion_options[$value['id']];
				}
		    }
		    else {
			    $unified_options_array[$value['id']] = $suffusion_options[$value['id']];
		    }
		}
	}
	if ($update && current_user_can('manage_options')) {
		suffusion_set_options_version(&$unified_options_array);
		update_option('suffusion_unified_options', $unified_options_array);
	}
	return $unified_options_array;
}

/**
 * Attempts to return the location of a CSS file where the customization options of the theme are/will be stored.
 * If the installation is a WP-MS site, a folder called "suffusion" is created in the user-specific files directory (if missing).
 * For regular installations the folder is created in the uploads directory (if missing).
 * The path to autogen-options.css is returned if the file exists or was successfully created. Otherwise "false" is returned.
 *
 * @return bool|string
 */
function suffusion_get_custom_css_location() {
	global $blog_id;
	$folder_queue = array();
	if (defined('MULTISITE') && MULTISITE && isset($blog_id) && $blog_id > 0) {
		$folder_queue = array('blogs.dir', $blog_id, 'files', 'suffusion');
		$suffusion_content_folder = WP_CONTENT_DIR.'/blogs.dir/'.$blog_id.'/files/suffusion/';
	}
	else {
		$folder_queue = array('uploads', 'suffusion');
		$suffusion_content_folder = WP_CONTENT_DIR.'/uploads/suffusion/';
	}

	if (!file_exists($suffusion_content_folder)) {
		$temp_folder = WP_CONTENT_DIR;
		foreach ($folder_queue as $sub_folder) {
			$temp_folder .= '/'.$sub_folder;
			if (!file_exists($temp_folder)) {
				$created = mkdir($temp_folder, 0777); // 777 needed because you want both write and execute permissions on this folder. O/w you get an access error.
				if (!$created) {
					return false;
				}
			}
			else {
				//Set the right permissions
				$perms = fileperms($temp_folder);
				$perms = substr(sprintf('%o', $perms), -4);
				if ($perms != '0777') {
					chmod($temp_folder, 0777);
				}
			}
		}
	}

	$autogen_file_path = $suffusion_content_folder.'autogen-options.css';
	return $autogen_file_path;
}

/**
 * Attempts to return the relative location of a CSS file where the customization options of the theme are/will be stored.
 * The relative path to autogen-options.css is returned if the file exists or was successfully created. Otherwise "false" is returned.
 *
 * @return bool|string
 */
function suffusion_get_custom_css_relative_location() {
	$path = suffusion_get_custom_css_location();
	if ($path == false) return false;
	$file_path = explode(WP_CONTENT_DIR, $path);
	if (is_array($file_path) && count($file_path) > 0) {
		return str_replace(WP_CONTENT_DIR, '../..', $path);
	}
	return false;
}

/**
 * Returns the URL of the custom CSS file. This function is sensitive to an installation being a WP-MS installation or a regular WP installation.
 * The URL to autogen-options.css is returned if the file exists or false is returned.
 *
 * @return bool|string
 */
function suffusion_get_custom_css_url() {
	global $blog_id;
	$path = suffusion_get_custom_css_location();
	if ($path == false) return false;
	if (defined('MULTISITE') && MULTISITE && isset($blog_id) && $blog_id > 0) {
		$path = WP_CONTENT_URL . '/files/suffusion/autogen-options.css';
	}
	else {
		$path = WP_CONTENT_URL . '/uploads/suffusion/autogen-options.css';
	}
	return $path;
}

/**
 * Returns the total memory usage for the script at any point.
 *
 * @param bool $echo Echoes the value if set to true
 * @return string
 */
function suffusion_get_memory_usage($echo = true) {
	$ret = "";
	if (function_exists('memory_get_usage')) {
		$mem = memory_get_usage();
		$unit = "B";
		if ($mem > 1024) {
			$mem = round($mem / 1024);
			$unit = "KB";
			if ($mem > 1024) {
				$mem = round($mem / 1024);
				$unit = "MB";
			}
		}
		$ret = $mem . $unit;
		if ($echo) {
			echo $ret;
		}
	}
	return $ret;
}

/**
 * Returns the name of the skin being used.
 *
 * @return string
 */
function suffusion_get_theme_name() {
    $suffusion_options = get_option('suffusion_options');
    if (!isset($suffusion_options['suf_color_scheme']) || $suffusion_options['suf_color_scheme'] === FALSE || $suffusion_options['suf_color_scheme'] == null || !isset($suffusion_options['suf_color_scheme'])) {
        $theme_name = 'root';
    }
    else {
        $theme_name = $suffusion_options['suf_color_scheme'];
    }
    return $theme_name;
}

/**
 * Sets the version of the theme in the suffusion_unified_options option. Starting from version 3.6.4 of the theme the unified options are stored
 * as a separate option in the database to avoid loading the huge $options array into memory. However, to account for cases where some default values
 * of options are changed or some new options with new default values are introduced, this workaround is required.
 * It stores the version number as a part of the options, so that if the current version is newer than the options version, the options are recalculated
 *
 * @param  $suffusion_unified_options
 * @return void
 * @since 3.6.4
 */
function suffusion_set_options_version($suffusion_unified_options) {
	if (is_array($suffusion_unified_options)) {
		$suffusion_theme = get_current_theme(); // Need this because a child theme might be getting used.
		$suffusion_theme_data = get_theme($suffusion_theme);
		$suffusion_theme_version = $suffusion_theme_data['Version'];
		$suffusion_unified_options['suffusion_options_version'] = $suffusion_theme_version;
	}
}

function suffusion_get_formatted_sitemap_contents_array($prefix) {
	global $suffusion_sitemap_entities, $spawned_options;

	foreach ($suffusion_sitemap_entities as $tab => $tab_options) {
		$spawned_options[count($spawned_options)] = array('id' => $prefix.'_'.$tab, 'type' => 'checkbox', 'parent' => $prefix, 'std' => 'false');
	}
	return $suffusion_sitemap_entities;
}

if (function_exists('add_theme_support')) {
	add_theme_support('post-thumbnails');
	add_theme_support('nav-menus'); // For Beta and RC versions of WP 3.0
	add_theme_support('menus'); // For the final release version of WP 3.0
	add_theme_support('automatic-feed-links');
}
require_once (TEMPLATEPATH . "/suffusion.php");
include_once (TEMPLATEPATH . "/widget-areas.php");
include_once (TEMPLATEPATH . "/functions/actions.php");
include_once (TEMPLATEPATH . "/functions/filters.php");
include_once (TEMPLATEPATH . "/functions/shortcodes.php");

if (isset($suffusion_options['suf_custom_php_file'])) {
	$custom_php_file = $suffusion_options['suf_custom_php_file'];
	$custom_php_file = stripslashes($custom_php_file);
	if (substr($custom_php_file, 0, 1) == "/") {
		$custom_php_file = substr($custom_php_file, 1);
	}
	if (trim($custom_php_file) != "" && file_exists(WP_CONTENT_DIR."/".$custom_php_file)) {
		include_once(WP_CONTENT_DIR."/".$custom_php_file);
	}
}

$suffusion_theme_hierarchy = array(
	'light-theme-gray-1' => array('style.css', 'skins/light-theme-gray-1/style.css'),
	'light-theme-gray-2' => array('style.css', 'skins/light-theme-gray-2/style.css'),
	'light-theme-green' => array('style.css', 'skins/light-theme-green/style.css'),
	'light-theme-orange' => array('style.css', 'skins/light-theme-orange/style.css'),
	'light-theme-pale-blue' => array('style.css', 'skins/light-theme-pale-blue/style.css'),
	'light-theme-purple' => array('style.css', 'skins/light-theme-purple/style.css'),
	'light-theme-red' => array('style.css', 'skins/light-theme-red/style.css'),
	'light-theme-royal-blue' => array('style.css', 'skins/light-theme-royal-blue/style.css'),
	'dark-theme-gray-1' => array('style.css', 'skins/light-theme-gray-1/style.css', 'dark-style.css', 'skins/dark-theme-gray-1/style.css'),
	'dark-theme-gray-2' => array('style.css', 'skins/light-theme-gray-2/style.css', 'dark-style.css', 'skins/dark-theme-gray-2/style.css'),
	'dark-theme-green' => array('style.css', 'skins/light-theme-green/style.css', 'dark-style.css', 'skins/dark-theme-green/style.css'),
	'dark-theme-orange' => array('style.css', 'skins/light-theme-orange/style.css', 'dark-style.css', 'skins/dark-theme-orange/style.css'),
	'dark-theme-pale-blue' => array('style.css', 'skins/light-theme-pale-blue/style.css', 'dark-style.css', 'skins/dark-theme-pale-blue/style.css'),
	'dark-theme-purple' => array('style.css', 'skins/light-theme-purple/style.css', 'dark-style.css', 'skins/dark-theme-purple/style.css'),
	'dark-theme-red' => array('style.css', 'skins/light-theme-red/style.css', 'dark-style.css', 'skins/dark-theme-red/style.css'),
	'dark-theme-royal-blue' => array('style.css', 'skins/light-theme-royal-blue/style.css', 'dark-style.css', 'skins/dark-theme-royal-blue/style.css'),
	'minima' => array('style.css', 'skins/minima/style.css'),
);

$suffusion = new Suffusion();
$suffusion->init();

?>