/*
 * Copyright (c) 2008-2010 Creative Cruising Concepts and Chris Myles
 *
 * Have a look but you are NOT free to copy this code .
 * it's the only way I make a living While sailing around the world
 * 
 * All Rights Reserved.. No Portion of this code may be copied without the written
 * permission of Creative Cruising Concepts.
 *
 *
 *  If you are interested in custom maps or blog integration contact us via
 *  www.creative-cruising.com 
 */


blurbBits.prototype.getElementsByClass=function(searchClass,node,tag) {
    var classElements = [];
    if (node == null) node = document;
    if (tag == null) tag = '*';
    var els = node.getElementsByTagName(tag);
    var elsLen = els.length;
    var pattern = new RegExp("(^|\\s)"+searchClass+"(\\s|$)");
    for (i = 0, j = 0; i < elsLen; i++) {
      if (pattern.test(els[i].className) ){
        classElements[j] = els[i];
        j++;
      }
    }
    return classElements;
}
// Allows * for search class *,*,* returns body
blurbBits.prototype.getElemsByClass=function(searchClass,node,tag){
    var classElements = [];
    if (node == null || node == "*") node = document;
    if (tag == null) tag = '*';
    if (tag == "*" && searchClass == "*")
       classElements[0] = document.body;
    else if (searchClass == "*"){
      // if you don't copy the elements out of the returned array weird things can happen
      els = node.getElementsByTagName(tag);
      var elsLen = els.length;
      for (i = 0, j = 0; i < elsLen; i++) {
          classElements[j] = els[i];
          j++;
      }
    }else{
      var els = node.getElementsByTagName(tag);
      var elsLen = els.length;
      var pattern = new RegExp("(^|\\s)"+searchClass+"(\\s|$)");
      for (i = 0, j = 0; i < elsLen; i++) {
        if (pattern.test(els[i].className) ){
          classElements[j] = els[i];
          j++;
        }
      }       
    }
    return classElements;
}
     
blurbBits.prototype.strGetLLZ=function(content){
    // look for explicit link
    // look for position
    // explicit links llz=lat,lng,zoom
    var llz = undefined;

    this._illz = 0;
    if ((iChk = content.search(/llz=/))> -1){
         llz = this.getLLZ(iChk,content);
    }
    
    if (this.isFF == 1)
      this._illz = iChk;
      
    return llz;
}

blurbBits.prototype.strGetLatLng=function(content){
    // look for explicit link
    // look for position
    // explicit links llz=lat,lng,zoom
    var llz = undefined;

    if (llz == undefined && (iChk = content.search(/[\s\u003e;]\d+[\d\.]*[\s'\u00b0\u00ba]?[\d\."'\s]*[NSns]+[\s\/\,]*\d+[\d\.]*[\s\u00b0'\u00ba]?[\d\."'\s]*[EWew]+/)) > -1){ 
         llz =  this.getLatLng(iChk,content);
    }          

    return llz;
}
//Mozilla and Opera expect font-size, font-family and background-color.
//IExplorer expects fontSize, fontFamily, backgroundColor, 
blurbBits.prototype.setStyle=function( object, styleText ) { 
    if (object == undefined)
      return;
    if( object.style.setAttribute ) { 
      object.style.setAttribute("cssText", styleText ); 
    } else { 
      object.setAttribute("style", styleText ); 
    } 
}
blurbBits.prototype.winWidth=function() {
  var wWidth = 0;
  if( typeof( window.innerWidth ) == 'number' )
    wWidth = window.innerWidth;
  else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) )
    wWidth = document.documentElement.clientWidth;
  else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) )
    wWidth = document.body.clientWidth;
  return wWidth;
}

blurbBits.prototype.popBlogBit=function(href,calc,e){
    var posx = 0;
    var posy = 0;
    var ihtml = "";
    var ifh = this.pheight;
    var ifw = this.pwidth
    if (calc == 1){
      var tmpv = parseInt(href.substr(href.indexOf("height")+7,4))
      if (!isNaN(tmpv))
        ifh = tmpv;
      tmpv = parseInt(href.substr(href.indexOf("width")+6,4));
      if (!isNaN(tmpv))
        ifw = tmpv;
    }else{
      href = this.bbpath + '?llz=' + href + this.popopt;    
    }
    if (this.mtooldiv == undefined){
       this.mtooldiv = document.createElement("div");
       this.mtooldiv.setAttribute('id', 'bb-pop');
       this.setStyle(this.mtooldiv,'position:absolute;width:' + ifw +'px;height:' + ifh +'px;overflow:hidden;border:1px black solid;background-color: #ffffff;visibility:visible;z-index:'+this.popzi);
       document.body.appendChild(this.mtooldiv);
    }else{
      this.mtooldiv.style.height = ifh + "px";
      this.mtooldiv.style.width = ifw + "px";
    }
    
    if (!e){ var e = window.event;}
    if (e.pageX || e.pageY)
    {
       posx = e.pageX;
       posy = e.pageY;

    }else if (e.clientX || e.clientY){
       
       posx = e.clientX;
       posy = e.clientY;
       if ((navigator.userAgent.indexOf('Opera') < 0) &&
            (navigator.userAgent.indexOf('MSIE') != -1))
       {
         var scrollTop = 0;
         var scrollLeft = 0;
         if (document.documentElement){
           // IE6 +4.01 and user has scrolled
           if (document.documentElement.scrollTop)
             scrollTop = document.documentElement.scrollTop;
           if (document.documentElement.scrollLeft)
             scrollLeft = document.documentElement.scrollLeft;
         }else if (document.body){
           if (document.body.scrollTop)
             scrollTop = document.body.scrollTop;
           if (document.body.scrollLeft)
             scrollLeft = document.body.scrollLeft;
         }
         posx += scrollLeft;
         posy += scrollTop;
       }
    }
    if (posx < 0) posx = 0;
    var wWidth = this.winWidth();
    if (wWidth > 0){
      var calcX = 0;
      calcX = posx + ifw;
      var difX = calcX - wWidth;
      if (difX > 0)
        posx -= (difX + 20);
    }
    posy +=10;
    // pop it up if the llz has changed
 
    if ((this.mtooldiv._href == undefined) | (this.mtooldiv._href != href)){
      ihtml += '<img border=0 src="'+this.rPath+'buttons/close.png" style="position:absolute;right:0px;top:0px;z-index:2;border:0px"';
      ihtml += 'onclick="document.getElementById(\'bb-pop\').style.visibility=\'hidden\'">';
      ihtml += '<iframe id="bb-popf" name="bb-popf" frameBorder=0 scrolling="no" style="width:' + ifw +'px;height:' + ifh +'px;overflow:hidden;border:0px;"';
      ihtml += 'src= "' + href + '&pup=true"></iframe>';
      this.mtooldiv.innerHTML = ihtml;
      this.mtooldiv._href = href;
    }
    this.mtooldiv.style.visibility = "visible";
    this.mtooldiv.style.left = posx + "px";
    this.mtooldiv.style.top =  posy + "px"; 
    if (window.event) e.returnValue = false;   
    return false;
}

blurbBits.prototype.strGetLLZs= function(content){
   var iIndex;
   // look for multiple lat longs
   var sum=0;
   var retvals = [];
   // 2DO store the first one and pass out as LLZ for content 
   while (( iIndex = content.search(/[\s\u003e;]\d+[\d\.]*[\s'\u00b0\u00ba]?[\d\."'\s]*[NS]+[\s\/\,]*\d+[\d\.]*[\s\u00b0'\u00ba]?[\d\."'\s]*[EW]+/)) > -1){ 
      var eIndex = iIndex;
      var ret = this.getLatLng(iIndex,content);
      // associative array to avoid duplicates in static map
      if (retvals[ret] == undefined)
         retvals[ret] = 1;
      else
         retvals[ret] += 1;
      // walk to the end and keep going
      while (eIndex != iIndex+25){
        if (eIndex == content.length) 
           break;       
        if (content.charAt[eIndex] == "E" || content.charAt[eIndex] == "W")
          break;
        eIndex++;
      }
      content = content.substring(eIndex+1);
   }    
   return retvals; 
}

blurbBits.prototype.getNodeValue=function(elem){
  if (elem.firstChild != undefined){
    var n = elem.firstChild;
    while (n != undefined && n.nodeType != 3)
      n = n.nextSibling;
    if (n != undefined)
      return n.nodeValue;
  }
  return undefined;
}

blurbBits.prototype.findGeo=function(){
  var llza = [];
  var elems = this.getElementsByClass("geo",document);
  for (var el = 0; el < elems.length; el++){   
    var elem = elems[el];
    var ll = undefined;
    if (elem.tagName == "ABBR"){
      //<abbr class="geo" title="52.686;-2.193">52.686, -2.193</abbr>
      var lla = elem.title.split(";");
      if (lla.length >1)
        ll = lla[0] + "," + lla[1];           
    }else if (elem.tagName == "SPAN"){
       //<span class='geo'>
       //<span class='latitude'>50.167958</span>; 
       //<span class='longitude'>-97.133185</span>
       //</span>
       var lla = [];
       var tmpe = this.getElementsByClass("latitude",elem,"span")[0];
       if (tmpe != undefined)
          lla[0]=this.getNodeValue(tmpe);
       var tmpe = this.getElementsByClass("longitude",elem,"span")[0];
       if (tmpe != undefined)
          lla[1]=this.getNodeValue(tmpe);

       //<span class="geo">52.686;-2.193</span>
       if (lla.length == 0)
         lla = this.getNodeValue(elem).split(";"); 
       if (lla.length > 1)
         ll = lla[0] + "," + lla[1];     
    }else  if (elem.tagName == "ADDRESS"){
       //<address class="geo">
       //Some text<br>
       //<abbr class="latitude" title="37.801324">N 37° 48.079</abbr> <br>
       //<abbr class="longitude" title="-122.424903">W 122° 25.494</abbr>
       //</address>

       var lla = [];
       var tmpe = this.getElementsByClass("latitude",elem,"abbr")[0];
       if (tmpe != undefined && tmpe.title != undefined && tmpe.title.length > 0)
          lla[0]=tmpe.title;
       var tmpe = this.getElementsByClass("longitude",elem,"abbr")[0];
       if (tmpe != undefined && tmpe.title != undefined && tmpe.title.length > 0)
          lla[1]=tmpe.title;
       
       if (lla.length == 0){
         //<span class='latitude'>50.167958</span>; 
         //<span class='longitude'>-97.133185</span>
         var tmpe = this.getElementsByClass("latitude",elem,"span")[0];
         if (tmpe != undefined)
            lla[0]=this.getNodeValue(tmpe);
         var tmpe = this.getElementsByClass("longitude",elem,"span")[0];
         if (tmpe != undefined)
            lla[1]=this.getNodeValue(tmpe);
       }
       if (lla.length > 1)
          ll = lla[0] + "," + lla[1];
    }
    if (ll != undefined){      
       //2DO handle geo in blog extraction
       if (elem.className.indexOf("blurbbit") < 0)
         this.addBBToElem(elem,ll)
       if (llza[ll] == undefined)
         llza[ll] = 1;
       else
         llza[ll] += 1;
    }
  }
  return llza;
}
blurbBits.prototype.findLoc=function(ret){
  var llza = [];
  if (ret != undefined){
    for (var i in ret){
       if (typeof ret[i] != "function")
         llza[i] = ret[i];
    }
  }
  var elems = this.getElementsByClass("post-location",document);
  for (var el = 0; el < elems.length; el++){   
    var elem = elems[el];
      
    var ll = undefined;
    var tmpe = elem.getElementsByTagName("a")[0];
    if (tmpe != undefined){

      var path;
      if (tmpe.href != undefined && tmpe.href.length > 0)
        path= tmpe.href;
      if (path == undefined)
        continue;
      var iStart = path.indexOf("%40");
      if (iStart > -1){
        // extract values between %40 and & or end
        iStart += 3;
        var iEnd = path.indexOf("&",iStart);
        if (iEnd == -1)
          iEnd = path.length;
        var lls = path.substring(iStart,iEnd);
        ll = lls.split("%2C").join(",");
      }    
    }
    
    if (ll != undefined){      
       this.addBBToElem(elem,ll)
       if (llza[ll] == undefined)
         llza[ll] = 1;
       else
         llza[ll] += 1;
    }
  }
  return llza;
}

blurbBits.prototype.addBBToElem=function(elem,llz){
   var addP = (elem.tagName == "DIV")? true:this.addP;
   if (addP == false && this.bbType == "imap")
      addP= true;
   var newe; 
   if (this.bbType == "imap"){
     if (this.skipimaps != true){
       newe = document.createElement("iframe");
       newe.id = 'ifrm' + this.mCnt++;
       newe.scrolling="no";
       newe.frameBorder=0;
       this.setStyle(newe,'width:'+this.pwidth +'px;height:'+this.pheight + 'px;overflow:hidden;border:0px');  
       newe.src=this.bbpath +'?init=map&llz=' + llz + this.popopt;    
     }              
   }
   else if (this.bbType != "none"){
     var newe=document.createElement('a');
     newe._llz = llz;
     if (this.bbType == "link"){
       newe.setAttribute('href',this.bbpath+'?llz=' + llz + this.popopt);
       newe.target="_new";
     }else{
       newe.setAttribute('href','javascript:void(0)');
       newe.onclick = this.bind(this.popBlogBit,llz,0);
     }
     newe.className = "bb-geo";
     // add icon if defined
     if (this.icon != undefined && this.icon.length > 0){
       var newimg=document.createElement('img');
       this.setStyle(newimg,'border:0px;padding:0px;margin:0px');  
       newimg.src=this.icon;
       newimg.alt="maplink";
       newe.appendChild(newimg);
     }
     if (this.itext != undefined && this.itext.length > 0)
       newe.appendChild(document.createTextNode(" " + this.itext + " "));
     else if (addP != true)
       newe.appendChild(document.createTextNode(" "));
   }
   if (newe != undefined){
     if (addP == true){
       var newp=document.createElement("p");       
       newp.appendChild(newe);
       newe = newp;
     }
     
     // add to node   
     if (elem.tagName == "DIV")
       elem.insertBefore(newe,elem.firstChild);
     else
       elem.parentNode.insertBefore(newe,elem);
   }
}

 
blurbBits.prototype.sElemsText= function(){
   var llza = [];
   var elems = this.getElemsByClass(this.sElemClass,this.sElem,this.sElemType);
     for (var el = 0; el < elems.length; el++){
       var elem = elems[el];
       var content;
       if (elem.nodeType == 1){
         content = elem.innerHTML;
         if (this.sElemClass == "gs-snippet"){
           content = " " + content.replace(/<\/*b>/gi, '');
         }
       }
       else if (elem.nodeType == 3)
         content = elem.nodeValue;
       if (content != undefined){
         var rcontent;              
         var skipmap = false;
         var incFF = 0;        
         
         var llz = this.strGetLLZ(content);

         // extra work for FF iframe removal bug
         if (llz != undefined && this.isFF == 1 && elem._removed != undefined){ 
           var iFP = content.search(/fullpost[^{]/); 
           if (iFP > 0 && iFP < this._illz)      
             incFF = 1;          
         }
         if (llz == undefined && elem._removed != undefined){
           var tnode = elem._removed;
           
           if (tnode.nodeType == 1)
             rcontent = tnode.innerHTML;
           else if (tnode.nodeType == 3)
             rcontent = tnode.nodeValue;   
           llz = this.strGetLLZ(rcontent);     
         }
         if (llz==undefined)
           llz = this.strGetLatLng(content);

         if (llz==undefined && rcontent != undefined)
           llz = this.strGetLatLng(rcontent);

         if (llz != undefined){
           if (llza[llz] == undefined){
              llza[llz] = 1;
           }else
              llza[llz] += 1;
           if (incFF == 0 && this.getElementsByClass("blurbbit",elem).length > 0)
             skipmap = true;
           if (skipmap == false)
              this.addBBToElem(elem,llz);             
         }
       }
     }
   return (llza);
}

blurbBits.prototype.getLLZ = function(iChk,content){
  var ret = undefined;
  var llzarg = unescape(content.substr(iChk+4,40)).split("&")[0]
  var llza = llzarg.split(",");
  if (llza[0] != undefined && llza[0].length > 0 &&
      llza[1] != undefined && llza[1].length > 0)
    ret = llza[0] + "," + parseFloat(llza[1]);
  if (llza[2] != undefined && llza[2].length > 0){
    zoom = parseInt(llza[2]);
    if (!isNaN(zoom))
      ret += "," + zoom;
  }
  return(ret);
}
blurbBits.prototype.strToPos = function(posStr){
    // accepts [+-]dd.dd or dd.dd[N|S] 
    //         [+-]dd mm.mm  or dd mm.mm [N|S]
    // also accepts +- (must be part of dd)
    // or NS EW can have leading or following spaces  
    var upStr = posStr.toUpperCase();
    var dma = posStr.split(" ");
    if (dma.length > 1){
      // handle minutes
      min = parseFloat(dma[1]); 
      // handles bad minutes or extra characters
      if (isNaN(min))
        min = 0.0;
      // add to degrees
      pos = parseFloat(dma[0]) + min/60;
      // resolve to accuracy of minutes could be minutes - 1 if .           
      pos = pos.toFixed(dma[1].length)
    }else if (dma.length > 0){
      // just degrees
      pos = parseFloat(dma[0]);
    }else
      // just degrees
      pos = parseFloat(dma);
    if (upStr.indexOf("S") > 0 || upStr.indexOf("W") > 0)
      pos = 0-pos;
    return(pos);
}

blurbBits.prototype.getLatLng = function (iChk,content){
    var llstr = content.substr(iChk,40).replace(/[\"\'\/\,\u00b0\u00ba]/g,' ').toUpperCase();
    if (llstr.indexOf(" ") == 0 || llstr.indexOf("\u003e") == 0 || llstr.indexOf(";") == 0)
      llstr = llstr.substring(1);
    var lat = "0.0";
    var lng = "0.0";
    var iEnd = llstr.search(/[NS]/);
   
    if (iEnd > 0){
      // split it at NS
      lat = llstr.substr(0,iEnd+1);
      lng = llstr.substring(iEnd+1);
      // try and cleanup lng (strip leading spaces and finish at [EW]
      iStart = lng.search(/\d/);
      iEnd = lng.search(/[EW]/);
      if (iEnd > 0)
        lng = lng.substr(iStart,iEnd+1); 
      return (this.strToPos(lat) + "," + this.strToPos(lng)); 
    }
    return ("0.0,0.0");
    
}
     
blurbBits.prototype.addIMap = function(div,src){     
    newe = document.createElement("iframe");
    newe.id='blurbMap' + this.mCnt++;
    newe.scrolling='no';
    newe.frameBorder=0;
    newe.className="blurbbit";
    this.setStyle(newe,'width:' + div.style.width + ';height:' + div.style.height + ';overflow:hidden;border:0px'); 
    if (src.indexOf("&mtitle=") < 0)
      src += "&mtitle=" + this.ovtitle;
    newe.src=src;
    if (this.rmINodes.length > 0)
      newe.onload = this.bind(this.clnFF);
    var fChild = div.firstChild;
    div.insertBefore(newe,fChild);
    if (fChild != undefined)
      div.removeChild(fChild);        
}
blurbBits.prototype.clnFF = function(){
    for (var i = 0; i < this.rmINodes.length; i ++){
      var ifrm = this.rmINodes[i];
      ifrm.parentNode.removeChild(ifrm);
    }
    
}

blurbBits.prototype.emptyIMap = function(div,src){
    var elems = this.getElemsByClass("gbsempty",div,"div");     
    if (elems.length == 0)
      this.addIMap(this.ovMap,this.bbpath +'?init=map&llz=0,0,0&mtitle=No%20GeoBlogs%20Found&nom=1' + this.fg + this.bg + this.ovopt);        
}


blurbBits.prototype.getStyle = function (el,styleProp){
    // make work for IE.. the #^$@ers
    var y = undefined;
    if (typeof el == "string")         
      el = document.getElementById(el);
    if (el.currentStyle)
      y = el.currentStyle[styleProp];
    else if (window.getComputedStyle)
      y = document.defaultView.getComputedStyle(el,null).getPropertyValue(styleProp);
    return y;
}

blurbBits.prototype.stripTags = function(text,clbr){
    if (clbr) // <br />
      text=text.replace(/<\/?br[^>]?>/gi,"@");
    return text.replace(/<\/?[^>]+>/gi, '');
}

 
blurbBits.prototype.initBlurbBits=function(){
    if (this.ovMapId != undefined){
      this.ovMap = document.getElementById(this.ovMapId);
      if (this.ovMap != undefined){
        if (this.bg == undefined || this.bg.length == 0){
          this.bg = this.getStyle(this.ovMap,(this.isIE ? "backgroundColor":"background-color"));
          if (this.bg == undefined)
            this.bg="";
          else
            this.bg="&bg=" + escape(this.bg)
        }
        if (this.fg == undefined || this.fg.length == 0){
          this.fg = this.getStyle(this.ovMap,"color");
          if (this.fg == undefined)
            this.fg="";
          else
            this.fg="&fg=" + escape(this.fg)
        }
      }
      if (this.fg != undefined)
        this.popopt += this.fg;
      if (this.bg != undefined)
        this.popopt += this.bg;
    }else{
      if (this.fg != undefined)
        this.popopt += this.fg;
      if (this.bg != undefined)
        this.popopt += this.bg;
    }
    var ret;
    if (this.sType == "text"){           
     if (this.sElemType == "*")
       ret = this.strGetLLZs(this.stripTags(this.sElem.innerHTML,0));
     else
       ret = this.sElemsText();
    }
    else if (this.sType == "geo"){
      ret = this.findGeo();
    } 
    ret = this.findLoc(ret);
       
    if (this.ovMap != undefined){
       var aout = "";
       var icnt=0;  
       for (var i in ret){
         if (typeof ret[i] != "function") { 
           aout += i + "+";
           icnt++;
         }       
       }
      var tcnt = (this.onlyone == "skip" ? 1:0);
      if (icnt > tcnt){
        this.ovMap.style.display="block";
        this.addIMap(this.ovMap,this.bbpath +'?init=map&llza=' + aout + this.fg + this.bg + this.ovopt);         
      }else{
        this.ovMap.style.display="block";
        this.emptyIMap();
      }       
    }
 }
var gIDR=0;
function initAtDR(func){
    fn = function(){if (!gIDR){gIDR=1;func()}};
    if (document.addEventListener)
      document.addEventListener("DOMContentLoaded",fn, false);
    else if (!gBlurbBits.isFramed && document.all && !window.opera){
      document.write('<script type="text/javascript" id="contentloadtag" defer="defer" src="javascript:void(0)"><\/script>');
      document.getElementById("contentloadtag").onreadystatechange=function(){if (this.readyState=="complete"){fn();}}
    }else if (window.addEventListener){
       window.addEventListener("load", fn, false);
    } else if (window.attachEvent){
       window.attachEvent("onload", fn);
    } 
}

var gBlurbBits;
function blurbBits(opts){
  
  this.isIE = 0;
  this.isFramed = (top.location != self.location) ? true : false;
  gBlurbBits = this;
  this.isNavUA = navigator.userAgent.toLowerCase();
  if ((this.isNavUA.indexOf('Opera') < 0) && (this.isNavUA.indexOf('MSIE') != -1)) this.isIE = 1;
    
  if ((location.hash.length > 0) && (location.hash.indexOf("skipmaps") > 0)){
     //return; 
     this.skipimaps = true;
  }
  this._opts = opts;
  if (opts.skipInit == undefined)
    initAtDR(this.bind(this.initBBSetup));
}

blurbBits.prototype.initBBSetup=function(){  
  this.rmINodes = [];
  this.isFF = 0;
  var opts = this._opts;
  if (this.isNavUA.indexOf('firefox') > -1)
    this.isFF = 1;

  var elems = this.getElementsByClass("post-body",document,"div");
  for (var el = 0; el < elems.length; el++){   
    var elem = elems[el];
    var tmpe = this.getElementsByClass("fullpost",elem,"span")[0];
    if (tmpe != undefined){
      if (this.getStyle(tmpe,"display") == "none"){
        var pNode = tmpe.parentNode;
        if (this.isFF == 1 && opts.nomaps == undefined && tmpe.getElementsByTagName("iframe")[0] != undefined){       
           this.rmINodes.push(tmpe);
           pNode._removed = tmpe;
        }else {
          var rchild = pNode.removeChild(tmpe);
          if (pNode._removed == undefined)
            pNode._removed = rchild;
        }
      }
    }else if ((tmpe = this.getElementsByClass("readmore",elem,"span")[0]) != undefined)
      tmpe.parentNode.removeChild(tmpe);    
  }


  if (opts == undefined)
    opts = {};

  if (opts.noexp == undefined){
    var tmpes = this.getElementsByClass("bblink",document.body,"a");
    for (var t = 0; t < tmpes.length; t++){
      var tmpe = tmpes[t];
      var href = tmpe.href;
      if (href != undefined)
        tmpe.onclick = this.bind(this.popBlogBit,href,1);
      if (this.getElementsByClass("bblimg",tmpe,"img")[0] == undefined){
        var newimg=document.createElement('img');
        this.setStyle(newimg,'border:0px;padding:0px;margin:0px;width:18px;height:18px');  
        newimg.src="http://www.creative-cruising.com/blurbbits/buttons/wpinright-18.png";
        newimg.alt="BBlink";
        newimg.className = "bblimg";
        newimg.title="BlurbBits";
        tmpe.insertBefore(newimg,tmpe.firstChild);
      }
    }
  }
      


  // 2DO 
  // extract link for feed and add undermap to view live..

  // don't add integrated maps if we are a child window of an active blurb map
  this.rPath = "http://www.creative-cruising.com/blurbbits/";
  this.bbpath = "http://www.creative-cruising.com/blurbbits/blurbbits.html";
  this.popopt = "";
  this.ovopt = "";
  this.popzi = opts.popzi ? opts.popzi:10;
  
if (opts.nomaps == undefined){
  // container to place the overview map also extracts color (bg and fg) styles
  this.ovMapId = opts.oid ? opts.oid:"blurbsmap"; 
  delete opts.oid;
  // Title of ov map
  var opt = escape(opts.otitle ? opts.otitle:"Geo Blogs on this page");
  this.ovtitle = opt;
  delete opts.otitle;


  // define what/where to search for position
  
  // search Mode 
  //     text (innerHTML for lat/lng "looking" fields
  //     geo uses microformat to extract position

  this.sType = opts.sType ? opts.sType:"text";
  delete opts.sType;

  // two types of text searching
  //   full body.. find all matching strings and add to summary map
  //        no container so individual maps can't be added
  //   loop elements  tagname:classname add blurbbit to element
  //         if element is div  .. it's added as firstChild of DIv
  //         otherwise it's added to parent before matching element.
  //
  //  Geo by default it finds all micro-formats
  //     can prune by stating which tagname you want to limit the class search too
  //
  //  By default search work on the document body.. adding ,parentNode
  //     will limit seach to children of that node only

  // text: *,main-elem
  // geo: abbr
  // geo: abbr,main-elem
  // geo: *,* or empty

  // text: div:*,main-elem  text: p 
  // text: div:post-body 
  this.onlyone = opts.onlyone ? opts.onlyone:"add";
  
  if (this.sType == "geo")
    opt = opts.sElems ? opts.sElems:"*";
  else
    opt = opts.sElems ? opts.sElems:"div:post-body";
  var toa = opt.split(",");
  this.sElem="body";
  if (toa.length > 0)
    this.sElemType = toa[0];
  if (this.sType == "geo"){
    if (this.sElemType == undefined)
      this.sElemType="*";   
  }else{
    if (this.sElemType == undefined)
      this.sElemType="div:*";   
    if (this.sElemType != "*"){ 
      
      this.sElemClass = "*";
      var iS;
      if ((iS = this.sElemType.indexOf(":")) > 0){
         opt = this.sElemType;
         this.sElemType = opt.substring(0,iS); 
         this.sElemClass = opt.substring(iS+1);
       
      }else
         this.sElemType = this.sElemType;
    }
  }
  if (toa.length > 1)
    this.sElem = toa[1];

  if (this.sElem == "body" || this.sElem == "*" || this.sElem == "all"){
    this.sElem = document.body;
  }else{
    var id = this.sElem;
    this.sElem = document.getElementById(id);
    if (this.sElem == undefined){
      alert("BlurbBits Search Target <" + id + "> not Found. Searching Document!");
      this.sElem = document.body;
      }
  }
  
  delete opts.sElems;

  // ok so you found some stuff now what should we add (if we can)..
  this.bbType = opts.bbType? opts.bbType: "imap"; // or link or imap else popup
  delete opts.bbType;

  // map titles
  opt = opts.title ? "&mtitle=" + escape(opts.title):"";
  this.popopt  += opt;
  delete opts.title;

  // icon and link text (for popup or link)
  this.icon = opts.icon ? opts.icon: this.rPath + "buttons/wpinright-18.png";
  delete opts.icon;
  this.itext = opts.itext ? opts.itext: "View map";
  delete opts.itext; 
  this.mCnt = 0;

  // add paragraph to link or popup (seperates from rest of text)
  this.addP = (opts.addP != undefined) ? !!opts.addP : false;
  delete this.addP;
  
  this.pwidth = opts.pw ? opts.pw:400;
  delete opts.pw;
  this.pheight = opts.ph ? opts.ph:200;
  delete opts.ph;
  

  for (i in opts){ 
    if (opts[i] != undefined && opts[i].length > 0){
      opt = "&" + i + "=" + escape(opts[i]);
      this.popopt += opt;
      this.ovopt += opt;
    }
  }
  // popoffset (left or normal)

 this.initBlurbBits();  
}
}
blurbBits.prototype.bind = function(method) {
    var self = this;
    var opt_args = [].slice.call(arguments, 1);
    return function() {
      var args = opt_args.concat([].slice.call(arguments));
      return method.apply(self, args);
    }
}