function $(s)
{
	return document.getElementById(s);
} 
function formatTime(s)
{
tsPub = Date.parse(s);
cur = new Date();
tsCur = cur.getTime();
res = new Date();
res.setTime(tsPub);
dif = Math.ceil((tsCur-tsPub)/1000);
if (dif > 60*60*24*7) return res.toLocaleDateString();
var r = "";
days = Math.floor(dif/60/60/24);
if (days == 1) r = days + " dag";
else if (days > 1) r = days + " dagar";
dif = dif - days*60*60*24;
hours = Math.floor(dif/60/60);
if (hours > 0)
{
r += (r != "")? ", ":"";
if (hours == 1) r += hours + " timme";
else if (hours > 1) r += hours + " timmar";
}
dif = dif - hours*60*60;
mins = Math.floor(dif/60);
if (mins > 0)
{
r += (r != "")? ", ":"";
if (mins == 1) r += mins + " minut";
else if (mins > 1) r += mins + " minuter";
}
return "F&ouml;r " + r + " sedan"; 
}

var myvs = null; 
var timeout;  
function initVideo()
{
  myvs = new videoSearch();
  myvs.init();  
  myvs.execute();
}
function initVideoL()
{
  myvs = new videoSearch();
  myvs.initL();  
  myvs.execute(); 
}
function videoSearch()
{
  this.loc = $("vidList");
  this.searcher = "";
  this.init = function()
  {
    var s = new google.search.VideoSearch();
    s.setResultOrder(google.search.Search.ORDER_BY_DATE);
    s.setResultSetSize(google.search.Search.LARGE_RESULTSET);
    s.setUserDefinedLabel("Video");
    s.setNoHtmlGeneration();
    s.setSearchCompleteCallback(this, this.searchComplete);
    this.searcher = s;
  }
  this.initL = function()
  {
    var s = new google.search.VideoSearch();
    s.setResultOrder(google.search.Search.ORDER_BY_DATE);
    s.setResultSetSize(google.search.Search.LARGE_RESULTSET);
    s.setUserDefinedLabel("Video");
    s.setNoHtmlGeneration();
    s.setSearchCompleteCallback(this, this.searchCompleteL);
    this.searcher = s;
  }
  this.execute = function()
  {
    s = $("q").value;
    this.searcher.execute(s);
  } 
  this.page = function(i)
  {
    this.searcher.gotoPage(i);
  } 
  this.searchComplete = function()
  {
    s = this.searcher;
    if (s.results && s.results.length > 0) 
    {
      var html = "<table class='vlist'>";
      for (i=0;i<s.results.length;i++)
      {
        r = s.results[i];
        tw = Math.min(r.tbWidth, 130);
        th = Math.min(r.tbHeight, 100);
        pub = formatTime(r.published);
        html += "<tr><td><a href='javascript:showVid(\"" + r.playUrl + "\")'><img src='" + r.tbUrl + "' height=" + th + " width=" + tw + "></a></td>";
        html += "<td><a href='javascript:showVid(\"" + r.playUrl + "\")'>" + r.title + "</a><br><span class=\"time\">" + pub + "</span><br>" + r.content + "</td></tr>";
      }
      if (s.cursor)
      {
        html += "<tr><td colspan='2' align='left'><p class='pagenav'><strong>Sidor:</strong> ";
        for (i=0;i<s.cursor.pages.length;i++)
        {
          if (s.cursor.currentPageIndex == i)
            html += "<span class='sel'>" + s.cursor.pages[i].label + "</span>";
          else
            html += "<span onMouseOver=\"this.className='hover'\" onMouseOut=\"this.className=''\" onMouseUp=\"vPage(" + i + ")\"><a href='javascript:vPage(" + i + ")'>" + s.cursor.pages[i].label + "</a></span>";
        }
        html += "<a target='_blank' href='" + s.cursor.moreResultsUrl + "'>Fler resultat &raquo;</a></p></td></tr>";
      }
      html += "</table>";
      this.loc.innerHTML = html;
    }
    else this.loc.innerHTML = "Hittade ingen relaterad video...";
  } 
  this.searchCompleteL = function()
  {
    s = this.searcher;
    if (s.results && s.results.length > 0) 
    {
      var html = "<table class='vlist'>";
      for (i=0;i<3;i++)
      {
        r = s.results[i];
        tw = Math.min(r.tbWidth, 130);
        th = Math.min(r.tbHeight, 100);
        pub = formatTime(r.published);
        html += "<tr><td><a href='javascript:showVid(\"" + r.playUrl + "\")'><img src='" + r.tbUrl + "' height=" + th + " width=" + tw + "></a></td>";
        html += "<td><a href='javascript:showVid(\"" + r.playUrl + "\")'>" + r.title + "</a><br><span class=\"time\">" + pub + "</span><br>" + r.content + "</td></tr>";
      }
      html += "</table>";
      this.loc.innerHTML = html;
    }
  }
}
function vPage(i) {myvs.page(i);}
function doV(){myvs.execute();}
function vSearch(){if(timeout) clearTimeout(timeout);timeout = setTimeout("doV()",300);}
function showVid(url){url += "&autoPlay=true";$("vidPlay").innerHTML = "<embed style='border: 1px solid #000' type='application/x-shockwave-flash' height='380px' width='480px' src='" + url + "'>";$("vidpWrap").style.display = 'block';$("vidlWrap").style.display = 'none';}
function closeVid(){$("vidpWrap").style.display = 'none';$("vidPlay").innerHTML = "";$("vidlWrap").style.display = 'block';}
function pretext(s){$("q").value=s;vSearch()}
