gEx = new Object();
gEx.rPath = "http://www.creative-cruising.com/blurbbits/";
gEx.icon = gEx.rPath + "buttons/wpinright-18.png"

var gIDR=0;
function initAtDR(func){
    fn = function(){if (!gIDR){gIDR=1;func()}};
    if (document.addEventListener)
      document.addEventListener("DOMContentLoaded",fn, false);
    else if (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);
    } 
}

function getElementsByClass(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;
}
function setStyle( object, styleText ) { 
    if (object == undefined)
      return;
    if( object.style.setAttribute ) { 
      object.style.setAttribute("cssText", styleText ); 
    } else { 
      object.setAttribute("style", styleText ); 
    } 
}
function fBind(obj,method) {
   var opt_args = [].slice.call(arguments, 2);
   return function() {
      var args = opt_args.concat([].slice.call(arguments));
      return method.apply(obj, args);
   };
}
function pBind(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);
    }
}


function showExample(elem,e){
  var val = elem.value; 
  if (val == undefined)
    alert("Nothing Defined");
  else if (val.indexOf("&") != 0)
    alert("Arguments Must begin with &");
  else{
    var src = gEx.rPath + "/blurbbits.html?design=true"+val;
    //popBlogBit(src,e);
    winContent = window.open(src, 'bbexamples', 'right=0,top=20,width=500,height=500,toolbar=yes,scrollbars=yes,resizable=yes,location=yes')
    winContent.focus()
  }
  elem.value = val;
  return false;
}
function winWidth() {
  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;
}
function popBlogBit(href,e){
    var posx = 0;
    var posy = 0;
    var ihtml = "";
    var ifh = "400";
    var ifw = "400";
    if (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;
    }
    if (gEx.mtooldiv == undefined){
       gEx.mtooldiv = document.createElement("div");
       gEx.mtooldiv.setAttribute('id', 'bb-pop');
       setStyle(gEx.mtooldiv,'position:absolute;width:' + ifw +'px;height:' + ifh +'px;overflow:hidden;border:1px black solid;background-color: #ffffff;visibility:visible;z-index:2');
       document.body.appendChild(gEx.mtooldiv);
    }else{
      gEx.mtooldiv.style.height = ifh + "px";
      gEx.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 = 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 ((gEx.mtooldiv._href == undefined) | (gEx.mtooldiv._href != href)){
      ihtml += '<img border=0 src="'+gEx.rPath+'buttons/close.png" style="position:absolute;right:0px;top:0px;z-index:1;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>';
      gEx.mtooldiv.innerHTML = ihtml;
      gEx.mtooldiv._href = href;
    }
    gEx.mtooldiv.style.visibility = "visible";
    gEx.mtooldiv.style.left = posx + "px";
    gEx.mtooldiv.style.top =  posy + "px"; 
    if (window.event) e.returnValue = false;   
    return false;
}

function dynEx(){
  var elems = getElementsByClass("post-body",document,"div");
  for (var el = 0; el < elems.length; el++){   
    var elem = elems[el];
    var tmpe = getElementsByClass("fullpost",elem,"span")[0];
    if (tmpe != undefined){
      if (getStyle(tmpe,"display") == "none"){
        var pNode = tmpe.parentNode;
        var rchild = tmpe.parentNode.removeChild(tmpe);
      }
    }else{
      if ((tmpe = getElementsByClass("readmore",elem,"span")[0]) != undefined)
        tmpe.parentNode.removeChild(tmpe);    
    }
  }
    var tmpes = 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 = pBind(popBlogBit,href);
        if (getElementsByClass("bblimg",tmpe,"img")[0] == undefined){
          var newimg=document.createElement('img');
          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);
        }
      }
    }
  
   var elems = getElementsByClass("bbexample",document,"div");

   for (var el = 0; el < elems.length; el++){   
     var elem = elems[el];
     var text = elem.innerHTML.split("&amp;").join("\&");    
    
     var ndiv=document.createElement('div');
     setStyle(ndiv,"border:0px;padding:0px 4px 0px 0px;margin:0px;height:25px");
     var form = document.createElement("form");
     var tin = document.createElement("input");
     tin.type = "text";
     tin.size = 60;
     tin.value = text;
     //form.action = 'javascript:void(0);false;'
     form.onsubmit = fBind(tin,showExample,tin);
     setStyle(form,"border:0px;padding:0px 4px 0px 0px;margin:0px");
     
     var newe=document.createElement('a');
     newe.setAttribute('href','javascript:void(0);');
     newe.onclick = fBind(tin,showExample,tin);//fBind(newe,showExample,newe);
     newe.title = "Click to Show Example";
     var newe2=document.createElement('img');
     setStyle(newe2,'border:0px;padding:0px 4px 0px 0px;margin:0px');  
     newe2.src=gEx.icon;
     newe2.alt="showex";
     
     newe.appendChild(newe2);
     form.appendChild(newe);
     form.appendChild(tin);    
     ndiv.appendChild(form);
     elem.appendChild(ndiv);
  }
}

     
initAtDR(dynEx);