<?php
if(isset($_GET['page']) && $_GET['page']=='theme_fontface'){
	if (! function_exists("theme_fontface_getfonts")){
		function theme_fontface_getfonts(){
			$fonts = array();
			$dirs = array_filter(glob(THEME_FONTFACE_DIR.'/*'), 'is_dir');
			
			foreach($dirs as $dir){
				$stylesheet = $dir.'/stylesheet.css';
				if(file_exists($stylesheet)){
					$file_content = file_get_contents($stylesheet);
					if( preg_match_all("/@font-face\s*{.*?font-family\s*:\s*('|\")(.*?)\\1.*?}/is", $file_content, $matchs) ){
						foreach($matchs[0] as $index => $css){
							$font_folder = basename($dir);
							$fonts[$font_folder.'|'.$matchs[2][$index]] = array(
								'folder' => $font_folder,
								'name' => $matchs[2][$index],
								'css' => $css,
							);
						}
						
					}
				}
			}
			return $fonts;
		}
	}
	
	if (! function_exists("theme_fontface_add_head")){
		function theme_fontface_add_head(){
			$fonts = theme_fontface_getfonts();
			
			$count = 1;
			$style = "<style type='text/css'>\n";
			foreach($fonts as $value => $font){
				wp_enqueue_style('font|'.$value , THEME_FONTFACE_URI . '/'.$font['folder'].'/stylesheet.css');
				$style .= "#preview-".$count."{\n\tfont-family:'".$font['name']."';\n}\n";
				$count ++;
			}
			
			$style .= "</style>";
			echo $style;
			
			wp_enqueue_script( 'ZeroClipboard', THEME_ADMIN_ASSETS_URI .'/js/ZeroClipboard.js');
			do_action('admin_print_scripts');
			do_action('admin_print_styles');
			$scripts = "<script type='text/javascript'>\njQuery(document).ready(function($) {\n";
			$scripts .= "ZeroClipboard.setMoviePath('".THEME_ADMIN_ASSETS_URI."/js/ZeroClipboard.swf');\njQuery('.fontface_font_name').each(function(){\nvar clip = new ZeroClipboard.Client();\nclip.setText(jQuery(this).text());\nclip.glue(this,jQuery(this).parent('.font_name_wrap')[0]);\n});";
			echo $scripts."});\n</script>";
		}
		add_filter('admin_head', 'theme_fontface_add_head');
	}
}
if (! function_exists("theme_fontface_fonts_options")) {
	function theme_fontface_fonts_options($value, $default){
		$font = array();
		$lists = get_font_list();
		if($default&&is_array($default)){
			$fonts = $default;
		}else{
			$fonts = array("file"=>"","include"=>array(),"exclude"=>implode(',',array_keys($lists))); 
		}
		/*$fonts = array("file"=>"","include"=>array(),"exclude"=>implode(',',array_keys($lists))); */
		/*$font_items = theme_cufon_get_fonts();*/
		$font_items = theme_get_option('fontface','loaded');
		echo '<tr><th scope="row"><h4>Loaded Font</h4></th><td>';
		echo '<select id="font_item">';
		if(!empty($font_items)){
			foreach($font_items as $font_name => $file_folder){
				if(!file_exists(THEME_FONTFACE_DIR.'/'.$file_folder.'/stylesheet.css')){
					unset($font_items[$font_name]);
				}
				else echo '<option id="'.$file_folder.' "value="'.$font_name.'">'.$font_name.'</option>';
			}
		}
		echo '</select></td></tr>';
		if(empty($font_items)){
			echo '<tr><td colspan="2"><b>There is no fontface style to load.</b></td></tr>';
		}else{
			if(!empty($fonts['include'])){
				foreach($fonts['include'] as $font_name => $has_area){
					if(!array_key_exists($font_name,$font_items)){
						$fonts['exclude'] = implode(',',array($fonts['exclude'],$fonts['include'][$font_name]));
						unset($fonts['include'][$font_name]);
					}
				}
			}
			echo '<tr><th scope="row">Control</th><td><span id="includeArea" class="font_span"><h3>Use this font</h3>';
			foreach ($font_items as $font_name => $file_folder){
				echo '<select id="'.$font_name.'" style="display:none" multiple="multiple" class="font_multi include" size=10>';
				if(array_key_exists($font_name,$fonts['include'])&&!empty($fonts['include'][$font_name])){
					$include = explode(',',$fonts['include'][$font_name]);
					foreach ($include as $item) {
						if(array_key_exists($item,$lists) && !empty($lists[$item]))
							echo '<option value="'.$item.'"/>'.$lists[$item].'</option>';
						$lists[$item] = '';
					}
				}else{
					$fonts['include'][$font_name] = '';
				}
				echo '</select>';
				/*echo '<input id="fontInclude_'.$font_name.'" name="fonts[include]['.$font_name.']" type="hidden" value=""/>';*/
				echo '<input id="fontInclude_'.$font_name.'" name="fonts[include]['.$font_name.']" type="hidden" value="'.$fonts['include'][$font_name].'"/>';
			}
			echo '</span><span id="font-control" class="font_span">';
			echo '<input type="button" value="<<" id="addButton"/>';
			echo '<input type="button" value=">>" id="removeButton"/></span>';
			echo '<input id="fontExclude" name="fonts[exclude]" type="hidden" value="'.$fonts['exclude'].'"/>';
			/*echo '<input id="fontFile" name="fonts[file]" type="hidden" value="'.$fonts['file'].'"/>';*/
			/*echo '<input id="fontExclude" name="fonts[exclude]" type="hidden" value="h1,h2,h3,h4,h5,h6"/>';
			echo '<input id="fontFile" name="fonts[file]" type="hidden" value=""/>';*/
			echo '<span id="excludeArea" class="font_span"><h3>Field</h3>';
			echo '<select id="exclude" multiple="multiple" class="font_multi" size=10>';
			if(!empty($fonts['exclude'])){
				$exclude = explode(",",$fonts['exclude']);
				foreach($exclude as $value){
					if(!empty($value)){
						if(array_key_exists($value,$lists) && !empty($lists[$value]))
							echo '<option value="'.$value.'">'.$lists[$value].'</option>';
						$lists[$value] = '';
					}
				}
			}
			foreach($lists as $key => $value){
				if(!empty($value))
					echo '<option value="'.$key.'">'.$value.'</option>';
			}
			echo '</select>';
			echo '</span>';
			echo '</td></tr>';
		}
	}
}
if (! function_exists("theme_fontface_fonts_load")) {
	function theme_fontface_fonts_load($value, $default) {
		echo '<tr colspan="2"><td style="padding:0"><table class="widefat cufon_table" cellspacing="0"><tbody>';
		$count = 1;
		foreach(theme_fontface_getfonts() as $font_id => $font){
			if(is_array($default)){
				$checked = array_key_exists($font['name'],$default)?' checked="checked"':'';
			}else{
				$checked = '';
			}
			echo '<tr>
				<td style="width:15%">
					<div class="font_name_wrap" style="position: relative;">
						<a class="cufon_font_name" href="#" title="">'.$font['name'].'</a>
					</div>
				</td>
				<td style="width:10%">
					<input type="checkbox" name="loaded['.$font['name'].']" class="toggle-button" value="'.$font['folder'].'"'.$checked.'/>
				</td>
				<td>
					<span class="cufon_preview" id="preview-'.$count.'">This is a preview of the <span style="color:  #379BFF;">'.$font['name'].'</span> font. Some numbers: 0123456789 &amp; so on..</span>
				</td>
			</tr>';
			$count ++;
		}
		echo '</tbody></table></td></tr>';
	}
}


$options = array(
	array(
		"name" => __("@font-face",THEME_ADMIN_DOMAIN),
		"type" => "title"
	),
	array(
		"name" => __("@font-face General",THEME_ADMIN_DOMAIN),
		"type" => "start"
	),
		array(
			"name" => __("@font-face Custom CSS",THEME_ADMIN_DOMAIN),
			"desc" => __('sample:<p><code>h1,h2,h3,h4,h5 { font-family:ColaborateLightRegular; }</code></p>',THEME_ADMIN_DOMAIN),
			"id" => "code",
			"default" => '',
			"rows" => '8',
			"type" => "textarea"
		),
	array(
		"type" => "end"
	),
	 array(
		"name" =>__('Set FontFace',THEME_ADMIN_DOMAIN),
		"type" => "start"
	),
	array(
		"id" => "fonts",
		"layout" => false,
		"function" => "theme_fontface_fonts_options",
		"default"=>false,
		/*"default" =>array("file"=>"","include"=>array(),"exclude"=>implode(',',array_keys(get_cufon_list()))), */
		"type" => "custom"
	),
	array(
		"type" => "end"
	),
	array(
		"name" => sprintf(__('Fonts located in "%s"',THEME_ADMIN_DOMAIN),'<code>'.str_replace( WP_CONTENT_DIR, '', THEME_ADMIN_DOMAIN ).'</code>'),
		"type" => "start"
	),
		array(
			"id" => "loaded",
			"layout" => false,
			"function" => "theme_fontface_fonts_load",
			"default" => '',
			"type" => "custom"
		),
	array(
		"type" => "end"
	),
);
return array(
	'auto' => true,
	'name' => 'fontface',
	'options' => $options
);
