/* 
   Project: Thumbshot Preview Bubble 
			www.thumbshots.de
   Author:  Kristian Fischer
   Date:    January 2010
   Version: 1.0.001

*/

var ThumbsConfig = {
// -------------------------------------------------------------------------------------------------------------------------------------
// Konfiguration, Hilfe finden Sie in der beiliegenden Datei konfiguration.txt
// -------------------------------------------------------------------------------------------------------------------------------------
   LinkClass:     "preview",  //    z.B: <href="/some_page.html" class="preview">Link</a> (Sternchen * statt "preview" für alle Links ausgeben)

// LinkClass:     "*",  	  //    z.B: <href="/some_page.html" class="preview">Link</a> (Sternchen * statt "preview" für alle Links ausgeben)

   LinkTarget:    "extern",   //    aktiviert nur externe Links, leerlassen fuer alle Links 
   LinkButton:    "off",       //    on erstellt einen Button neben dem Link off aktiviert den Link selbst
// -------------------------------------------------------------------------------------------------------------------------------------
// -------------------------------------------------------------------------------------------------------------------------------------
//
//
// -------------------------------------------------------------------------------------------------------------------------------------
// -------------------------------------------------------------------------------------------------------------------------------------
//  Erweiterte Konfiguration fuer Profiaccounts von ThumbShots.de  (http://www.thumbshots.de/artikel-2-thumbshot_profi_accounts.html)
// -------------------------------------------------------------------------------------------------------------------------------------
// -------------------------------------------------------------------------------------------------------------------------------------
  
   Server:            "thumb.thumbshots.de",		//    nach Anmeldung zwischen "" eintragen z.B.:  c1234567.thumbshots.de
   Pfad:              "/cgi-bin/thumbs.fcgi",		//    /cgi-bin/thumbs.fcgi oder /freie_wortwahl/
   Ausgabeformat:     250,							//    120 150 200 250 300
   Seitenformat:      "1024_768",					//    800_600 1024_768 1240_1024
   Qualitaet:         100,							//    100 95 90 85 80 75 70 65 60
   Farbtiefe:         32,							//    8 16 24 32 
   Border:            "off",						//    off 1 2 3 4 5 6
   Boderfarbe:        "cccccc",						//    #000000 - #ffffff 
   Wartezeit:         5,							//    1 - 60
   Maximalzeit:       30,							//    10 - 180
   Refresh:           30,							//    30 => 30 Tage
   Robot:             "on",							//    on off
   Alternative:       ""                            //    http://www.target.tld/path/image.jpg

// ----------------------------------- Grösse 150 pix mit Bild als Hintergrund -------------------------------------------------------  

// Server:            "thumb.thumbshots.de",		//    z.B.:  c1234567.thumbshots.de
// Pfad:              "/cgi-bin/thumbs.fcgi",		//    /cgi-bin/thumbs.fcgi oder /freie_wortwahl/
// Ausgabeformat:     150,							//    120 150 200 250 300
// Seitenformat:      "1024_768",					//    800_600 1024_768 1240_1024
// Qualitaet:         100,							//    100 95 90 85 80 75 70 65 60
// Farbtiefe:         32,							//    8 16 24 32 
// Border:            "off",						//    off 1 2 3 4 5 6
// Boderfarbe:        "cccccc",						//    #000000 - #ffffff 
// Wartezeit:         5,							//    1 - 60
// Maximalzeit:       30,							//    10 - 180
// Refresh:           30,							//    30 => 30 Tage
// Robot:             "on",							//    on off
// Alternative:       ""                            //    http://www.target.tld/path/image.jpg

// ------------------------------ hier sind die standard Einstellungen auf Thumbs von www.thumbshots.de     --------------------------

//    Server:            "www.thumbshots.de",			//    z.B.:  c1234567.thumbshots.de
//    Pfad:              "/cgi-bin/show.cgi",			//    /cgi-bin/thumbs.fcgi oder /freie_wortwahl/
//    Ausgabeformat:     120,							//    120 150 200 250 300
//    Seitenformat:      "1024_768",					//    800_600 1024_768 1240_1024
//    Qualitaet:         90,							//    100 95 90 85 80 75 70 65 60
//    Farbtiefe:         16,							//    8 16 24 32 
//    Border:            "off",							//    off 1 2 3 4 5 6
//    Boderfarbe:        "cccccc",						//    #000000 - #ffffff 
//    Wartezeit:         5,								//    1 - 60
//    Maximalzeit:       30,							//    10 - 180
//    Refresh:           30,							//    30 => 30 Tage
//    Robot:             "on",							//    on off
//    Alternative:       ""                             //    http://www.target.tld/path/image.jpg
// --------------------------------------------------------------------------------------------------------------------------------------

};

var BaseFunctions = {
   addEvent: function(objRef, eventType, functionToCall, captureSwitch) {
      if (objRef.addEventListener) {
         objRef.addEventListener(eventType, functionToCall, captureSwitch);
	 return true;
      } else if (objRef.attachEvent) {
         return objRef.attachEvent("on"+eventType, functionToCall);
      } else {
         return false;
      }
   },
   removeEvent: function(objRef, eventType, functionToCall, captureSwitch) {
      if (objRef.removeEventListener) {
         objRef.removeEventListener(eventType, functionToCall, captureSwitch);
         return true;
      } else if (objRef.detachEvent) {
         return objRef.detachEvent("on"+eventType, functionToCall);
      } else {
         return false;
      }
    },
    findPos: function(obj){
        var curleft = curtop = 0;
	if(obj.offsetParent){
	    curleft = obj.offsetLeft
	    curtop = obj.offsetTop
	    while(obj = obj.offsetParent){
	        curleft += obj.offsetLeft;
		curtop  += obj.offsetTop;
	    }
	}
	return [curleft,curtop];
    },
    catchMouseEvent: function (e) {
        var targ;
	if(!e){
	    var e = window.event;
	}
	if(e.target){
	    targ = e.target;
	}else if(e.srcElement){
	    targ = e.srcElement;
	}
	if(targ){
	    if((targ.nodeType)&&(targ.nodeType == 3)){
	        targ  = targ.parentNode;
	    }
	}
	return targ;
    }
};

var ThumbsPreview = {
   init: function () {
	ThumbsPreview.selfURL = window.location.href;
	var parts = ThumbsPreview.selfURL.split("/");
	ThumbsPreview.selfBase = parts[0] + "//" + parts[2];
	BaseFunctions.addEvent(window, "load", ThumbsPreview.populate, false);
    },
    populate: function () {
	var res = ThumbsPreview.activateLinks(document.body);
    },
    activateLinks: function (thisNode) {

	var catchThis = /^a/i;
	var evRes;
	if(catchThis.test(thisNode.tagName)){

	    if (thisNode.hasChildNodes) {
	        var noText = 0;
		for (var i=0; i<thisNode.childNodes.length; i++) {
		    if(thisNode.childNodes[i].nodeType != 3){
		       noText = 1; break;
	            }
		}
		if(noText){ return; }
	    }
	    if((thisNode.className != ThumbsConfig.LinkClass)&&(ThumbsConfig.LinkClass != "*")){
		return;
	    }

	    if(ThumbsConfig.LinkTarget == "extern"){
	       if((thisNode.href.indexOf('http') != 0) || (thisNode.href.indexOf(ThumbsPreview.selfBase) == 0)){
		   return;	    
	       }
	    }

	    if(thisNode.className == "preview_link"){
		return;
	    }
	    if(thisNode.className == "preview_active"){
		return;
	    }

            thisNode.className = "preview_active";
	    if(ThumbsConfig.LinkButton != "on"){
	       evRes = BaseFunctions.addEvent(thisNode, "mouseover", ThumbsPreview.mouseOverLinks, false);
	       evRes = BaseFunctions.addEvent(thisNode, "mouseout", ThumbsPreview.mouseOutLinks, false);
	       evRes = BaseFunctions.addEvent(thisNode, "click", ThumbsPreview.mouseClickLinks, false);
	    } else {
	       var targetNode    = thisNode.parentNode;
	       var thisA         = document.createElement("a");
	       thisA.href        = thisNode.href;
	       thisA.className   = "preview_link";
	       var thisTXT       = document.createTextNode(' ');		
	       thisA.appendChild(thisTXT);

	       targetNode.replaceChild(thisA,thisNode); 
	       targetNode.insertBefore(thisNode,thisA);

	       evRes = BaseFunctions.addEvent(thisA, "mouseover", ThumbsPreview.mouseOverLinks, false);
	       evRes = BaseFunctions.addEvent(thisA, "mouseout", ThumbsPreview.mouseOutLinks, false);
	       evRes = BaseFunctions.addEvent(thisA, "click", ThumbsPreview.mouseClickLinks, false);
            }
            return;
	}
	if (thisNode.hasChildNodes) {
            for (var i=0; i<thisNode.childNodes.length; i++) {
	       var res = ThumbsPreview.activateLinks(thisNode.childNodes[i]);
	    }
	}
    },
    mouseOutLinks: function (e) {
 	var res  = PopUpFunctions.dispatch('out');
	return true;	        
    },
    mouseOverLinks: function (e) {
        var targ = BaseFunctions.catchMouseEvent(e);
	var pos  = BaseFunctions.findPos(targ);
	if(targ != ThumbsPreview.selfURL){
	    var res  = PopUpFunctions.dispatch('over', targ, pos);
	}	    
	return true;	        
    },
    mouseClickLinks: function (e) {
	var res  = PopUpFunctions.dispatch('out');
	return true;	        
    }
};
var PopUpFunctions = {
        dispatch: function(cmd,url,mpos){
           if(cmd == "over"){
              var p_size = PopUpFunctions.calcPopupSize();
	      PopUpFunctions.showPreviewPopup(url,mpos,p_size);
	   }
           if(cmd == "out"){
	      PopUpFunctions.closeInfoPopup();
	   }
	},
	calcPopupSize: function(){
           var x_size = ThumbsConfig['Ausgabeformat'];
           var y_size = (x_size / 4 * 3);
	   x_size = x_size + 20;
	   y_size = y_size + 20;
          return [x_size,y_size];            
	},
	closeInfoPopup: function() {
	   var pNode  = document.getElementsByTagName("body")[0];
	   var divs   = document.getElementsByTagName("div");
	   var this_div = false;

	   for(var i=0;i<divs.length;i++){
	      this_div = divs[i];
	      if(this_div.className == "PreviewPopUp"){
	         pNode.removeChild(this_div);
	      }
	   }	   
	   return false;
	},
	showPreviewPopup: function(url,mpos,wsize) {
	   var pcoords = PopUpFunctions.calcPopupPosition(mpos,wsize);
	   var pos_x   = pcoords[0]  + "px";
	   var pos_y   = pcoords[1]  + "px";

	   var thisDiv = document.createElement("div");
	   thisDiv.className = "PreviewPopUp";

	   thisDiv.style.position = "absolute";
	   thisDiv.style.width    = wsize[0] + "px";
	   thisDiv.style.height   = wsize[1] + "px";
	   thisDiv.style.top      = pos_y;
	   thisDiv.style.left     = pos_x;
	   thisDiv.style.display  = "inline";

	   var thisP   = document.createElement("p");
	   thisP.className = "PopUpHeader";
	   var thisTXT = document.createTextNode("Seiten - Vorschau");	   
	   thisP.appendChild(thisTXT);
	   thisDiv.appendChild(thisP);
	   
	   var thisP   = document.createElement("p");
	   thisP.className = "PopUpThumbshot";
	   var thisImg = document.createElement("img");
	   thisImg.src = "http://" + ThumbsConfig['Server'] + ThumbsConfig['Pfad'] + "?thumb_size=" + ThumbsConfig['Ausgabeformat'] + "&page_size=" + ThumbsConfig['Seitenformat'] + 
	   "&quality=" + ThumbsConfig['Qualitaet'] + "&max_time=" + ThumbsConfig['Maximalzeit'] + "&wait_time=" + ThumbsConfig['Wartezeit'] + "&use_robot=" + ThumbsConfig['Robot'] + 
	   "&add_border=" + ThumbsConfig['Border'] + "&border_color=" + ThumbsConfig['Boderfarbe'] + "&color_depth=" + ThumbsConfig['Farbtiefe'] + "&refresh_time=" + ThumbsConfig['Refresh'] + 
	   "&url=" + encodeURIComponent(url);
	   thisP.appendChild(thisImg);

	   thisDiv.appendChild(thisP);

	   document.getElementsByTagName("body")[0].appendChild(thisDiv);	   	   
	   return false;
	   
	},
	calcPopupPosition: function(mcoords,winsize){
	   var xoff = yoff = xwin = ywin = xpos = ypos = 0;

	   if(window.pageYOffset){
	      yoff = window.pageYOffset;
	   }else if(document.documentElement && document.documentElement.scrollTop){
	      yoff = document.documentElement.scrollTop;
	   }else if(document.body && document.body.scrollTop){
	      yoff = document.body.scrollTop;
	   }

	   if(window.pageXOffset){
	      xoff = window.pageXOffset;
	   }else if(document.documentElement && document.documentElement.scrollLeft){
	      xoff = document.documentElement.scrollLeft;
	   }else if(document.body && document.body.scrollLeft){
	      xoff = document.body.scrollLeft;
	   }

	   if(self.innerHeight){
	      xwin = self.innerWidth;
	      ywin = self.innerHeight;
	   }else if (document.documentElement && document.documentElement.clientHeight){
	      xwin = document.documentElement.clientWidth;
	      ywin = document.documentElement.clientHeight;
	   }else if(document.body){
	      xwin = document.body.clientWidth;
	      ywin = document.body.clientHeight;
	   }

	   if((mcoords[1] - yoff - winsize[1] - 35) < 0){
	      ypos = mcoords[1] + 40;
	   } else {
	      ypos = (mcoords[1] - (40 + winsize[1]));
	   }

	   if((mcoords[0] - xoff + (winsize[0] / 2)) > xwin){
	      xpos = (xwin - 20 - winsize[0] + xoff);	
	   } else if((mcoords[0] - xoff - (winsize[0] / 2)) < 0){
	      xpos =  20 + xoff;
	   } else{
	      xpos = mcoords[0] - (winsize[0] / 2);
	   }

	   return [xpos,ypos];
	},
	uriEncodeString: function(txt){
	   if(typeof encodeURIComponent=="function"){
	      return encodeURIComponent(txt);
	   }else{
	      return escape(txt);
	   }
	}
};
ThumbsPreview.init();

