<?php

$slist = $this->get_style_list();
$llist = $this->get_language_list();
$font_list=$this->get_font_list();
if(isset($_POST['submit']) && !$_POST['resetall']){
	$option = array();
	$option['color'] = $this->color_handler($_POST['color']);
	$option['hidecode'] = intval($_POST['hidecode']);
	$option['rownum'] = intval($_POST['rownum']);
	$option['edit_code'] = intval($_POST['edit_code']);
	$option['nowrap'] = intval($_POST['nowrap']);
	$option['tab-size'] = max(intval($_POST['nowrap']),1);
	$option['html_tag_p'] = intval($_POST['html_tag_p']);
	$option['handle_comments'] = intval($_POST['handle_comments']);
	$option['use_custom_color'] = (bool)$_POST['use_custom_color'];
	$option['system_color'] = in_array($_POST['system_color'],$slist,true)?$_POST['system_color']:'Gray';
	$option['font_style']= in_array($_POST['font_style'],array_keys($font_list),true)?$_POST['font_style']:'consolas';
	$option['allow-download-font'] = intval($_POST['allow-download-font']);
	$option['border-width'] = min(max(intval($_POST['border-width']),0),9);
	$option['formatted']=$_POST['formatted'];
	$option['formatted']=array('pre'=>1,'bb'=>0,'code'=>0);
	//$option['formatted']= array_map('intval',$option['formatted']);
	$option['highlighter'] = intval($_POST['highlighter']);

	$option['mce-button-row-position'] = intval($_POST['mce-button-row-position']);

	$option['highlight'] = (bool)$_POST['highlight'];
	if(count($_POST['lang_enable'])){
		$option['lang_enable'] = array_intersect($llist,$_POST['lang_enable']);
	}else{
		$option['highlight'] = false;
		$option['lang_enable']= array();
	}

	$option['hlcolor']= $this->color_handler($_POST['hlcolor']);

	update_option($this->adminname,$option);
	echo '<div class="updated notice is-dismissible"><p>'.__('Settings saved.').'</p></div>';
}elseif(true == $_POST['resetall']){
	delete_option($this->adminname);
	$this->read_options();
	$option = $this->option;
	echo '<div class="error notice is-dismissible"><strong>'.__('Options Reset').'</strong></div>';
}else{
	$option = $this->getOptions();
}

print_r($option);


$rownumradio = array($option['rownum']=>' checked="checked"');
$hiddenradio = array($option['hidecode']=>' checked="checked"');
$edit_code_radio = array($option['edit_code']=>' checked="checked"');
$userstyleradio = array($option['use_custom_color']=>' checked="checked"');
if(in_array($_GET['op'],array('general','appearance'))){
	$op=$_GET['op'];
}else{
	$op='general';
}
$oparr=array($op=>'nav-tab-active');


echo'<script type="text/javascript" src="'.$this->plugindir.'/adminpage/admin.js"></script>';
echo '<link rel="stylesheet" type="text/css" id="easycodecss" href="'.$this->plugindir.'/adminpage/admin.css" />';

?>
<div class='wrap'>
<h1 class="header-title">EasyCode Options</h1>

<h2 class="nav-tab-wrapper">
<a onclick="ecAdmin.changeTab(this.id);" class="nav-tab nav-tab-active" id="tab-general"><?php _e('General Settings'); ?></a>
<a onclick="ecAdmin.changeTab(this.id);ecAdmin.refreshColorShow();ecAdmin.changeStyleRadio();" class="nav-tab" id="tab-appearance"><?php _e('Appearance'); ?></a>
<a onclick="ecAdmin.changeTab(this.id);" class="nav-tab" id="tab-highlight"><?php _e('Highlight'); ?></a>
<a onclick="ecAdmin.changeTab(this.id);" class="nav-tab" id="tab-advanced"><?php _e('Advanced'); ?></a>
</h2>

<form method="post" action="">

<div id="op-general">
<table class="form-table">
	<tr valign="top">
		<th scope="row"><?php _e('Highlight','easycode'); ?></th>
		<td><a href="javascript:ecAdmin.changeTab('tab-highlight');"><?php _e($option['highlighter']?'Enabled. Click to <em>Highlight Tab</em>.':'Disabled! Please go to <em>Highlight Tab</em> to enable.','easycode');?></a></td>
	</tr>

	<tr valign="top">
		<th scope="row"><?php _e('Collapsed code blocks','easycode'); ?></th>
		<td>
			<label><input type="radio" name="hidecode" value="1" <?php echo $hiddenradio[1]; ?>><?php _e('Collapsed code blocks by default','easycode'); ?></label><br>
			<label><input type="radio" name="hidecode" value="0" <?php echo $hiddenradio[0]; ?>><?php _e('Showed code blocks by default','easycode'); ?></label><br>
		</td>
	</tr>

	<tr valign="top">
		<th scope="row"><?php _e('Line numbers','easycode');?></th>
		<td>
			<label><input type="radio" name="rownum" value="0" <?php  echo $rownumradio[0]; ?>><?php _e('No line numbers','easycode'); ?></label><br />
			<label><input type="radio" name="rownum" value="1" <?php  echo $rownumradio[1]; ?>><?php _e('No leading zeros','easycode'); ?></label><br />
			<label><input type="radio" name="rownum" value="2" <?php  echo $rownumradio[2]; ?>><?php _e('Display leading zeros','easycode'); ?></label>
		</td>
	</tr>

	<tr valign="top">
		<th scope="row"><?php _e('Editing code','easycode'); ?></th>
		<td>
			<label><input type="radio" name="edit_code" value="0" <?php  echo $edit_code_radio[0]; ?>><?php _e('Disabled','easycode'); ?></label><br />
			<label><input type="radio" name="edit_code" value="1" <?php  echo $edit_code_radio[1]; ?>><?php _e('Float textarea','easycode'); ?></label><br />
			<label><input type="radio" name="edit_code" value="2" <?php  echo $edit_code_radio[2]; ?>><?php _e('In a new window','easycode'); ?></label>
		</td>
	</tr>

	<tr valign="top">
		<th scope="row"><?php _e('Other','easycode'); ?></th>
		<td>
			<!-- <label><input type="checkbox" name="html_tag_p" value="1" <?php echo $option['html_tag_p']?'checked="checked"':''; ?>><?php _e('HTML TAG:use &lt;p&gt; instead of &lt;ol&gt;','easycode'); ?></label><br /> -->
			<label><input type="checkbox" name="handle_comments" value="1" <?php echo $option['handle_comments']?'checked="checked"':''; ?>><?php _e('Process code in comments','easycode'); ?></label><br />
		</td>
	</tr>

	<tr valign="top">
		<th scope="row"><?php _e('Rich text editor','easycode'); ?></th>
		<td>
			<?php _e('Which row is the Inserting-code button placed into?','easycode'); ?><br>
			<small><?php _e('@ Inserting-code button can be hidden if placed in Row 2. ','easycode'); ?></small><br>
			<label><input type="radio" name="mce-button-row-position" value="0" <?php  echo $option['mce-button-row-position'] <1?'checked':''; ?>><?php _e('No button in TinyMCE editor','easycode'); ?></label><br>
			<label><input type="radio" name="mce-button-row-position" value="1" <?php  echo $option['mce-button-row-position']==1?'checked':''; ?>><?php _e('Row 1','easycode'); ?></label><br>
			<label><input type="radio" name="mce-button-row-position" value="2" <?php  echo $option['mce-button-row-position'] >1?'checked':''; ?>><?php _e('Row 2','easycode'); ?></label><br>
		</td>
	</tr>

	<!-- <tr valign="top">
		<th scope="row"><?php _e('Formatted Tag','easycode'); ?></th>
		<td>
			<label><input type="checkbox" name="formatted[bb]" value="1" <?php echo $option['formatted']['bb']?'checked="checked"':''; ?>>[code <i>title</i>]...[/code]</label><br />
			<label><input type="checkbox" name="formatted[pre]" value="1" checked="checked" disabled="disabled">HTML <?php _e('Tag','easycode');?>: &lt;pre&gt;</label><br />
			<label><input type="checkbox" name="formatted[code]" value="1" <?php echo $option['formatted']['code']?'checked="checked"':''; ?>>HTML <?php _e('Tag','easycode');?>: &lt;code&gt;</label><small> (<?php _e('The title is not avalible.','easycode');?>)<br />
		</td>
	</tr> -->
	<tr valign="top">
		<th scope="row"><?php _e('Formatted Tag','easycode'); ?></th>
		<td>
			<small><?php _e('Short tag [code] and Html tag &lt;code&gt; have been deprecated since version 1.4.2, only html tag &lt;pre&gt; is still available.','easycode'); ?></small><br>
			<label><input type="checkbox" disabled>[code <i>title</i>]...[/code]</label><br>
			<label><input type="checkbox" disabled checked>HTML <?php _e('Tag','easycode');?>: &lt;pre&gt;</label><br>
			<label><input type="checkbox" disabled>HTML <?php _e('Tag','easycode');?>: &lt;code&gt;</label><br>
		</td>
	</tr>
</table>
</div>

<div id="op-appearance" class="">
	<p>
		<h3><?php _e('Color schemes','easycode'); ?></h3>
		<label><input type="radio" name="use_custom_color" value="0" onclick="ecAdmin.changeStyleRadio(0);" <?php echo $userstyleradio[0];?>><?php _e('Use system color schemes','easycode');?>&nbsp;&nbsp;</label>
		<select name="system_color" id="system_color">
		<?php if (!in_array($option['system_color'],$slist,TRUE)) $option['system_color']='Gray';
		foreach($slist as $sname){
			$che = ($option['system_color']===$sname)?' selected="selected"':'';
			echo "<option value=\"$sname\" $che>$sname</option>";
		}?>
		</select>
		<br /><br />
		<label><input type="radio" id="use_custom_color" name="use_custom_color" value="1" onclick="ecAdmin.changeStyleRadio(1);" <?php echo $userstyleradio[1];?>><?php _e('Use custom color schemes','easycode');?></label>
	</p>

	<table id="inputcolor">
		<col align="left" />
		<tr>
			<td><?php _e('Background');?></td>
			<td>
				<input type="text" class="colorinput" class="colorinput" name="color[bg]" maxlength="7" onblur="ecAdmin.colorHandler(this);" value="<?php echo $option['color']['bg']; ?>" />
				<span class="colorshow">&nbsp;</span>
			</td>
			<td></td>
		</tr>
		<tr>
			<td><?php echo __('Background'),'(',__('active'),')';?></td>
			<td>
				<input type="text" class="colorinput" name="color[bga]" maxlength="7" onblur="ecAdmin.colorHandler(this);" value="<?php echo $option['color']['bga']; ?>" />
				<span class="colorshow">&nbsp;</span>
			</td>
			<td></td>
		</tr>
		<tr>
			<td><?php _e('Text Color');?></td>
			<td>
				<input type="text" class="colorinput" name="color[text]" maxlength="7" onblur="ecAdmin.colorHandler(this);" value="<?php echo $option['color']['text']; ?>" />
				<span class="colorshow">&nbsp;</span>
			</td>
			<td></td>
		</tr>
		<tr>
			<td><?php echo __('Text Color'),'(',__('active'),')';?></td>
			<td>
				<input type="text" class="colorinput" name="color[texta]" maxlength="7" onblur="ecAdmin.colorHandler(this);" value="<?php echo $option['color']['texta']; ?>" />
				<span class="colorshow">&nbsp;</span>
			</td>
			<td></td>
		</tr>
		<tr>
			<td><?php _e('Side color','easycode');?></td>
			<td>
				<input type="text" class="colorinput" name="color[side]" maxlength="7" onblur="ecAdmin.colorHandler(this);" value="<?php echo $option['color']['side']; ?>" />
				<span class="colorshow">&nbsp;</span>
			</td>
			<td></td>
		</tr>
		<tr>
			<td><?php _e('Border');?></td>
			<td>
				<input type="text" class="colorinput" name="color[border]" maxlength="7" onblur="ecAdmin.colorHandler(this);" value="<?php echo $option['color']['border']; ?>" />
				<span class="colorshow">&nbsp;</span>
			</td>
			<td></td>
		</tr>
		<tr>
			<td><?php _e('Border Width','easycode');?>&nbsp;(0-9px)</td>
			<td><input type="text" name="border-width" maxlength="1" value="<?php echo $option['border-width']; ?>"/></td>
		</tr>

		<!-- highlight color -->
		<tr>
			<td colspan="2">
				<strong><?php _e('Default highlight colors','easycode'); ?></strong><br />
				<small><?php _e('If a language is not set highlight colors specifically,the following colors will be used.','easycode'); ?></small>
			</td>
		</tr>
		<tr>
			<td>Function</td>
			<td>
				<input type="text" class="colorinput" name="hlcolor[function]" maxlength="7" onblur="ecAdmin.colorHandler(this);" value="<?php echo $option['hlcolor']['function']; ?>" />
				<span class="colorshow">&nbsp;</span>
			</td>
		</tr>
		<tr>
			<td>Variable</td>
			<td>
				<input type="text" class="colorinput" name="hlcolor[variable]" maxlength="7" onblur="ecAdmin.colorHandler(this);" value="<?php echo $option['hlcolor']['variable']; ?>" />
				<span class="colorshow">&nbsp;</span>
			</td>
		</tr>
		<tr>
			<td>Constant</td>
			<td>
				<input type="text" class="colorinput" name="hlcolor[constant]" maxlength="7" onblur="ecAdmin.colorHandler(this);" value="<?php echo $option['hlcolor']['constant']; ?>" />
				<span class="colorshow">&nbsp;</span>
			</td>
		</tr>
		<tr>
			<td>String</td>
			<td>
				<input type="text" class="colorinput" name="hlcolor[string]" maxlength="7" onblur="ecAdmin.colorHandler(this);" value="<?php echo $option['hlcolor']['string']; ?>" />
				<span class="colorshow">&nbsp;</span>
			</td>
		</tr>
		<tr>
			<td>Number</td>
			<td>
				<input type="text" class="colorinput" name="hlcolor[number]" maxlength="7" onblur="ecAdmin.colorHandler(this);" value="<?php echo $option['hlcolor']['number']; ?>" />
				<span class="colorshow">&nbsp;</span>
			</td>
		</tr>
		<tr>
			<td>Comment</td>
			<td>
				<input type="text" class="colorinput" name="hlcolor[comment]" maxlength="7" onblur="ecAdmin.colorHandler(this);" value="<?php echo $option['hlcolor']['comment']; ?>" />
				<span class="colorshow">&nbsp;</span>
			</td>
		</tr>
		<tr>
			<td>Keyword</td>
			<td>
				<input type="text" class="colorinput" name="hlcolor[keyword]" maxlength="7" onblur="ecAdmin.colorHandler(this);" value="<?php echo $option['hlcolor']['keyword']; ?>" />
				<span class="colorshow">&nbsp;</span>
			</td>
		</tr>
		<tr>
			<td>Important</td>
			<td>
				<input type="text" class="colorinput" name="hlcolor[important]" maxlength="7" onblur="ecAdmin.colorHandler(this);" value="<?php echo $option['hlcolor']['important']; ?>" />
				<span class="colorshow">&nbsp;</span>
			</td>
		</tr>
		<tr>
			<td>Symbol</td>
			<td>
				<input type="text" class="colorinput" name="hlcolor[symbol]" maxlength="7" onblur="ecAdmin.colorHandler(this);" value="<?php echo $option['hlcolor']['symbol']; ?>" />
				<span class="colorshow">&nbsp;</span>
			</td>
		</tr>
		<tr>
			<td>Char</td>
			<td>
				<input type="text" class="colorinput" name="hlcolor[char]" maxlength="7" onblur="ecAdmin.colorHandler(this);" value="<?php echo $option['hlcolor']['char']; ?>" />
				<span class="colorshow">&nbsp;</span>
			</td>
		</tr>
	</table>

	<h3><?php _e('More Appearance Options','easycode'); ?></h3>
	<table class="form-table">
		<tr valign="top">
			<th><label><?php _e('Wrap control','easycode'); ?>&nbsp;</label></th>
			<td>
			<label>
			<input type="checkbox" name="nowrap" value="1" <?php echo $option['nowrap']?'checked="checked"':''; ?>>
			<?php _e('Disable auto-wrap and word-break.','easycode'); ?>
			</label>
			</td>
		</tr>
		<tr valign="top">
			<th><label><?php _e('Font','easycode'); ?>&nbsp;</label></th>
			<td>
				<select name="font_style" id="font_style">
				<?php
				if(!in_array($option['font_style'],array_keys($font_list),TRUE)) $option['font_style']='consolas';
				foreach($font_list as $short_name=>$data){
					$che = ($option['font_style']===$short_name)?' selected':'';
					echo "<option value=\"$short_name\" $che>$data[0]</option>";
				}?>
				</select>
				<br>
				<label><input type="checkbox" name="allow-download-font" value="1" <?php echo $option['allow-download-font']?'checked':''; ?>>
				<?php _e('Allow broswers to download missing fonts','easycode'); ?></label>
			</td>
		</tr>
	</table>
</div>

<div id="op-highlight">

	<h3>Highlighter</h3>
	<p style="line-height:1.6;">
	<label><input type="radio" name="highlighter" value="0" <?php echo $option['highlighter']==0?'checked':''; ?>><?php _e('Disabled','easycode'); ?></label><br>
	<label><input type="radio" name="highlighter" value="1" <?php echo $option['highlighter']==1?'checked':''; ?>>SyntaxHighlighter&nbsp;</label><small><a href="http://alexgorbatchev.com/SyntaxHighlighter">Link&raquo;</a></small><br>
	<label><input type="radio" name="highlighter" value="2" <?php echo $option['highlighter']==2?'checked':''; ?>>GeSHi</label>&nbsp;<small><a href="http://qbnz.com/highlighter/">Link&raquo;</a></small><br>
	<label style="display:none"><input type="radio" name="highlighter" value="3" <?php echo $option['highlighter']==3?'checked':''; ?>>Old JS Lib <small> | Highlighter of version 1.3 and 1.4</small></label>
	</p>
	<p>
	<?php _e('If SyntaxHighlighter selected, code will be highlighted by client browsers. GeSHi will process code with servers. Enable the former to ease the strain on the servers or enable the latter to provide better performance and smoother experience for visitors.','easycode'); ?>
	</p>

	<h3>Languages enabled</h3>
	<p>
	<em><?php echo count($llist);?> languages avaliable.</em><br>
	<?php _e('Please disable unused language to avoid loading extra scripts.','easycode');?>
	</p>

	<ul class="lang-list">
	<?php
	foreach($llist as $lname){
		$checked = in_array($lname,(array)$option['lang_enable'])?'checked':'';
		$displayed_name = $this->get_language_map($lname,'name');
		echo "<li><label><input type=\"checkbox\" name=\"lang_enable[]\" value='$lname' id=\"\" $checked /><span>$displayed_name</span></label></li>";
	}?>
	</ul>

</div>

<div id="op-advanced">
  <h4><?php _e('Reset ALL plugin options','easycode');?></h4>
  <p><input type="checkbox" name="resetall" value="true" class="button" onclick="this.checked=false" ondblclick="this.checked=true"><?php _e('Double click to check','easycode'); ?></p>
  <div class="warning-text">
  <em><?php _e('<strong>WARNING ::</strong> ALL plugin options will be erased if the checkbox selected ! ','easycode'); ?></em>
  </div>
  </p>
</div>

<p class="submit"><input name="submit" class="button-primary button" id="submit" type="submit" value="<?php _e('Save Changes'); ?>"/></p>

</form>

<p class="info-bottom"><?php printf(__('Thank you for using Easy Code! ( Current version: %s ) ','easycode'),$this->version); ?> &raquo;<?php printf('<a href="%s" target="_blank">%s</a>',$this->about_url,__('Page on wordpress.org')); ?></p>

<script>ecAdmin.changeTab('general');</script>
</div>
