var vs_cap, vs_url, vs_name;

String.prototype.trim = function() {return this.replace(/^\s+|\s+$/g,"");}

$(document).ready(function() {

  $("#vs_what").change(function() {

    vs_cap = this.options[this.selectedIndex].title;
    vs_url = this.options[this.selectedIndex].value;

    if(vs_cap == "")
    {
      $("#vs_what_title").hide();
      $("#vs_what_text_td").hide();
    }
    else
    {
      $("#vs_what_title").html("Enter " + vs_cap);
      $("#vs_what_title").show();
      $("#vs_what_text_td").show();
      $("#vs_what_text").focus();
    }

  });

  $("#vs_what").keydown(function() { $(this).change(); });
  $("#fvs").submit(function() { $("#vs_stream").click(); return false; });

  $("#vs_stream").click(function() {

    if(vs_cap != "")
    {
      vs_name = $("#vs_what_text").attr("value");
      vs_name = (vs_name == undefined ? "" : vs_name.trim());
      $("#vs_what_text").attr("value", vs_name);
      if(vs_cap != "" && (vs_name == "" || vs_name == undefined))
        return alert("Please enter " + vs_cap);
      vs_name = "/" + vs_name;
    }
    else
    {
      vs_name = "";
    }

    vs_full = escape(vs_url + vs_name);
    $("#vs_steal_code").attr("value", getEmbedCode(vs_full, false)).select();
    $("#vs_steal").show();
    showChimeTV("vs_preview", vs_full);

  });

  $("#vs_what").change();

});

function getEmbedCode(url, playMode)
{
 return '<p align="center"><object width="400" height="340"><param name="movie" value="http://chime.tv/e/' + url + (playMode ? '_play' : '') + '"></param><param name="allowFullScreen" value="true"></param><param name="bgcolor" value="000000"></param><embed src="http://chime.tv/e/' + url + (playMode ? '_play' : '') + '" type="application/x-shockwave-flash" allowFullScreen="true" bgcolor="000000" width="400" height="340"></embed></object><br /><a href="http://chime.tv/' + url + '">View Playlist</a> | <a href="http://chir.ag/video-stream">Get your own TV!</a></p>';
}