<?php
function elisium_get_default_options() {
	$options = array(
		'logo' => ''
	);
	return $options;
}

function elisium_options_init() {
     $elisium_options = get_option( 'theme_elisium_options' );
	 
     if ( false === $elisium_options ) {
          $elisium_options = elisium_get_default_options();
		  add_option( 'theme_elisium_options', $elisium_options );
     }
}

// Initialize Theme options
add_action( 'after_setup_theme', 'elisium_options_init' );

function elisium_options_setup() {
	global $pagenow;
	if ('media-upload.php' == $pagenow || 'async-upload.php' == $pagenow) {
		add_filter( 'gettext', 'replace_thickbox_text' , 1, 2 );
	}
}
add_action( 'admin_init', 'elisium_options_setup' );

function replace_thickbox_text($translated_text, $text ) {	
	if ( 'Insert into Post' == $text ) {
		$referer = strpos( wp_get_referer(), 'elisium-settings' );
		if ( $referer != '' ) {
			return __('I want this to be my logo!', 'elisium' );
		}
	}

	return $translated_text;
}

// Add "Elisium Logo Options" link to the "Appearance" menu
function elisium_menu_options() {
	//add_theme_page( $page_title, $menu_title, $capability, $menu_slug, $function);
     add_theme_page('Elisium Logo Options', 'Elisium Logo Options', 'edit_theme_options', 'elisium-settings', 'elisium_admin_options_page');
}
// Load the Admin Options page
add_action('admin_menu', 'elisium_menu_options');

function elisium_admin_options_page() {
	?>
		
		<div class="wrap">
			
			<div id="icon-themes" class="icon32"><br /></div>
		
			<h2><?php _e( 'Elisium Options', 'elisium' ); ?></h2>
			
			<!-- If we have any error by submiting the form, they will appear here -->
			<?php settings_errors( 'elisium-settings-errors' ); ?>
			
			<form id="form-options" action="options.php" method="post" enctype="multipart/form-data">
			
				<?php
					settings_fields('theme_elisium_options');
					do_settings_sections('elisium');
				?>
			
				<p class="submit">
					<input name="theme_elisium_options[submit]" id="submit_options_form" type="submit" class="button-primary" value="<?php esc_attr_e('Save Settings', 'elisium'); ?>" />
					<input name="theme_elisium_options[reset]" type="submit" class="button-secondary" value="<?php esc_attr_e('Reset Defaults', 'elisium'); ?>" />		
				</p>
			
			</form>
			
		</div>
	<?php
}

function elisium_options_validate( $input ) {
	$default_options = elisium_get_default_options();
	$valid_input = $default_options;
	
	$elisium_options = get_option('theme_elisium_options');
	
	$submit = ! empty($input['submit']) ? true : false;
	$reset = ! empty($input['reset']) ? true : false;
	$delete_logo = ! empty($input['delete_logo']) ? true : false;
	
	if ( $submit ) {
		if ( $elisium_options['logo'] != $input['logo']  && $elisium_options['logo'] != '' )
			delete_image( $elisium_options['logo'] );
		
		$valid_input['logo'] = $input['logo'];
	}
	elseif ( $reset ) {
		delete_image( $elisium_options['logo'] );
		$valid_input['logo'] = $default_options['logo'];
	}
	elseif ( $delete_logo ) {
		delete_image( $elisium_options['logo'] );
		$valid_input['logo'] = '';
	}
	
	return $valid_input;
}

function delete_image( $image_url ) {
	global $wpdb;
	
	// We need to get the image's meta ID..
	$query = "SELECT ID FROM wp_posts where guid = '" . esc_url($image_url) . "' AND post_type = 'attachment'";  
	$results = $wpdb -> get_results($query);

	// And delete them (if more than one attachment is in the Library
	foreach ( $results as $row ) {
		wp_delete_attachment( $row -> ID );
	}	
}

/********************* JAVASCRIPT ******************************/
function elisium_options_enqueue_scripts() {
	wp_register_script( 'upload', get_template_directory_uri() .'/options/upload.js', array('jquery','media-upload','thickbox') );	

	if ( 'appearance_page_elisium-settings' == get_current_screen() -> id ) {
		wp_enqueue_script('jquery');
		
		wp_enqueue_script('thickbox');
		wp_enqueue_style('thickbox');
		
		wp_enqueue_script('media-upload');
		wp_enqueue_script('upload');
		
	}
	
}
add_action('admin_enqueue_scripts', 'elisium_options_enqueue_scripts');


function elisium_options_settings_init() {
	register_setting( 'theme_elisium_options', 'theme_elisium_options', 'elisium_options_validate' );
	
	// Add a form section for the Logo
	add_settings_section('elisium_settings_header', __( 'Logo Options', 'elisium' ), 'elisium_settings_header_text', 'elisium');
	
	// Add Logo uploader
	add_settings_field('elisium_setting_logo',  __( 'Logo', 'elisium' ), 'elisium_setting_logo', 'elisium', 'elisium_settings_header');
	
	// Add Current Image Preview 
	add_settings_field('elisium_setting_logo_preview',  __( 'Logo Preview', 'elisium' ), 'elisium_setting_logo_preview', 'elisium', 'elisium_settings_header');
}
add_action( 'admin_init', 'elisium_options_settings_init' );

function elisium_setting_logo_preview() {
	$elisium_options = get_option( 'theme_elisium_options' );  ?>
	<div id="upload_logo_preview" style="min-height: 100px;">
		<img style="max-width:100%;" src="<?php echo esc_url( $elisium_options['logo'] ); ?>" />
	</div>
	<?php
}

function elisium_settings_header_text() {
	?>
		<p><?php _e( 'Manage Logo Options for elisium Theme.', 'elisium' ); ?></p>
	<?php
}

function elisium_setting_logo() {
	$elisium_options = get_option( 'theme_elisium_options' );
	?>
		<input type="hidden" id="logo_url" name="theme_elisium_options[logo]" value="<?php echo esc_url( $elisium_options['logo'] ); ?>" />
		<input id="upload_logo_button" type="button" class="button" value="<?php _e( 'Upload Logo', 'elisium' ); ?>" />
		<?php if ( '' != $elisium_options['logo'] ): ?>
			<input id="delete_logo_button" name="theme_elisium_options[delete_logo]" type="submit" class="button" value="<?php _e( 'Delete Logo', 'elisium' ); ?>" />
		<?php endif; ?>
		<span class="description"><?php _e('Upload an image for the banner.', 'elisium' ); ?></span>
	<?php
}

?>