/**
 * Render the plugin settings menu
 */
function flowplayer_settings()
{
    echo flowplayer::admin_head();
    flowplayer::get_nonce();
    if (wp_verify_nonce($_POST['flowplayer_noncename'], FLOWPLAYER_NONCE_FILE)) {
        flowplayer::_setautoplay($_POST['flowplayer_autoplay']);
        flowplayer::_setkey($_POST['flowplayer_key']);
        flowplayer::_setautobuffer($_POST['flowplayer_autobuffer']);
        flowplayer::_setbackgroundColor($_POST['flowplayer_backgroundColor']);
        flowplayer::_setcanvas($_POST['flowplayer_canvas']);
        flowplayer::_setsliderColor($_POST['flowplayer_sliderColor']);
        flowplayer::_setbuttonColor($_POST['flowplayer_buttonColor']);
        flowplayer::_setbuttonOverColor($_POST['flowplayer_buttonOverColor']);
        flowplayer::_setdurationColor($_POST['flowplayer_durationColor']);
        flowplayer::_settimeColor($_POST['flowplayer_timeColor']);
        flowplayer::_setprogressColor($_POST['flowplayer_progressColor']);
        flowplayer::_setbufferColor($_POST['flowplayer_bufferColor']);
        flowplayer::_setlogo($_POST['flowplayer_logo']);
        flowplayer::_setlogolink($_POST['flowplayer_logolink']);
    }
    echo flowplayer::flowplayer_settings();
}
    function flowplayer_settings()
    {
        $html = '
					<script type="text/javascript">
	WPFP(document).ready(function() {
		//load player
		$f("player", "' . (flowplayer::_getkey() ? flowplayer::commercial_url() : flowplayer::gpl_url()) . '", {
				plugins: {
  					 controls: {    					
      					' . (flowplayer::_getbuttonOverColor() != '' ? 'buttonOverColor: \'' . flowplayer::_getbuttonOverColor() . '\',' : '') . '
      					' . (flowplayer::_getsliderColor() != '' ? 'sliderColor: \'' . flowplayer::_getsliderColor() . '\',' : '') . '
      					' . (flowplayer::_getbufferColor() != '' ? 'bufferColor: \'' . flowplayer::_getbufferColor() . '\',' : '') . '
      					sliderGradient: \'none\',
      					progressGradient: \'medium\',
      					' . (flowplayer::_getdurationColor() != '' ? 'durationColor: \'' . flowplayer::_getdurationColor() . '\',' : '') . '
      					' . (flowplayer::_getprogressColor() != '' ? 'progressColor: \'' . flowplayer::_getprogressColor() . '\',' : '') . '
      					' . (flowplayer::_getbackgroundColor() != '' ? 'backgroundColor: \'' . flowplayer::_getbackgroundColor() . '\',' : '') . '
      					' . (flowplayer::_gettimeColor() != '' ? 'timeColor: \'' . flowplayer::_gettimeColor() . '\',' : '') . '
      					' . (flowplayer::_getbuttonColor() != '' ? 'buttonColor: \'' . flowplayer::_getbuttonColor() . '\',' : '') . '
      					backgroundGradient: \'none\',
      					bufferGradient: \'none\',
   						opacity:1.0
   						}
				},
				clip: {
					url:\'http://saiweb.co.uk/wp-content/videos/wpfp_config_demo.mp4\',
					autoPlay: ' . (flowplayer::_getautoplay() == 'true' ? 'true' : 'false') . ',
       				autoBuffering: ' . (flowplayer::_getautobuffer() == 'true' ? 'true' : 'false') . '
				},
				' . (flowplayer::_getkey() ? 'key:\'' . flowplayer::_getkey() . '\',' : '');
        if (strlen(flowplayer::_getlogo()) > 0) {
            $html .= '
		logo: {  
        url: \'' . flowplayer::_getlogo() . '\',  
        displayTime: 0,
        fullscreenOnly: false,
        linkUrl: \'' . flowplayer::_getlogolink() . '\' 
    },';
        }
        $html .= '
				canvas: {
					backgroundColor:\'' . flowplayer::_getcanvas() . '\'
				},
				onLoad: function() {
					$(":input[name=tgt]").removeAttr("disabled");		
				},
				onUnload: function() {
					$(":input[name=tgt]").attr("disabled", true);		
				}
			});
		//colour picker call back to api and hidden vars		
		WPFP(\'#colourpicker\').farbtastic(function(color){
			var tgt = $(":input[name=tgt]:checked").val();
			tgtArr = tgt.split(\'_\');
			//set to hidden input
			$(":input[name="+tgt+"]").val(color);
			var player = $f("player");
				
			if (player.isLoaded()) {						

			// adjust canvas bgcolor. uses undocumented API call. not stabilized yet
			if (tgtArr[1] == \'canvas\') {					
				player._api().fp_css("canvas", {backgroundColor:color});
				
			// adjust controlbar coloring
			} else {
				window.canvasColor = color;
				player.getControls().css(tgtArr[1], color);
			}			
			
		} else {
			player.load();	
		}			
		});
	});
</script>
						<div class="wrap">
						<form id="wpfp_options" method="post">
						<div id="icon-options-general" class="icon32"><br></div>
						<h2><a href="http://www.saiweb.co.uk">Saiweb</a> Flowplayer for Wordpress</h2>
						';
        if (!flowplayer::_getkey()) {
            $html .= '
						<h3>Like this plugin?</h3>
							A lot of development time and effort went into Flowplayer and this plugin, you can help support further development by purchasing a comercial license for flowplayer.
						<h3><a href="http://Flowplayer.ojrq.net/c/12651/7417/510" target="_blank">Get a commercial license now!</a></h3>
						';
        }
        $html .= '<h3>Please set your default player options below</h3>
						<table>
							<tr>
								<td>AutoPlay: </td>
								<td>
		 							<select name="flowplayer_autoplay">
										' . flowplayer::bool_select(flowplayer::_getautoplay()) . '
		 							</select>
		 							
		 						</td>
							</tr>
							<tr>
								<td>Commercial License Key: </td>
								<td>
									<input type="text" size="20" name="flowplayer_key" id="flowplayer_key" value="' . flowplayer::_getkey() . '" />	
									(Required for certain features i.e. custom logo)
								</td>
							</tr>
							<tr>
								<td>Logo URL: </td>
								<td>
									<input type="text" size="20" name="flowplayer_logo" id="flowplayer_logo" value="' . flowplayer::_getlogo() . '" />	
									(only works with valid license key)
								</td>
							</tr>
							<tr>
								<td>Logo Link: </td>
								<td>
									<input type="text" size="20" name="flowplayer_logolink" id="flowplayer_logolink" value="' . flowplayer::_getlogolink() . '" />	
									(only works with valid license key)
								</td>
							</tr>
							<tr>
								<td>Auto Buffering:</td>
								<td><select name="flowplayer_autobuffer">' . flowplayer::bool_select(flowplayer::_getautobuffer()) . '</select></td>
							</tr>
							<tr>
								<td><div id="colourpicker"></div></td>
								<td>
									' . flowplayer::flowplayer_colours() . '
								</td>
							</tr>
							<tr>
								<td>&nbsp;</td>
								<td>
									<input type="submit" name="submit" class="button-primary" value="Save Changes" />
								</td>
							</tr>
						</table>
						<input type="hidden" name="flowplayer_noncename" id="flowplayer_noncename" value="' . flowplayer::get_nonce() . '" />
						</form>
						<div id="player" style="width:300px;height:200px;"></div>
					</div>


';
        return $html;
    }