<?php

function option_image_upload_tabs ($tabs) {
	$tabs['type_url'] = 'From Source';
    return $tabs;
}

function option_image_form_url($form_action_url, $type){
	$form_action_url = $form_action_url.'&option_image_upload=1&target='.$_GET['target'];
	return $form_action_url;
}

function option_image_attachment_fields_to_edit($form_fields, $post){

	unset($form_fields['align']);
	unset($form_fields['image-size']);
	$filename = basename( $post->guid );
	$attachment_id = $post->ID;
	if ( current_user_can( 'delete_post', $attachment_id ) ) {
		if ( !EMPTY_TRASH_DAYS ) {
			$delete = "<a href='" . wp_nonce_url( "post.php?action=delete&amp;post=$attachment_id", 'delete-attachment_' . $attachment_id ) . "' id='del[$attachment_id]' class='delete'>" . __( 'Delete Permanently' , THEME_ADMIN_DOMAIN ) . '</a>';
		} elseif ( !MEDIA_TRASH ) {
			$delete = "<a href='#' class='del-link' onclick=\"document.getElementById('del_attachment_$attachment_id').style.display='block';return false;\">" . __( 'Delete' , THEME_ADMIN_DOMAIN ) . "</a>
			 <div id='del_attachment_$attachment_id' class='del-attachment' style='display:none;'>" . sprintf( __( 'You are about to delete <strong>%s</strong>.' , THEME_ADMIN_DOMAIN ), $filename ) . "
			 <a href='" . wp_nonce_url( "post.php?action=delete&amp;post=$attachment_id", 'delete-attachment_' . $attachment_id ) . "' id='del[$attachment_id]' class='button'>" . __( 'Continue' , THEME_ADMIN_DOMAIN ) . "</a>
			 <a href='#' class='button' onclick=\"this.parentNode.style.display='none';return false;\">" . __( 'Cancel' , THEME_ADMIN_DOMAIN ) . "</a>
			 </div>";
		} else {
			$delete = "<a href='" . wp_nonce_url( "post.php?action=trash&amp;post=$attachment_id", 'trash-attachment_' . $attachment_id ) . "' id='del[$attachment_id]' class='delete'>" . __( 'Move to Trash' , THEME_ADMIN_DOMAIN ) . "</a>
			<a href='" . wp_nonce_url( "post.php?action=untrash&amp;post=$attachment_id", 'untrash-attachment_' . $attachment_id ) . "' id='undo[$attachment_id]' class='undo hidden'>" . __( 'Undo' , THEME_ADMIN_DOMAIN ) . "</a>";
		}
	} else {
		$delete = '';
	}
	$mime  =  explode('/',$post->post_mime_type);
	$type  =  '';
	if($mime[0]=="image")
		$type="image";
	if($mime[1]=="x-shockwave-flash")
		$type="video";
	$form_fields['buttons'] = array( 
		'tr' => "\t\t<tr><td></td><td><input type='button' class='button' onclick='mediaUploader.OptionUploaderFromForm(".$post->ID.",\"".$post->post_title."\",\"".$type."\",\"". $_REQUEST['target']."\")' value='" . __( 'Use this' , THEME_ADMIN_DOMAIN ) . "' /> $delete</td></tr>\n"
	);
	return $form_fields;
}
function type_url_form_option(){
	if(isset($_GET['target']) && $_GET['target']=='_media_params'){
		return '
			<p class="symplex-media-type">
			<label><input type="radio" checked="checked" id="imageLabel" value="image" name="symplex_media_type" class="symplex-media-value"> Image</label> &nbsp; &nbsp; 
			<label><input type="radio" id="videoLabel" value="video" name="symplex_media_type"  class="symplex-media-value"> Html5 Video</label>&nbsp; &nbsp; 
			<label><input type="radio" id="youtubeLabel" value="youtube" name="symplex_media_type" class="symplex-media-value"> Youtube</label>&nbsp; &nbsp; 
			<label><input type="radio" id="vimeoLabel" value="vimeo" name="symplex_media_type" class="symplex-media-value"> Vimeo</label>&nbsp; &nbsp; 
			<label><input type="radio" id="flashLabel" value="flash" name="symplex_media_type" class="symplex-media-value"> SWF Flash</label>&nbsp; &nbsp; 
			</p>
				<input type="hidden" id="mediaType" value="image"/>
				<input type="hidden" id="noImage" value="'.esc_url( admin_url( 'images/no.png' )).'"/>
				<input type="hidden" id="yesImage" value="'.esc_url( admin_url( 'images/yes.png' )).'"/>
				<input type="hidden" id="loadImage" value="'.esc_url( admin_url( 'images/wpspin_light.gif' )).'"/>
				<input type="hidden" id="target" value="'. $_REQUEST['target'].'"/>
			<table class="describe"><tbody>
			<tr class="image">
			<th valign="top" scope="row" class="label">
				<span class="alignleft"><label for="image">' . __('Image URL',THEME_ADMIN_DOMAIN). '</label></span>
				<span class="alignright"><abbr id="status_img" title="required" class="required">*</abbr></span>
			</th>
			<td class="field"><input id="image" name="image" value="" type="text" aria-required="true" class="mediaText"></td>
			</tr>
			<tr class="flash">
			<th valign="top" scope="row" class="label">
				<span class="alignleft"><label for="flash">' . __('Flash URL',THEME_ADMIN_DOMAIN). '</label></span>
				<span class="alignright"><abbr id="status_swf" title="required" class="required">*</abbr></span>
			</th>
			<td class="field"><input id="flash" name="flash" value="" type="text" aria-required="true" class="mediaText"></td>
			</tr>
			<tr class="video">
			<th valign="top" scope="row" class="label">
				<span class="alignleft"><label for="murl">' . __('MP4 Source',THEME_ADMIN_DOMAIN). '</label></span>
				<span class="alignright"><abbr id="status_mp4" title="required" class="required">*</abbr></span>
			</th>
			<td class="field"><input id="murl" name="murl" value="" type="text" aria-required="true"  class="mediaText"/></td>
			</tr>
			<tr class="video">
			<th valign="top" scope="row" class="label">
				<span class="alignleft"><label for="ourl">' . __('Ogg Source',THEME_ADMIN_DOMAIN). '</label></span>
				<span class="alignright"><abbr id="status_ogg" title="required" class="required">*</abbr></span>
			</th>
			<td class="field"><input id="ourl" name="ourl" value="" type="text" aria-required="true"  class="mediaText"/></td>
			</tr>
			<tr class="video">
			<th valign="top" scope="row" class="label">
				<span class="alignleft"><label for="wurl">' . __('WebM Source',THEME_ADMIN_DOMAIN). '</label></span>
				<span class="alignright"><abbr id="status_webm" title="required" class="required">*</abbr></span>
			</th>
			<td class="field"><input id="wurl" name="wurl" value="" type="text" aria-required="true"  class="mediaText"/></td>
			</tr>
			<tr class="video">
			<th valign="top" scope="row" class="label">
				<span class="alignleft"><label for="furl">' . __('Flv Source',THEME_ADMIN_DOMAIN). '</label></span>
				<span class="alignright"><abbr id="status_flv" title="required" class="required">*</abbr></span>
			</th>
			<td class="field"><input id="furl" name="furl" value="" type="text" aria-required="true"  class="mediaText"/></td>
			</tr>
			<tr class="video">
			<th valign="top" scope="row" class="label">
				<span class="alignleft"><label for="vurl">' . __('Wmv Source',THEME_ADMIN_DOMAIN). '</label></span>
				<span class="alignright"><abbr id="status_wmv" title="required" class="required">*</abbr></span>
			</th>
			<td class="field"><input id="vurl" name="vurl" value="" type="text" aria-required="true"  class="mediaText"/></td>
			</tr>
			<tr class="youtube vimeo">
			<th valign="top" scope="row" class="label">
				<span class="alignleft"><label for="videoId">' . __('Video ID',THEME_ADMIN_DOMAIN) . '</label></span>
				<span class="alignright"><abbr id="status_videoid" title="required" class="required">*</abbr></span>
			</th>
			<td class="field"><input id="videoId" name="videoId" value="" type="text" aria-required="true"  class="mediaText"/></td>
			</tr>
			<tr class="all">
			<th valign="top" scope="row" class="label">
				<span class="alignleft"><label for="title">' . __('Title',THEME_ADMIN_DOMAIN) . '</label></span>
				<span class="alignright"><abbr id="status_title" title="required" class="required">*</abbr></span>
			</th>
			<td class="field"><input id="title" name="title" value="" type="text" aria-required="true"  class="mediaText"/></td>
			</tr>
			<tr class="all">
			<td></td>
			<td>
			<input type="button" class="button" id="go_button" value="' . esc_attr__('Use this',THEME_ADMIN_DOMAIN) . '" />
			</td>
			</tr>
			</tbody></table>';
	}
	
	return '
		<input type="hidden" id="mediaType" value="image"/>
		<input type="hidden" id="noImage" value="'.esc_url( admin_url( 'images/no.png' )).'"/>
		<input type="hidden" id="yesImage" value="'.esc_url( admin_url( 'images/yes.png' )).'"/>
		<input type="hidden" id="loadImage" value="'.esc_url( admin_url( 'images/wpspin_light.gif' )).'"/>
		<input type="hidden" id="target" value="'. $_REQUEST['target'].'"/>
		<table class="describe"><tbody>
		<tr>
			<th valign="top" scope="row" class="label">
				<span class="alignleft"><label for="image">' . __('URL',THEME_ADMIN_DOMAIN). '</label></span>
				<span class="alignright"><abbr id="status_img" title="required" class="required">*</abbr></span>
			</th>
			<td class="field"><input id="image" name="image" value="" type="text" aria-required="true" class="mediaText"></td>
		</tr>
		<tr>
			<th valign="top" scope="row" class="label">
				<span class="alignleft"><label for="title">' . __('Title',THEME_ADMIN_DOMAIN) . '</label></span>
				<span class="alignright"><abbr id="status_title" title="required" class="required">*</abbr></span>
			</th> 
			<td class="field"><input id="title" name="title" value="" type="text" aria-required="true" class="mediaText"></td>
		</tr>
		<tr>
			<td></td>
			<td>
			<input type="button" class="button" id="go_button"  value="' . esc_attr__('Use this',THEME_ADMIN_DOMAIN) . '" />
			</td>
		</tr>
	</tbody></table>';
}
function option_image_swfupload_post_params($params){
	$params['option_image_upload']=1;
	$params['target']=$_REQUEST['target'];
	return $params;
}
function option_image_swfupload_success_handler($handler){
	return 'optionImageUploadSuccess';
}
function option_image_upload_init(){
	add_filter('media_upload_tabs', 'option_image_upload_tabs');
	add_filter('attachment_fields_to_edit', 'option_image_attachment_fields_to_edit', 10, 2);
	add_filter('media_upload_form_url', 'option_image_form_url', 10, 2);
	add_filter('type_url_form_file', 'type_url_form_option', 10, 2);
	add_filter('type_url_form_media', 'type_url_form_option', 10, 2); /* wordpree > 3.3 */
	wp_enqueue_script('theme-mediaUploader', THEME_ADMIN_ASSETS_URI . '/js/mediaUploader.js');
	add_filter('swfupload_post_params', 'option_image_swfupload_post_params');
	add_filter('swfupload_success_handler','option_image_swfupload_success_handler');
	add_filter('upload_post_params', 'option_image_upload_post_params');
}
function option_image_upload_post_params($params){
	  $params['option_image_upload']=1;
	  $params['target']=$_REQUEST['target'];
 	  unset($params['short']);
 	  return $params;
}
if (isset($_GET['option_image_upload']) || isset($_POST['option_image_upload'])) {
	add_action('admin_init', 'option_image_upload_init');
}

//option insert image ajax action callback
function option_get_media_action_callback() {
	theme_print_media($_POST['media'],$_POST['title'],$_POST['type'],$_POST['method'],$_POST['imagewidth'],$_POST['imageheight']);
	die();
	/*$original = wp_get_attachment_image_src($_POST['id'],'full');
	if (! empty($original)) {
		echo $original[0];
	} else {
		die(0);
	}
	die();*/
}
add_action('wp_ajax_theme-option-get-media', 'option_get_media_action_callback');



function theme_print_media($media,$title,$type,$method,$imagewidth,$imageheight){
	if(!empty($media)){
		switch($type){
		/*case 'audio':
			echo "<div>";
			echo '<img src="<?php echo esc_url( admin_url( \'images/media-button-music.gif?ver=20100531\' ));?>"/>';
			echo '<strong style="font-size:15px;color:#2784B0"><?php echo $title;?></strong>';
			echo '<br />
			<br />
			</div>';
			break;*/
		case 'youtube':
		case 'vimeo':
		case 'flash':
		case 'video':
?>
			<div>
				<img src="<?php echo esc_url( admin_url( 'images/media-button-video.gif?ver=20100531' ));?>"/>
				<strong style="font-size:15px;color:#2784B0"><?php echo $title;?></strong>
				<br />
				<br />
			</div>
<?php
			break;
		case 'image':
			echo '<div>'; 
			if(empty($imagewidth))
				$imagewidth = '600';
			if(empty($imageheight))
				$imageheight = '350';
			if($method=='form'){
				$src = wp_get_attachment_image_src($media,'full');
				$media= $src[0];
			}
			echo '<a class="thickbox" href="'.$media.'?"><img width="'.$imagewidth.'" height="'.$imageheight.'" src="'.$media.'"/></a>';
			break;
		default:
			echo __("<b style='color:red'>The type of media can not show</b>",THEME_ADMIN_DOMAIN);
		}
	}
}

