<?php

/**
 * Generates the options fields that are used in the form.
 */

function grain_fields() {

	global $grain_allowedtags;
	$grain_settings = get_option('grain');
	
	// Get the theme name so we can display it up top
	$grain_themename = get_theme_data(STYLESHEETPATH . '/style.css');
	$grain_themename = $grain_themename['Name'];

	// Gets the unique option id
	if (isset($grain_settings['id'])) {
		$grain_option_name = $grain_settings['id'];
	}
	else {
		$grain_option_name = 'grain';
	};

	$grain_settings = get_option($grain_option_name);
    $grain_options = grain_options();
        
    $grain_counter = 0;
	$grain_menu = '';
	$grain_output = '';
	
	foreach ($grain_options as $grain_value) {
	   
		$grain_counter++;
		$grain_val = '';
		$select_value = '';
		$grain_checked = '';
		
		// Wrap all options
		if ( ($grain_value['type'] != "heading") && ($grain_value['type'] != "info") ) {

			// Keep all ids lowercase with no spaces
			$grain_value['id'] = preg_replace('/\W/', '', strtolower($grain_value['id']) );

			$grain_id = 'section-' . $grain_value['id'];

			$grain_class = 'section ';
			if ( isset( $grain_value['type'] ) ) {
				$grain_class .= ' section-' . $grain_value['type'];
			}
			if ( isset( $grain_value['class'] ) ) {
				$grain_class .= ' ' . $grain_value['class'];
			}

			$grain_output .= '<div id="' . esc_attr( $grain_id ) .'" class="' . esc_attr( $grain_class ) . '">'."\n";
			$grain_output .= '<h4 class="heading">' . esc_html( $grain_value['name'] ) . '</h4>' . "\n";
			$grain_output .= '<div class="option">' . "\n" . '<div class="controls">' . "\n";
		 }
		
		// Set default value to $grain_val
		if ( isset( $grain_value['std']) ) {
			$grain_val = $grain_value['std'];
		}
		
		// If the option is already saved, ovveride $grain_val
		if ( ($grain_value['type'] != 'heading') && ($grain_value['type'] != 'info')) {
			if ( isset($grain_settings[($grain_value['id'])]) ) {
					$grain_val = $grain_settings[($grain_value['id'])];
					// Striping slashes of non-array options
					if (!is_array($grain_val)) {
						$grain_val = stripslashes($grain_val);
					}
			}
		}
		                                
		switch ( $grain_value['type'] ) {
		
		// Basic text input
		case 'text':
			$grain_output .= '<input id="' . esc_attr( $grain_value['id'] ) . '" class="of-input" name="' . esc_attr( $grain_option_name . '[' . $grain_value['id'] . ']' ) . '" type="text" value="' . esc_attr( $grain_val ) . '" />';
		break;
		
		// Radio Box
		case "radio":
			$grain_name = $grain_option_name .'['. $grain_value['id'] .']';
			foreach ($grain_value['options'] as $grain_key => $grain_option) {
				$grain_id = $grain_option_name . '-' . $grain_value['id'] .'-'. $grain_key;
				$grain_output .= '<input class="of-input of-radio" type="radio" name="' . esc_attr( $grain_name ) . '" id="' . esc_attr( $grain_id ) . '" value="'. esc_attr( $grain_key ) . '" '. checked( $grain_val, $grain_key, false) .' /><label for="' . esc_attr( $grain_id ) . '">' . esc_html( $grain_option ) . '</label>';
			}
		break;
		
		// Uploader
		case "upload":
			$grain_output .= grain_medialibrary_uploader( $grain_value['id'], $grain_val, null ); // New AJAX Uploader using Media Library	
		break;

		// Heading for Navigation
		case "heading":
			if($grain_counter >= 2){
			   $grain_output .= '</div>'."\n";
			}
			$grain_jquery_click_hook = preg_replace('/\W/', '', strtolower($grain_value['name']) );
			$grain_jquery_click_hook = "of-option-" . $grain_jquery_click_hook;
			$grain_menu .= '<a id="'.  esc_attr( $grain_jquery_click_hook ) . '-tab" class="nav-tab" title="' . esc_attr( $grain_value['name'] ) . '" href="' . esc_attr( '#'.  $grain_jquery_click_hook ) . '">' . esc_html( $grain_value['name'] ) . '</a>';
			$grain_output .= '<div class="group" id="' . esc_attr( $grain_jquery_click_hook ) . '">';
			$grain_output .= '<h3>' . esc_html( $grain_value['name'] ) . '</h3>' . "\n";
			break;
		}

		if ( ( $grain_value['type'] != "heading" ) && ( $grain_value['type'] != "info" ) ) {
			if ( $grain_value['type'] != "checkbox" ) {
				$grain_output .= '<br/>';
			}
			$grain_explain_value = '';
			if ( isset( $grain_value['desc'] ) ) {
				$grain_explain_value = $grain_value['desc'];
			}
			$grain_output .= '</div><div class="explain">' . wp_kses( $grain_explain_value, $grain_allowedtags) . '</div>'."\n";
			$grain_output .= '<div class="clear"></div></div></div>'."\n";
		}
	}
    $grain_output .= '</div>';
    return array($grain_output,$grain_menu);
}