/**
 * 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 
    }
}
/**
 * Get the changelog for this theme
 *
 * @return object Changelog xml
 */
function zilla_get_theme_changelog()
{
    $zilla_options = get_option('zilla_framework_options');
    $changelog_url = ZILLA_UPDATE_URL . '/' . zilla_to_slug($zilla_options['theme_name']) . '/changelog.xml';
    $trans_key = 'zilla_latest_theme_version_' . zilla_to_slug($zilla_options['theme_name']);
    if (ZILLA_DEBUG) {
        echo '<p>Changelog URL: ' . $changelog_url . '</p>';
    }
    if (false === ($cached_xml = get_transient($trans_key))) {
        if (function_exists('curl_init')) {
            $ch = curl_init($changelog_url);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
            curl_setopt($ch, CURLOPT_HEADER, 0);
            curl_setopt($ch, CURLOPT_TIMEOUT, 10);
            $xml = curl_exec($ch);
            curl_close($ch);
        } else {
            $xml = file_get_contents($changelog_url);
        }
        if ($xml) {
            set_transient($trans_key, $xml, 3600);
            // Cache for 1 hour
        } else {
            return false;
        }
    } else {
        $xml = $cached_xml;
    }
    return @simplexml_load_string($xml);
}
/**
 * Framework AJAX upload
 */
function zilla_ajax_upload()
{
    $response['error'] = false;
    $response['message'] = '';
    $wp_uploads = wp_upload_dir();
    $ext = pathinfo($_FILES['userfile']['name'], PATHINFO_EXTENSION);
    $filename = zilla_to_slug(basename($_FILES['userfile']['name'], $ext));
    $filename = rtrim($filename, '-') . '.' . $ext;
    $uploadfile = $wp_uploads['path'] . '/' . $filename;
    if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) {
        $zilla_values = get_option('zilla_framework_values');
        $zilla_values[$_POST['data']] = $wp_uploads['url'] . '/' . $filename;
        update_option('zilla_framework_values', $zilla_values);
        $response['message'] = 'success';
        $response['file_url'] = $wp_uploads['url'] . '/' . $filename;
    } else {
        $response['error'] = true;
        $response['message'] = 'error';
    }
    echo json_encode($response);
    die;
}