
var months = new Array("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec");
var playlists = new Array("picks","popular","latest","suggest","profile","random","search","user");

if (typeof XMLHttpRequest == "undefined" && window.ActiveXObject)
{
  function XMLHttpRequest()
  {
    var arrSignatures = ["MSXML2.XMLHTTP.5.0", "MSXML2.XMLHTTP.4.0","MSXML2.XMLHTTP.3.0", "MSXML2.XMLHTTP","Microsoft.XMLHTTP"];
    for (var i=0; i < arrSignatures.length; i++)
    {
      try
      {
        var oRequest = new ActiveXObject(arrSignatures[i]);
        return oRequest;
      }
      catch (oError)
      {
        //ignore
      }
    }
    throw new Error("MSXML is not installed on your system.");
  }
}

function playlistReset()
{

        showOtherPlaylist();

        if(typeof popularFull=='boolean')
        {
            toggleExpandShrinkLink('popular');
            popularFull=false;
            popularOffset = 0;
            popularOffsetStep = 5;
            showLoadText('popular');
            doPopularRequest(popularFull, popularOffset);
            if(document.getElementById('popularExpandShrinkLink') != null)
            {
                document.getElementById('popularExpandShrinkLink').innerHTML = "more";
            }
        }

        if(typeof picksFull=='boolean')
        {
            toggleExpandShrinkLink('picks');
            picksFull=false;
            picksOffset = 0;
            picksOffsetStep = 5;
            showLoadText('picks');
            doPicksRequest(picksFull, picksOffset);
            if(document.getElementById('picksExpandShrinkLink') != null)
            {
                document.getElementById('picksExpandShrinkLink').innerHTML = "more";
            }
        }

        if(typeof latestFull=='boolean'&&latestFull)
        {
            toggleExpandShrinkLink('latest');
            latestFull=false;
            latestOffset = 0 ;
            latestOffsetStep = 5;
            showLoadText('latest');
            doLatestRequest(latestFull, latestOffset);
            if(document.getElementById('latestExpandShrinkLink') != null)
            {
                document.getElementById('latestExpandShrinkLink').innerHTML = "more";
            }
        }
        else if(typeof randomFull=='boolean'&&randomFull)
        {
            toggleExpandShrinkLink('random');
            randomFull=false;
            randomOffset  = 0 ;
            showLoadText('random');
            doRandomRequest(randomFull,randomOffset);
            if(document.getElementById('randomExpandShrinkLink') != null)
            {
                document.getElementById('randomExpandShrinkLink').innerHTML = "more";
            }
        }
        else if(typeof profileFull=='boolean'&&profileFull)
        {
            toggleExpandShrinkLink('profile');
            profileFull=false;
            showLoadText('profile');
            profileOffset = 0 ;
            doProfileRequest(profileFull,profileOffset);
            if(document.getElementById('profileExpandShrinkLink') != null)
            {
                document.getElementById('profileExpandShrinkLink').innerHTML = "more";
            }
        }
        else if(typeof suggestFull=='boolean'&&suggestFull)
        {
            toggleExpandShrinkLink('suggest');
            suggestFull=false;
            suggestOffset = 0 ;
            showLoadText('suggest');
            doSuggestRequest(suggestFull,suggestOffset);
            if(document.getElementById('suggestExpandShrinkLink') != null)
            {
                document.getElementById('suggestExpandShrinkLink').innerHTML = "more";
            }
        }
    }


function splitOnWhitespacePattern(sIn)
{
  var pattern = new RegExp("[\\s]+","g");
  return (sIn.split(pattern,-1));
}

function splitWordsLongerThan(sIn, maxLength)
{
  var sOut = null;
  if(sIn!=null&&maxLength>0)
  {
    var words = splitOnWhitespacePattern(sIn);
    for(var i=0;i<words.length;i++)
    {
      if(!(words[i].length>maxLength))
      {
        if(sOut!=null)
        {
          sOut+= words[i];
        }
        else
        {
          sOut = words[i];
        }
        sOut+= ' ';
      }
      else
      {
        var tempWord = words[i];
        do
        {
          if(sOut!=null)
          {
            sOut+= tempWord.substring(0,maxLength);
          }
          else
          {
            sOut = tempWord.substring(0,maxLength);
          }
          tempWord=tempWord.substring(maxLength);
          sOut+= ' ';
        }
        while(tempWord.length>maxLength);
        sOut+= tempWord;
        sOut+= ' ';
      }
    }
    return sOut.substring(0,((sOut.length)-1));
  }
}

function showLoadText(type)
{
  var oLoad = document.getElementById(type+"Load");
  if(oLoad)
  {
    oLoad.style.visibility="visible";
  }
}

function hideLoadText(type)
{
  var oLoad = document.getElementById(type+"Load");
  if(oLoad)
  {
    oLoad.style.visibility="hidden";
  }
}

function hideOtherPlaylist(type)
{
  for(var i=0,iMax=playlists.length;i<iMax;i++)
  {
    var thisPlaylist = playlists[i];
    var oTBody = document.getElementById(thisPlaylist+"TableBody");
    if(oTBody)
    {
			var oHead1 = document.getElementById(thisPlaylist+"TableHeading1"),
					oHead2 = document.getElementById(thisPlaylist+"TableHeading2"),
					oMovieLink = document.getElementById(thisPlaylist+"PlayMovieLink"),
					oVerticalBar = document.getElementById(thisPlaylist+"PlayVerticalBar"),
					oExpandShrinkList = document.getElementById(thisPlaylist+"ExpandShrinkLink"),
					oResizeableTable = document.getElementById(thisPlaylist+"ResizeableTable");
    	if(thisPlaylist!=type)
			{
				oTBody.style.display='none';
    		if(oHead1) oHead1.style.display='none';
				if(oHead2) oHead2.style.display='inline';
		  	if(oMovieLink) oMovieLink.style.display='none';
		  	if(oVerticalBar) oVerticalBar.style.display='none';
				if(oExpandShrinkList) oExpandShrinkList.style.display='none';
				if(oResizeableTable)
				{
				  oResizeableTable.style.height='30px';
					oResizeableTable.height='30px';
				}
			}
			else
			{
				oTBody.style.display='';
    		if(oHead1) oHead1.style.display='inline';
				if(oHead2) oHead2.style.display='none';
		  	if(oMovieLink) oMovieLink.style.display='inline';
		  	if(oVerticalBar) oVerticalBar.style.display='inline';
				if(oExpandShrinkList) oExpandShrinkList.style.display='inline';
			}
		}
  }
}


function showOtherPlaylist()
{
  for(var i=0,iMax=playlists.length;i<iMax;i++)
  {
    var thisPlaylist = playlists[i];
    var oTBody = document.getElementById(thisPlaylist+"TableBody");
    if(oTBody)
    {
    	oTBody.style.display='';
    	var oHead1 = document.getElementById(thisPlaylist+"TableHeading1"),
					oHead2 = document.getElementById(thisPlaylist+"TableHeading2"),
					oMovieLink = document.getElementById(thisPlaylist+"PlayMovieLink"),
					oVerticalBar = document.getElementById(thisPlaylist+"PlayVerticalBar"),
					oExpandShrinkList = document.getElementById(thisPlaylist+"ExpandShrinkLink"),
					oResizeableTable = document.getElementById(thisPlaylist+"ResizeableTable");
			if(oHead1) oHead1.style.display='inline';
			if(oHead2) oHead2.style.display='none';
		  if(oMovieLink) oMovieLink.style.display='inline';
		  if(oVerticalBar) oVerticalBar.style.display='inline';
			if(oExpandShrinkList) oExpandShrinkList.style.display='inline';
			if(oResizeableTable) oResizeableTable.height='';
		}
  }
}

function toggleExpandShrinkLink(type)
{
  if(document.getElementById(type+"ExpandShrinkLink")!=null)
  {
    if(document.getElementById(type+"ExpandShrinkLink").title=="Expand Results List")
    {
      document.getElementById(type+"ExpandShrinkLink").title="Shrink Results List";
      document.getElementById(type+"ExpandShrinkLink").innerHTML="less";
    }
    else
    {
      document.getElementById(type+"ExpandShrinkLink").title="Expand Results List";
      document.getElementById(type+"ExpandShrinkLink").innerHTML="more";
    }
  }
}

function showHand(type)
{
  if(document.getElementById(type+"ExpandShrinkLink")!=null)
  {
    document.getElementById(type+"ExpandShrinkLink").style.cursor="pointer";
  }
}

function showLeftArrow(display, type)
{
  var leftArrowLink = document.getElementById(type+"LeftArrowLink");
  if(!display)
  {
    leftArrowLink.style.visibility = "hidden";
  }
  else
  {
    leftArrowLink.style.visibility = "visible";
  }
}

function showRightArrow(display, type)
{
  var rightArrowLink = document.getElementById(type+"RightArrowLink");
  if(!display)
  {
    rightArrowLink.style.visibility = "hidden";
  }
  else
  {
    rightArrowLink.style.visibility = "visible";
  }
}

function readCookie(sName)
{
  var sRE = "(?:; )?" + sName + "=([^;]*);?";
  var oRE = new RegExp(sRE);
  if (oRE.test(document.cookie))
  {
    return decodeURIComponent(RegExp["$1"]);
  }
  else
  {
    return null;
  }
}

function writeCookie(sName, sValue, oExpires, sPath, sDomain, bSecure)
{
  var sCookie = sName + "=" + encodeURIComponent(sValue);
  if(oExpires)
  {
    sCookie += "; expires=" + oExpires.toGMTString();
  }
  if(sPath)
  {
    sCookie += "; path=" + sPath;
  }
  if(sDomain)
  {
    sCookie += "; domain=" + sDomain;
  }
  if(bSecure)
  {
    sCookie += "; secure";
  }
  document.cookie = sCookie;
}

function httpRequest(url, http, responseFunction)
{
  // do the request, add random parameter to avoid caching
  url+="&nocache=" + parseInt(Math.random()*10000);
  http.open("GET", url, true);
  http.onreadystatechange=responseFunction;
  http.send(null);
}

function clip(id, ref, title, username, views, flashClickLink, customTitle, customLink, adult)
{
  this.id=id;
  this.ref=ref;
  this.title=title;
  this.username=username;
  this.views=views;
  this.flashClickLink=flashClickLink;
  this.customTitle=customTitle;
  this.customLink=customLink;
  this.adult=adult;
}

function user(name, imageURL, regDate, age, gender, location)
{
  name?this.name=name:this.name='';
  this.imageURL=imageURL;
  regDate?this.regDate=regDate:this.regDate='';
  age?this.age=age:this.age='';
  gender?this.gender=gender:this.gender='';
  location?this.location=location:this.location='';
}

function Channel(channelID, description, favourite, name, ownerID, visibility, ownedByCurrentUser, ownerName, ref, flashClickLink, subscribedToByCurrentUser,adult)
{
  this.id = g_flashCounter++;
  this.channelID=channelID;
  this.description=description;
  this.favourite=favourite;
  this.name=name;
  this.ownerID=ownerID;
  this.visibility=visibility;
  this.ownedByCurrentUser=ownedByCurrentUser;
  this.ownerName = ownerName;
  this.ref = ref;
  this.flashClickLink = flashClickLink;
  this.subscribedToByCurrentUser = subscribedToByCurrentUser;
  this.adult = adult;
}

function addClipIcons(allowUserManagement,clipCellTBody,clipsHolder,clipCounter,allowChannelManagement,channelID)
{
    var userMgtRow = document.createElement('tr');
    var userMgtCol = document.createElement('td');
    userMgtCol.innerHTML ="<span class='textSmall' style='color:#808080;'><a href='javascript:void(window.open(\"addToChannel.do?reloadPath=addToChannel.do&refreshWindow=true&ref=" +clipsHolder[clipCounter].ref+ "\",\"\",\"dependent=yes,status=No,titlebar=No,menubar=No,toolbar=No,location=No,scrollbars=yes,status=No,resizable=Yes,height=600,width=650\"));'><img src=" + channelClipImg  + " onMouseOver=\"javascript:window.status='Add Video To Channel';\" onMouseOut=\"javascript:window.status='';\" alt='Add Video To Channel' width='17px' height='16px' border='0'/></a></span>";
    if(allowChannelManagement)
    {
      userMgtCol.innerHTML +="<span class='textSmall' style='color:#808080;'><a href='javascript:void(removeClipFromChannel(\"" + clipsHolder[clipCounter].ref + "\"," + channelID + "));'><img src=" + removeFromChannelImg  + " alt='Remove Video From Channel' width='17px' height='16px' border='0'/></a></span>";
    }
    if(allowUserManagement)
    {
      userMgtCol.innerHTML += "<span class='textSmall' style='color:#808080;'><a href='gotoEditClip.do?ref=" +clipsHolder[clipCounter].ref+ "'><img src=" + editClipImg  + " onMouseOver=\"javascript:window.status='Edit This Video';\" onMouseOut=\"javascript:window.status='';\" alt='Edit This Video' width='16px' height='16px' border='0'/></a></span><span class='textSmall' style='color:#808080;'><a href=\"javascript:void(remove('" + clipsHolder[clipCounter].ref + "'));\"><img src=" + deleteClipImg  + " alt='remove this video' width='16px' height='16px' border='0'/></a></span>";
    }
    userMgtRow.appendChild(userMgtCol);
    clipCellTBody.appendChild(userMgtRow);
}

function removeClipFromChannel(sRef, iChannelID)
{
  if(confirm("Are you sure you want to attempt remove this video from your channel?"))
  {
    window.open('removeClipFromChannel.do?ref=' + sRef + "&channelID=" + iChannelID,'_blinkx','');
    showLoadText('channel');
  }
}

function matchesLoggedInUser(sMatch)
{
  if((logoutOk.length<1)&&(((loginCookieValue!=null)&&(loginCookieValue.split(',')[0]==sMatch))||((loginOk.length>0)&&(loginOk==sMatch))))
  {
    return true;
  }
  else
  {
    return false;
  }
}

function httpResponse(http,type,offset,offsetStep,bForceLeftArrow,bForceRightArrow,bIsChannelPlaylist)
{
  var totalResults = 0;
  try
  {
    if (http.readyState==4)
    {
      if (http.status==200)
      {
        xml=http.responseXML;

        var totalElementsList = xml.getElementsByTagName("total");
        if(totalElementsList.length>0)
        {
          var totalElement = totalElementsList.item(0);
          if(totalElement.hasChildNodes())
          {
            totalResults = totalElement.firstChild.nodeValue;
          }
        }
        var oResizeableTable = document.getElementById(type+"ResizeableTable");
        if(type=="user")
        {
          var users = xml.getElementsByTagName("user");
          var usersTBody = document.getElementById(type+"sTableBody");
					var usersNum = users.length;
					if(users!=null&&usersNum>0)
          {
            if(oResizeableTable)
        		{
								oResizeableTable.style.height= (Math.ceil(usersNum/5)*2).toString() + '00px';
								oResizeableTable.height=(Math.ceil(usersNum/5)*2).toString() + '00px';
						}
				    var usersHolder = new Array();
            var name,imageURL,regDate,age,gender,location;
            for(i=0; i<usersNum; i++)
            {
              if(users.item(i).childNodes[0].firstChild!=null)
              {
                name = users.item(i).childNodes[0].firstChild.nodeValue;
              }
              if(users.item(i).childNodes[1].firstChild!=null)
              {
                imageURL = users.item(i).childNodes[1].firstChild.nodeValue;
              }
              if(users.item(i).childNodes[2].firstChild!=null)
              {
                regDate = users.item(i).childNodes[2].firstChild.nodeValue;
              }
              usersHolder[i]=new user(name,imageURL,regDate,age,gender,location);
              name=null;
              imageURL=null;
              regDate=null;
              age=null;
            }
            var usersTRows = usersTBody.rows;
            while(usersTRows.length>0)
            {
              usersTBody.removeChild(usersTRows[0]);
            }
            for(var rowCounter=0; rowCounter<Math.ceil(usersNum/5); rowCounter++)
            {
              var usersTRow = document.createElement('tr');
              for(var userCounter=rowCounter*5; userCounter<(usersNum>(rowCounter+1)*5?(rowCounter+1)*5:usersNum); userCounter++)
              {
                var usersTCol = document.createElement('td');
                usersTCol.vAlign="top"
                usersTCol.align="left";
                usersTCol.width="96px";
                var userCellTable = document.createElement('table');
                userCellTable.width="96px";
                var usersThisRow = 0;
                var userCellTBody = document.createElement('tbody');
                for(var i=0;i<4;i++)
                {
                  var userCellTRow = document.createElement('tr');
                  var userCellTCol = document.createElement('td');
                  if(i==0)
                  {
                    if(usersHolder[userCounter].imageURL==null)
                    {
                      userCellTCol.innerHTML="<tr><td><div style='color:white;background-color:white;width:80px;height:60px;border-style:solid;border-width:1px;border-color:black;'><a href='viewprofile.do?username=" + usersHolder[userCounter].name + "'><img border='0' src='http://us-store.blinkx.com/images/Selfcast/silhouette.png' alt='loading' width='80px'/></a></div></td></tr>";
                    }
                    else
                    {
                      userCellTCol.innerHTML="<tr><td><div style='color:white;background-color:white;width:80px;border-style:solid;border-width:1px;border-color:black;'><a href='viewprofile.do?username=" + usersHolder[userCounter].name + "'><img border='0' src='http://us-store.blinkx.com" + usersHolder[userCounter].imageURL  + "' alt='view profile' width='80px'/></a></div></td></tr>";
                    }
                  }
                  if(i==1)
                  {
                    userCellTCol.innerHTML="<a href='viewprofile.do?username=" + usersHolder[userCounter].name + "' class='redLinkSmall' title='"+ usersHolder[userCounter].name +"'>" + usersHolder[userCounter].name + "</a>";
                  }
                  if(i==2)
                  {
                    var sqlDate = usersHolder[userCounter].regDate;
                    var mon = sqlDate.substring(5,7);
                    var iMon = parseInt(mon);
                    if(iMon>0 && iMon<13)
                    {
                      mon=months[iMon-1];
                    }
                    else
                    {
                      mon=months[0];
                    }
                    var displayDate = sqlDate.substring(8,10) + "&nbsp;" + mon + "&nbsp;" + sqlDate.substring(0,4);
                    userCellTCol.innerHTML="<span class='textSmall' style='color:#808080;'>member since: " + displayDate + "</span>";
                  }
                  userCellTRow.appendChild(userCellTCol);
                  userCellTBody.appendChild(userCellTRow);
                }
                userCellTable.appendChild(userCellTBody);
                usersTCol.appendChild(userCellTable);
                usersTRow.appendChild(usersTCol);
                usersThisRow++;
              }
              if(usersThisRow<5)
              {
                var extraUsersCol = document.createElement('td');
                extraUsersCol.width="100%";
                extraUsersCol.innerHTML="&nbsp";
                usersTRow.appendChild(extraUsersCol);
              }
              usersTBody.appendChild(usersTRow);
            }
          }
          else
          {
            var tempTRow = document.createElement('tr');
            var tempTCol = document.createElement('td');
            tempTCol.vAlign = 'top';
            tempTCol.innerHTML="<span class='skyBlueText' style='font-size:10pt;'>No matching users</span>"
            tempTRow.appendChild(tempTCol);
            usersTBody.appendChild(tempTRow);
            if(oResizeableTable)
        		{
								oResizeableTable.style.height= '30px';
								oResizeableTable.height='30px';
						}
          }
        }
        else if(type=="channelSearch" || type=="latestChannel" || type=="randomChannel")
        {
          var channels = xml.getElementsByTagName("channel");
          var channelsNum = channels.length;
          var channelsTBody = document.getElementById(type+"sTableBody");
          if(channels!=null&&channelsNum>0)
          {
            if(oResizeableTable)
            {
                oResizeableTable.style.height= (Math.ceil(channelsNum/5)*2).toString() + '00px';
                oResizeableTable.height=(Math.ceil(channelsNum/5)*2).toString() + '00px';
            }
            var channelsHolder = new Array();
            var channelID,owner,name,description,defaultReference;

            for(i=0; i<channelsNum; i++)
            {
              var oChannel = new Channel;
              oChannel.ownerID = -1;
              oChannel.visibility = '';

              var channelElement = channels.item(i);
              for(j=0;j<6;j++)
              {
                var node = channelElement.childNodes[j];
                if(node)
                {
                  var nodeName = node.nodeName;
                  var firstChild = node.firstChild;
                  if(firstChild)
                  {
                    var nodeValue = firstChild.nodeValue;
                    switch(nodeName)
                    {
                      case "id":
                        oChannel.id = nodeValue;
                        oChannel.flashClickLink = "javascript:window.location.href='playChannel.do?singleChannelID=" + nodeValue + "'";
                        break;
                      case "owner":
                        oChannel.ownerName = nodeValue;
                        oChannel.ownedByCurrentUser = matchesLoggedInUser(nodeValue);
                        break;
                      case "name":
                        oChannel.name = nodeValue;
                        break;
                      case "description":
                        oChannel.description = nodeValue;
                        break;
                      case "defaultReference":
                        oChannel.ref = nodeValue;
                        break;
                      case "adult":
                        oChannel.adult = nodeValue;
                        break;
                      default:
                    }
                  }
                }
              }
              channelsHolder.push(oChannel);
            }

            var channelsTRows = channelsTBody.rows;
            while(channelsTRows.length>0)
            {
              channelsTBody.removeChild(channelsTRows[0]);
            }
            for(var rowCounter=0; rowCounter<Math.ceil(channelsNum/5); rowCounter++)
            {
              var channelsTRow = document.createElement('tr');
              for(var channelCounter=rowCounter*5; channelCounter<(channelsNum>(rowCounter+1)*5?(rowCounter+1)*5:channelsNum); channelCounter++)
              {
                var channelsTCol = document.createElement('td');
                channelsTCol.vAlign="top"
                channelsTCol.align="left";
                channelsTCol.width="96px";
                var channelCellTable = document.createElement('table');
                channelCellTable.width="96px";
                var channelsThisRow = 0;
                var channelCellTBody = document.createElement('tbody');
                var bCensor = channelsHolder[channelCounter].adult && isFilterOn();
                for(var i=0;i<4;i++)
                {
                  var channelCellTRow = document.createElement('tr');
                  var channelCellTCol = document.createElement('td');
                  if(i==0)
                  {
                    if(bCensor)
                    {
                      channelCellTCol.innerHTML="<tr><td><div id=linkFlashNoMasq" + channelsHolder[channelCounter].id + " style='color:white;background-color:black;width:80px;height:60px;border-style:solid;border-width:1px;border-color:black;'><a href='javascript:doUnCensor();' title='*Censored* - click here to turn your filter off'><img src='http://us-store.blinkx.com/images/defaultswf/selfcastdefault.jpg' border='0'/></a></div></td></tr>";
                    }
                    else if(channelsHolder[channelCounter].ref==null)
                    {
                      channelCellTCol.innerHTML="<tr><td><div style='color:white;background-color:white;width:80px;height:60px;border-style:solid;border-width:1px;border-color:black;'><img src='http://us-store.blinkx.com/images/Selfcast/emptyChannel.gif' alt='loading' width='80x' height='60px'/></div></td></tr>";
                    }
                    else
                    {
                      channelCellTCol.innerHTML="<tr><td><div id=linkFlashMasq" + channelsHolder[channelCounter].id + " style='color:white;background-color:black;width:80px;height:60px;border-style:solid;border-width:1px;border-color:black;'><img src='http://us-store.blinkx.com/images/defaultswf/selfcastdefault.jpg' alt='loading'/></div></td></tr>";
                    }
                  }
                  if(i==1)
                  {
                    if(bCensor)
                    {
                    	channelCellTCol.innerHTML="<a href='javascript:doUnCensor();' class='blueLinkSmall' title='*Censored* - click here to turn your filter off'>*Censored*</a>";
                   	}
                    else
                    {
                      channelCellTCol.innerHTML="<a href='playChannel.do?singleChannelID=" + channelsHolder[channelCounter].id + "' class='blueLinkSmall' title='"+ channelsHolder[channelCounter].name +"'>" + channelsHolder[channelCounter].name + "</a>";
                    }
                  }
                  if(i==2)
                  {
                    channelCellTCol.innerHTML="<span class='textSmall' style='color:#808080;'>by: </span><a href='viewprofile.do?username=" + channelsHolder[channelCounter].ownerName + "' class='redLinkSmall' title='" + channelsHolder[channelCounter].ownerName + "'>" + channelsHolder[channelCounter].ownerName + "</a>";
                  }
                  channelCellTRow.appendChild(channelCellTCol);
                  channelCellTBody.appendChild(channelCellTRow);
                }
                channelCellTable.appendChild(channelCellTBody);
                channelsTCol.appendChild(channelCellTable);
                channelsTRow.appendChild(channelsTCol);
                channelsThisRow++;
              }
              if(channelsThisRow<5)
              {
                var extraChannelsCol = document.createElement('td');
                extraChannelsCol.width="100%";
                extraChannelsCol.innerHTML="&nbsp";
                channelsTRow.appendChild(extraChannelsCol);
              }
              channelsTBody.appendChild(channelsTRow);
            }
            showFlash(channelsHolder);
          }
          else
          {
            var tempTRow = document.createElement('tr');
            var tempTCol = document.createElement('td');
            tempTCol.vAlign = 'top';
            tempTCol.innerHTML="<span class='skyBlueText' style='font-size:10pt;'>No matching channels</span>"
            tempTRow.appendChild(tempTCol);
            channelsTBody.appendChild(tempTRow);
            if(oResizeableTable)
            {
              oResizeableTable.style.height= '30px';
              oResizeableTable.height='30px';
            }
          }
        }
        else
        {
          if(bIsChannelPlaylist)
          {
            var allowChannelManagement = false;
            var ownerElementsList = xml.getElementsByTagName("owner");
            if(ownerElementsList.length>0)
            {
              var ownerElement = ownerElementsList.item(0);
              if(ownerElement.hasChildNodes())
              {
                if(matchesLoggedInUser(ownerElement.firstChild.nodeValue))
                {
                  allowChannelManagement=true;
                }
              }
            }
          }


          if(type=="search")
          {
            var oRow = document.getElementById("makeNewChannelRow");
            if(oRow)
            {
              if(totalResults>0)
              {
                oRow.style.display="block";
              }
              else
              {
                oRow.style.display="none";
              }
            }
          }

          var clips = xml.getElementsByTagName("clip");
          var clipsNum = clips.length;
          var clipsTBody = document.getElementById(type+"ClipsTableBody");
          var clipsTRows = clipsTBody.rows;
          while(clipsTRows.length>0)
          {
            clipsTBody.removeChild(clipsTRows[0]);
          }
          if(clips!=null&&clipsNum>0)
          {
            if(oResizeableTable)
            {
              oResizeableTable.style.height= (Math.ceil(clipsNum/5)*2).toString() + '00px';
              oResizeableTable.height=(Math.ceil(clipsNum/5)*2).toString() + '00px';
            }
            var clipsHolder = new Array();
            for(i=0; i<clipsNum; i++)
            {
              var oClip = new clip;
              oClip.id=g_flashCounter++;
              var clipElement = clips.item(i);
              for(j=0;j<7;j++)
              {
                var node = clipElement.childNodes[j];
                if(node)
                {
                  var nodeName = node.nodeName;
                  var firstChild = node.firstChild;
                  if(firstChild)
                  {
                    var nodeValue = firstChild.nodeValue;
                    switch(nodeName)
                    {
                      case "ref":
                        oClip.ref = nodeValue;
                        oClip.flashClickLink = "javascript:window.location.href='playVideo.do?ref=" + nodeValue + "'";
                        break;
                      case "title":
                        oClip.title = nodeValue;
                        break;
                      case "username":
                        oClip.username = nodeValue;
                        break;
                      case "views":
                        oClip.views = nodeValue;
                        break;
                      case "adult":
                        oClip.adult = nodeValue;
                        break;
                      case "customTitle":
                        oClip.customTitle = nodeValue;
                        break;
                      case "customLink":
                        oClip.customLink = nodeValue
                        break;
                      default:
                    }
                  }
                }
              }
              clipsHolder.push(oClip);
            }
            for(var rowCounter=0; rowCounter<Math.ceil(clipsNum/5); rowCounter++)
            {
              var clipsTRow = document.createElement('tr');
              clipsTRow.id="clipsRow" + g_rowCounter++;
              for(var clipCounter=rowCounter*5; clipCounter<(clipsNum>(rowCounter+1)*5?(rowCounter+1)*5:clipsNum); clipCounter++)
              {
                var allowUserManagement = false;
                if(matchesLoggedInUser(clipsHolder[clipCounter].username))
                {
                  allowUserManagement=true;
                }
                var clipsTCol = document.createElement('td');
                clipsTCol.vAlign="top"
                clipsTCol.align="left";
                clipsTCol.width="96px";
                var clipCellTable = document.createElement('table');
                clipCellTable.width="96px";
                var clipsThisRow = 0;
                var clipCellTBody = document.createElement('tbody');

                var fullTitle = clipsHolder[clipCounter].title;
                var shortTitle = splitWordsLongerThan(fullTitle,12);
                if(shortTitle.length>24)
                {
                  shortTitle = shortTitle.substring(0,21) + "...";
                }
                var fullUsername = clipsHolder[clipCounter].username;
                var shortUsername = (fullUsername.length>12?fullUsername.substring(0,9):fullUsername) + (fullUsername.length>12?"...":"");
                var bCensor = clipsHolder[clipCounter].adult && isFilterOn()
                for(var i=0;i<4;i++)
                {
                  var clipCellTRow = document.createElement('tr');
                  var clipCellTCol = document.createElement('td');
                  if(i==0)
                  {
                    if(bCensor)
                    {
                      clipCellTCol.innerHTML="<tr><td><div id=linkFlashNoMasq" + clipsHolder[clipCounter].id + " style='color:white;background-color:black;width:80px;height:60px;border-style:solid;border-width:1px;border-color:black;'><a href='javascript:doUnCensor();' title='*Censored* - click here to turn your filter off'><img src='http://us-store.blinkx.com/images/defaultswf/selfcastdefault.jpg' border='0'/></a></div></td></tr>";
                    }
                    else
                    {
                      clipCellTCol.innerHTML="<tr><td><div id=linkFlashMasq" + clipsHolder[clipCounter].id + " style='color:white;background-color:black;width:80px;height:60px;border-style:solid;border-width:1px;border-color:black;'><img src='http://us-store.blinkx.com/images/defaultswf/selfcastdefault.jpg' alt='loading'/></div></td></tr>";
                    }
                  }
                  if(i==1)
                  {
                    if(bCensor)
                    {
                      clipCellTCol.innerHTML="<a href='javascript:doUnCensor();' class='blueLinkSmall' title='*Censored* - click here to turn your filter off'>*Censored*</a>";
                    }
                    else
                    {
                      clipCellTCol.innerHTML="<a href='playVideo.do?ref=" + clipsHolder[clipCounter].ref + "' class='blueLinkSmall' title='"+ fullTitle +"'>" + shortTitle + "</a>";
                    }
                  }
                  if(i==2)
                  {
                    clipCellTCol.innerHTML="<span class='textSmall' style='color:#808080;'>by: </span><a href='viewprofile.do?username=" + fullUsername + "' class='redLinkSmall' title='" + fullUsername+ "'>" + shortUsername + "</a>";
                  }
                  if(i==3)
                  {
                    clipCellTCol.innerHTML="<span class='textSmall' style='color:#808080;'>views: " + clipsHolder[clipCounter].views + "</span>";
                  }
                  clipCellTRow.appendChild(clipCellTCol);
                  clipCellTBody.appendChild(clipCellTRow);
                }

                //Do function to add clip Icons
                if(bIsChannelPlaylist)
                {
                  if(type=="userChannel")
                  {
                    addClipIcons(allowUserManagement,clipCellTBody,clipsHolder,clipCounter,allowChannelManagement,userChannelID);
                  }
                  else if(type=="subscribedChannel")
                  {
                    addClipIcons(allowUserManagement,clipCellTBody,clipsHolder,clipCounter,allowChannelManagement,subscribedChannelID);
                  }
                  else if(type=="singleChannel")
                  {
                    addClipIcons(allowUserManagement,clipCellTBody,clipsHolder,clipCounter,allowChannelManagement,singleChannelID);
                  }
                }
                else if(!bCensor)
                {
                  addClipIcons(allowUserManagement,clipCellTBody,clipsHolder,clipCounter);
                }
                if(clipsHolder[clipCounter].customTitle!=null&&clipsHolder[clipCounter].customTitle.length>0)
                {
                   var customRow = document.createElement('tr');
                   var customCol = document.createElement('td');
                   customCol.innerHTML ="<span class='textSmall' style='color:#808080;'><a href='"+ clipsHolder[clipCounter].customLink + "'>" + clipsHolder[clipCounter].customTitle + "</a></span>";
                   customRow.appendChild(customCol);
                   clipCellTBody.appendChild(customRow);
                }
                clipCellTable.appendChild(clipCellTBody);
                clipsTCol.appendChild(clipCellTable);
                clipsTRow.appendChild(clipsTCol);
                allowUserManagement=false;
                clipsThisRow++;
              }
              if(clipsThisRow<5)
              {
                var extraClipsCol = document.createElement('td');
                extraClipsCol.width="100%";
                extraClipsCol.innerHTML="&nbsp;";
                clipsTRow.appendChild(extraClipsCol);
              }
              clipsTBody.appendChild(clipsTRow);
            }
            showFlash(clipsHolder);
          }  // end if(clips!=null&&clips.length>0)
          else
          {
            var tempTRow = document.createElement('tr');
            var tempTCol = document.createElement('td');
            tempTCol.vAlign = 'top';
            if(bIsChannelPlaylist)
            {
              tempTCol.innerHTML="<span class='skyBlueText' style='font-size:10pt;'>No videos in channel</span>"
            }
            else
            {
              tempTCol.innerHTML="<span class='skyBlueText' style='font-size:10pt;'>No matching videos</span>"
            }
            tempTRow.appendChild(tempTCol);
            clipsTBody.appendChild(tempTRow);
            if(oResizeableTable)
            {
              oResizeableTable.style.height= '30px';
              oResizeableTable.height='30px';
            }
          }
        }

        //total results for channel search playlist
        if(type == "channelSearch" && totalResults > 0 && document.getElementById("channelSerachTotalResults")!= null)
        {
            document.getElementById("channelSerachTotalResults").innerHTML = "-&nbsp;" +  totalResults;
        }

        //show hide the movie mode button depending if there are results in the view profile page
        if(type == "subscribedChannel" && document.getElementById("subscribedChannelPlayMovieLink")!= null)
        {
            if(totalResults > 0)
                document.getElementById("subscribedChannelPlayMovieLink").style.display              = 'inline';
            else
                document.getElementById("subscribedChannelPlayMovieLink").style.display              = 'none';
        }
        if(type == "userChannel" && document.getElementById("userChannelPlayMovieLink")!= null)
        {
            if(totalResults > 0)
            {
                document.getElementById("userChannelPlayMovieLink").style.display              = 'inline';
            }
            else
            {
                document.getElementById("userChannelPlayMovieLink").style.display              = 'none';
            }
        }

        if(type == "singleChannel" && document.getElementById("singleChannelPlayMovieLink")!= null)
        {
            if(totalResults > 0)
            {
                document.getElementById("singleChannelPlayMovieLink").style.display              = 'inline';
            }
            else
            {
                document.getElementById("singleChannelPlayMovieLink").style.display             = 'none';
            }
            if(totalResults > 5 )
            {
                document.getElementById("singleChannelPlayVerticalBar").style.display           = 'inline';
                document.getElementById("singleChannelExpandShrinkLink").style.display      = 'inline';
            }
            else
            {
                document.getElementById("singleChannelPlayVerticalBar").style.display           = 'none';
                document.getElementById("singleChannelExpandShrinkLink").style.display      = 'none';
            }
        }


        if(type == "profile"  && document.getElementById("profilePlayMovieLink")!= null)
        {
            if(totalResults > 0)
            {
                document.getElementById("profilePlayMovieLink").style.display              = 'inline';
            }
            else
            {
                document.getElementById("profilePlayMovieLink").style.display             = 'none';
            }
            if(totalResults > 5 )
            {
                document.getElementById("profilePlayVerticalBar").style.display           = 'inline';
                document.getElementById("profileExpandShrinkLink").style.display                = 'inline';
            }
            else
            {
                document.getElementById("profilePlayVerticalBar").style.display           = 'none';
                document.getElementById("profileExpandShrinkLink").style.display          = 'none';
            }

        }


        //decide whether to show or hide left and right arrow
        if(bForceRightArrow || offset+offsetStep<totalResults)
        {
          showRightArrow(true, type);
        }
        else
        {
          showRightArrow(false,type);
        }
        if(bForceLeftArrow || offset>1)
        {
          showLeftArrow(true, type);
        }
        else
        {
          showLeftArrow(false, type);
        }
      }
      return true;
    }
  }
  catch(ep)
  {
  }
  return false;
}

function showFlash(clipsHolder)
{
  for(var i=0; i<clipsHolder.length; i++)
  {
    var destinationElementName = "linkFlashMasq" + clipsHolder[i].id;
    if(document.getElementById(destinationElementName))
    {
      var linkFlashTemp = new SWFObject("/Selfcast/linkflash.swf", "linkFlash" + clipsHolder[i].id, "80", "60", "7", "#000000");
      linkFlashTemp.addParam("allowScriptAccess", "sameDomain");
      linkFlashTemp.addParam("movie", "linkflash.swf");
      linkFlashTemp.addParam("quality", "high");
      linkFlashTemp.addParam("wmode", "transparent");
      linkFlashTemp.addParam("bgcolor", "#000000");
      linkFlashTemp.addParam("flashvars", "SizeX=80&Filename=http://us-store.blinkx.com/images/video/" + clipsHolder[i].ref+ ".swf&AlternateFilename=http://us-store.blinkx.com/images/defaultswf/selfcastdefault.jpg&Jscript="+clipsHolder[i].flashClickLink);
      linkFlashTemp.write(destinationElementName);
    }
  }
}

function setFilter(bValue)
{
  var oFilter = document.getElementById("filter");
  if(oFilter)
  {
    if(bValue)
    {
      oFilter.innerHTML = "ON";
      oFilter.className="skyBlueLink";
    }
    else
    {
      oFilter.innerHTML = "OFF";
      oFilter.className="redLink";
    }
  }
  var oExpires = new Date();
  oExpires.setYear(parseInt(oExpires.getFullYear())+1);
  writeCookie("Selfcastfilter", bValue, oExpires, "/Selfcast");
}

function switchFilter()
{
  switchFilterInternal()
  location.reload(true);
}

function switchFilterInternal()
{
  var oFilter = document.getElementById("filter");
  if(oFilter)
  {
    if(oFilter.innerHTML == "ON")
    {
      setFilter(false);
    }
    else
    {
      setFilter(true);
    }
  }
}

function isFilterOn()
{
  var value = readCookie("Selfcastfilter");
  return value==null || value=='true';
}

function doUnCensor()
{
  if(isFilterOn() && confirm("This video has been marked as adult.  To view this video, you must turn the safe filter off.  SelfcastTV's safe filter blocks unsuitable videos.  If you choose to turn the filter off, unfiltered content will be available, leaving SelfcastTV with no control over the content returned.\n\nPlease select OK to turn the filter off, or Cancel to leave the filter on."))
  {
    setFilter(false);
    location.reload(true);
  }
}
var sAdultConfirmText = "Your search may return unsuitable results.  SelfcastTV's safe filter blocks unsuitable content from the Internet.  If you choose to turn the filter off, unfiltered content will be available, leaving SelfcastTV with no control over the content returned.\n\nPlease select OK to turn the filter off, or Cancel to leave the filter on.";

function submitQueryForm(sTextID)
{
  var oSearchBox = document.getElementById(sTextID);
  if(oSearchBox)
  {
    if(containsStopWords(oSearchBox.value) && isFilterOn() && confirm(sAdultConfirmText))
    {
      switchFilterInternal();
    }
    return true;
  }
  return false;
}

function submitQuery(sTextID)
{
  if(submitQueryForm(sTextID)) document.forms['queryForm'].submit();
}