var feedLoaded = false;
var videoTabs = Array;
var activeTab = "vt1";
var activeSubTab = "vt1_0";

function addVideoTab(id, name, source) {
  videoTabs[id] = {"id": id,
                   "name": name,
                   "source": source,
                   "feeds": new Array() };
}

function showVideoDescription(id, type) {
  full_desc = document.getElementById("vdesc_" + id);
  short_desc = document.getElementById("vsdesc_" + id);

  full_desc.className = "hidden";
  short_desc.className = "hidden";

  if (type == "more")
    full_desc.className = "";
  else
    short_desc.className = "";
}

function addVideoSubTab(id, name, source) {
  var index = videoTabs[id].feeds.length;
  videoTabs[id].feeds[index] = {"name": name,
                                "source": source };
}

function getVideoSubNav(selectTab) {
  var feeds = videoTabs[selectTab].feeds;
  var innerContent = "";
  var selectClass = "selected";
  for (var i=0; i<feeds.length; i++) {
    var name = feeds[i].name;
    var source = feeds[i].source;
    if (i!=0) {
      selectClass = "";
      innerContent += " | ";
    }
    innerContent += "<a class='fakeLink " + selectClass + "' id='" + 
                    selectTab + "_" + i + "' " +
                    "onclick=\"loadVideoItems('" + source + "', " + i + 
                    "); selectVideoSubTab('" +
                    selectTab + "_" + i + "')\" >" +
                    name + "</a>";
  }
  activeSubTab = selectTab + "_0";
  return innerContent;
}

function selectVideoTab(selectTab) {
  document.getElementById(activeTab).className = 'fakeLink tab icon';
  document.getElementById(selectTab).className = 'fakeLink tab selected icon';
 
  subNav = document.getElementById("videoSubNavBar");
  subNav.innerHTML = getVideoSubNav(selectTab);
  
  activeTab = selectTab;
  //reload the ad
  try {
    refreshIframeAd('mb');
  } catch (e) {}
}

function setMoreVideosLink(url, text) {
  morevideos = document.getElementById("moreVideosLink");
  morevideos.innerHTML = "<a target=\"_blank\" href=\""+url+"\">"+Locale.getString('moreVidsOn')+text+"</a>";
}

function setUploadVideosLink(url, text) {
  try {
    uploadvideos = document.getElementById("uploadVideosLink");
    if (url == "")
      uploadvideos.innerHTML = "";
    else
      uploadvideos.innerHTML = "<a target=\"_blank\" href=\""+url+"\">" +Locale.getString('uploadUrOwnVids')+ "</a> | ";
  } catch (e) {}
}

function selectVideoSubTab(selectSubTab) {
  try {
    document.getElementById(activeSubTab).className = 'fakeLink';
  } catch (e) {
  }
  document.getElementById(selectSubTab).className = 'fakeLink selected';
  activeSubTab = selectSubTab;
  //reload the ad
  refreshIframeAd('mb');
}

function showVideoItems(html) {
  target = document.getElementById('video_items');
  target.innerHTML = html;
  feedLoaded = true;
}

function showVideoTimeout() {
  if (feedLoaded)
    return true;
  target = document.getElementById('video_items');
  target.innerHTML = "<span class='redboldtext'>" +Locale.getString('problemloadingVids')+"</span>";
}

function loadVideoItems(partner, feed, max) {
  var argv = loadVideoItems.arguments;
  var argc = argv.length;
  if (argc == 2) {
    autoloadFirstVideo = true;
    selected = "&selected=" + parseInt(argv[2]);
  } else
    selected = "";
  if (max != null)
    maxarg = "&max="+max;
  else
    maxarg = "";
  url = "vfeeds.php?src="+partner+maxarg+"&output=html&feed="+feed+selected;
  showVideoItems("<span class='strong sn_dkblue'>" +Locale.getString('loadingVideos')+ "</span>");
  feedLoaded = false;
  sendXMLHttpRequestText(url, 'showVideoItems');
  setTimeout('showVideoTimeout()', 10000);
}
    
function loadVideo(partner, title, duration, guid, total, id) {
  url = "addmedia.php?pcode=12345&action=addvideo&type="+partner+"&id=";
  visitedClass = "viewedVideo";
  if (total > 0) {
    for (i=1; i<=total; i++) {
      target = document.getElementById("vi_"+i);
      target.className = "";
      target = document.getElementById("vplay_"+i);
      target.className = "greenButton";
      target.innerHTML = "Watch Video";
    }
    target = document.getElementById("vi_"+id);
    target.className = "selectedVideo";
    target = document.getElementById("viewed_"+id);
    target.className = "viewedVideo";
    target = document.getElementById("vplay_"+id);
    target.className = "dkRedButton";
    target.innerHTML = "Now Playing";
    
    embedCode = document.getElementById("vembed_"+id);
  
    innerHTML = embedCode.innerHTML;
    target = document.getElementById("video_title");
    target.innerHTML = title;
    target = document.getElementById("video_length");
    target.innerHTML = duration;
    target = document.getElementById("video_preview");
    target.innerHTML = innerHTML;
  }

  //reload the ad
  refreshIframeAd('mb');
  refreshIframeAd('horiz');
}

//Start special communication JS (mainly for emailing/forwarding links to friends)
function f_messageLinkToFriend(subj, body, url) {
  if (subj == null) {
    subj = Locale.getString('checkOut');
  }

  if (body == null) {
    body = "";
  }

  if (url == null) {
    url = window.location;
  }

  message = body + "\n\n" + url;

  form = document.createElement('form');
  form.setAttribute("method", "post");
  form.setAttribute("action", "sendmessage.php?action=multiple");
  field1 = document.createElement('input');
  field1.setAttribute("type", "hidden");
  field1.setAttribute("name", "fwdsubj");
  field1.setAttribute("value", subj);
  field2 = document.createElement('input');
  field2.setAttribute("type", "hidden");
  field2.setAttribute("name", "fwdbody");
  field2.setAttribute("value", message);
  form.appendChild(field1);
  form.appendChild(field2);
  document.body.appendChild(form);

  form.submit();
}

function f_emailLinkToFriend(message, url) {
  if (message == null) {
    message = Locale.getString('checkOut');
  }

  if (url == null) {
    url = window.location;
  }

  body = message + " " + escape(url);

  link = "mailto:?subject=" + message + "&body=" + body;
  window.location = link;
}

function f_IMlinkToFriend(message, url) {
  if (message == null) {
    message = Locale.getString('checkOut');
  }

  if (url == null) {
    url = window.location;
  }

  message = message.replace(/\s+/g, "+") + "+" + escape(url);

  link = "aim:GoIM?message=" + message;
  window.location = link;
}
//end special communications JS
