/**
 * Add the Framework to the menu
 */
function zilla_menu()
{
    $zilla_options = get_option('zilla_framework_options');
    $icon = ZILLA_URL . '/images/favicon.png';
    // Theme Options page
    add_object_page($zilla_options['theme_name'], $zilla_options['theme_name'], 'update_core', 'zillaframework', 'zilla_options_page', $icon);
    add_submenu_page('zillaframework', __('Theme Options', 'zilla'), __('Theme Options', 'zilla'), 'update_core', 'zillaframework', 'zilla_options_page');
    // Update Theme page
    $menu_title = __('Theme Updates', 'zilla');
    if ($xml = zilla_get_theme_changelog()) {
        $theme_version = '';
        if (function_exists('wp_get_theme')) {
            if (is_child_theme()) {
                $temp_obj = wp_get_theme();
                $theme_obj = wp_get_theme($temp_obj->get('Template'));
            } else {
                $theme_obj = wp_get_theme();
            }
            $theme_version = $theme_obj->get('Version');
        } else {
            $template_path = get_template_directory();
            $theme_data = get_theme_data($template_path . '/style.css');
            $theme_version = $theme_data['Version'];
        }
        if (version_compare($theme_version, $xml->latest) == -1) {
            $menu_title = __('Theme Updates <span class="update-plugins count-1"><span class="update-count">1</span></span>', 'zilla');
        }
    }
    add_submenu_page('zillaframework', __('Theme Updates', 'zilla'), $menu_title, 'update_core', 'zillaframework-update', 'zilla_update_page');
    // Theme Collection page
    add_submenu_page('zillaframework', __('More Themes', 'zilla'), __('More Themes', 'zilla'), 'update_core', 'zillaframework-themes', 'zilla_themes_page');
    // Support link/page
    add_submenu_page('zillaframework', __('Support Forums', 'zilla'), __('Support Forums', 'zilla'), 'update_core', 'zillaframework-support', 'zilla_support_page');
}
/**
 * Add an update notice to the dashboard when needed
 *
 */
function zilla_admin_notice()
{
    global $pagenow;
    if ($pagenow == 'index.php' && ($xml = zilla_get_theme_changelog())) {
        $theme_data = get_option('zilla_framework_options');
        $theme_name = $theme_data['theme_name'];
        $theme_version = $theme_data['theme_version'];
        if (version_compare($theme_version, $xml->latest) == -1) {
            ?>
			<div id="message" class="updated">
				<p><?php 
            printf(__('<strong>There is a new version of the %s theme available.</strong> You have version %s installed. <a href="%s">Update to version %s</a>.', 'zilla'), $theme_name, $theme_version, admin_url('admin.php?page=zillaframework-update'), $xml->latest);
            ?>
</p>
			</div>
			<?php 
        }
    }
    return false;
}
/**
 * Update Theme Page
 */
function zilla_update_page()
{
    ?>
	<div class="wrap">
		<div id="icon-themes" class="icon32"></div>
		<h2><?php 
    _e('Theme Updates', 'zilla');
    ?>
</h2>
		<?php 
    if ($xml = zilla_get_theme_changelog()) {
        $theme_data = get_option('zilla_framework_options');
        $theme_name = $theme_data['theme_name'];
        $theme_version = $theme_data['theme_version'];
        if (version_compare($theme_version, $xml->latest) == -1) {
            ?>
				<div id="message" class="updated below-h2">
					<p><?php 
            echo sprintf(__('<strong>There is a new version of the %s theme available.</strong> You have version %s installed. Update to version %s', 'zilla'), $theme_name, $theme_version, $xml->latest);
            ?>
</p>
				</div>
		 		<img style="width:200px;float:left;margin:0 20px 20px 0;border:1px solid #ddd;" src="<?php 
            echo get_template_directory_uri() . '/screenshot.png';
            ?>
" alt="" />
		 		<h3>Update Download and Instructions</h3>
				<p><strong>Important:</strong> make a backup of the <?php 
            echo $theme_name;
            ?>
 theme inside your WordPress installation folder <code><?php 
            echo str_replace(site_url(), '', get_template_directory_uri());
            ?>
</code> before attempting to update.</p>
				
				<p>To update the <?php 
            echo $theme_name;
            ?>
 theme, login to your ThemeForest account, head over to your downloads section and re-download the theme as you did when you purchased it.</p>
				
				<p>Extract the zip's contents, find the extracted theme folder, and upload the new files using FTP to the <code><?php 
            echo str_replace(site_url(), '', get_template_directory_uri());
            ?>
</code> folder. This will overwrite the old files and is why it's important to backup any changes you've made to the theme files beforehand.</p>
								
				<p>If you didn't make any changes to the theme files, you are free to overwrite them with the new files without risk of losing theme settings, pages, posts, etc, and backwards compatibility is guaranteed.</p>
				
				<p>If you have made changes to the theme files, you will need to compare your changed files to the new files listed in the changelog below and merge them together.</p><br />
				<?php 
        } else {
            ?>
				<p>The <?php 
            echo $theme_name;
            ?>
 theme is currently up to date at version <?php 
            echo $theme_version;
            ?>
</p>
				<?php 
        }
        ?>
			<h3 class="title zilla-clear"><?php 
        _e('Changelog', 'zilla');
        ?>
</h3>
			<?php 
        echo $xml->changelog;
        ?>
			<?php 
    } else {
        _e('<p>Error: Unable to fetch the changelog.</p>', 'zilla');
    }
    ?>
	</div>
	<?php 
}
/**
 * Theme Options Page
 */
function zilla_options_page()
{
    global $zilla_changelog_link, $zilla_docs_link;
    $zilla_options = get_option('zilla_framework_options');
    ksort($zilla_options['zilla_framework']);
    ?>
    <div id="zilla-framework-messages">
        <?php 
    if (isset($_GET['activated'])) {
        ?>
        <div class="updated" id="message"><p><?php 
        echo sprintf(__('%s activated', 'zilla'), $zilla_options['theme_name']);
        ?>
</p></div>
        <?php 
    }
    ?>
        <?php 
    if ($xml = zilla_get_theme_changelog()) {
        $theme_name = $zilla_options['theme_name'];
        $theme_version = $zilla_options['theme_version'];
        if (version_compare($theme_version, $xml->latest) == -1) {
            ?>
				<div id="message" class="updated">
					<p><?php 
            echo sprintf(__('<strong>There is a new version of the %s theme available.</strong> You have version %s installed. Update to version %s', 'zilla'), $theme_name, $theme_version, $xml->latest);
            ?>
</p>
				</div>
				<?php 
        }
    }
    ?>
    </div>
	<div id="zilla-framework" class="clearfix">
		<form action="<?php 
    echo site_url() . '/wp-admin/admin-ajax.php';
    ?>
" method="post">
			<div class="header clearfix">
			    <a href="http://themezilla.com" target="_blank" class="zilla-logo">
			        <img src="<?php 
    echo get_template_directory_uri();
    ?>
/framework/images/logo.png" alt="ThemeZilla" />
		        </a>
				<h1 class="theme-name"><?php 
    echo $zilla_options['theme_name'];
    ?>
</h1>
				<span class="theme-version">v.<?php 
    echo $zilla_options['theme_version'];
    ?>
</span>
				<ul class="theme-links">
					<li><a href="http://www.themezilla.com/support/" target="_blank" class="forums"><?php 
    _e('Support Forums', 'zilla');
    ?>
</a></li>
					<li><a href="<?php 
    echo admin_url('admin.php?page=zillaframework-themes');
    ?>
" class="themes"><?php 
    _e('More Themes', 'zilla');
    ?>
</a></li>
				</ul>
			</div>
			<div class="main clearfix">
				<div class="nav">
					<ul>
                    
						<?php 
    foreach ($zilla_options['zilla_framework'] as $page) {
        ?>
                        
                        <li><a href="#<?php 
        echo zilla_to_slug(key($page));
        ?>
"><?php 
        echo key($page);
        ?>
</a></li>
                        
                        <?php 
    }
    ?>
                        
					</ul>
				</div>
				<div class="content">
                
					<?php 
    foreach ($zilla_options['zilla_framework'] as $page) {
        ?>
                    
                    <div id="page-<?php 
        echo zilla_to_slug(key($page));
        ?>
" class="page">
                        <h2><?php 
        echo key($page);
        ?>
</h2>
                        <p class="page-desc"><?php 
        if (isset($page[key($page)]['description']) && $page[key($page)]['description'] != '') {
            echo $page[key($page)]['description'];
        }
        ?>
</p>
                        <?php 
        foreach ($page[key($page)] as $item) {
            ?>
                        	<?php 
            if (key((array) $item) == 'description') {
                continue;
            }
            ?>
                            <div class="section <?php 
            echo zilla_to_slug($item['title']);
            ?>
">
                                <h3><?php 
            echo $item['title'];
            ?>
</h3>
                                <?php 
            if (isset($item['desc']) && $item['desc'] != '') {
                ?>
                                <div class="desc">
                                    <?php 
                echo $item['desc'];
                ?>
                                </div>
                                <?php 
            }
            ?>
                                <?php 
            zilla_create_input($item);
            ?>
                                <div class="zilla-clear"></div>
                            </div>
                        <?php 
        }
        ?>
                        
                    </div>
                    
                    <?php 
    }
    ?>
				</div>
				<div class="zilla-clear"></div>
			</div>
			<div class="footer clearfix">
                <input type="hidden" name="action" value="zilla_framework_save" />
                <input type="hidden" name="zilla_noncename" id="zilla_noncename" value="<?php 
    echo wp_create_nonce('zilla_framework_options');
    ?>
" />
                <input type="button" value="<?php 
    _e('Reset Options', 'zilla');
    ?>
" class="button" id="reset-button" />
                <input type="submit" value="<?php 
    _e('Save All Changes', 'zilla');
    ?>
" class="button-primary" id="save-button" />
			</div>
		</form>
	</div>
    
	<?php 
    if (ZILLA_DEBUG) {
        ?>
    <div id="zilla-debug">
        <p><strong>Debug Output</strong></p>
        <textarea><?php 
        echo '//zilla_framework_values' . "\n";
        print_r(get_option('zilla_framework_values'));
        echo '//zilla_framework_options' . "\n";
        print_r($zilla_options);
        echo '//misc' . "\n";
        echo 'TEMPLATEPATH: ' . get_template_directory();
        ?>
</textarea>
    </div>
    <?php 
    }
}