
function qtmovie(path, id,w,h) {
    var code = '<embed src="' + path + '" pluginspage="http://www.apple.com/quicktime/download/" scale="tofit" kioskmode="false" ';
    code += 'qtsrc="' + path + '" cache="false" height="'+h+'" width="'+w+'" controller="true" type="video/quicktime" autoplay="true">\n';
    var d = $(id);
    d.innerHTML = code;
}

function update_channel_selection(chan) {
  //alert("update_channel_selection "+chan);
  update_channel_selection_in(chan,document.getElementById("channeltable"));
  update_channel_selection_in(chan,document.getElementById("mychanneltable"));
}

function update_channel_selection_in(chan,tbl) {
  if (tbl==null) {
    return
  }
  var id = 'row-'+chan;
  var body = tbl;
    //alert("node name: "+body.nodeName+" node type: "+body.nodeType+" #rows: "+body.rows.length);
  for (var i=0; i<body.rows.length; i++) {
    var row = body.rows[i];
    //alert("node name: "+row.nodeName+" node type: "+row.nodeType);
    //alert(row.id);
    if (row.id== id) {
      row.className = "selectedChannel";
      //alert("select "+row.id+" that matches "+id);
    } else {
      row.className = "";
      //alert("unselect "+row.id+" that does not match "+id);
    }
  }
}

function update_channel_visibility() {
var tbl = document.getElementById("channeltable");
  if (tbl==null) {
    return
  }
  var body = tbl;
    //alert("node name: "+body.nodeName+" node type: "+body.nodeType+" #rows: "+body.rows.length);
  for (var i=0; i<body.rows.length; i++) {
    var row = body.rows[i];
    //alert("node name: "+row.nodeName+" node type: "+row.nodeType);
    //alert(row.id);
    if (channel_counts[''+row.id]>0) {
      row.style.visibility = 'visible';
      //alert("select "+row.id+" that matches "+id);
    } else {
      if (show_all_channels) {
        row.style.visibility = 'visible';
        row.style.height = null;
      } else {
        row.style.visibility = 'hidden';
        row.style.height = 0;
      }
      //alert("unselect "+row.id+" that does not match "+id);
    }
  }
}

var ismychannel = false;

function update_channel_title(chan) {
  var id = 'ch-'+chan;
  var url = '/home/channel/'+chan;
  if (ismychannel) {
  	url = '/home/mychannel/'+chan;
  }
  //alert("update_channel_title "+id+" => "+url);
  new Ajax.Updater(id, url,{asynchronous:true,evalScripts:true});
  update_channel_selection(chan)
}

function update_channels(chan) {
  var url = '/home/channellist';
  //alert("update_channels "+chan);
  new Ajax.Updater("channellist", url,{asynchronous:true,evalScripts:true});
  //update_channel_selection(chan)
}

function update_video_selection() {
  //alert("update_video_selection "+cur_page+" "+cur_video);
  for (var i=0; i<document.images.length; i++) {
    var img = document.images[i];
    //alert("id "+img.id+" == "+id+"?");
      if (img.id=="v-"+cur_video) {
        //alert("set "+img.id+" to currenticon class");
        img.className = "currenticon";
      } else if (img.id.indexOf("v-")>=0){
        //alert("set "+img.id+" to icon class");
        img.className = "icon";
      }
  }
}

var cur_video = "";
var cur_page = "1";

function view_video(feedid,id,all) {
  var args = '?vid='+id;
  if (all!=null && all!="") {
    args = args+'&amp;all=1'
  }
  //alert(args);
  new Ajax.Updater('justplayer','/content/videopartial/'+feedid+args,{asynchronous:true,evalScripts:true});
  new Ajax.Updater('title','/content/title/'+feedid+args,{asynchronous:true,evalScripts:true});
  var notifier = document.getElementById("notify");
  notifier.innerHTML = "";
  cur_video = id;
  update_video_selection()
}

function ensure_page(feedid,page,all) {
  //alert("ensure_page cur "+cur_page+" new "+page);
  if (cur_page!=page) {
    var args = '?page='+page;
    if (all!=null && all!="") {
      args = args+'&amp;all=1'
    }
    //alert("all = "+all+" => args = "+args);
    cur_page = page;
    new Ajax.Updater('iconnav','/content/iconnav/'+feedid+args,{asynchronous:true,evalScripts:true});
    update_video_selection();
  }
}

function show_video_title(id) {
  var i = document.getElementById("v-"+id);
  var t = document.getElementById("vt-"+id);
  t.style.visibility = 'visible';
  //t.style.left = i.style.left;
  //t.style.bottom = ""+(parseInt(i.style.top)+20)+"px";
  //t.style.right = ""+(parseInt(t.style.left)+300)+"px";
  //t.style.top = ""+(parseInt(t.style.top)+30)+"px";
}

function hide_video_title(id) {
  var t = document.getElementById("vt-"+id);
  t.style.visibility = 'hidden';
}

function ok_video(feedid) {
  //alert("ok video: "+cur_video);
  var args = "?vid="+cur_video;
  new Ajax.Updater('notify','/content/thumbsup/'+feedid+args,{asynchronous:true,evalScripts:true});
}

function add_video(feedid) {
  //alert("ok video: "+cur_video);
  var args = "?vid="+cur_video;
  new Ajax.Updater('notify','/content/addvideotochannel/'+feedid+args,{asynchronous:true,evalScripts:true});
  //alert("done with add_video");
}

function send_video(id,all) {
  //alert("ok video: "+cur_video);
    var args = "?vid="+cur_video;
    if (all!=null && all!="") {
      args = args+'&amp;all=1'
    }
  document.location = "/send/video/"+id+args;
}

function TrimString(sInString) {
  sInString = sInString.replace( /^\s+/g, "" );// strip leading
  return sInString.replace( /\s+$/g, "" );// strip trailing
}

function add_to(email) {
  var t = document.getElementById("to");
  var cur = t.value;
  if (cur.length==0) {
    t.value = email+"\n";
  } else {
    var bynl = cur.split("\n");
    var nonl = bynl.join(",");
    var existing = nonl.split(",");
    var kept = new Array();
    for (var i=0; i<existing.length; i++) {
      var ts = TrimString(existing[i]);
      if (ts.length>0) {
        if (ts==email) {
          return;
        }
        kept.push(ts);
      }
    }
    kept.push(email);
    t.value = kept.join("\n");
  }
}

function select_video(id) {
  //alert("select video "+id);
  e = document.getElementById("v-"+id);
  if (e!=null) {
    if (e.className=="selectedvideo") {
      e.className = "";
    } else {
      e.className = "selectedvideo";
    }
  }
}

function watch_selected_videos() {
  f = document.getElementById("scanform");
  t = document.getElementById("scantable");
  if (f==null) {
    alert("no scan form");
  }
  if (t==null) {
    alert("no scan table");
  }
  //alert("watch_selected_videos: "+t.nodeName);
  for (var i=0; i<t.rows.length; i++) {
    var r = t.rows[i];
    //alert("row "+r.nodeName);
    for (var j=0; j<r.cells.length; j++) {
      cell = r.cells[j]
      if (cell.className=="selectedvideo") {
        //alert("selectedvideo "+cell.id);
        hidden = document.getElementById("s"+cell.id);
        //alert("hidden "+hidden.name);
        hidden.value = '1';
      } else {
      }
    }
  }
  f.submit();
}

function select_all_videos() {
  t = document.getElementById("scantable");
  if (t==null) {
    alert("no scan table");
  }
  //alert("watch_selected_videos: "+t.nodeName);
  for (var i=0; i<t.rows.length; i++) {
    var r = t.rows[i];
    //alert("row "+r.nodeName);
    for (var j=0; j<r.cells.length; j++) {
      cell = r.cells[j]
      cell.className = "selectedvideo";
    }
  }
}

function deselect_all_videos() {
  t = document.getElementById("scantable");
  if (t==null) {
    alert("no scan table");
  }
  //alert("watch_selected_videos: "+t.nodeName);
  for (var i=0; i<t.rows.length; i++) {
    var r = t.rows[i];
    //alert("row "+r.nodeName);
    for (var j=0; j<r.cells.length; j++) {
      cell = r.cells[j]
      cell.className = "";
    }
  }
}


