var globalWBR;
var kamakama = 0;
var googStart = 1;
var googTitle = 'Annunci Google';
var googNorow = '';
var googRow = '';
var googRight = '';
var googSave = new Array();

/*{{{ BrowserDetect */
var BrowserDetect = {
init: function () {
          this.browser = this.searchString(this.dataBrowser) || "unknown";
          this.version = this.searchVersion(navigator.userAgent)
              || this.searchVersion(navigator.appVersion)
              || "unknown";
          this.OS = this.searchString(this.dataOS) || "unknown";
          this.wb = this.wordBreak();        
      },
searchString: function (data) {
                  for (var i=0;i<data.length;i++) {
                      var dataString = data[i].string;
                      var dataProp = data[i].prop;
                      this.versionSearchString = data[i].versionSearch || data[i].identity;
                      if (dataString) {
                          if (dataString.indexOf(data[i].subString) != -1)
                              return data[i].identity;
                      }
                      else if (dataProp)
                          return data[i].identity;
                  }
              },
searchVersion: function (dataString) {
                   var index = dataString.indexOf(this.versionSearchString);
                   if (index == -1) return;
                   return parseFloat(dataString.substring(index+this.versionSearchString.length+1));
               },
wordBreak: function () {
               if ( this.browser == 'Opera'  || this.browser == 'Safari' || (this.browser == 'Explorer' && this.version <= 5.5))
                   return '&shy;';
               else
                   return '<wbr>';
           },
dataBrowser: [
             { string: navigator.vendor, subString: "Apple", identity: "Safari" },
             { prop: window.opera, identity: "Opera" },
             { string: navigator.userAgent, subString: "MSIE", identity: "Explorer", versionSearch: "MSIE" }
      ],
          dataOS : [
          { string: navigator.platform, subString: "Win", identity: "Windows" },
          { string: navigator.platform, subString: "Mac", identity: "Mac" },
          { string: navigator.platform, subString: "Linux", identity: "Linux" }
      ]

};
/*}}}*/

BrowserDetect.init();
googWbr = BrowserDetect.wb;

/*{{{ highlightText */
function highlightText( testo, ricerca, startTag, endTag) {
if ((!startTag) || (!endTag)) {
    startTag = "<b>";
    endTag = "</b>";
}
var newText = "";
var i = -1;
var lcSearchTerm = ricerca.toLowerCase();
var lcBodyText = testo.toLowerCase();

while (testo.length > 0) {
    i = lcBodyText.indexOf(lcSearchTerm, i+1);
    if (i < 0) {
        newText += testo;
        testo = "";
    } else {
        // skip anything inside an HTML tag
        if (testo.lastIndexOf(">", i) >= testo.lastIndexOf("<", i)) {
            // skip anything inside a <script> block
            if (lcBodyText.lastIndexOf("/script>", i) >= lcBodyText.lastIndexOf("<script", i)) {
                newText += testo.substring(0, i) + startTag + testo.substr(i, ricerca.length) + endTag;
                testo = testo.substr(i + ricerca.length);
                lcBodyText = testo.toLowerCase();
                i = -1;
            }
        }
    }
}

return newText;
}
/*}}}*/


/*{{{ google_ad_safe() */
function google_ad_safe() {

    var s = '';
        
    if (googSave.length > 0) {
        var dsc = '';
        var i;

        s += '<div class="gsponsor '+ googNorow +'">';
        s += '<h4>'+ googTitle +'</h4>';
        for(i=0; i< googSave.length - 1; ++i) {
            dsc = googSave[i].line2 + ' ' + googSave[i].line3;
            vis_url = new String(googSave[i].visible_url);

            var rex = {'&amp;':'&','\/':'/','&':'&','%':'%','=':'=','\\?':'?','\\.':'.'};
            for (j in rex) {
                var re = new RegExp(j, "gi");
                vis_url = vis_url.replace(re, rex[j] + googWbr);
            }

            s += '<p class="spazio">';
            s += '<a href="' + googSave[i].url + '" ';
            s += 'onMouseOut="window.status=\'\'; return true;" ';
            s += 'onMouseOver="window.status=\'go to http://'+googSave[i].visible_url+'\'; return true;" class="gtit" rel="nofollow">';
            s += googSave[i].line1;
            s += '</a><br />';
            s += '<a href="'+ googSave[i].url+'" ';
            s += 'onMouseOut="window.status=\'\'; return true;" ';
            s += 'onMouseOver="window.status=\'go to http://'+googSave[i].visible_url+'\'; return true;" class="gtxt" rel="nofollow">';
            s += dsc;
            s += '</a>';
            s += '<br />';
            s += '<small><a href="'+googSave[i].url+'" ';
            s += 'onMouseOut="window.status=\'\'; return true;" ';
            s += 'onMouseOver="window.status=\'go to http://'+googSave[i].visible_url+'\'; return true;" class="glnk" rel="nofollow">';
            s += vis_url;
            s += '</a> </small> </p>';
            googStart += 1;
        } 
        //s += '<label style="display:block; font-size: 10px; text-align: right; font-weight: bold; color: #999999">Annunci Google</label>';
        s += '</div>';
        if (document.getElementById('GSAFE')) {
            document.getElementById('GSAFE').innerHTML = s;
        }
    }

    googNorow = '';
    //document.write(s);
    return;
}
/*}}}*/


/*{{{ google_ad_request(pos, just, len) */
function google_ad_request(pos, just, len) {

    var s = '';
    var i;

    //len = undefined;

    if (len == undefined) {
        len = global_xgiox_ads.length - googStart + 1;
    }

    first = googStart - 1;
    last = first + len - 1;

    if (global_xgiox_ads.length <= last)
        last = global_xgiox_ads.length - 1;

    if (global_xgiox_ads.length == 0) {
        return;
    }

    if (first > global_xgiox_ads.length) {
        return;
    }

    /*
    var t_z = new String(window.location.href);
    if(t_z.match(/\.tecnozoom\./i) && pos == 'TCR') {
        pos = 'TCR2';
    }*/

   
    //if (global_xgiox_ads[first].type == 'text') {
        if (global_xgiox_ads.length > first) {
            if (global_xgiox_ads.length < last) {
                last = global_xgiox_ads.length;
            }
            switch(pos) {
                case 'TC1':
                case 'TC2':
                case 'TCR2':
                case 'TCMORE':
                    if (pos == 'TC2') {
                        googNorow = 'noriga';
                        googRow   = 'rigabottom';
                        googRight = 'ris-google-right';
                    }            

                    var dsc = '';

                    s += '<ul>';
                    for(i=first; i<= last; ++i) {

                        dsc = global_xgiox_ads[i].line2+' '+global_xgiox_ads[i].line3;
                        vis_url = new String(global_xgiox_ads[i].visible_url);

                        var rex = {'&amp;':'&','\/':'/','&':'&','%':'%','=':'=','\\?':'?','\\.':'.'};
                        for (j in rex) {
                            var re = new RegExp(j, "gi");
                            vis_url = vis_url.replace(re, rex[j] + googWbr);
                        }

                        s += '<li>';
                        s += '<h1><a href="'+global_xgiox_ads[i].url+'" onMouseOver="window.status=\'go to http://'+global_xgiox_ads[i].visible_url+'\'; return true;" onMouseOut="window.status=\'\'; return true;">'+global_xgiox_ads[i].line1+'</a></h1>';
                        s += '<div class="desc"><a href="'+global_xgiox_ads[i].url+'" onMouseOver="window.status=\'go to http://'+global_xgiox_ads[i].visible_url+'\'; return true;" onMouseOut="window.status=\'\'; return true;">'+dsc+'</a></div>';
                        s += '<div class="link"><a href="'+global_xgiox_ads[i].url+'" onMouseOver="window.status=\'go to http://'+global_xgiox_ads[i].visible_url+'\'; return true;" onMouseOut="window.status=\'\'; return true;" class="ris-web">'+vis_url+'</a></div>';
                        s += '</li>';

                        googStart += 1;

                    if (pos == 'TCR') {
                        googTitle1 = googTitle;
                        googTitle = 'Collegamenti sponsorizzati';
                    }  

                    if (pos == 'TCR')
                        googTitle = googTitle1;
                    }
                    s += '</ul>';
                    s += '<div class="title">'+ googTitle +'</div>';

                    break;
                case 'TCR':
                    if (pos == 'TC2') {
                        googNorow = 'noriga';
                        googRow   = 'rigabottom';
                        googRight = 'ris-google-right';
                    }            

                    var dsc = '';

                    for(i=first; i<= last; ++i) {

                        dsc = global_xgiox_ads[i].line2+' '+global_xgiox_ads[i].line3;
                        vis_url = new String(global_xgiox_ads[i].visible_url);

                        var rex = {'&amp;':'&','\/':'/','&':'&','%':'%','=':'=','\\?':'?','\\.':'.'};
                        for (j in rex) {
                            var re = new RegExp(j, "gi");
                            vis_url = vis_url.replace(re, rex[j] + googWbr);
                        }

                        azizu = '';
                        if (i == last) {
                            azizu = ' ris-google-search-last';
                        }

                        s += '<div class="item item_google">';
                        s += '<h1><a href="'+global_xgiox_ads[i].url+'" onMouseOver="window.status=\'go to http://'+global_xgiox_ads[i].visible_url+'\'; return true;" onMouseOut="window.status=\'\'; return true;" >'+global_xgiox_ads[i].line1+'<span>Annunci Google</span></a></h1>';
                        s += '<div class="txt"><a href="'+global_xgiox_ads[i].url+'" onMouseOver="window.status=\'go to http://'+global_xgiox_ads[i].visible_url+'\'; return true;" onMouseOut="window.status=\'\'; return true;" >'+dsc+'</a></div>';
                        s += '<div class="meta"><a href="'+global_xgiox_ads[i].url+'"';
                        s += 'onMouseOver="window.status=\'go to http://'+global_xgiox_ads[i].visible_url+'\'; return true;" ';
                        s += 'onMouseOut="window.status=\'\'; return true;" class="ris-web">'+vis_url+'</a></div>';
	                    s += '</div>';

                        googStart += 1;

                        var parole = searchedkey.split(" ");
                        for (j = 0; j < parole.length; j++){
                            s = highlightText(s, parole[j], "<b>", "</b>");
                        }
                    }
                    if (pos == 'TCR') {
                        googTitle1 = googTitle;
                        googTitle = 'Collegamenti sponsorizzati';
                    }  
                    if (pos == 'TCR')
                        googTitle = googTitle1;

                    break;
                case 'TCSAVE':
                    k = 0;
                    last = global_xgiox_ads.length;
                    if (first <=last) {
                        for (i=first; i<= last; ++i) {
                            googSave[k] = global_xgiox_ads[i];
                            k += 1;
                            googStart += 1;
                        }
                    }
                    s = '';
                    break;
            }
        }

    //} 

    document.write(s);
    googNorow = '';
    googRow = '';
    googRight = '';
    return;
}
/*}}}*/
