<?php

		// Modified from James Wilson's YouTube widget
		// http://ja.meswilson.com/blog/2007/05/31/wordpress-youtube-widget/


		function c12_youtube_video_widget($c12_v_ops) {

		extract($c12_v_ops);
		$before_title="<h2>";
		$after_title="</h2>";

		$c12_ytbe_v_ops = get_option('c12_youtube_video_widget');
		$c12_ytbe_v_id = $c12_ytbe_v_ops['videoid'];
		$c12_ytbe_widget_background = $c12_ytbe_v_ops['widgetbackground'];
		$c12_ytbe_v_obj_width = $c12_ytbe_v_ops['width'];
		$c12_ytbe_v_obj_height = $c12_ytbe_v_ops['height'];
		$c12_ytbe_v_widget_h2_title = ($c12_ytbe_v_ops['title'] != "") ? $before_title.$c12_ytbe_v_ops['title'].$after_title : "";
		$c12_ytbe_v_autop = ($c12_ytbe_v_ops['autoplay'] == '1') ? '&autoplay=1' : '';



		$c12_ytbe_widget = '
		<object width="%c12_ytbe_v_obj_width%" height="%c12_ytbe_v_obj_height%">
		<param name="movie" value="http://www.youtube.com/v/%c12_ytbe_v_id%%autoplay%"></param>
		<param name="wmode" value="transparent"></param>
		<embed src="http://www.youtube.com/v/%c12_ytbe_v_id%%autoplay%" type="application/x-shockwave-flash" wmode="transparent" width="%c12_ytbe_v_obj_width%" height="%c12_ytbe_v_obj_height%"></embed>
		</object>
		';

		$c12_ytbe_widget_output = str_replace(
			array(
				"%c12_ytbe_v_id%",
				"%c12_ytbe_widget_background%",
				"%c12_ytbe_v_obj_width%",
				"%c12_ytbe_v_obj_height%",
				"%autoplay%"
			),
			array(
				$c12_ytbe_v_id,
				$c12_ytbe_widget_background,
				$c12_ytbe_v_obj_width,
				$c12_ytbe_v_obj_height,
				$c12_ytbe_v_autop
			),
			$c12_ytbe_widget
		);

		echo "<div class=\"widget-hw\">";
		echo $c12_ytbe_v_widget_h2_title;
		echo "<div style=\"margin-top:10px;background:$c12_ytbe_widget_background;padding:5px;text-align:center;\">";
		echo $c12_ytbe_widget_output;
		echo "</div>";
		echo "</div>";

	}


	function c12_youtube_video_widget_control() {

		$c12_ytbe_v_ops = get_option('c12_youtube_video_widget');
		if ( !is_array($c12_ytbe_v_ops) )
			$c12_ytbe_v_ops = array('title'=>'',
				'video'=> 'http://www.youtube.com/watch?v=a0jZzBEKIMc',
				'widgetbackground'=> '#f2f2f2',
				'videoid' => 'a0jZzBEKIMc',
				'width' => '200',
				'height' => '165',
				'autoplay' => '0'
			);

		if ( $_POST['c12_ytbe_widget_submit'] ) {


			$c12_ytbe_v_ops['title'] = strip_tags(stripslashes($_POST['c12_ytbe_widget_title']));
			$c12_ytbe_v_ops['video'] = strip_tags(stripslashes($_POST['c12_ytbe_widget_video']));
			$c12_ytbe_v_ops['widgetbackground'] = strip_tags(stripslashes($_POST['c12_ytbe_widget_background']));
			$c12_ytbe_v_ops['width'] = strip_tags(stripslashes($_POST['c12_ytbe_widget_width']));
			$c12_ytbe_v_ops['height'] = strip_tags(stripslashes($_POST['c12_ytbe_widget_height']));
			$c12_ytbe_v_ops['autoplay'] = ($_POST['c12_ytbe_widget_autoplay'] == "on" ) ? "1" : "0";


				$c12_ytbe_v_id = str_ireplace(
				array(
					"http://www.youtube.com/watch?v=",
					"http:/youtube.com/watch?v=",
					"http://br.youtube.com/watch?v=",
					"http://uk.youtube.com/watch?v=",
					"http://fr.youtube.com/watch?v=",
					"http://ie.youtube.com/watch?v=",
					"http://it.youtube.com/watch?v=",
					"http://jp.youtube.com/watch?v=",
					"http://nl.youtube.com/watch?v=",
					"http://pl.youtube.com/watch?v=",
					"http://es.youtube.com/watch?v="
				),
				"",
				$c12_ytbe_v_ops['video']
			);

			$c12_ytbe_vid_usrc = strpos($c12_ytbe_v_id, "&");
			if($c12_ytbe_vid_usrc != FALSE AND $c12_ytbe_vid_usrc > 0)
				$c12_ytbe_v_id = substr($c12_ytbe_v_id, 0, $c12_ytbe_vid_usrc);

			$c12_ytbe_v_ops['videoid'] = strip_tags(stripslashes($c12_ytbe_v_id));

			update_option('c12_youtube_video_widget', $c12_ytbe_v_ops);
		}

		$c12_ytbe_v_widget_h2_title = htmlspecialchars($c12_ytbe_v_ops['title'], ENT_QUOTES);
		$c12_ytbe_the_video = htmlspecialchars($c12_ytbe_v_ops['video'], ENT_QUOTES);
		$c12_ytbe_widget_background = htmlspecialchars($c12_ytbe_v_ops['widgetbackground'], ENT_QUOTES);
		$c12_ytbe_v_obj_width = htmlspecialchars($c12_ytbe_v_ops['width'], ENT_QUOTES);
		$c12_ytbe_v_obj_height = htmlspecialchars($c12_ytbe_v_ops['height'], ENT_QUOTES);
		$c12_ytbe_v_autop = htmlspecialchars($c12_ytbe_v_ops['autoplay'], ENT_QUOTES);


		echo '<p style="text-align:right;"><label for="c12_ytbe_widget_title">' . __('Title:') . ' <input style="width: 200px;" id="c12_ytbe_widget_title" name="c12_ytbe_widget_title" type="text" value="'.$c12_ytbe_v_widget_h2_title.'" /></label></p>';
		echo '<p style="text-align:right;"><label for="c12_ytbe_widget_video">' . __('Video URL:', 'widgets') . ' <input style="width: 200px;" id="c12_ytbe_widget_video" name="c12_ytbe_widget_video" type="text" value="'.$c12_ytbe_the_video.'" /></label></p>';
		echo '<p style="text-align:right;"><label for="c12_ytbe_widget_background">' . __('Widget Background Color:', 'widgets') . ' <input style="width: 200px;" id="c12_ytbe_widget_background" name="c12_ytbe_widget_background" type="text" value="'.$c12_ytbe_widget_background.'" /></label></p>';
		echo '<p style="text-align:right;"><label for="c12_ytbe_widget_width">' . __('Width:', 'widgets') . ' <input style="width: 200px;" id="c12_ytbe_widget_width" name="c12_ytbe_widget_width" type="text" value="'.$c12_ytbe_v_obj_width.'" /></label></p>';
		echo '<p style="text-align:right;"><label for="c12_ytbe_widget_height">' . __('Height:', 'widgets') . ' <input style="width: 200px;" id="c12_ytbe_widget_height" name="c12_ytbe_widget_height" type="text" value="'.$c12_ytbe_v_obj_height.'" /></label></p>';
		echo '<p style="text-align:right;"><label for="c12_ytbe_widget_autoplay">' . __('Autoplay:', 'widgets') . ' <input id="c12_ytbe_widget_autoplay" name="c12_ytbe_widget_autoplay" type="checkbox" ' . (  ($c12_ytbe_v_autop == '1') ? "checked=\"checked\"" : "" ) . '</label></p>';
		echo '<input type="hidden" id="c12_ytbe_widget_submit" name="c12_ytbe_widget_submit" value="1" />';
	}


	?>
