    
    var jSharingBox = new Object();
	jSharingBox.context4LogBI={};
	
	function clickLogBIGooglePlus() {
		clickLogBI("googleplus", jSharingBox.context4LogBI);
	};
	
	function clickLogBI(service, trgurl, sharingboxcontext) {
		var randomNumber = Math.floor(1000000*Math.random());
		var urlBI = "http://adimg.virgilio.it/tracks/bi/images/bi_clk.gif?"
			+ "RND="+randomNumber
			+ "&CONTEXT="	+ sharingboxcontext.biCONTEXT
			+ "&ACTION=" + sharingboxcontext.biACTION
			+ "&FQDN=" + sharingboxcontext.biKW
			+ "&POSID=" + sharingboxcontext.biPosId[service]
			+ "&TITOLO=" + encodeURIComponent(sharingboxcontext.biTITOLO)
			+ "&CONTENT=" + service
			+ "&TOPOLOGY=" + sharingboxcontext.biTOPOLOGY
			+ "&PAGEFROM=" + sharingboxcontext.pageurl
			+ "&TRGURL=" + trgurl;
			custom = sharingboxcontext.biCUSTOM;
		if (custom != undefined && custom != null)
			urlBI += "&" + custom;
		jQuery("#"+sharingboxcontext.currentObjId).remove('#'+sharingboxcontext.currentObjId+'_shr_box_img');
		jQuery("#"+sharingboxcontext.currentObjId).append('<img width="1" height="1" id="'+sharingboxcontext.currentObjId+'_shr_box_img" src="'+urlBI+'" />');
	}
	
	
    //extend jquery with the plugin
    jSharingBoxClass=function(){
        this.defaults= {
            finalurl : {
                'print' : 'window.print()',
                'mail' : '/common/VIRGILIO/sendtoafriend/popup_invia.html?pageTitle=@##title##@&linkdainviare=@##url##@',
                'facebook' : 'http://www.facebook.com/sharer.php?u=@##url##@',
                'twitter' : 'http://twitter.com/share?text=@##title##@&url=@##url##@',
                'oknotizie' : 'http://oknotizie.alice.it/post?title=@##title##@&url=@##url##@',
                'segnalo' : 'http://segnalo.virgilio.it/post.html.php?url=@##url##@',
                'people' : 'http://virgiliopeople.virgilio.it/share/pages/index.html?url=@##url##@',
                'windowslive' : 'http://profile.live.com/badge?title=@##title##@&url=@##url##@&screenshot=@##screenshot##@',
                'fbconnect' : ''
            },
            biPosId : {
                'facebook' : '1050',
                'twitter' : '1051',
                'oknotizie' : '1054',
                'segnalo' : '1053',
                'people' : '1057',
                'windowslive' : '1055',
                'print' : '001',
                'mail' : '002',
                'fb-like' : '2010',
                'fb-recommend' : '2020',
                'fb-none' : '2030',
                'googleplus' : '2040'
            },
            patterMod : {
                'mod1' : '(facebook)|(twitter)|(windowslive)|(googleplus)',
                'mod2' : '(oknotizie)|(segnalo)|(people)|(print)|(mail)',
                'mod4' : '(fb-like)|(fb-recommend)|(fb-none)',
                'mod5' : '(fb-like)|(fb-recommend)|(fb-none)'
            },
            currentObjId:"shr_box",
            pageurl : window.location.href,
            pagetitle : document.title ,
            inviamail : '/common/VIRGILIO/sendtoafriend/popup_invia.html?linkdainviare='+escape(window.location.href),
            show_cond : new Array( 'facebook', 'twitter', 'windowslive', 'googleplus', 'fb-recommend', 'oknotizie', 'segnalo', 'people', 'print', 'mail'),
            biCONTEXT : 'virgilio',
            biACTION : 'NAV',
            biKW: getWebabacusCookie(),
            biTITOLO : document.title,
            biTOPOLOGY : document.location.host
    
        }
        
        this.getContext= function(){
			return this.sharingboxcontext;
		}
        this.setPageUrl= function (pageUrl){
		    var configPageUrl = {"pageurl":pageUrl}
			this.sharingboxcontext = $.extend({}, this.sharingboxcontext, configPageUrl)
			jSharingBox.context4LogBI = this.sharingboxcontext;
			var sSBC =  this.sharingboxcontext.show_cond.toString();
            if (sSBC.indexOf('googleplus')>-1){
                jQuery("#"+this.sharingboxcontext.currentObjId+"_shr_gpl").empty().html(createGPlusboxIn(this.sharingboxcontext));
            }
			return this.sharingboxcontext;
        },
		
        this.jSharingbox=function(config, fileNames) {
            //use defaults or properties supplied by user
            
            this.sharingboxcontext = $.extend({}, this.defaults, config);
			jSharingBox.context4LogBI = this.sharingboxcontext;
            //var sharingboxcontext = this.sharingboxcontext;
            //get the id of the selected element
            
            var sFinalbox='';
            var  html_mod1 = '';
            var  html_mod2 = '';
            var  html_mod4 = '';
            var  html_mod5 = '';

            //impostazioni di default
            var sSBC =  this.sharingboxcontext.show_cond.toString();
            if (getVerifyService('mod1',sSBC, this.sharingboxcontext).length > 0){
                html_mod1 += '<div id="'+this.sharingboxcontext.currentObjId+'_shr_mod1" class="shr_mod1">';
                if (sSBC.indexOf('facebook')>-1)
                        html_mod1 +='<a id="'+this.sharingboxcontext.currentObjId+'_shr_fb" class="shr_fb" href="#"><span class="shr_tlt" style="left: -35.5px;">Facebook</span></a>';
                if (sSBC.indexOf('twitter')>-1)
                        html_mod1 +='<a id="'+this.sharingboxcontext.currentObjId+'_shr_tw" class="shr_tw" href="#"><span class="shr_tlt" style="left: -35.5px;">Twitter</span></a>';
                if (sSBC.indexOf('windowslive')>-1)
                        html_mod1 +='<a id="'+this.sharingboxcontext.currentObjId+'_shr_wl" class="shr_wl" href="#"><span class="shr_tlt" style="left: -35.5px;">Windows Live</span></a>';
                if (sSBC.indexOf('googleplus')>-1){
					  html_mod1 += createGPlusbox(this.sharingboxcontext);
                }
                html_mod1 += '</div>';
            }

            if (getVerifyService('mod4',sSBC, this.sharingboxcontext).length > 0){
                html_mod4 += '<div id="'+this.sharingboxcontext.currentObjId+'_shr_mod4" class="shr_mod4">';
                if (sSBC.indexOf('fb-like')>-1)
                    html_mod4 +=createFBbox('like', 'button_count', this.sharingboxcontext);
                if (sSBC.indexOf('fb-recommend')>-1)
                    html_mod4 +=createFBbox('recommend', 'button_count', this.sharingboxcontext);
                html_mod4 += '</div>';
            }

            if (getVerifyService('mod2',sSBC, this.sharingboxcontext).length > 0){
                html_mod2 += '<div id="'+this.sharingboxcontext.currentObjId+'_shr_mod2" class="shr_mod2"><a href="#"></a><div id="'+this.sharingboxcontext.currentObjId+'_shr_cnt"><div class="aligner">';
                if (sSBC.indexOf('oknotizie')>-1)
                        html_mod2 +='<a id="'+this.sharingboxcontext.currentObjId+'_shr_on" href="#" class="shr_on"><span class="shr_tlt" style="left: -50px;">Ok Notizie</span></a><span></span>';
                if (sSBC.indexOf('segnalo')>-1)
                        html_mod2 +='<a id="'+this.sharingboxcontext.currentObjId+'_shr_sn" href="#" class="shr_sn"><span class="shr_tlt" style="left: -50px;">Segnalo</span></a><span></span>';
                if (sSBC.indexOf('people')>-1)
                        html_mod2 +='<a id="'+this.sharingboxcontext.currentObjId+'_shr_pp" href="#" class="shr_pp"><span class="shr_tlt" style="left: -50px;">People</span></a><span></span>';
            if (sSBC.indexOf('print')>-1)
                html_mod2 +='<a id="'+this.sharingboxcontext.currentObjId+'_shr_st" href="#" class="shr_st"><span class="shr_tlt" style="left: -50px;">Stampa</span></a><span></span>';
            if (sSBC.indexOf('mail')>-1)
                html_mod2 +='<a id="'+this.sharingboxcontext.currentObjId+'_shr_in" href="#" class="shr_in"><span class="shr_tlt" style="left: -50px;">Invia</span></a><span></span>';
                html_mod2 += '</div></div></div>';
            }

            if (getVerifyService('mod5',sSBC, this.sharingboxcontext).length > 0){
                html_mod5 += '<div id="'+this.sharingboxcontext.currentObjId+'_shr_mod5" class="shr_mod5">';
                if (sSBC.indexOf('fb-like')>-1)
                        html_mod5 +=createFBbox('like', 'standard', this.sharingboxcontext);
                if (sSBC.indexOf('fb-recommend')>-1)
                        html_mod5 +=createFBbox('recommend', 'standard', this.sharingboxcontext);
                html_mod5 += '</div>';
                jQuery('#articoloSu, .su_articolo').append(html_mod5);
            }

            var listModule = getListModule(sSBC, this.sharingboxcontext);
            for (i=0;i<listModule.length;i++)
                sFinalbox += eval('html_' + listModule[i]);
            
            
            jQuery('#'+this.sharingboxcontext.currentObjId).empty(); 
            jQuery('#'+this.sharingboxcontext.currentObjId).html(sFinalbox);
            jQuery('#'+this.sharingboxcontext.currentObjId+'_shr_mod2 span:last-child').remove();
			var sharingboxcontext4Link = this.sharingboxcontext;
			jQuery('.shr_tlt').each(function(){
				var shrg = (jQuery(this).parent().width())/2;                           
				var tlt_left = (-50+shrg);
				jQuery(this).css('left',tlt_left)
			})    
			jQuery('#'+this.sharingboxcontext.currentObjId+'_shr_mod2>a').click(function(){
				jQuery('#'+tsharingboxcontext4Link.currentObjId+'_shr_mod2').toggleClass('shr_on');
				jQuery('#'+sharingboxcontext4Link.currentObjId+'_shr_cnt').toggleClass('shr_vis');
				return false
			})
			jQuery('#'+this.sharingboxcontext.currentObjId+'_shr_cnt a').click(function(){
				jQuery('#'+sharingboxcontext4Link.currentObjId+'_shr_mod2').removeClass('shr_on');
				jQuery('#'+sharingboxcontext4Link.currentObjId+'_shr_cnt').removeClass('shr_vis')
			})
			jQuery('BODY').click(function(){
				jQuery('#'+sharingboxcontext4Link.currentObjId+'_shr_mod2').removeClass('shr_on');
				jQuery('#'+sharingboxcontext4Link.currentObjId+'_shr_cnt').removeClass('shr_vis')
			})
			jQuery("#"+this.sharingboxcontext.currentObjId+"_shr_in").click(function (){
				clickShareStin('mail');
			})
			jQuery("#"+this.sharingboxcontext.currentObjId+"_shr_st").click(function (){
				clickShareStin('print');
			})
			jQuery("#"+this.sharingboxcontext.currentObjId+"_shr_pp").click(function (){
				clickShare('people', sharingboxcontext4Link);
			})
			jQuery("#"+this.sharingboxcontext.currentObjId+"_shr_sn").click(function (){
				clickShare('segnalo', sharingboxcontext4Link);
			})
			jQuery("#"+this.sharingboxcontext.currentObjId+"_shr_on").click(function (){
				clickShare('oknotizie', sharingboxcontext4Link);
			})
			jQuery("#"+this.sharingboxcontext.currentObjId+"_shr_wl").click(function (){
				clickShare('windowslive', sharingboxcontext4Link);
			})
			jQuery("#"+this.sharingboxcontext.currentObjId+"_shr_tw").click(function (){
				clickShare('twitter', sharingboxcontext4Link);
			})
			jQuery("#"+this.sharingboxcontext.currentObjId+"_shr_fb").bind('click',function (){
				clickShare('facebook', sharingboxcontext4Link);
			})
        }
        
    function getServiceUrl(service, sharingboxcontext){
        return sharingboxcontext.finalurl[service].split('?', 1);
    }
    function getVerifyService(modulo, services, sharingboxcontext){
        var sPattern = sharingboxcontext.patterMod[modulo];
        var regPattern = new RegExp(sPattern, "g");
        var regResult = services.match(regPattern);
        regResult = regResult == null ? '' : regResult;
        return regResult;
    }
    function getListModule(services, sharingboxcontext){
        services = services.replace(new RegExp(sharingboxcontext.patterMod.mod1, "g"),'mod1');
        services = services.replace(new RegExp(sharingboxcontext.patterMod.mod2, "g"),'mod2');
        services = services.replace(new RegExp(sharingboxcontext.patterMod.mod4, "g"),'mod4');
        return(getUniqueModule(services.split(',')));
    }
    function getUniqueModule(listaMod) {
        var i, e, o = new Object();
        for (i = 0; e = listaMod[i]; i++) {o[e] = 1;};
        var a = new Array();
        for (e in o) {a.push(e);};
        return a;
    } 
    
	function createFBbox(fbVerb, fbLayout, sharingboxcontext) { 
        var FBbox = '';
        var fbWidth = fbLayout == 'standard' ? fbWidth = 530 : fbWidth = 130;
        FBbox += '<iframe id="'+sharingboxcontext.currentObjId+'_shr_fbrec" src="http://www.facebook.com/plugins/like.php?'
            +'href='+encodeURIComponent(sharingboxcontext.pageurl)
            +'&amp;layout=' + fbLayout
            +'&amp;ref=' + 'shrbox_' + fbVerb
            +'&amp;show_faces=true'
            +'&amp;width=' + fbWidth
            +'&amp;action='+fbVerb    // standard, button_count
            +'&amp;colorscheme=light&amp;height=25&amp;locale=it_IT" scrolling="no" frameborder="0" ' 
            +'style="border:none; overflow:hidden; width:'+fbWidth+'px; height:25px;" allowTransparency="true">'
            +'</iframe>'
        return FBbox;
    }

	function createGPlusbox(sharingboxcontext) {
		return '<div id="'+sharingboxcontext.currentObjId+'_shr_gpl" class="shr_gpl">'
				+createGPlusboxIn(sharingboxcontext)
				+'</div>';
	}
	
	
  function createGPlusboxIn(sharingboxcontext) {
		return '<div class="g-plusone" data-href="'+sharingboxcontext.pageurl+'" data-callback="clickLogBIGooglePlus"></div>' 
			+ '<script type="text/javascript">'
			+ 'jQuery(function(){'
			+ '     gapi.plusone.go("'+sharingboxcontext.currentObjId+'_shr_gpl");})'
			+ '</script>';
			
  }
  
  
    function getWebabacusCookie(){
        c_name = 'webabacus_id';
        if (document.cookie.length>0){
          c_start=document.cookie.indexOf(c_name + '=');
          if (c_start!=-1){
              c_start=c_start + c_name.length+1;
              c_end=document.cookie.indexOf(';',c_start);
              if (c_end==-1)
          c_end=document.cookie.length;
              return unescape(document.cookie.substring(c_start,c_end));
            }
        }
        return '';
    }
    
       
    function clickShare(service, sharingboxcontext) {
        // chiamata verso il recommender
        try {
            new RecService().trackItemRequest('','','share');
        }catch(e){
        }
        // funzione libera per permettere alla pagina di gestire i parametri prima del click,
        // ad esempio la modifica della url prima di fare lo share
        try{
            sharingboxcontext.initClick();
        }catch(e){
        }
		try{
			context4LogBI=sharingboxcontext;
			sharingboxcontext = sharingboxcontext.setUrlbeforeClick();       
		}catch(e){
        }
			
        var shrparamurl = '';
        if (sharingboxcontext.pageurl.indexOf('?') != -1)
            shrparamurl = '&shrbox=';
        else
            shrparamurl = '?shrbox=';

        // fix per IE
        if (jQuery.browser.msie) {
            sharingboxcontext.pagetitle = document.title;
            sharingboxcontext.biTITOLO = document.title;
        }
        
        urlPopup = sharingboxcontext.finalurl[service].replace("@##url##@", encodeURIComponent(sharingboxcontext.pageurl+shrparamurl+service));
        urlPopup = urlPopup.replace("@##title##@", encodeURIComponent(sharingboxcontext.pagetitle.replace(/\n/g,' ')));
        urlPopup = urlPopup.replace("@##screenshot##@", encodeURIComponent(jQuery("link[rel='image_src']:first").attr("href")));
        // click tracciamento BI
        clickLogBI(service, urlPopup, sharingboxcontext);
        window.open(urlPopup,'sharewin','menubar=no,location=yes,resizable=yes,scrollbars=yes,status=yes,width=1000,height=400');
        return false;
    }
    function clickShareStin(service, sharingboxcontext){
        // aggiungo parametro di riconoscimento per back url
        if (service == "print"){
            window.print();
        }else {
            // funzione libera per permettere alla pagina di gestire i parametri prima del click,
            // ad esempio la modifica della url prima di fare lo share
            try{
                sharingboxcontext.initClick();
            }catch(e){
            }
            var shrparamurl = '';
            if (sharingboxcontext.pageurl.indexOf('?') != -1)
                shrparamurl = '&shrbox=';
            else
                shrparamurl = '?shrbox=';
               
            // fix per IE
            if (jQuery.browser.msie) {
                sharingboxcontext.pagetitle = document.title;
                sharingboxcontext.biTITOLO = document.title;
            }
            
            var urlInviaMail = "";
            if (sharingboxcontext.inviamail==undefined || sharingboxcontext.inviamail==null)
            {
                urlInviaMail = finalurl.mail;
            }
            else
            {
                if (sharingboxcontext.inviamail.indexOf("?")!=-1)
                    urlInviaMail = sharingbox.config.inviamail.substring(0,sharingboxcontext.inviamail.indexOf("?"));
                else
                    urlInviaMail = sharingboxcontext.inviamail;
                
                urlInviaMail=urlInviaMail +"?pageTitle=@##title##@&linkdainviare=@##url##@";
            }    
            urlPopup = urlInviaMail.replace("@##url##@", encodeURIComponent(sharingboxcontext.pageurl+shrparamurl+service));
            urlPopup = urlPopup.replace("@##title##@", encodeURIComponent(sharingboxcontext.pagetitle));
            window.open(urlPopup,'mailpost','toolbar=no,location=no,status=no,menubar=no,scrollbars=no,resizable=no,width=584,height=690');
        }
    }
    };
	
	


