

var supported = /MSIE ((5\.5)|[6])/.test(navigator.userAgent) && navigator.platform == "Win32";
var realSrc;
var blankSrc = "http://www.vodpod.com/images/blank.gif";


var widget = document.getElementById('vodpod_recent_videos_13566');
if (widget == null)
{
  widget = document.getElementById('vodpod_recent_videos');
}
var vp_widget_id = widget.id;

// Hide the widget, until it has been created
widget.style.display = 'none';
widget.className = "vodpod_videos_widget";

// Load style
var cssNode = null;
var styleSheet = null;

  // Kill any existing vodpod css
  cssNode = document.getElementById('vodpod_widget_css');
  if (cssNode)
  {
    cssNode.parentNode.removeChild(cssNode);
  }
  
  cssNode = document.createElement('link');
  cssNode.type = 'text/css';
  cssNode.rel = 'stylesheet';
  cssNode.href = 'http://s3.amazonaws.com/vodpod.com/stylesheets/widgets/sidebar1.css';
  cssNode.media = 'all';
  cssNode.id = 'vodpod_widget_css';
  document.getElementsByTagName("head")[0].appendChild(cssNode);
  if (cssNode.sheet)
  {
    styleSheet = cssNode.sheet;
  }
  else
  {
    styleSheet = cssNode.styleSheet;
  }
  vp_createWidget();


function vp_isCSSLoaded() {
  // If no cssNode, we're not loading external styles, so return true
  if (cssNode == null)
  {
    return true;
  }
  
  try {
    if (styleSheet.cssRules)
    {
      return (styleSheet.cssRules.length > 0);
    }
    else if (styleSheet.rules)
    {
      return (styleSheet.rules.length > 0);
    }
  } catch(e) {
    return false;
  }
}

// This will display the widget once the stylesheet has been loaded
function vp_createWidgetOnCssLoad(attempt) {
  if (attempt > 25)
  {
    widget.innerHTML = "Error loading VodPod Widget.";
    widget.style.display = 'block';
    return false;
  }
  
  if (vp_isCSSLoaded())
  {
    //console.log("creating widget")
    vp_createWidget();
  } else {
    //console.log("CSS Not loaded: " + attempt);
    setTimeout(function(){vp_createWidgetOnCssLoad(attempt+1)}, 100);
  }
}

function vp_createWidget() {
  
  widget_header = '<div class=\"vodpod_header\" title=\"bwfo\'s video collection\">    <a href=\"http://widgets.vodpod.com/group/13566\" target=\"_new\">bwfo\'s video collection</a></div>';
  
  widget.innerHTML += widget_header;
  
  
  video_html = '<div class="vodpod_video_container" id="vodpod_video_container_13566">';
  
  
  
  video_object_130557 = '<div id=\"embed_holder\" style=\"width:400px;height:320px\">  <embed wmode=\"transparent\" flashvars=\"config=http://www.comedycentral.com/motherload/xml/data_synd.jhtml?vid=88994%26myspace=false&\" src=\'http://www.comedycentral.com/motherload/syndicated_player/index.jhtml\' quality=\'high\' bgcolor=\"363636\" width=\'100%\' height=\'100%\' name=\'comedy_player\' align=\'middle\' allowScriptAccess=\'always\' allownetworking=\'external\' type=\'application/x-shockwave-flash\' pluginspage=\'http://www.macromedia.com/go/getflashplayer\'></embed></div>';
  video_title_130557 = 'The Daily Show - Barack Tones';
  video_views_130557 = '0';
  video_date_130557 = '30 Jun 07';
  
  video_html += '<div class="vodpod_video" id="vodpod_video_130557" onclick="vp_loadVideo(130557);return false;">\
    <a href="#" class="vodpod_video_thumbnail">\
      <img src="http://s3.amazonaws.com/vodpod.com.videos.thumbnail/118752.medium160.jpg" title="" />\
    </a>\
    <div class="vodpod_video_placeholder1"></div>\
    <a href="#" class="vodpod_video_title" title="The Daily Show - Barack Tones">\
      The Daily Show - Barack Tones\
    </a>\
    <div class="vodpod_video_views">\
      <span class="vodpod_video_views_num">0</span>\
      <span class="vodpod_video_views_label">views</span>\
    </div>\
    <div class="vodpod_video_created_at">30 JUN 07</div>\
    <div class="vodpod_video_placeholder2"></div>\
    <div id="vodpod_video_clear"></div>\
  </div>';
  
  
  video_object_119681 = '<div id=\"embed_holder\" style=\"width:400px;height:320px\">  <embed src=\"http://www.youtube.com/v/kmXErDlCguM&autoplay=1\" type=\"application/x-shockwave-flash\" wmode=\"transparent\" width=\"100%\" height=\"100%\"></embed></div>';
  video_title_119681 = 'Obama at Take Back America 2007 Part 1 of 4';
  video_views_119681 = '2';
  video_date_119681 = '23 Jun 07';
  
  video_html += '<div class="vodpod_video" id="vodpod_video_119681" onclick="vp_loadVideo(119681);return false;">\
    <a href="#" class="vodpod_video_thumbnail">\
      <img src="http://s3.amazonaws.com/vodpod.com.videos.thumbnail/108725.medium160.jpg" title="He delivered one of his best speeches since his debut at the 2004 DNC. Most reviewers gave him positive praise and said he outshined the other democratic candidates who spoke. " />\
    </a>\
    <div class="vodpod_video_placeholder1"></div>\
    <a href="#" class="vodpod_video_title" title="Obama at Take Back America 2007 Part 1 of 4">\
      Obama at Take Back America 2007 Part 1 of 4\
    </a>\
    <div class="vodpod_video_views">\
      <span class="vodpod_video_views_num">2</span>\
      <span class="vodpod_video_views_label">views</span>\
    </div>\
    <div class="vodpod_video_created_at">23 JUN 07</div>\
    <div class="vodpod_video_placeholder2"></div>\
    <div id="vodpod_video_clear"></div>\
  </div>';
  
  
  video_object_119679 = '<div id=\"embed_holder\" style=\"width:400px;height:320px\">  <embed src=\"http://www.youtube.com/v/KTsmnNP6Jf0&autoplay=1\" type=\"application/x-shockwave-flash\" wmode=\"transparent\" width=\"100%\" height=\"100%\"></embed></div>';
  video_title_119679 = '';
  video_views_119679 = '0';
  video_date_119679 = '23 Jun 07';
  
  video_html += '<div class="vodpod_video" id="vodpod_video_119679" onclick="vp_loadVideo(119679);return false;">\
    <a href="#" class="vodpod_video_thumbnail">\
      <img src="http://s3.amazonaws.com/vodpod.com.videos.thumbnail/108723.medium160.jpg" title="" />\
    </a>\
    <div class="vodpod_video_placeholder1"></div>\
    <a href="#" class="vodpod_video_title" title="">\
      \
    </a>\
    <div class="vodpod_video_views">\
      <span class="vodpod_video_views_num">0</span>\
      <span class="vodpod_video_views_label">views</span>\
    </div>\
    <div class="vodpod_video_created_at">23 JUN 07</div>\
    <div class="vodpod_video_placeholder2"></div>\
    <div id="vodpod_video_clear"></div>\
  </div>';
  
  
  video_object_119675 = '<div id=\"embed_holder\" style=\"width:400px;height:320px\">  <embed src=\"http://www.youtube.com/v/Li70JXL7xWc&autoplay=1\" type=\"application/x-shockwave-flash\" wmode=\"transparent\" width=\"100%\" height=\"100%\"></embed></div>';
  video_title_119675 = 'Obama at Take Back America 2007 Part 3 of 4';
  video_views_119675 = '0';
  video_date_119675 = '23 Jun 07';
  
  video_html += '<div class="vodpod_video" id="vodpod_video_119675" onclick="vp_loadVideo(119675);return false;">\
    <a href="#" class="vodpod_video_thumbnail">\
      <img src="http://s3.amazonaws.com/vodpod.com.videos.thumbnail/108719.medium160.jpg" title="He delivered one of his best speeches since his debut at the 2004 DNC. Most reviewers gave him positive praise and said he outshined the other democratic candidates who spoke. (more)" />\
    </a>\
    <div class="vodpod_video_placeholder1"></div>\
    <a href="#" class="vodpod_video_title" title="Obama at Take Back America 2007 Part 3 of 4">\
      Obama at Take Back America 2007 Part 3 of 4\
    </a>\
    <div class="vodpod_video_views">\
      <span class="vodpod_video_views_num">0</span>\
      <span class="vodpod_video_views_label">views</span>\
    </div>\
    <div class="vodpod_video_created_at">23 JUN 07</div>\
    <div class="vodpod_video_placeholder2"></div>\
    <div id="vodpod_video_clear"></div>\
  </div>';
  
  
  video_object_119672 = '<div id=\"embed_holder\" style=\"width:400px;height:320px\">  <embed src=\"http://www.youtube.com/v/S34x07CZaE8&autoplay=1\" type=\"application/x-shockwave-flash\" wmode=\"transparent\" width=\"100%\" height=\"100%\"></embed></div>';
  video_title_119672 = 'Obama at Take Back America 2007 Part 4 of 4';
  video_views_119672 = '0';
  video_date_119672 = '23 Jun 07';
  
  video_html += '<div class="vodpod_video" id="vodpod_video_119672" onclick="vp_loadVideo(119672);return false;">\
    <a href="#" class="vodpod_video_thumbnail">\
      <img src="http://s3.amazonaws.com/vodpod.com.videos.thumbnail/108716.medium160.jpg" title="He delivered one of his best speeches since his debut at the 2004 DNC. Most reviewers gave him positive praise and said he outshined the other democratic candidates who spoke." />\
    </a>\
    <div class="vodpod_video_placeholder1"></div>\
    <a href="#" class="vodpod_video_title" title="Obama at Take Back America 2007 Part 4 of 4">\
      Obama at Take Back America 2007 Part 4 of 4\
    </a>\
    <div class="vodpod_video_views">\
      <span class="vodpod_video_views_num">0</span>\
      <span class="vodpod_video_views_label">views</span>\
    </div>\
    <div class="vodpod_video_created_at">23 JUN 07</div>\
    <div class="vodpod_video_placeholder2"></div>\
    <div id="vodpod_video_clear"></div>\
  </div>';
  
  video_html += "<div id=\"vodpod_video_container_clear\"></div>\
  </div>";
  widget.innerHTML += video_html;
  
  
  widget.innerHTML += '<div class="vodpod_footer_separator">&nbsp;</div>\
  <div class="vodpod_footer">\
    <a href="http://bwfo.vodpod.com/" target="_new" class="vodpod_see_all" style="display:block;">see all 11 videos &gt;</a>\
    <a href="http://www.vodpod.com/site/register?r=widget" target="_new" class="vodpod_powered_by">\
      get your own <b>vod:pod</b><!--img src="http://widgets.vodpod.com/images/vodpod_logo_white.png" id="powered_by" class="vodpod_powered_by_logo" align="top" style="filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(src=\'http://widgets.vodpod.com/images/vodpod_logo_white.png\',sizingMethod=\'crop\');"/-->\
    </a>\
    <div id="vodpod_footer_clear"></div>\
  </div>';
   
  
  
  
  // Set custom widget color
  widget_header = ''
  
      els = vp_getElementsByClassName(widget.parentNode, 'vodpod_footer');
      for (i = 0; i<els.length; i++)
      {
        els[i].style.background = '#777777';
      }
  
      els = vp_getElementsByClassName(widget.parentNode, 'vodpod_header');
      for (i = 0; i<els.length; i++)
      {
        els[i].style.background = '#777777';
      }
  
      els = vp_getElementsByClassName(widget.parentNode, 'vodpod_videos_widget');
      for (i = 0; i<els.length; i++)
      {
        els[i].style.border = '2px solid #777777';
      }
  
      els = vp_getElementsByClassName(widget.parentNode, 'vodpod_video_placeholder1');
      for (i = 0; i<els.length; i++)
      {
        els[i].style.background = '#777777';
      }
  
  
  widget.style.display = 'block';
  
}

function vp_loadVideo(id) {
  vid_html = eval('video_object_'+id);
  var vodpod_video = document.getElementById('vodpod_video_' + id);
  var video_div = document.createElement('div');
  video_div.id = "vodpod_player_container";
  video_div.style.position = 'absolute';
  video_div.style.border = "2px solid #777777";
  video_div.style.width = '400px';
  video_div.style.zIndex = '10000';
  video_div.style.top = vp_offsetTop(vodpod_video) + 'px';
  //video_div.style.left = vp_offsetLeft(vodpod_video) + 'px';
  video_div.innerHTML = '<div style="background:#777777;font:14px arial;padding:3px;height:22px;color:#fff;overflow:hidden;">\
    <div style="float:left;padding:3px;width:350px;height:15px;overflow:hidden;margin:0px;">' + eval('video_title_'+id) +
    '<img src="http://www.vodpod.com/images/external_view.gif?id=' + id + '&' + parseInt(Math.random()*50000) + '"/></div>\
    <a href="#" onclick="vp_closeVideo();return false;" style="display:block;float:right;color:#fff;margin:0px;padding:0px;">\
      <img src="http://widgets.vodpod.com/images/close_orange.png" style="border:none;margin:3px;"/>\
    </a>\
    <div style="clear:both;"></div>\
    </div>';
  
  // Determine if widget is on right or left side of the page
  var widget_left = vp_offsetLeft(widget);
  var widget_width = widget.offsetWidth;
  var widget_right = widget_left + widget_width;
  var video_thumb_left = vp_offsetLeft(vodpod_video);
  
  // If widget is horizontally positioned (width > 400)
  // then display the videos from left to right
  if (widget_width > 400)
  {
    // Don't let video extend beyond widget
    if ((video_thumb_left + 400) > widget_right)
    {
      video_left = widget_right - 400;
    } else {
      video_left = video_thumb_left;
    }
  }
  else 
  {
    // Otherwise, if the widget is vertical, figure out what side of the page it's on,
    // and display the videos accordingly
    if (widget_left < document.body.clientWidth/2)
    {
      // left
      video_left = widget_left + widget.offsetWidth;
    } else {
      // right
      video_left = widget_left - 400 - 2;
    }
    
  }
  
  video_div.style.left = video_left + "px";
  video_div.innerHTML += '<div style="background:#fff;">' + vid_html + '</div>\
    <div style="color:#ddd;height:20px;line-height:20px;background:#777777;text-align:right;">\
      <div style="float:left;">\
        Viewed ' + eval('video_views_'+id) + ' times since ' + eval('video_date_'+id) + '</div>\
      <div style=\"float:right;\">\
        <a href=\"http://www.vodpod.com\" style="color:#fff;text-decoration:none;" target="_new">\
          powered by <img src="http://widgets.vodpod.com/images/vodpod_trans_logo_14px.png" align="top" style="display:inline;border:none;padding-top:2px;filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(src=\'http://widgets.vodpod.com/images/vodpod_trans_logo_14px.png\',sizingMethod=\'crop\');"/>\
        </a>\
      </div>\
      <div style="clear:both"></div>\
    </div>';
  
  // Clear the previous embed holder
  vp_closeVideo();
  
  document.body.appendChild(video_div);
  
  //for(i=0;i<video_left;i++)
  //{
  //  setTimeout('video_div.style.left = i + "px"', 100);
  //}  
}

function vp_closeVideo() {
  embed_holder = document.getElementById('vodpod_player_container');
  if (embed_holder != null)
    embed_holder.parentNode.removeChild(embed_holder);
}

function vp_offsetTop(obj) {
    curtop = 0;
    if (obj.offsetParent) {
    curtop = obj.offsetTop
    while (obj = obj.offsetParent) {
      curtop += obj.offsetTop
    }
  }
  return curtop;
}

function vp_offsetLeft(obj) {
  curtop = 0;
  if (obj.offsetParent) {
    curtop = obj.offsetLeft;
    while (obj = obj.offsetParent) {
      curtop += obj.offsetLeft;
    }
  }
  return curtop;
}

function vp_getElementsByClassName(node, classname)
{
    var a = [];
    var re = new RegExp('\\b' + classname + '\\b');
    var els = node.getElementsByTagName("*");
    for(var i=0,j=els.length; i<j; i++)
        if(re.test(els[i].className))a.push(els[i]);
    return a;
}

