<?php

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

function optionsframework_fields() {

	$optionsframework_settings = get_option('optionsframework');

	// Gets the unique option id
	if (isset($optionsframework_settings['id'])) {
		$option_name = $optionsframework_settings['id'];
	}
	else {
		$option_name = 'optionsframework';
	};

	$settings = get_option($option_name);
    $options = optionsframework_options();
        
    $counter = 0;
	$menu = '';
	$output = '';
	
	foreach ($options as $value) {
	   
		$counter++;
		$val = '';
		$select_value = '';
		
		// Wrap all options
		if ( ($value['type'] != "heading") && ($value['type'] != "info") ) {
		
			// Keep all ids lowercase with no spaces
			$value['id'] = preg_replace('/\W/', '', strtolower($value['id']) );
			
		 	$class = ''; if(isset( $value['class'] )) { $class = $value['class']; }
			$output .= '<div id="section-' . $value['id'] .'" class="section section-'.$value['type'].' '. $class .'">'."\n";
			$output .= '<h3 class="heading">'. $value['name'] .'</h3>'."\n";
			$output .= '<div class="option">'."\n" . '<div class="controls">'."\n";
		 }
		
		// Set default value to $val
		if ( isset($value['std']) ) {
			$val = $value['std'];
		}
		
		// If the option is already saved, ovveride $val
		if ( ($value['type'] != 'heading') && ($value['type'] != 'info')) {
			if ( isset($settings[($value['id'])]) ) {
					$val = $settings[($value['id'])];
					// Striping slashes of non-array options
					if (!is_array($val)) {
						$val = stripslashes($val);
					}
			}
		}
		                                
		switch ( $value['type'] ) {
		
		// Basic text input
		case 'text':
			$output .= '<input id="'. $value['id'] .'" class="of-input" name="'.$option_name.'['.$value['id'].']" type="'. $value['type'] .'" value="'. $val .'" />';
		break;
		case 'range':
			$output .= '<input id="'. $value['id'] .'" type="range" name="'.$option_name.'['.$value['id'].']" value="'. $val .'"';
			if (isset($value['min'])) {
			$output .= '" min="' . $value['min'];
		}
		if (isset($value['max'])) {
			$output .= '" max="' . $value['max'];
		}
		if (isset($value['step'])) {
			$output .= '" step="' . $value['step'];
		}
		$output .= '" />';
		if (isset($value['suffix'])) {
			$output .= '<span class="space">'. $value['suffix'] .'</span>';
		}
				break;
		
		// Textarea
		case 'textarea':
			$cols = '8';
			$ta_value = '';
			
			if(isset($value['options'])){
				$ta_options = $value['options'];
				if(isset($ta_options['cols'])){
					$cols = $ta_options['cols'];
				} else { $cols = '8'; }
			}
			
			$val = stripslashes( $val );
			
			$output .= '<textarea id="'. $value['id'] .'" class="of-input" name="'.$option_name.'['.$value['id'].']" cols="'. $cols .'" rows="8">'.$val.'</textarea>';
		break;

		// Textarea
		case 'textarearich':
			$cols = '8';
			$ta_value = '';
			
			if(isset($value['options'])){
				$ta_options = $value['options'];
				if(isset($ta_options['cols'])){
					$cols = $ta_options['cols'];
				} else { $cols = '8'; }
			}
			
			$val = stripslashes( $val );
			
			$output .= '<textarea id="'. $value['id'] .'" class="of-input wysiwyg-editor" name="'.$option_name.'['.$value['id'].']" cols="'. $cols .'" rows="8">'.$val.'</textarea>';
		break;

		
		
		// Select Box
		case ($value['type'] == 'select'):
			$output .= '<select class="of-input" name="'.$option_name.'['.$value['id'].']" id="'. $value['id'] .'">';
			
			foreach ($value['options'] as $key => $option ) {
				$selected = '';
				 if( $val != '' ) {
					 if ( $val == $key) { $selected = ' selected="selected"';} 
			     }
				 $output .= '<option'. $selected .' value="' . $key . '">';
				 $output .= $option;
				 $output .= '</option>';
			 } 
			 $output .= '</select>';
		break;
		
		

		
		// Radio Box
		case "radio":
			foreach ($value['options'] as $key => $option) {
				$checked = '';
				if($val != '') {
					if ( $val == $key) { $checked = 'checked="checked"'; } 
				} 
				$name = $option_name .'['. $value['id'] .']';
				$id = $name .'-'. $key;
				$output .= '<input class="of-input of-radio" type="radio" name="'. $name .'" id="'. $id .'" value="'. $key .'" '. $checked .' /><label for="'. $id .'">'. $option .'</label><br />';
			}
		break;
		
		// Checkbox
		case "checkbox": 
		
			$checked = '';
		   
			if ( $val == 'true') {
				$checked = 'checked="checked"';
			}
			
			$output .= '<input id="'. $value['id'] .'" class="checkbox iphonecheck of-input" type="checkbox" name="'.$option_name.'['.$value['id'].']" value="true" '. $checked .' />';
		break;
		
		// Multicheck
		case "multicheck":
			$output .= '<input id="'. $value['id'] .'" type="hidden" name="'.$option_name.'['.$value['id'].']" />';
			foreach ($value['options'] as $key => $option) {
				$checkbox_name = $option;
				$option = preg_replace('/\W/', '', strtolower($key));
				$checkbox_id = $option_name.'['.$value['id'].'_'. $option .']';
				$checked = '';
				
			    if ( isset($val[$option]) ) {
					if ( $val[$option] == 'true') {
			   			$checked = 'checked="checked"';
					}
				}
				
			$output .= '<input id="'. $checkbox_id .'" class="checkbox of-input" type="checkbox" name="'. $checkbox_id .'" value="true" '. $checked .' /><label for="'. $checkbox_id .'">'. $checkbox_name .'</label><br />';						
			}
		break;
		
		// Color picker
		case "color":
			$output .= '<div id="' . $value['id'] . '_picker" class="colorSelector"><div style="background-color:'.$val.'"></div></div>';
			$output .= '<input class="of-color" name="'.$option_name.'['.$value['id'].']" id="'. $value['id'] .'" type="text" value="'. $val .'" />';
		break; 
		
		// Uploader
		case "upload":
			$output .= optionsframework_medialibrary_uploader( $value['id'], $val, null ); // New AJAX Uploader using Media Library	
		break;
		
		// Typography
		case 'typography':	
		
			// Set main option
			$output .= '<input id="'. $value['id'] .'" type="hidden" name="'.$option_name.'['.$value['id'].']" />';
			$typography_stored = $val;
			
			// Font Size
			$output .= '<select class="of-typography of-typography-size" name="'.$option_name.'['.$value['id'].'_size]" id="'. $value['id'].'_size">';
			for ($i = 9; $i < 77; $i++) { 
				$size = $i.'px';
				if ($typography_stored['size'] == $size) { $selected = ' selected="selected"'; }
				$output .= '<option value="'. $i .'px" ' . selected($typography_stored['size'], $size, false) . '>'. $i .'px</option>'; 
			}
			$output .= '</select>';
		
			// Font Face
			$output .= '<select class="of-typography of-typography-face" name="'.$option_name.'['.$value['id'].'_face]" id="'. $value['id'].'_face">';
			$faces = array('arial'=>'Arial',
							'tahoma'=>'Tahoma',
							'HacenVanillaRegular'=>'Hacen Vanilla Regular',
							'Hacen Casablanca'=>'Hacen Casablanca',
							'Hacen Casablanca Heavy'=>'Hacen Casablanca Heavy',
							'Hacen Casablanca Light'=>'Hacen Casablanca Light',
							'Hacen Dalal'=>'Hacen Dalal',
							'Hacen Dalal St'=>'Hacen Dalal St',
							'Hacen Dalal Text'=>'Hacen Dalal Text',
							'Hacen Extender'=>'Hacen Extender',
							'Hacen Extender X4'=>'Hacen Extender X4',
							'Hacen Extender X4 Deeper'=>'Hacen Extender X4 Deeper',
							'Hacen Extender X4 Super_Fit'=>'Hacen Extender X4 Super_Fit',
							'Hacen_Extender_X-Slant'=>'Hacen_Extender_X-Slant',
							'Hacen Saudi Arabia XL'=>'Hacen Saudi Arabia XL',
							'Hacen Saudi Arabia'=>'Hacen Saudi Arabia',
							'Hacen Vanilla Ultra Light'=>'Hacen Vanilla Ultra Light',
							'tnaskh'=>'tnaskh',
							'tnaskh bold'=>'tnaskh bold',
							'Hacen Beirut'=>'Hacen Beirut',
							'Hacen Beirut Hd'=>'Hacen Beirut Hd',
							'Hacen_Beirut_Light'=>'Hacen_Beirut_Light',
							'Hacen_Egypt'=>'Hacen_Egypt',
							'Hacen_Freehand'=>'Hacen_Freehand',
							'Hacen_Lebanon'=>'Hacen_Lebanon',
							'Hacen_Liner_Print-out'=>'Hacen_Liner_Print-out',
							'Hacen_Liner_Print-out_Light'=>'Hacen_Liner_Print-out_Light',
							'Hacen_Liner_Screen'=>'Hacen_Liner_Screen',
							'Hacen_Liner_Screen_Bd'=>'Hacen_Liner_Screen_Bd',
							'Hacen_Liner_XL'=>'Hacen_Liner_XL',
							'Hacen_Liner_XXL'=>'Hacen_Liner_XXL',
							'Hacen_Newspaper'=>'Hacen_Newspaper',
							'Hacen_Promoter'=>'Hacen_Promoter',
							'Hacen_Promoter_Lt'=>'Hacen_Promoter_Lt',
							'Hacen_Promoter_Md'=>'Hacen_Promoter_Md',
							'Hacen_Sahafa'=>'Hacen_Sahafa',
							'Hacen_Samra'=>'Hacen_Samra',
							'Hacen_Samra_Lt'=>'Hacen_Samra_Lt',
							'Hacen_Typographer'=>'Hacen_Typographer',
							'Hacen_Typographer_Bold'=>'Hacen_Typographer_Bold',
							'Hacen_Typographer_Book'=>'Hacen_Typographer_Book',
							'Hacen_Typographer_Heavy'=>'Hacen_Typographer_Heavy',
							'Aljazeera'=>'Aljazeera',
							'mohammad_bold'=>'mohammad_bold'
							);	
				foreach ($faces as $key => $face) {
				$output .= '<option value="'. $key .'" ' . selected($typography_stored['face'], $key, false) . '>'. $face .'</option>';
			}			
			$output .= '</select>';	
			
			// Font Weight
			$output .= '<select class="of-typography of-typography-style" name="'.$option_name.'['.$value['id'].'_style]" id="'. $value['id'].'_style">';
			
			$styles = array('' => 'Weight', 'normal'=>'Normal',
							'bold'=>'Bold',
							'bolder'=>'Bolder',
							'lighter'=>'Lighter',
							'100'=>'100',
							'200'=>'200',
							'300'=>'300',
							'400'=>'400',
							'500'=>'500',
							'600'=>'600',
							'700'=>'700',
							'800'=>'800',
							'900'=>'900'
							);
							
			foreach ($styles as $key => $style) {
				$output .= '<option value="'. $key .'" ' . selected($typography_stored['style'], $key, false) . '>'. $style .'</option>';		
			}
			$output .= '</select>';
			
			// Font Color		
			$output .= '<div id="' . $value['id'] . '_color_picker" class="colorSelector"><div style="background-color:'.$typography_stored['color'].'"></div></div>';
			$output .= '<input class="of-color of-typography of-typography-color" name="'.$option_name.'['.$value['id'].'_color]" id="'. $value['id'] .'_color" type="text" value="'. $typography_stored['color'] .'" />';
			
		break;
		
		// Background
		case 'background':
		
			//Set main option
			$output .= '<input id="'. $value['id'] .'" type="hidden" name="'.$option_name.'['.$value['id'].']" />';
			
			$background = $val;
			
			// Background Color
			if (!isset($background['color'])) {
				$background['color'] = '';
			}
			
			$output .= '<div id="' . $value['id'] . '_color_picker" class="colorSelector"><div style="background-color:'.$background['color'].'"></div></div>';
			
			$output .= '<input class="of-color of-background of-background-color" name="'.$option_name.'['.$value['id'].'_color]" id="'. $value['id'] .'_color" type="text" value="'. $background['color'] .'" />';
			
			
			// Background Image - New AJAX Uploader using Media Library
			if (!isset($background['image'])) {
				$background['image'] = '';
			}
			
			$output .= optionsframework_medialibrary_uploader( $value['id'] . '_image', $background['image'], null );
			if ($background['image'] == '') {$hide = ' hide ';} else { $hide=''; }
			$output .= '<div class="of-background-properties' . $hide . '">';
			
			// Background Repeat
			$output .= '<select class="of-background of-background-repeat" name="'.$option_name.'['.$value['id'].'_repeat]" id="'. $value['id'].'_repeat">';
			$repeats = array("no-repeat"=>"No Repeat","repeat-x"=>"Repeat Horizontally","repeat-y"=>"Repeat Vertically","repeat"=>"Repeat All");
			
			foreach ($repeats as $key => $repeat) {
				$output .= '<option value="'. $key .'" ' . selected($background['repeat'], $key, false) . '>'. $repeat .'</option>';
			}
			$output .= '</select>';
			
			// Background Position
			$output .= '<select class="of-background of-background-position" name="'.$option_name.'['.$value['id'].'_position]" id="'. $value['id'].'_position">';
			$positions = array("top left"=>"Top Left","top center"=>"Top Center","top right"=>"Top Right","center left"=>"Middle Left","center center"=>"Middle Center","center right"=>"Middle Right","bottom left"=>"Bottom Left","bottom center"=>"Bottom Center","bottom right"=>"Bottom Right");
			
			foreach ($positions as $key=>$position) {
				$output .= '<option value="'. $key .'" ' . selected($background['position'], $key, false) . '>'. $position .'</option>';
			}
			$output .= '</select>';
			
			// Background Attachment
			$output .= '<select class="of-background of-background-attachment" name="'.$option_name.'['.$value['id'].'_attachment]" id="'. $value['id'].'_attachment">';
			$attachments = array("scroll"=>"Scroll Normally","fixed"=>"Fixed in Place");
			
			foreach ($attachments as $key => $attachment) {
				$output .= '<option value="'. $key .'" ' . selected($background['attachment'], $key, false) . '>'. $attachment .'</option>';
			}
			$output .= '</select>';
			$output .= '</div>';
		
		break; 
		
		
			
	//border
			case 'border':
		
			//Set main option
			$output .= '<input id="'. $value['id'] .'" type="hidden" name="'.$option_name.'['.$value['id'].']" />';
			
			$border = $val;
			
			// border Color
			if (!isset($border['color'])) {
				$border['color'] = '';
			}
			
			$output .= '<div id="' . $value['id'] . '_color_picker" class="colorSelector"><div style="background-color:'.$border['color'].'"></div></div>';
			$output .= '<input class="of-color of-border of-border-color" name="'.$option_name.'['.$value['id'].'_color]" id="'. $value['id'] .'_color" type="text" value="'. $border['color'] .'" />';

//border size
			if (!isset($border['size'])) {
				$border['size'] = '';
			}
					$output .= '<div class="mini"><div class="controls"><input id="'. $value['id'] .'_size" class="of-input mini" name="'.$option_name.'['.$value['id'].'_size]" type="text" value="'. $border['size'] .'" /> </div></div><span class="space"> Border Size</span><br />';


		//border position
			if (!isset($border['position'])) {
				$border['position'] = '';
			}
			$output .= '<select class="of-border of-border-position" name="'.$option_name.'['.$value['id'].'_position]" id="'. $value['id'].'_position">';
			$positions = array("" => "Border All", "-top"=>"Border Top", "-right"=>"Border Right","-bottom"=>"Border Bottom","-left"=>"Border Left");
			foreach ($positions  as $key => $position) {
				$output .= '<option value="'. $key .'" ' . selected($border['position'], $key, false) . '>'. $position .'</option>';
			}
			$output .= '</select>';
		//border Style
			if (!isset($border['style'])) {
				$border['style'] = '';
			}
			$output .= '<select class="of-border of-border-style" name="'.$option_name.'['.$value['id'].'_style]" id="'. $value['id'].'_style">';
			$styles = array("none" => "none", "hidden" => "hidden", "dotted" => "dotted", "dashed" => "dashed", "solid" => "solid", "double" => "double", "groove" => "groove", "ridge" => "ridge", "inset" => "inset", "outset" => "outset", "inherit" => "inherit", );
			foreach ($styles  as $key => $style) {
				$output .= '<option value="'. $key .'" ' . selected($border['style'], $key, false) . '>'. $style .'</option>';
			}
			$output .= '</select>';
			
			break;
                
             //shadow
			case 'shadow':
		
			//Set main option
			$output .= '<input id="'. $value['id'] .'" type="hidden" name="'.$option_name.'['.$value['id'].']" />';
			
			$shadow = $val;
	
//shadow size
			if (!isset($shadow['size'])) {
				$shadow['size'] = '';
			}
				$output .= '<input id="'. $value['id'] .'_size" class="of-input mini" name="'.$option_name.'['.$value['id'].'_size]" type="text" value="'. $shadow['size'] .'" />';
	
			// shadow Color
			if (!isset($shadow['color'])) {
				$shadow['color'] = '';
			}
			
			$output .= '<div id="' . $value['id'] . '_color_picker" class="colorSelector"><div style="background-color:'.$shadow['color'].'"></div></div>';
			$output .= '<input class="of-color of-shadow of-shadow-color" name="'.$option_name.'['.$value['id'].'_color]" id="'. $value['id'] .'_color" type="text" value="'. $shadow['color'] .'" />';
			break;
                

			case 'position':
		
			
			//Set main option
			$output .= '<input id="'. $value['id'] .'" type="hidden" name="'.$option_name.'['.$value['id'].']" />';
			
			$position = $val;
	
			//position top
			if (!isset($position['top'])) {
				$position['top'] = '';
			}
					$output .= '<div class="mini posit"><div class="controls"><input id="'. $value['id'] .'_top" class="of-input mini" name="'.$option_name.'['.$value['id'].'_top]" type="text" value="'. $position['top'] . '" /><span class ="space2">Top</span></div></div>';
	
			//position right
			if (!isset($position['right'])) {
				$position['right'] = '';
			}
				$output .= '<div class="mini posit"><div class="controls"><input id="'. $value['id'] .'_right" class="of-input mini" name="'.$option_name.'['.$value['id'].'_right]" type="text" value="'. $position['right'] . '" /><span class ="space2">Right</span></div></div>';
	
	
			//position bottom
			if (!isset($position['bottom'])) {
				$position['bottom'] = '';
			}
			$output .= '<div class="mini posit"><div class="controls"><input id="'. $value['id'] .'_bottom" class="of-input mini" name="'.$option_name.'['.$value['id'].'_bottom]" type="text" value="'. $position['bottom'] . '" /><span class ="space2">bottom</span></div></div>';
	
	
			//position left
			if (!isset($position['left'])) {
				$position['left'] = '';
			}
$output .= '<div class="mini posit"><div class="controls"><input id="'. $value['id'] .'_left" class="of-input mini" name="'.$option_name.'['.$value['id'].'_left]" type="text" value="'. $position['left'].'" /><span class ="space2">Left</span></div></div>';
	
				break;
	
    
		// Image Selectors
		case "images":
			$i = 0;
				   
			foreach ($value['options'] as $key => $option) { 
				$i++;
				$checked = '';
				$selected = '';
				if ($val != '') {
					if ( $val == $key) { $checked = ' checked'; $selected = 'of-radio-img-selected'; }
				}
				
				$output .= '<span>';
				$output .= '<input type="radio" id="of-radio-img-' . $value['id'] . $i . '" class="checkbox of-radio-img-radio" value="'.$key.'" name="'.$option_name.'['.$value['id'].']" '.$checked.' />';
				$output .= '<div class="of-radio-img-label">'. $key .'</div>';
				$output .= '<img src="'.$option.'" alt="" class="of-radio-img-img '. $selected .'" onClick="document.getElementById(\'of-radio-img-'. $value['id'] . $i.'\').checked = true;" />';
				$output .= '</span>';
			}
		break;   
		
		// Info
		case "info":
			$class = ''; if(isset( $value['class'] )) { $class = $value['class']; }
			$output .= '<div class="section section-'.$value['type'].' '. $class .'">'."\n";
			if ( isset($value['name']) )  { $output .= '<h3 class="heading">'. $value['name'] .'</h3>'."\n"; }
			if ( $value['desc'] )  { $output .= '<p>'. $value['desc'] .'</p>'."\n"; }
			$output .= '<div class="clear"></div></div>'."\n";
		break;                       

		// Divider 
		case "divider":
			$output .= '<div class="divider"><div class="clear"></div></div>';
		break;                       
		
		// Heading for Navigation
		case "heading":
			if($counter >= 2){
			   $output .= '</div>'."\n";
			}
			$jquery_click_hook = preg_replace('/\W/', '', strtolower($value['slug']) );
			$jquery_click_hook = "of-option-" . $jquery_click_hook;
			$menu .= '<li><a title="'.  $value['name'] .'" href="#'.  $jquery_click_hook  .'">'.  $value['name'] .'</a></li>';
			$output .= '<div class="group" id="'. $jquery_click_hook  .'"><h2>'.$value['name'].'</h2>'."\n";
		break;                                  
		}
		
		
		if ( ($value['type'] != "heading") && ($value['type'] != "info") ) { 
			if ( $value['type'] != "checkbox" ) 
				{ 
				$output .= '<br/>';
				}
			if(!isset($value['desc'])){ $explain_value = ''; } else{ $explain_value = $value['desc']; } 
			$output .= '</div><div class="explain">'. $explain_value .'</div>'."\n";
			$output .= '<div class="clear"> </div></div></div>'."\n";
		}
	}
    $output .= '</div>';
    return array($output,$menu);
}


