/*
link-preview v1.4 by frequency-decoder.com
Released under a creative commons Attribution-ShareAlike 2.5 license (http://creativecommons.org/licenses/by-sa/2.5/)
Please credit frequency-decoder in any derivative work - thanks.
You are free:
  * to copy, distribute, display, and perform the work
  * to make derivative works
  * to make commercial use of the work

Under the following conditions:
  by Attribution.
  --------------
  You must attribute the work in the manner specified by the author or licensor.

  sa
  --
  Share Alike. If you alter, transform, or build upon this work, you may distribute the resulting work only under a license identical to this one.

* For any reuse or distribution, you must make clear to others the license terms of this work.
* Any of these conditions can be waived if you get permission from the copyright holder.

References:
Dustan Diaz: http://www.dustindiaz.com/sweet-titles-finalized
Arc90: http://lab.arc90.com/2006/07/link_thumbnail.php
*/
var webShot = {
  x:0,
  y:0,
  obj:{},
  img:null,
  lnk:null,
  timer:null,
  opacityTimer:null,
  errorTimer:null,
  hidden:true,
  linkPool: {},
  baseURI: "/img/",
  imageCache: [],
  init: function() {
    var lnks = document.getElementsByTagName('a');
    var i = lnks.length || 0;
    var cnt = 0;
    while(i--) {
		if (lnks[i].getAttribute('noshot')!='1' && lnks[i].href.indexOf('mailto',0)==-1 && lnks[i].href.indexOf('javascript',0)==-1){
        webShot.addEvent(lnks[i], ["focus", "mouseover"], webShot.initThumb);
        webShot.addEvent(lnks[i], ["blur",  "mouseout"],  webShot.hideThumb);
        webShot.linkPool[lnks[i].href] = cnt++;
      }
    }
    if(cnt) {
      webShot.preloadImages();
      webShot.obj = document.createElement('div');

      webShot.ind = document.createElement('div');
      webShot.ind.className= "imageLoaded";
      webShot.img = document.createElement('img');
      webShot.img.alt = "preview";
      webShot.img.id = "fdImage";
      webShot.addEvent(webShot.img, ["load"], webShot.imageLoaded);
      webShot.addEvent(webShot.img, ["error"], webShot.imageError);
      webShot.obj.id = "fdImageThumb";
      webShot.obj.style.visibility = "hidden";
      webShot.obj.style.top = "0";
      webShot.obj.style.left = "0";
      webShot.addEvent(webShot.img, ["mouseout"],  webShot.hideThumb);
      webShot.obj.appendChild(webShot.ind);
      webShot.obj.appendChild(webShot.img);
      webShot.addEvent(webShot.obj, ["mouseout"], webShot.hideThumb);
      document.getElementsByTagName('body')[0].appendChild(webShot.obj);
    }
  },
  preloadImages: function() {
    var imgList = ["lt.png", "lb.png", "rt.png", "rb.png", "error.gif", "loading.gif"];
    var imgObj  = document.createElement('img');

    for(var i = 0, img; img = imgList[i]; i++) {
      webShot.imageCache[i] = imgObj.cloneNode(false);
      webShot.imageCache[i].src = webShot.baseURI + img;
    }
  },
  imageLoaded: function() {
    if(webShot.errorTimer) clearTimeout(webShot.errorTimer);
    if(!webShot.hidden) webShot.img.style.visibility = "visible";
    webShot.ind.className= "imageLoaded";
    webShot.ind.style.visibility = "hidden";
  },
  imageError: function(e) {
    if(webShot.errorTimer) clearTimeout(webShot.errorTimer);
    webShot.ind.className= "imageError";
    webShot.errorTimer = window.setTimeout("webShot.hideThumb()",2000);
  },
  initThumb: function(e) {
    e = e || event;

    webShot.lnk       = this;
    var positionClass  = "left";

    var heightIndent;
    var indentX = 0;
    var indentY = 0;
    var trueBody = (document.compatMode && document.compatMode!="BackCompat")? document.documentElement : document.body;

    if(String(e.type).toLowerCase().search(/mouseover/) != -1) {
      if (document.captureEvents) {
        webShot.x = e.pageX;
        webShot.y = e.pageY;
      } else if ( window.event.clientX ) {
        webShot.x = window.event.clientX+trueBody.scrollLeft;
        webShot.y = window.event.clientY+trueBody.scrollTop;
      }
      indentX = 10;
      heightIndent = parseInt(webShot.y-(webShot.obj.offsetHeight))+'px';
    } 
    else {
      var obj = this;
      var curleft = curtop = 0;
      if (obj.offsetParent) {
        curleft = obj.offsetLeft;
        curtop = obj.offsetTop;
        while (obj = obj.offsetParent) {
          curleft += obj.offsetLeft;
          curtop += obj.offsetTop;
        }
      }
      curtop += this.offsetHeight;
    
      webShot.x = curleft;
      webShot.y = curtop;
    
      heightIndent = parseInt(webShot.y-(webShot.obj.offsetHeight)-this.offsetHeight)+'px';
    }
                
    if ( parseInt(trueBody.clientWidth+trueBody.scrollLeft) < parseInt(webShot.obj.offsetWidth+webShot.x) + indentX) {
      webShot.obj.style.left = parseInt(webShot.x-(webShot.obj.offsetWidth+indentX))+'px';
      positionClass = "right";
    } else {
      webShot.obj.style.left = (webShot.x+indentX)+'px';
    }
    if ( parseInt(trueBody.clientHeight+trueBody.scrollTop) < parseInt(webShot.obj.offsetHeight+webShot.y) + indentY ) {
      webShot.obj.style.top = heightIndent;
      positionClass += "Top";
    } else {
      webShot.obj.style.top = (webShot.y + indentY)+'px';
      positionClass += "Bottom";
    }

    webShot.obj.className = positionClass;
    webShot.timer = window.setTimeout("webShot.showThumb()",500);
  },
  showThumb: function(e) {
    webShot.hidden = false;
    webShot.obj.style.visibility = webShot.ind.style.visibility = 'visible';
    webShot.obj.style.opacity = webShot.ind.style.opacity = '0';
    webShot.img.style.visibility = "hidden";
    
    var addy = String(webShot.lnk.href);

    webShot.errorTimer = window.setTimeout("webShot.imageError()",30000);
    webShot.img.src = 'http://www.web-screenshots.com/free/'+ webShot.lnk.href

    /*@cc_on@*/
    /*@if(@_win32)
    return;
    /*@end@*/
    
    webShot.fade(10);
  },
  hideThumb: function(e) {
    // Don't mouseout if over the bubble
    e = e || window.event;

    // Check if mouse(over|out) are still within the same parent element
    if(e.type == "mouseout") {
      var elem = e.relatedTarget || e.toElement;
      if(!elem){
        return
      }
      if(elem.id && elem.id.search("fdImage") != -1) return false;
    }

    webShot.hidden = true;
    if(webShot.timer) clearTimeout(webShot.timer);
    if(webShot.errorTimer) clearTimeout(webShot.errorTimer);
    if(webShot.opacityTimer) clearTimeout(webShot.opacityTimer);
    webShot.obj.style.visibility = 'hidden';
    webShot.ind.style.visibility = 'hidden';
    webShot.img.style.visibility = 'hidden';
    webShot.ind.className= "imageLoaded";
  },
  fade: function(opac) {
    var passed  = parseInt(opac);
    var newOpac = parseInt(passed+10);
    if ( newOpac < 90 ) {
      webShot.obj.style.opacity = webShot.ind.style.opacity = '.'+newOpac;
      webShot.opacityTimer = window.setTimeout("webShot.fade('"+newOpac+"')",20);
    } else {
      webShot.obj.style.opacity = webShot.ind.style.opacity = '1';
    }
  },
  addEvent: function( obj, types, fn ) {
    var type;
    for(var i = 0; i < types.length; i++) {
      type = types[i];
      if ( obj.attachEvent ) {
        obj['e'+type+fn] = fn;
        obj[type+fn] = function(){obj['e'+type+fn]( window.event );}
        obj.attachEvent( 'on'+type, obj[type+fn] );
      } 
      else obj.addEventListener( type, fn, false );
    }
  }
}

webShot.addEvent(window, ['load'], webShot.init);

