
WebApp.Share = function(){
	var _elencoConf = [];
	var _elencoItem = [];
	var _oldElem = null;
	var _idContShare = "idcontainersharethis";
	var winPopup = null;
	
  function action(id,idConf) {
		var conf = _elencoConf[idConf];
    conf.win.width = conf.win.width == null ? 640 : conf.win.width;
    conf.win.height = conf.win.height == null ? 480 : conf.win.height;
    conf.win.scrollbars = conf.win.scrollbars == null ? 1 : conf.win.scrollbars;
    var url = conf.win.getUrl(WebApp.baseUrl + '/hcDocumento.do?id=' + id, _elencoItem["_" + id].title);
    if (winPopup != null) {
      try {
	      winPopup.close();
      }catch(e){}
    }                      
    winPopup = window.open(url, 'sharer', 'resizable=1,toolbar=0,status=0,width=' + conf.win.width + ',height=' + conf.win.height + ',scrollbars=' + conf.win.scrollbars);
    winPopup.focus(); 
  }
	
	return {
		shareTo : function(id, title,idCont, s) {
			if (s == null) {
				s = {top: 0, left: 0};
			}
			s.top = s.top == null ? 0 : s.top;
			s.left = s.left == null ? 0 : s.left;			
			var idContShare = "share_" + id;
			if (_elencoItem["_" + id] == null) {
  			var html = '<div class="shareWrapper" id="' + idContShare + '"><ul>';
				html += '<a href="javascript:void(0)" onclick="WebApp.Share.chiudi()" class="close">chiudi</a>';
  			_elencoItem["_" + id] = {};
  			_elencoItem["_" + id].title = title; 
  			for (var i = 0; i < _elencoConf.length; i++) {
  				html += '<li><a href="javascript:void(0)" onclick="WebApp.Share.action(' + id + ', ' + i + ')" class="' + _elencoConf[i].className + '">' + _elencoConf[i].label + '</a></li>';
  			}
  			html += '</ul></div>';
  			_elencoItem["_" + id].html = html;				
			}
			_oldElem = document.getElementById(_idContShare);
			if (_oldElem == null) {
				_oldElem = document.createElement("div");
				_oldElem.id = _idContShare;
				var cont = document.getElementsByTagName("body")[0];
				cont.appendChild(_oldElem);
			}
			_oldElem.innerHTML = _elencoItem["_" + id].html;
			_oldElem.style.display = "block";
			var pos = Element.getPosition(document.getElementById(idCont));
			_oldElem.style.position = "absolute";
			_oldElem.style.top = (pos.y - (_elencoConf.length * 20) + s.top) + 'px';
			_oldElem.style.left = (pos.x + s.left) + 'px';
		},
		chiudi : function() {
			var obj = document.getElementById(_idContShare);
			if (obj != null) {
				obj.style.display = "none";
			}
		},
		action : function(id,i) {
			action(id,i)
		},
		addConfiguration : function(conf) {
			conf.enabled = conf.enabled == null ? true : conf.enabled;
			if (conf.enabled) {
				_elencoConf[_elencoConf.length] = conf;
			}
		}
	}
}();


WebApp.Share.addConfiguration({
		label: 'MySpace',
  	className: 'myspace', 
  	win: {getUrl: function(U,T){
            return 'http://www.myspace.com/index.cfm?fuseaction=postto&t=' + encodeURIComponent(T) + '&u=' + encodeURIComponent(U.replace(/\?/,'/').replace(/&|=/gi,'/')) + '&l=3';
        }, 
	  	width: 800 
		}                    
});
WebApp.Share.addConfiguration({
		label: 'Facebook',
		className: 'facebook', 
		win: {getUrl: function(U,T){
						return 'http://www.facebook.com/sharer.php?u=' + encodeURIComponent(U) + '&t='+encodeURIComponent(T);
      	}, 
			scrollbars: 0 
		}                    
});
WebApp.Share.addConfiguration({
		label: 'Twitter', 
		className: 'twitter', 
  	win: {getUrl: function(U,T){
  					return 'http://twitter.com/home?status=' + encodeURIComponent(U + '&tn' + T);
        }, 
		  	width: 800  
			}                 
});
WebApp.Share.addConfiguration({
		label: 'Digg', 
		className: 'digg', 
		win: {getUrl: function(U,T){
						return 'http://digg.com/submit?url=' + encodeURIComponent(U) + '&title=' + encodeURIComponent(T) + '&topic=TOPIC';
      	}, 
			width: 950
		}                      
});
WebApp.Share.addConfiguration({
		label: 'Windows Live',
		className: 'live', 
		win: {getUrl: function(U,T){
						return 'https://skydrive.live.com/sharefavorite.aspx%2f.SharedFavorites??&mkt=en-us&url=' + encodeURIComponent(U) + '&title=' +encodeURIComponent(T) + '&top=1';
      }
		}                    
});
WebApp.Share.addConfiguration({
		label: 'FriendFeed',
		className: 'friendfeed', 
		win: {getUrl: function(U,T){
						return 'http://friendfeed.com/?url=' + encodeURIComponent(U) + '&title=' + encodeURIComponent(T);
      }, 
			width: 950
		}                    
});
WebApp.Share.addConfiguration({
		label: 'Delicious',
		className: 'delicious', 
		win: {getUrl: function(U,T){
						return 'http://delicious.com/save?url=' + encodeURIComponent(U) + '&title=' + encodeURIComponent(T) + '&notes=&tags=&v=4&noui=&share=yes&jump=close';
      }, 
			width: 850
		}                    
});
WebApp.Share.addConfiguration({
		label: 'Google',
		className: 'google', 
		win: {getUrl: function(U,T){
						return 'http://www.google.com/bookmarks/mark?op=add&nui=1&service=bookmarks&bkmk=' + encodeURIComponent(U) + '&title='+encodeURIComponent(T);
      }, 
			width: 700
		}                     
});
