var bubblediv=document.createElement("div");
var bubbleimg=document.createElement("img");

function initPreviewBubble() {
  if (!document.getElementsByTagName){
    return;
  }
  if (navigator.userAgent.toLowerCase().indexOf( "msie" ) != -1 ) {
    bubblediv.style.width="194px";
    bubblediv.style.position="absolute";
    bubblediv.style.top="-1000px";
    bubblediv.style.zIndex=99999;
    bubblediv.style.left="-1000px";
    bubblediv.style.textAlign="left";
    bubblediv.style.height="161px";
    bubblediv.style.paddingTop="0";
    bubblediv.style.paddingLeft="0";
    bubblediv.style.paddingBottom="0";
    bubblediv.style.paddingRight="0"; 
    bubblediv.style.marginTop="0";
    bubblediv.style.marginLeft="0";
    bubblediv.style.marginBottom="0";
    bubblediv.style.marginRight="0";
    bubblediv.style.filter="progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + basepath + "img/previewbubble2.png',sizingMethod='image')";

    bubbleimg.style.paddingTop="0";
    bubbleimg.style.paddingLeft="0";
    bubbleimg.style.paddingBottom="0";
    bubbleimg.style.paddingRight="0";
    bubbleimg.style.margin="auto";
    bubbleimg.style.marginTop="24px";
    bubbleimg.style.marginLeft="17px";
    bubbleimg.style.marginBottom="0";
    bubbleimg.style.marginRight="0";
    bubbleimg.style.borderTop="0";
    bubbleimg.style.borderLeft="0";
    bubbleimg.style.borderBottom="0";
    bubbleimg.style.borderRight="0";
  }
  else {
    bubblediv.setAttribute("style","text-align: left; z-index: 99999; position: absolute; top: -1000px; left: -1000px; background: url("+ basepath + "img/previewbubble2.png) no-repeat; width: 194px; height: 161px; padding: 0; margin: 0;");
  }

  bubbleimg.setAttribute("style","padding-top: 0; padding-left: 0; padding-right: 0; padding-bottom: 0; margin-top: 24px; margin-left: 17px; margin-bottom: 0; margin-right: 0; border: 0");
  bubbleimg.setAttribute("src", "/img/previewbubble-loader.gif");
  bubbleimg.setAttribute("width",160);
  bubbleimg.setAttribute("height",120);
  bubbleimg.setAttribute("alt","");
  
  bubblediv.appendChild(bubbleimg);

  document.getElementsByTagName("body")[0].appendChild(bubblediv);

  var anchors = document.getElementsByTagName('a');
  for (var i=0; i<anchors.length; i++){
    if (anchors[i].getAttribute('previewlink')){
      anchors[i].onmouseover = function() { showPreviewBubble(this, String(this.getAttribute('previewlink')), false); }
      anchors[i].onmouseout = function() { hidePreviewBubble(this); }
    }
  }
}
function showPreviewBubble(elem, url, local){
  var bubbleimage = "http://www.mythumbshot.com/get?size=m&url=" + encodeURIComponent(url);

  if (bubbleimg.src != bubbleimage) {
    bubbleimg.src = basepath + "img/previewbubble-loader.gif";
    bubbleimg.src = bubbleimage;
  }
  var pos = bytefx.$position(elem);
  pos.x -= 8;
  pos.y += 14;

  if (navigator.userAgent.toLowerCase().indexOf( "msie" ) != -1 ) {
    bytefx.position(bubblediv, pos);
  }
  else {
    bytefx.size$(bubblediv, {width:236,height:1});
    bytefx.position(bubblediv, pos);
    bytefx.size(bubblediv, {width:236,height:193}, 20);
  }
}
function hidePreviewBubble(elem){
  if (navigator.userAgent.toLowerCase().indexOf( "msie" ) != -1 ) {
    var pos = bytefx.$position(elem);
    pos.x -= 1000;
    pos.y -= 1000;
    bytefx.position(bubblediv, pos);
  }
  else {
    bytefx.size( bubblediv, {width:236,height:1}, 20, function(){
      var pos = bytefx.$position(elem);
      pos.x -= 1000;
      pos.y -= 1000;
      bytefx.position(bubblediv, pos);
    });
  }
}



if(window.addEventListener){
  addEventListener("load",initPreviewBubble,false);
}
else{
  attachEvent("onload",initPreviewBubble);
}

