var scriptSource = (function() 
{ 
    var scripts = document.getElementsByTagName('script'), 
        script = scripts[scripts.length - 1];
    return script.getAttribute('src', 2);
}()); 

var host = "";
try {
	host = scriptSource.match(/^https?:\/\/([^\/]+)\/?/)[1]
} catch(e) {
	host = window.location.host;
}

var config ={
	API_URL: 'http://www.stuffbuff.com/webservice/stuffit',
	COOKIE_URL: 'http://'+host+'/webservice/stuffit',
	imagesUrl: 'http://'+host+'/static/btn/',
	cartURL: 'http://'+host+'/shopcart',
	productImageUrl: 'http://'+host,
	maxStrLength: 200
};

//////////////////////////////////////////////////////////////////////////////
var seccessSpeed,
    stuffBuffDiv,
    stuffInfoDiv,
    itemDiv,
    imagesDiv,
    infoDiv,
    successDiv,
    cartDiv,
    successTimeout,
    noResFound,
    twitterButton,
    facebookButton,
    backgroundDiv,
    viewCartDiv,
    sbActivator,
    iframeDiv,
    iframeDiv2,
    re;
//////////////////////////////////////////////////////////////////////////////

config.for_buyers_url = config.productImageUrl + '/content/for_buyers';
///////////////////////// Check COOKIE query //////////////////////////////
f=document.createElement("script");
f.defer="true";
f.src=config.COOKIE_URL+'?query={"stuffbuff":{"affiliateId":"'+SB_Affiliate_ID+'","cmd":"COOKIE_CHECK"}}&callback=stuffBuffCookieCheck';
document.getElementsByTagName("head")[0].appendChild(f);

function stuffBuffCookieCheck(data) {
    
    if(data.stuffbuff_stuffit_isactive) {

        (function (){

            Event = (function() {

              var guid = 0

              function fixEvent(event) {
                event = event || window.event

                if ( event.isFixed ) {
                  return event;
                }
                event.isFixed = true;

                event.preventDefault = event.preventDefault || function(){this.returnValue = false}
                event.stopPropagation = event.stopPropagaton || function(){this.cancelBubble = true}

                if (!event.target) {
                    event.target = event.srcElement;
                }

                if (!event.relatedTarget && event.fromElement) {
                    event.relatedTarget = event.fromElement == event.target ? event.toElement : event.fromElement;
                }

                if ( event.pageX == null && event.clientX != null ) {
                    var html = document.documentElement, body = document.body;
                    event.pageX = event.clientX + (html && html.scrollLeft || body && body.scrollLeft || 0) - (html.clientLeft || 0);
                    event.pageY = event.clientY + (html && html.scrollTop || body && body.scrollTop || 0) - (html.clientTop || 0);
                }

                if ( !event.which && event.button ) {
                    event.which = (event.button & 1 ? 1 : ( event.button & 2 ? 3 : ( event.button & 4 ? 2 : 0 ) ));
                }

                return event
              }  


              function commonHandle(event) {
                event = fixEvent(event)

                var handlers = this.events[event.type]

                for ( var g in handlers ) {
                  var handler = handlers[g]

                  var ret = handler.call(this, event)
                  if ( ret === false ) {
                      event.preventDefault()
                      event.stopPropagation()
                  }
                }
              }

              return {
                add: function(elem, type, handler) {
                  if (elem.setInterval && ( elem != window && !elem.frameElement ) ) {
                    elem = window;
                  }

                  if (!handler.guid) {
                    handler.guid = ++guid
                  }

                  if (!elem.events) {
                    elem.events = {}
                        elem.handle = function(event) {
                          if (typeof Event !== "undefined") {
                                return commonHandle.call(elem, event)
                          }
                    }
                  }

                  if (!elem.events[type]) {
                    elem.events[type] = {}        

                    if (elem.addEventListener)
                          elem.addEventListener(type, elem.handle, false)
                        else if (elem.attachEvent)
                      elem.attachEvent("on" + type, elem.handle)
                  }

                  elem.events[type][handler.guid] = handler
                },

                remove: function(elem, type, handler) {
                  var handlers = elem.events && elem.events[type]

                  if (!handlers) return

                  delete handlers[handler.guid]

                  for(var any in handlers) return 
                  if (elem.removeEventListener)
                        elem.removeEventListener(type, elem.handle, false)
                  else if (elem.detachEvent)
                        elem.detachEvent("on" + type, elem.handle)

                  delete elem.events[type]


                  for (var any in elem.events) return
                  try {
                    delete elem.handle
                    delete elem.events 
                  } catch(e) { // IE
                    elem.removeAttribute("handle")
                    elem.removeAttribute("events")
                  }
                } 
              }
            }());

            function getPosition(e){
                var left = 0
                var top  = 0

                while (e.offsetParent){
                        left += e.offsetLeft
                        top  += e.offsetTop
                        e	 = e.offsetParent
                }

                left += e.offsetLeft
                top  += e.offsetTop

                return {x:left, y:top}
            }   

            //////////////////////////////////////////////

            var popUpDiv = document.createElement('div');
            popUpDiv.innerHTML = '<div style="cursor: pointer;position:absolute;z-index:2147483649;width:100px;height:30px;display:none;">'+
            '<div style="position: absolute; width: 0px; height: 22px; z-index: 1000; background-image: url('+config.imagesUrl+'progress-bar.png);top:3px;left:2px;"></div>'+
            '<img src="'+config.imagesUrl+'stuff-it.png" />'+
            '</div>'+
            '<div style="text-align:left;background:url('+config.imagesUrl+'bg-popup.png) no-repeat;position:absolute;z-index:2147483649;width:357px;height:395px;display:none;">'+
            '<div style="width:357px;height:80px;">'+
            '<img src="'+config.imagesUrl+'popup-close.png" style="cursor:pointer;float:right;margin-top:10px;margin-right:10px;" />'+
            '<div style="margin:0px;padding:0px;width:339px;height:38px;float:left;margin-top:15px;">'+
            '<div style="margin:0px;padding:0px;width:15px;height:38px;float:left;"></div>'+
            '<div style="cursor: pointer;background:url('+config.imagesUrl+'tabs.png) no-repeat -10px 0;float:left;height:38px;width:78px;"></div>'+
            '<div style="cursor: pointer;background:url('+config.imagesUrl+'tabs.png) no-repeat -95px -114px;float:left;height:38px;width:78px;margin-left:4px;"></div>'+
            '<div style="cursor: pointer;background:url('+config.imagesUrl+'tabs.png) no-repeat -179px -114px;float:left;height:38px;width:78px;margin-left:4px;"></div>'+
            //'<div style="cursor: pointer;background:url('+config.imagesUrl+'tabs.png) no-repeat -263px -114px;float:left;height:38px;width:78px;margin-left:4px;"></div>'+
            '</div></div>'+
            '<div style="width:337px;height:195px;float:left;clear:both;padding-top:15px;padding-left:20px;">'+
            '<div style="width:321px;height:190px;float:left;display:block;overflow-y: auto;"></div>'+
            '<div style="width:321px;height:190px;float:left;display:none;overflow-y: auto;"></div>'+
            '<div style="width:321px;height:190px;float:left;display:none;overflow-y: auto;"></div>'+
            '<div style="width:323px;height:190px;float:left;display:none;overflow-y: auto;"></div>'+
            '<div style="position:relative;z-index:2147483659;width:320px;height:30px;display:none;color:white;margin-top:-100px;float:left;clear:both;"><center style="padding-top:5px;font-weight:bold;font-size:12px;"><span style="background-color:#525760;padding:2px 5px 2px 5px;">Item in Cart</span></center></div>'+
            '</div>'+
            '<div style="width:357px;height:50px;float:left;">'+
            '<div style="width:172px;height:50px;float:left;">'+
            '<div style="width:140px;height:16px;margin-top:10px;margin-left:25px;">'+
            '<span style="font-size:10px;font-weight:bold;margin-right:10px;color:#525760;">SHARE VIA:</span>'+
            '<a href="#" style="text-decoration:none;"><img style="border:none;" src="'+config.imagesUrl+'ico-twitter.png" /></a>'+
            '<a href="#" style="margin-left:10px;text-decoration:none;"><img style="border:none;" src="'+config.imagesUrl+'ico-fb.png" /></a>'+
            '</div></div>'+
            '<div style="width:160px;height:50px;float:left;"></div>'+
            '<div style="width:320px;height:20px;float:left;clear:both;"><center><div style="margin-left:40px;text-decoration:none;color:#B5B5B5;font-size:12px;font-weight:bold;font-family:Arial,Helvetica,sans-serif;"><span style="cursor:pointer;">VIEW CART</span></div></center></div>'+
            '<div style="width:320px;height:20px;float:left;clear:both;font-size:12px;color:#B8B8B8;font-family:Arial,Helvetica,sans-serif;font-weight:bold;"><center><div style="padding-left:32px;"><input type="checkbox" /> Do not show me this again</div></center></div>'+
            '<div style="display: none; z-index: 2147483645; background-color: #ffffff;"></div>'+
            '<div style="display: none; z-index: 2147483645; background-color: #ffffff;"></div>'+
            '<div style="opacity: 0.50; z-index: 2147483644; background-color: black; left: 0; position: fixed; top: 0; display: none;"></div>'+
            '</div>';
            popUpDiv.setAttribute('id', 'stuffBuffDiv');
            document.documentElement.appendChild(popUpDiv);

            /////////////////////////////////////////////
            var t,
                mX,
                mY,
                speed = 15, /* Interval speed*/
                dragObj = null,
                intervalFilt,
                yMargin = 40,
                yMarginInfo = 185,
                xMargin = 50,
                pageVar,
                stuffBuffDiv = document.getElementById('stuffBuffDiv'),
                popUpdiv = stuffBuffDiv.firstChild,
                stuffInfoDiv = stuffBuffDiv.lastChild,
                closeInfoButt = stuffInfoDiv.firstChild.firstChild,
                divFilter = popUpdiv.firstChild,
                stuffInfoDrag = stuffInfoDiv.firstChild,
            /////// Buttons ////////    
                itemButt = stuffInfoDiv.firstChild.lastChild.childNodes[1],
                imagesButt = stuffInfoDiv.firstChild.lastChild.childNodes[2],
                infoButt = stuffInfoDiv.firstChild.lastChild.childNodes[3],
                //compareButt = stuffInfoDiv.firstChild.lastChild.childNodes[4],
            /////// Content Div's ///
                itemDiv = stuffInfoDiv.childNodes[1].childNodes[0],
                imagesDiv = stuffInfoDiv.childNodes[1].childNodes[1],
                infoDiv = stuffInfoDiv.childNodes[1].childNodes[2],
                //compareDiv = stuffInfoDiv.childNodes[1].childNodes[3],
                search,
                dlina;
            /////////////////////////////////////////////
            function  getPageSize(){
                   var xScroll, yScroll;

                   if (window.innerHeight && window.scrollMaxY) {
                           xScroll = document.body.scrollWidth;
                           yScroll = window.innerHeight + window.scrollMaxY;
                   } else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
                           xScroll = document.body.scrollWidth;
                           yScroll = document.body.scrollHeight;
                   } else if (document.documentElement && document.documentElement.scrollHeight > document.documentElement.offsetHeight){ // Explorer 6 strict mode
                           xScroll = document.documentElement.scrollWidth;
                           yScroll = document.documentElement.scrollHeight;
                   } else { // Explorer Mac...would also work in Mozilla and Safari
                           xScroll = document.body.offsetWidth;
                           yScroll = document.body.offsetHeight;
                   }

                   var windowWidth, windowHeight;
                   if (self.innerHeight) { // all except Explorer
                           windowWidth = self.innerWidth;
                           windowHeight = self.innerHeight;
                   } else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
                           windowWidth = document.documentElement.clientWidth;
                           windowHeight = document.documentElement.clientHeight;
                   } else if (document.body) { // other Explorers
                           windowWidth = document.body.clientWidth;
                           windowHeight = document.body.clientHeight;
                   }

                   // for small pages with total height less then height of the viewport
                   if(yScroll < windowHeight){
                           pageHeight = windowHeight;
                   } else {
                           pageHeight = yScroll;
                   }

                   // for small pages with total width less then width of the viewport
                   if(xScroll < windowWidth){
                           pageWidth = windowWidth;
                   } else {
                           pageWidth = xScroll;
                   }

                   return [pageWidth,pageHeight,windowWidth,windowHeight];
            }

            config.SB_Page_Size = getPageSize();
            ////////////////////////////////////////////

            function popUpOnSelect(event) {
                divFilter.style.width = '0px';
                clearInterval(intervalFilt);
                


                if (window.getSelection) {  // all browsers, except IE before version 9
                    var selRange = window.getSelection ();
                    dlina = selRange.toString();
                    if(t !== selRange.toString () && dlina.length < config.maxStrLength){
                        t = selRange.toString ();
                        if(/[A-Za-z0-9]/ig.test(t)){                    
                            //////////////                    
                            var node = selRange.anchorNode;
                            var endNode = selRange.focusNode;
                            var st1 = selRange.anchorOffset;
                            var en1 = selRange.focusOffset;
                            var rangeObj = document.createRange();            

                            var counter = 0,
                                 start,
                                 end,
                                 st,
                                 en,
                                 last;
                             if(node === endNode){
                                 if(en1 < st1){
                                  st = en1;
                                  en = st1
                                  }
                                  else{
                                      st = st1;
                                      en = en1;
                                  }
                                 rangeObj.selectNodeContents (node);
                                 last = rangeObj.endOffset;
                                 while (true) {
                                     if(st- counter - 1 < 0){                    
                                         start = st - counter;
                                         break;
                                     }
                                     rangeObj.setStart(node, st- counter - 1);
                                     rangeObj.setEnd(node, st - counter);

                                     if (/\s/.test(rangeObj.toString())) {
                                         start = st - counter;
                                         break;
                                     }

                                     counter++;
                                 }

                                 counter = 0;
                                 while (true) {


                                     rangeObj.setStart(node, en + counter);
                                     if(en + counter + 1 > last){
                                         end = en + counter;
                                         break;
                                     }
                                     rangeObj.setEnd(node, en + counter + 1);

                                     if (/\s/.test(rangeObj.toString())) {
                                         end = en + counter;
                                         break;
                                     }

                                     counter++;
                                 }
                                 rangeObj.setStart(node, start);
                                 rangeObj.setEnd(node, end);
                            }
                            else{
                                st = st1;
                                en = en1;
                                rangeObj.selectNodeContents (endNode);
                                last = rangeObj.endOffset;;
                                while (true) {
                                     if(st- counter - 1 < 0){                    
                                         start = st - counter;
                                         break;
                                     }
                                     rangeObj.setStart(node, st- counter - 1);
                                     rangeObj.setEnd(node, st - counter);

                                     if (/\s/.test(rangeObj.toString())) {
                                         start = st - counter;
                                         break;
                                     }

                                     counter++;
                                 }

                                 counter = 0;
                                 while (true) {
                                     rangeObj.setStart(endNode, en + counter);
                                     if(en + counter + 1 > last){
                                         end = en + counter;
                                         break;
                                     }
                                     rangeObj.setEnd(endNode, en + counter + 1);

                                     if (/\s/.test(rangeObj.toString())) {
                                         end = en + counter;
                                         break;
                                     }

                                     counter++;
                                 }
                                 rangeObj.setStart(node, start);
                                 rangeObj.setEnd(endNode, end);                        
                            }

                             selRange.addRange(rangeObj);
                             var searchTerm = selRange.toString ();
                             var re = /\s+/ig;
                             search = encodeURIComponent(searchTerm.replace(re, " "));
                    //////////////
                            stuffInfoDiv.style.display = 'none';

                            pageVar = getPageSize();
                            if(pageVar[0] < event.pageX+120){
                                popUpdiv.style.left = pageVar[0]-120+'px';
                            }                    
                            else{
                                if(event.pageX-50 < 0){
                                    popUpdiv.style.left = event.pageX+'px';
                                }
                                else{
                                    popUpdiv.style.left = event.pageX-xMargin+'px';
                                }
                            }
                            if(event.pageY-40 < 0){
                                popUpdiv.style.top = event.pageY+15+'px';
                            }
                            else{
                                popUpdiv.style.top = event.pageY-yMargin+'px';
                            }                                                           
                            popUpdiv.style.display = 'block';
                            if(SB_Auto_Load){
                                intervalFilt = setInterval(function(){
                                    if(parseInt(divFilter.style.width) < 88){
                                    divFilter.style.width = (parseInt(divFilter.style.width)+1)+'px';
                                    }
                                    else{
                                        buttonsRestart();
                                        itemDiv.innerHTML = '<img src="'+config.imagesUrl+'ajax-loader.gif" style="margin-left:140px;margin-top:50px;" />';
                                        clearInterval(intervalFilt);
                                        divFilter.style.width = '0px';
                                        popUpdiv.style.display = 'none';                                
                                        if(pageVar[1] < event.pageY+450){
                                            stuffInfoDiv.style.top = pageVar[1]-400+'px';
                                        }
                                        else{
                                            if(event.pageY < 185){
                                                stuffInfoDiv.style.top = event.pageY+'px';
                                            }
                                            else{
                                                stuffInfoDiv.style.top = event.pageY-yMarginInfo+'px';
                                            }
                                        }
                                        if(pageVar[0] < event.pageX+450){
                                            stuffInfoDiv.style.left = pageVar[0]-400+'px';
                                        }
                                        else{
                                            stuffInfoDiv.style.left = event.pageX+xMargin+'px';
                                        }
                                        stuffInfoDiv.style.display = 'block';
                                        b=document.createElement("script");b.defer="true";b.src=config.API_URL+"?query={%22stuffbuff%22:{%22affiliateId%22:%22"+SB_Affiliate_ID+"%22,%22search%22:%22"+search+"%22,%22cmd%22:%22LOOKUP%22}}&callback=stuffBuffFunctionLookup";document.getElementsByTagName("head")[0].appendChild(b);
                                    }            
                                },speed);
                            }                                  
                        }
                        else{
                            popUpdiv.style.display = 'none';                    
                        }
                    }
                }
                else {
                    if (document.selection) {        // Internet Explorer
                        textRange = document.selection.createRange ();
                        dlina = textRange.text;
                        if(t !== textRange.text && dlina.length < config.maxStrLength){
                            t = textRange.text;
                            searchTerm = textRange.text;

                            var re = /\s+/ig;
                            search = encodeURIComponent(searchTerm.replace(re, " "));
                            if(/[A-Za-z0-9]/ig.test(t)){
                                stuffInfoDiv.style.display = 'none';
                                pageVar = getPageSize();
                                if(pageVar[0] < event.pageX+120){
                                    popUpdiv.style.left = pageVar[0]-120+'px';
                                }                    
                                else{
                                    if(event.pageX-50 < 0){
                                        popUpdiv.style.left = event.pageX+'px';
                                    }
                                    else{
                                        popUpdiv.style.left = event.pageX-xMargin+'px';
                                    }
                                }
                                if(event.pageY-40 < 0){
                                    popUpdiv.style.top = event.pageY+15+'px';
                                }
                                else{
                                    popUpdiv.style.top = event.pageY-yMargin+'px';
                                }                                                           
                                popUpdiv.style.display = 'block';
                                if(SB_Auto_Load){
                                    intervalFilt = setInterval(function(){
                                        if(parseInt(divFilter.style.width) < 88){
                                        divFilter.style.width = (parseInt(divFilter.style.width)+1)+'px';
                                        }
                                        else{
                                            buttonsRestart();
                                            itemDiv.innerHTML = '<img src="'+config.imagesUrl+'ajax-loader.gif" style="margin-left:140px;margin-top:50px;" />';
                                            clearInterval(intervalFilt);
                                            divFilter.style.width = '0px';
                                            popUpdiv.style.display = 'none';
                                            if(pageVar[1] < event.pageY+450){
                                                stuffInfoDiv.style.top = pageVar[1]-400+'px';
                                            }
                                            else{
                                                if(event.pageY < 185){
                                                    stuffInfoDiv.style.top = event.pageY+'px';
                                                }
                                                else{
                                                    stuffInfoDiv.style.top = event.pageY-yMarginInfo+'px';
                                                }
                                            }
                                            if(pageVar[0] < event.pageX+450){
                                                stuffInfoDiv.style.left = pageVar[0]-400+'px';
                                            }
                                            else{
                                                stuffInfoDiv.style.left = event.pageX+xMargin+'px';
                                            }
                                            stuffInfoDiv.style.display = 'block';
                                            b=document.createElement("script");b.defer="true";b.src=config.API_URL+"?query={%22stuffbuff%22:{%22affiliateId%22:%22"+SB_Affiliate_ID+"%22,%22search%22:%22"+search+"%22,%22cmd%22:%22LOOKUP%22}}&callback=stuffBuffFunctionLookup";document.getElementsByTagName("head")[0].appendChild(b);
                                        }            
                                    },speed);
                                }
                            }
                            else{
                                popUpdiv.style.display = 'none';
                            }
                        }                
                    }
                }
            }

            Event.add(document, 'mouseup', popUpOnSelect);

            function DivOnClick(event){
                buttonsRestart();
                itemDiv.innerHTML = '<img src="'+config.imagesUrl+'ajax-loader.gif" style="margin-left:140px;margin-top:50px;" />';
                clearInterval(intervalFilt);
                pageVar = getPageSize();
                if(pageVar[1] < event.pageY+450){
                    stuffInfoDiv.style.top = pageVar[1]-400+'px';
                }
                else{
                    if(event.pageY < 185){
                        stuffInfoDiv.style.top = event.pageY+'px';
                    }
                    else{
                        stuffInfoDiv.style.top = event.pageY-yMarginInfo+'px';
                    }
                }
                if(pageVar[0] < event.pageX+450){
                    stuffInfoDiv.style.left = pageVar[0]-400+'px';
                }
                else{
                    stuffInfoDiv.style.left = event.pageX+xMargin+'px';
                }
                stuffInfoDiv.style.display = 'block';
                popUpdiv.style.display = 'none';
                b=document.createElement("script");b.defer="true";b.src=config.API_URL+"?query={%22stuffbuff%22:{%22affiliateId%22:%22"+SB_Affiliate_ID+"%22,%22search%22:%22"+search+"%22,%22cmd%22:%22LOOKUP%22}}&callback=stuffBuffFunctionLookup";document.getElementsByTagName("head")[0].appendChild(b);
            }

            Event.add(popUpdiv, 'click', DivOnClick);

            function mouseOverDiv(event){
                /*intervalFilt = setInterval(function(){
                    if(parseInt(divFilter.style.width) < 88){
                    divFilter.style.width = (parseInt(divFilter.style.width)+1)+'px';
                    }
                    else{
                        buttonsRestart();
                        itemDiv.innerHTML = '<img src="'+config.imagesUrl+'ajax-loader.gif" style="margin-left:140px;margin-top:50px;" />';
                        clearInterval(intervalFilt);
                        divFilter.style.width = '0px';
                        popUpdiv.style.display = 'none';
                        pageVar = getPageSize();
                        if(pageVar[1] < event.pageY+450){
                            stuffInfoDiv.style.top = pageVar[1]-400+'px';
                        }
                        else{
                            if(event.pageY < 185){
                                stuffInfoDiv.style.top = event.pageY+'px';
                            }
                            else{
                                stuffInfoDiv.style.top = event.pageY-yMarginInfo+'px';
                            }
                        }
                        if(pageVar[0] < event.pageX+450){
                            stuffInfoDiv.style.left = pageVar[0]-400+'px';
                        }
                        else{
                            stuffInfoDiv.style.left = event.pageX+xMargin+'px';
                        }
                        stuffInfoDiv.style.display = 'block';
                        b=document.createElement("script");b.defer="true";b.src=config.API_URL+"?query={%22stuffbuff%22:{%22affiliateId%22:%22"+SB_Affiliate_ID+"%22,%22search%22:%22"+search+"%22,%22cmd%22:%22LOOKUP%22}}&callback=stuffBuffFunctionLookup";document.getElementsByTagName("head")[0].appendChild(b);
                    }            
                },speed);*/
            }

            function mouseOutDiv(event){
                /*clearInterval(intervalFilt);*/
            }

            if(!SB_Auto_Load){
                Event.add(popUpdiv, 'mouseover', mouseOverDiv);
                Event.add(popUpdiv, 'mouseout', mouseOutDiv);
            }

            function closeButt(event){
                t = '';
                stuffInfoDiv.style.display = 'none';
            }
            Event.add(closeInfoButt, 'click', closeButt);

            function dragInfoDiv(event){
                document.body.onselectstart = function() {return false}        
                dragObj = stuffInfoDiv;
                pos = getPosition(this);
                mX = event.pageX - pos.x;
                mY = event.pageY - pos.y;
            }

            function moveInfoDiv(event){
                if(dragObj != null){
                    if(window.getSelection)window.getSelection().removeAllRanges();
                    stuffInfoDiv.style.opacity = 0.4;
                    stuffInfoDiv.style.filter = 'progid:DXImageTransform.Microsoft.Alpha(opacity=40)';
                    stuffInfoDiv.style.top = event.pageY-mY+'px';
                    stuffInfoDiv.style.left = event.pageX-mX+'px';
                }
            }

            function dropeInfoDiv(event){
                document.body.onselectstart = function() {return true}
                dragObj = null;
                stuffInfoDiv.style.opacity = 1.0;
                stuffInfoDiv.style.filter = 'progid:DXImageTransform.Microsoft.Alpha(opacity=100)';
            }

            Event.add(stuffInfoDrag, 'mousedown', dragInfoDiv);
            Event.add(document, 'mousemove', moveInfoDiv);
            Event.add(stuffInfoDrag, 'mouseup', dropeInfoDiv);

            function stuffInfoDivOver(event){
                Event.remove(document, 'mouseup', popUpOnSelect);
            }

            function stuffInfoDivOut(event){
                Event.add(document, 'mouseup', popUpOnSelect);
            }

            Event.add(stuffInfoDiv, 'mouseover', stuffInfoDivOver);
            Event.add(stuffInfoDiv, 'mouseout', stuffInfoDivOut);

            ///// Button Events ///////
            function itemButtOver(event){
                if(itemDiv.style.display != 'block'){
                    itemButt.style.background = 'url("'+config.imagesUrl+'tabs.png") no-repeat -10px -76px';
                }
            }
            function itemButtOut(event){
                if(itemDiv.style.display != 'block'){
                    itemButt.style.background = 'url("'+config.imagesUrl+'tabs.png") no-repeat -10px -114px';
                }
            }

            function itemButtClick(event){
                if(itemDiv.style.display != 'block'){
                    infoDiv.style.display = 'none';
                    imagesDiv.style.display = 'none';
                    //compareDiv.style.display = 'none';
                    infoButt.style.background = 'url("'+config.imagesUrl+'tabs.png") no-repeat -179px -114px';
                    imagesButt.style.background = 'url("'+config.imagesUrl+'tabs.png") no-repeat -95px -114px';
                    //compareButt.style.background = 'url("'+config.imagesUrl+'tabs.png") no-repeat -263px -114px';

                    itemDiv.style.display = 'block';
                    itemButt.style.background = 'url("'+config.imagesUrl+'tabs.png") no-repeat -10px 0';
                }
            }

            Event.add(itemButt, 'click', itemButtClick);
            Event.add(itemButt, 'mouseover', itemButtOver);
            Event.add(itemButt, 'mouseout', itemButtOut);

            function imagesButtOver(event){
                if(imagesDiv.style.display != 'block'){
                    imagesButt.style.background = 'url("'+config.imagesUrl+'tabs.png") no-repeat -95px -76px';
                }
            }
            function imagesButtOut(event){
                if(imagesDiv.style.display != 'block'){
                    imagesButt.style.background = 'url("'+config.imagesUrl+'tabs.png") no-repeat -95px -114px';
                }
            }

            function imagesButtClick(event){
                if(imagesDiv.style.display != 'block'){
                    infoDiv.style.display = 'none';
                    itemDiv.style.display = 'none';
                    //compareDiv.style.display = 'none';
                    infoButt.style.background = 'url("'+config.imagesUrl+'tabs.png") no-repeat -179px -114px';
                    itemButt.style.background = 'url("'+config.imagesUrl+'tabs.png") no-repeat -10px -114px';
                    //compareButt.style.background = 'url("'+config.imagesUrl+'tabs.png") no-repeat -263px -114px';

                    imagesDiv.style.display = 'block';
                    imagesButt.style.background = 'url("'+config.imagesUrl+'tabs.png")  no-repeat -95px 0';
                }
            }

            Event.add(imagesButt, 'click', imagesButtClick);
            Event.add(imagesButt, 'mouseover', imagesButtOver);
            Event.add(imagesButt, 'mouseout', imagesButtOut);

            function infoButtOver(event){
                if(infoDiv.style.display != 'block'){
                    infoButt.style.background = 'url("'+config.imagesUrl+'tabs.png") no-repeat -179px -76px';
                }
            }
            function infoButtOut(event){
                if(infoDiv.style.display != 'block'){
                    infoButt.style.background = 'url("'+config.imagesUrl+'tabs.png") no-repeat -179px -114px';
                }
            }

            function infoButtClick(event){
                if(infoDiv.style.display != 'block'){
                    imagesDiv.style.display = 'none';
                    itemDiv.style.display = 'none';
                    //compareDiv.style.display = 'none';
                    imagesButt.style.background = 'url("'+config.imagesUrl+'tabs.png") no-repeat -95px -114px';
                    itemButt.style.background = 'url("'+config.imagesUrl+'tabs.png") no-repeat -10px -114px';
                    //compareButt.style.background = 'url("'+config.imagesUrl+'tabs.png") no-repeat -263px -114px';

                    infoDiv.style.display = 'block';
                    infoButt.style.background = 'url("'+config.imagesUrl+'tabs.png") no-repeat -179px 0';
                }
            }

            Event.add(infoButt, 'click', infoButtClick);
            Event.add(infoButt, 'mouseover', infoButtOver);
            Event.add(infoButt, 'mouseout', infoButtOut);

            /*function compareButtOver(event){
                if(compareDiv.style.display != 'block'){
                    compareButt.style.background = 'url("'+config.imagesUrl+'tabs.png") no-repeat -263px -76px';
                }
            }
            function compareButtOut(event){
                if(compareDiv.style.display != 'block'){
                    compareButt.style.background = 'url("'+config.imagesUrl+'tabs.png") no-repeat -263px -114px';
                }
            }

            function compareButtClick(event){
                if(compareDiv.style.display != 'block'){
                    imagesDiv.style.display = 'none';
                    itemDiv.style.display = 'none';
                    infoDiv.style.display = 'none';
                    imagesButt.style.background = 'url("'+config.imagesUrl+'tabs.png") no-repeat -95px -114px';
                    itemButt.style.background = 'url("'+config.imagesUrl+'tabs.png") no-repeat -10px -114px';
                    infoButt.style.background = 'url("'+config.imagesUrl+'tabs.png") no-repeat -179px -114px';

                    compareDiv.style.display = 'block';
                    compareButt.style.background = 'url("'+config.imagesUrl+'tabs.png") no-repeat -263px 0';
                }
            }

            Event.add(compareButt, 'click', compareButtClick);
            Event.add(compareButt, 'mouseover', compareButtOver);
            Event.add(compareButt, 'mouseout', compareButtOut);*/

            function buttonsRestart(){
                itemDiv.style.display = 'block';
                imagesDiv.style.display = 'none';
                infoDiv.style.display = 'none';
                //compareDiv.style.display = 'none';

                infoButt.style.background = 'url("'+config.imagesUrl+'tabs.png") no-repeat -179px -114px';
                imagesButt.style.background = 'url("'+config.imagesUrl+'tabs.png") no-repeat -95px -114px';
                //compareButt.style.background = 'url("'+config.imagesUrl+'tabs.png") no-repeat -263px -114px';
                itemButt.style.background = 'url("'+config.imagesUrl+'tabs.png") no-repeat -10px 0';
            }
        })();

        ////////////////////////////////////////////
            seccessSpeed = 1000;
            stuffBuffDiv = document.getElementById('stuffBuffDiv');
            stuffInfoDiv = stuffBuffDiv.lastChild;
            itemDiv = stuffInfoDiv.childNodes[1].childNodes[0];
            imagesDiv = stuffInfoDiv.childNodes[1].childNodes[1];
            infoDiv = stuffInfoDiv.childNodes[1].childNodes[2];
            successDiv = stuffInfoDiv.childNodes[1].childNodes[4];
            cartDiv = stuffInfoDiv.childNodes[2].childNodes[1];
            twitterButton = stuffInfoDiv.childNodes[2].firstChild.firstChild.childNodes[2];
            facebookButton = stuffInfoDiv.childNodes[2].firstChild.firstChild.childNodes[1];
            backgroundDiv = stuffInfoDiv.lastChild.childNodes[6];
            viewCartDiv = stuffInfoDiv.lastChild.childNodes[2].firstChild.firstChild.firstChild;
            sbActivator = stuffInfoDiv.lastChild.childNodes[3].firstChild.firstChild.firstChild;
            iframeDiv = stuffInfoDiv.lastChild.childNodes[4];
            iframeDiv2 = stuffInfoDiv.lastChild.childNodes[5];
            re = /\s+/ig;
        ///////////////////////////////////////////////
        sbActivator.onclick = function() {
            g=document.createElement("script");
            g.defer="true";
            g.src=config.COOKIE_URL+"?query={%22stuffbuff%22:{%22affiliateId%22:%22"+SB_Affiliate_ID+"%22,%22cmd%22:%22COOKIE_INACTIVE%22}}&callback=stuffBuffCookieInactive";
            document.getElementsByTagName("head")[0].appendChild(g);
            
        }        
    }
}

///////////////////Global Functions ////////////////////////////

function stuffBuffCookieInactive(data) {
    if(data.status === 'SUCCESS') {
        document.documentElement.removeChild(stuffBuffDiv);
    }
}
///////////////////////////////////////////////
function SB_doIframe(urlFrame) {
    /////////// Window Size //////////
    var winW = 630, winH = 460;
    if (document.body && document.body.offsetWidth) {
     winW = document.body.offsetWidth;
     winH = document.body.offsetHeight;
    }
    if (document.compatMode=='CSS1Compat' &&
        document.documentElement &&
        document.documentElement.offsetWidth ) {
     winW = document.documentElement.offsetWidth;
     winH = document.documentElement.offsetHeight;
    }
    if (window.innerWidth && window.innerHeight) {
     winW = window.innerWidth;
     winH = window.innerHeight;
    }
    /////////////////////////////////////
    backgroundDiv.style.width = winW+"px";
    backgroundDiv.style.height = winH+"px";
    backgroundDiv.style.display = "block";
    var dispWidth = document.body.clientWidth;
    var heightIfrm = winH-100;
    var heigthIframeDiv = winH-65;
    if(winW > 924) {
        ifrmWidth = 920;
        ifrdDivWidth = 924;
    }
    else {
        ifrmWidth = winW-54;
        ifrdDivWidth = winW-50;
    }
    var leftMarg = ((dispWidth / 2) - ifrdDivWidth / 2);
    var sb_logo_margin = (ifrdDivWidth / 2) - 70;
    iframeDiv.innerHTML = '<div style="border-top-left-radius: 5px; border-top-right-radius: 5px; border-bottom-left-radius: 5px; border-bottom-right-radius: 5px; position: fixed; top: 50px; left: '+ leftMarg +'px; height:'+heigthIframeDiv+'px; width: '+ifrdDivWidth+'px; z-index: 2147483645; background-color: #89BFE2; padding: 2px 5px 5px 5px;">'+
        '<div style="background:url('+config.imagesUrl+'quick_preview.png) no-repeat -32px 0; width:32px;height:32px; float: right; margin-right:20px;"></div>'+
        '<div style="background:url('+config.imagesUrl+'quick_preview.png) no-repeat; width:32px;height:32px; float: right; margin-right:10px;"></div>'+
        '<div style="background:url(http://stuffbuff/img/sb_logo_small.png) no-repeat; width:75px;height:32px;float:left;margin-left:'+sb_logo_margin+'px;"></div>'+
        '<div style="margin-left:10px;margin-top:12px;font-weight:bold;cursor:pointer;font-size:11px;color:#4F4F4F;height:15px;width:77px;float:left;">Learn More</div>'+
    '<iframe src="'+urlFrame+'" style="height:'+heightIfrm+'px;width:'+ifrmWidth+'px;"></iframe></div>';
    iframeDiv.style.display = 'block';
    var closeIframe = iframeDiv.firstChild.firstChild,
        learn_more = iframeDiv.firstChild.childNodes[3]
        opentInWindow = iframeDiv.firstChild.childNodes[1];
    closeIframe.style.cursor = 'pointer';
    opentInWindow.style.cursor = 'pointer';
    closeIframe.onclick = function() {
        backgroundDiv.style.display = "none";
        iframeDiv.style.display = 'none';
        while (iframeDiv.hasChildNodes()) {
            iframeDiv.removeChild(iframeDiv.lastChild);
        }
    }

    opentInWindow.onclick = function() {
        window.open(urlFrame, "_blank");
    }

    backgroundDiv.onclick = function() {
        backgroundDiv.style.display = "none";
        iframeDiv.style.display = 'none';
        while (iframeDiv.hasChildNodes()) {
            iframeDiv.removeChild(iframeDiv.lastChild);
        }
    }

    learn_more.onclick = function() {
        backgroundDiv.style.display = "none";
        iframeDiv.style.display = 'none';
        while (iframeDiv.hasChildNodes()) {
            iframeDiv.removeChild(iframeDiv.lastChild);
        }
        SB_doIframe(config.productImageUrl);
    }
}
///////////////////////////////////////////////
function stuffBuffFunctionLookup(data){
    cartDiv.onclick = function(){}
    if(data.title != null){
        clearTimeout(noResFound);
        c=document.createElement("script");c.defer="true";c.src=config.API_URL+"?query={%22stuffbuff%22:{%22affiliateId%22:%22"+SB_Affiliate_ID+"%22,%22productId%22:%22"+data.productId+"%22,%22cmd%22:%22GET_ITEM%22}}&callback=stuffBuffFunctionGetItem";document.getElementsByTagName("head")[0].appendChild(c);
        twitterButton.setAttribute('href','http://www.facebook.com/sharer/sharer.php?u=http://www.stuffbuff.com/product/none/'+data.title.replace(re, "-")+'/'+data.productId);
        twitterButton.setAttribute('target','_blank');
        facebookButton.setAttribute('href','http://twitter.com/home?status=http://www.stuffbuff.com/product/none/'+data.title.replace(re, "-")+'/'+data.productId);
        facebookButton.setAttribute('target','_blank');

    }
    else{
        noResFound = setTimeout(function(){
            clearTimeout(noResFound);
            stuffInfoDiv.style.display = 'none';
        },seccessSpeed+1200);
        twitterButton.setAttribute('href','#');
        twitterButton.setAttribute('target','_self');
        facebookButton.setAttribute('href','#');
        facebookButton.setAttribute('target','_self');
        cartDiv.innerHTML = '';
        itemDiv.innerHTML = '<center><div style="background:url('+config.imagesUrl+'img8.jpg) no-repeat;wight:50px;height:50px;margin-left:135px;"></div><br /><b style="color:#525760;padding-top:40px;font-size:15px;font-weight:bold;">Sorry, no results found</b><br /><a href="' + config.for_buyers_url + '">What is Stuff It?</a></center>';
        infoDiv.innerHTML = '<center><div style="background:url('+config.imagesUrl+'img8.jpg) no-repeat;wight:50px;height:50px;margin-left:135px;"></div><br /><b style="color:#525760;padding-top:40px;font-size:15px;font-weight:bold;">Sorry, no results found</b></center>';
        imagesDiv.innerHTML = '<center><div style="background:url('+config.imagesUrl+'img8.jpg) no-repeat;wight:50px;height:50px;margin-left:135px;"></div><br /><b style="color:#525760;padding-top:40px;font-size:15px;font-weight:bold;">Sorry, no results found</b></center>';
        stuffInfoDiv.onclick = function() {
            clearTimeout(noResFound);
        }        
    }
}
function stuffBuffFunctionGetItem(data){
    var img_url = config.productImageUrl +  data.thumb_img;
    if (data.thumb_img.substr(0,7) == 'http://') {
         img_url = data.thumb_img;
    }
    
    itemDiv.innerHTML = '<img src="' + img_url + '" style="float:left;width:100px;height:100px;" onerror="this.onerror=\'\';this.src=\''+config.imagesUrl+'no-image-zebra-lg.gif\';" />'+
        '<span style="font-size:13px;font-weight:bold;float:right;width:200px;font-family:Helvetica,Arial,Helvetica,sans-serif;color:#525760;">'+data.title+'</span><br />'+        
        '<span style="font-size:16px;font-weight:bold;float:right;width:200px;margin-top:10px;font-family:Helvetica,Arial,Helvetica,sans-serif;color:#525760;">$'+data.price+'</span>';        

        var imagesString = '';
        for (var i = 0; i < data.images.length; i++) {
                if(data.images[i]){
                    var img_url = config.productImageUrl + data.images[i];
                    if (data.images[i].substr(0,7) == 'http://') {
                        img_url = data.thumb_img;
                    }
                    imagesString += '<span style="width:200px;margin-left:50px;"><img src="'+img_url+'" style="width:200px;height:200px;margin-top:5px;border:1px solid #CDC9C9" onerror="this.onerror=\'\';this.src=\''+config.imagesUrl+'no-image-zebra-lg.gif\';" /></span>';
                }
        }    

    imagesDiv.innerHTML = '<span style="font-size:13px;font-weight:bold;font-family:Helvetica,Arial,Helvetica,sans-serif;color:#525760;">'+data.title+'</span><br />'+imagesString;

    infoDiv.innerHTML = '<span style="font-size:13px;font-weight:bold;font-family:Helvetica,Arial,Helvetica,sans-serif;color:#525760;">'+data.title+'</span><br />'+
        '<span style="font-size:13px;float:left;margin-top:10px;">'+data.description+'</span>';

    cartDiv.innerHTML = '<span style="color:white;font-size:14px;font-weight:bold;font-family:Arial,Helvetica,sans-serif;width:134px;height:31px;float:right;background:url('+config.imagesUrl+'bg-add-to-cart.gif) no-repeat;cursor:pointer;"><div style="margin-top:8px;padding-left:40px;">Add to Cart</div></span>';

    var cartButton = cartDiv.firstChild;    

    cartButton.onmouseover = function(){
        this.style.background = 'url('+config.imagesUrl+'bg-add-to-cart.gif) no-repeat 0 -31px';
    }
    cartButton.onmouseout = function(){
        this.style.background = 'url('+config.imagesUrl+'bg-add-to-cart.gif) no-repeat';
    }

    if(!data.third_party_vendor_url) {
        cartButton.onclick = function(){
            d=document.createElement("script");d.defer="true";d.src=config.API_URL+"?query={%22stuffbuff%22:{%22affiliateId%22:%22"+SB_Affiliate_ID+"%22,%22productId%22:%22"+data.productId+"%22,%22cmd%22:%22ADD_ITEM%22}}&callback=stuffBuffFunctionAddItem";document.getElementsByTagName("head")[0].appendChild(d);

            function cursorPrgress(event){
                this.style.cursor = 'progress';
            }
            var IE='\v'=='v';
            if(!IE){
                stuffInfoDiv.style.cursor = 'progress';
            }
        }

        viewCartDiv.onclick = function() {
            if(typeof SB_Button_POPUP_Active == 'undefined') {
                SB_doIframe(config.cartURL);
            }

            else {
                window.open(config.cartURL, "_blank");
            }
        }
    }
    else {
        cartDiv.innerHTML = '<span style="color:white;font-size:14px;font-weight:bold;font-family:Arial,Helvetica,sans-serif;width:134px;height:31px;float:right;background:url('+config.imagesUrl+'bg-add-to-cart.gif) no-repeat;cursor:pointer;"><div style="margin-top:8px;padding-left:40px;">View Store</div></span>';

        cartButton = cartDiv.firstChild;    

        cartButton.onmouseover = function(){
            this.style.background = 'url('+config.imagesUrl+'bg-add-to-cart.gif) no-repeat 0 -31px';
        }
        cartButton.onmouseout = function(){
            this.style.background = 'url('+config.imagesUrl+'bg-add-to-cart.gif) no-repeat';
        }

        cartDiv.onclick = function(){
            if(typeof SB_Button_POPUP_Active == 'undefined') {
                SB_doIframe(data.third_party_vendor_url);
            }
            else {
                window.open(data.third_party_vendor_url, "_blank");
            }
        }

        viewCartDiv.onclick = function() {
            if(typeof SB_Button_POPUP_Active == 'undefined') {
                SB_doIframe(data.third_party_vendor_url);
            }
            else {
                window.open(data.third_party_vendor_url, "_blank");
            }
        }
    }
}
function stuffBuffFunctionAddItem(data){
    if(data.status === 'SUCCESS'){        
        successDiv.style.display = 'block';
        successTimeout = setTimeout(function(){
                clearTimeout(successTimeout);
                successDiv.style.display = 'none';            
        },seccessSpeed);
        cartDiv.innerHTML = '<span style="text-decoration:none;color:white;font-family: Arial,Helvetica,sans-serif;"><span style="color:#61320A;font-size:15px;font-weight:bold;font-family:Arial,Helvetica,sans-serif;width:134px;height:31px;float:right;background:url('+config.imagesUrl+'view_cart.gif) no-repeat;cursor:pointer;"><div style="margin-top:8px;padding-left:35px;">View Cart</div></span></span>';
        viewButton = cartDiv.firstChild.firstChild;
        viewButton.onmouseover = function(){
            this.style.background = 'url('+config.imagesUrl+'view_cart.gif) no-repeat 0 -31px';
        }

        viewButton.onmouseout = function(){
            this.style.background = 'url('+config.imagesUrl+'view_cart.gif) no-repeat';
        }

        cartDiv.onclick = function() {
            if(typeof SB_Button_POPUP_Active == 'undefined') {
                SB_doIframe(config.cartURL);
            }

            else {
                window.open(config.cartURL, "_blank");
            }
        }

        viewCartDiv.onclick = function() {
            if(typeof SB_Button_POPUP_Active == 'undefined') {
                SB_doIframe(config.cartURL);
            }

            else {
                window.open(config.cartURL, "_blank");
            }
        }
    }
    stuffInfoDiv.style.cursor = 'auto';  
}

