<?php
require_once(THEME_ADMIN_FUNCTIONS .'/font-list.php');
if (! function_exists("theme_cufon_get_fonts")){
	function theme_cufon_get_fonts(){
		$fonts = array();
		foreach(glob(THEME_FONT_DIR."/*.js") as $font_file){
			$file_content = file_get_contents($font_file);
			if(preg_match('/font-family":"(.*?)"/i',$file_content,$match)){
				$fonts[$match[1]] = basename($font_file);
			}
		}
		return $fonts;
	}
}
if(isset($_GET['page']) && $_GET['page']=='theme_cufon'){
	if (! function_exists("theme_cufon_add_script_option")){
		function theme_cufon_add_script_option(){
			wp_enqueue_script( 'cufon-yui', THEME_JS .'/cufon-yui.js');
			wp_enqueue_script( 'ZeroClipboard', THEME_ADMIN_ASSETS_URI .'/js/ZeroClipboard.js');
			$cufon_scripts = "<script type='text/javascript'>\njQuery(document).ready(function($) {\n";
			$count = 1;
			$cufon_scripts .= 'jQuery("#font_item").change(function(){var select=jQuery("#font_item").val();jQuery("#fontTypeName").html(select); var cufonStr="{ fontFamily:\'"+jQuery("#font_item").val()+"\'}";var cufonParam=eval("("+cufonStr+")");Cufon("#example",cufonParam);});';
			foreach(theme_cufon_get_fonts() as $font_name => $file_name){
				wp_enqueue_script( $font_name, THEME_FONT_URI .'/'.$file_name);
				$cufon_scripts .= stripslashes("Cufon('#preview-$count', { fontFamily: '$font_name' });\n");
				$count ++;
			}
			do_action('admin_print_scripts');
			$cufon_scripts .= "ZeroClipboard.setMoviePath('".THEME_ADMIN_ASSETS_URI."/js/ZeroClipboard.swf');\njQuery('.cufon_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 $cufon_scripts."});\n</script>";
		}
		add_filter('admin_head', 'theme_cufon_add_script_option');
	}
	
}
if (! function_exists("theme_cufon_fonts_load")) {
	function theme_cufon_fonts_load($value, $default) {
		echo '<tr colspan="2"><td style="padding:0"><table class="widefat cufon_table" cellspacing="0"><tbody>';
		$count = 1;
		foreach(theme_cufon_get_fonts() as $font_name => $file_name){
			if(is_array($default)){
				$checked = in_array($file_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="'.$file_name.'">'.$font_name.'</a>
					</div>
				</td>
				<td style="width:10%">
					<input type="checkbox" name="loaded['.$font_name.']" class="toggle-button" value="'.$file_name.'"'.$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>';
	}
}

if (! function_exists("theme_cufon_fonts_options")) {
	function theme_cufon_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('cufon','loaded');
		echo '<tr><th scope="row"><h4>Used Font</h4></th><td>';
		echo '<select id="font_item">';
		if(!empty($font_items)){
			foreach($font_items as $font_name => $file_name){
				if(!file_exists(THEME_FONT_DIR.'/'.$file_name)){
					unset($font_items[$font_name]);
				}
				else echo '<option id="'.$file_name.' "value="'.$font_name.'">'.$font_name.'</option>';
			}
		}
		echo '</select></td></tr>';
		if(empty($font_items)){
			echo '<tr><td colspan="2"><b>'.__('There is no cufon library to load',THEME_ADMIN_DOMAIN).'</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">Example</th><td><span class="cufon_preview" id="example">abcdefjhigklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890</span></td></tr>';	*/
			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_name){
				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>';
		}
	}
}
$options=array(
	array(
		"name" => __("Cufón",THEME_ADMIN_DOMAIN),
		"type" => "title"
	),
	array(
		"name" => __("Cufón General",THEME_ADMIN_DOMAIN),
		"type" => "start"
	),
	array(
		"name" => __("Cufón Code",THEME_ADMIN_DOMAIN),
		"desc" => __('sample:<p><code>Cufon.replace("h1,h2,h3,h4,h5", {fontFamily : "Vegur"});</code></p><p><code>Cufon.replace("#site_name", {fontFamily : "Vegur", color: \'-linear-gradient(white, black)\'});</code></p><p>For more code tips go to official <a href="http://wiki.github.com/sorccu/cufon/styling">Cufon\'s site</a>',THEME_ADMIN_DOMAIN),
		"id" => "code",
		"default" => '',
		"rows" => '8',
		"type" => "textarea"
	),
	array(
		"type" => "end"
	),
	 array(
		"name" =>__('Set Cufón',THEME_ADMIN_DOMAIN),
		"type" => "start"
	),
	array(
		"id" => "fonts",
		"layout" => false,
		"function" => "theme_cufon_fonts_options",
		"default" => false,
		/*"default" =>array("file"=>"","include"=>array(),"exclude"=>implode(',',array_keys(get_font_list()))), */
		"type" => "custom"
	),
	array(
		"type" => "end"
	),
	array(
		"name" => sprintf(__('Fonts located in "%s"',THEME_ADMIN_DOMAIN),'<code>'.str_replace( WP_CONTENT_DIR, '', THEME_FONT_DIR ).'</code>'),
		"type" => "start"
	),
	array(
		"id" => "loaded",
		"layout" => false,
		"function" => "theme_cufon_fonts_load",
		"default" => array('Segan'=>'Segan_300.font.js'),
		"type" => "custom"
	),
	array(
		"type" => "end"
	),
);
return array(
	'auto' => true,
	'name' => 'cufon',
	'options' => $options
);
