var flashVars = '';
var sourceSwf = '';
var width = '';
var height = '';
var extraVars= 0;
var defaultValues = {
	initialBufferTime: '0.1',
	expandedBufferTime: '10',
	minContinuousPlaybackTime: '30'
}
var defaultWidth = 480;
var defaultHeight = 385;
var defaultSrc = 'http://osmf.org/configurator/videos/strobe.flv'; 

function changeSWFSource(sw) {
  if (sw == "true") {
    $("#source").val("http://fpdownload.adobe.com/strobe/FlashMediaPlayback_101.swf");
  } else {
    $("#source").val("http://fpdownload.adobe.com/strobe/FlashMediaPlayback.swf");
  }
}

$(window).load(function () {
  $("#source").val("http://fpdownload.adobe.com/strobe/FlashMediaPlayback.swf");   
  generateCode();
  defaultWidth = $("#width").val();
  defaultHeight = $("#height").val();
  defaultSrc = $("#fv_src").val();
});

function toggleDisplay(itm) {
	$("#"+itm).toggle();
}


function generateCode() {
	changeSWFSource($("input:radio[name=use101]:checked").val());
	getVars();
	embedPlayer();		
}

function embedPlayer() {
	var embedCode =  '<object width="' + width  + '" height="' + height +'"> ';
	embedCode += '<param name="movie" value="'+ sourceSwf + '"></param>';
	embedCode += '<param name="flashvars" value="'+ flashVars + '"></param>';
	embedCode += '<param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param>';
	embedCode += '<embed src="' + sourceSwf + '" type="application/x-shockwave-flash"';
	embedCode += ' allowscriptaccess="always" allowfullscreen="true" ';
	embedCode += ' width="' + width + '" height="'+ height +'" ';
	embedCode += 'flashvars="' + flashVars + '">';
	embedCode += '</embed></object>';

	$("#player").html(embedCode);
	$("#swfobjcode").text(embedCode);		
	$("#swfobjcode").selText();
	//printFlashVars();
}

function generatePreviewCode() {
	var embedCode =  '&lt;object width=&quot;' + width  + '&quot; height=&quot;' + height +'&quot;&gt; <br/>';
	embedCode += '&lt;param name=&quot;movie&quot; value=&quot;'+ sourceSwf + '&quot;&gt;&lt;/param&gt;<br/>';
	embedCode += '&lt;param name=&quot;flashvars&quot; value=&quot;'+ flashVars + '&quot;&gt;&lt;/param&gt;<br/>';
	embedCode += '&lt;param name=&quot;allowFullScreen&quot; value=&quot;true&quot;&gt;&lt;/param&gt;&lt;param name=&quot;allowscriptaccess&quot; value=&quot;always&quot;&gt;&lt;/param&gt;<br/>';
	embedCode += '&lt;embed src=&quot;' + sourceSwf + '&quot; type=&quot;application/x-shockwave-flash&quot;<br/>';
	embedCode += ' allowscriptaccess=&quot;always&quot; allowfullscreen=&quot;true&quot;<br/> ';
	embedCode += ' width=&quot;' + width + '&quot; height=&quot;'+ height +'&quot;<br/> ';
	embedCode += 'flashvars=&quot;' + flashVars + '&quot;&gt; <br/>';
	embedCode += '&lt;/embed&gt;&lt;/object&gt; <br/>';
}

function getVars() {
	var values = $("input[value!=],select[value!=]").filter('[id^=fv_]').
	filter(
		function(index)
		{
			return !defaultValues[this.name] || this.value != defaultValues[this.name];
		}
	);
	
    flashVars = values.serialize();
 
	sourceSwf = $("#source").val();
	width = $("#width").val();
	height = $("#height").val();
		
}

function printFlashVars() {
	var flashVarsArray = $("input[value!=],select[value!=]").filter('[id^=fv_]').serializeArray();
	var outputVars = '';
	outputVars += 'Source Swf: ' + sourceSwf + ' <br> ';
	outputVars += ' width: ' + width + ' height: '+ height +' <br> ';
	 jQuery.each(flashVarsArray, function(i, flashvar){
        outputVars += flashvar.name + ': ' + flashvar.value + ' <br> ';
      });
	 
	 $("#flashvarscode").html(outputVars);
	
}

function resetFields(){
  $("#source").val("http://osmf.org/configurator/StrobeMediaPlayback.swf");
  $(".default").attr("checked", "checked");
  $(".defclear").val("");
  $("#fv_streamType option[value='']").attr('selected', 'selected');
  $("#fv_scaleMode option[value='']").attr('selected', 'selected');
  $("#fv_initialBufferTime").val("0.1");
  $("#fv_expandedBufferTime").val("10");
  $("#width").val(defaultWidth);
  $("#height").val(defaultHeight);
  $("#fv_src").val(defaultSrc);
  //$("#fv_backgroundColor").color.fromString("FFFFFF");
  document.getElementById('fv_backgroundColor').color.fromString('FFFFFF');
  $('#fv_backgroundColor').val("");
  generateCode();
}


jQuery.fn.selText = function() {
    var obj = this[0];
    if ($.browser.msie) {
        var range = obj.offsetParent.createTextRange();
        range.moveToElementText(obj);
        range.select();
    } else if ($.browser.mozilla || $.browser.opera) {
        var selection = obj.ownerDocument.defaultView.getSelection();
        var range = obj.ownerDocument.createRange();
        range.selectNodeContents(obj);
        selection.removeAllRanges();
        selection.addRange(range);
    } else if ($.browser.safari) {
        var selection = obj.ownerDocument.defaultView.getSelection();
        selection.setBaseAndExtent(obj, 0, obj, 1);
    }
    return this;
}
