if(!EL2) {
  var EL2 = {
	_version :'1.0',
	namespace : function(namespace) {
		var _names = namespace.split(".");
		var _root = this;
		for ( var i = 0; i < _names.length; i++) {
			if (_root[_names[i]] == undefined) {
				_root[_names[i]] = {};
			}
			_root = _root[_names[i]];
		}
	},
	host : ''
  };
}

EL2.namespace("core");
EL2.core = {
	name : 'ns:EL2.core',
	extend : function(src,dest) {
		for(key in src) {
			if(src[key] != undefined) dest[key] = src[key];
		}
		return dest;
	},
	loadModule : function(url) {
		var _source = document.createElement('script');
		_source.setAttribute('type', 'text/javascript');
		_source.setAttribute('src', url);
		document.getElementsByTagName('head')[0].appendChild(_source);
	},
	bbimport : function(module) {		
		loadModule("js/" + module);
	}, 
	log : function(str, obj) {
		
	}
};
 EL2.host = "http://api.elephant2.com";if(!$J) {
	var $J = $;
}

EL2.namespace("sb");
EL2.sb = {
	name : "ns:EL2.namespace.sb",
	_locale : {},
	el2D : document,
	el2H : EL2.host,
	el2I : EL2.host + "/el2sb/images/",
	el2_pgtitle : document.title,
	el2_pgurl : document.location.href,
	el2_timer : {},
	el2_delay : 800,
	el2_hex : '999',
	el2_size : {'width':'320','height':'20'},
	el2_img : EL2.host + '/el2sb/images/toolbar320.gif',
	el2_background: 'rect',
	el2_cssfile: null,
	el2_sid: 1,
	el2_lang : 'pt-BR',	
	el2_sbs_top: 0,
	el2_sbs_left: 0,
	el2_container : 'el2sb',
	el2_sbs : {"sbs":[	
	    /*{"name":"+1","dir":"plusone","css":"el2_pone","callback":"plusOneCallback","href":"@@U@@", "htmlTag":"g:plusone", "size":"small", "count":"false"},*/                  
	    {"name":"Facebook","dir":"facebook","css":"el2_fbok","url":"http://www.facebook.com/share.php?u=@@U@@"},
	    {"name":"Twitter","dir":"twitter","css":"el2_twitt","url":"http://www.twitter.com/?status=@@T@@%20-%20@@U@@"},
	    /*{"name":"Google Buzz","dir":"gbuzz","css":"el2_gbuzz","url":"http://www.google.com/buzz/post?url=@@U@@&imageurl=@@T@@"},*/	    
	    {"name":"Linkk","dir":"linkk","css":"el2_linkk","url":"http://www.linkk.com.br/submit.php?url=@@U@@"},
	    {"name":"Sonico","dir":"sonico","css":"el2_gsonic","url":"http://www.sonico.com/share.php?title=@@T@@&url=@@U@@"},
	    {"name":"Google Reader","dir":"greader","css":"el2_gread","url":"xxx"},	    	     		
	    {"name":"Plaxo","dir":"plaxo","css":"el2_plax","url":"xxx"},
	    {"name":"Ueba","dir":"ueba","css":"el2_ueba","url":"http://ueba.com.br/NovoLink?url=@@U@@&titulo=@@T@@&origem=EL2SB"},		
		{"name":"Y! Bookmark","dir":"ybookmarks","css":"el2_ybook","url":"http://myweb2.search.yahoo.com/myresults/bookmarklet?u=@@U@@&t=@@T@@"},		
		{"name":"Thumblr","dir":"thumblr","css":"el2_thum","url":"xxx"},
		{"name":"Gmail","dir":"gmail","css":"el2_gmai","url":"xxx"},
		{"name":"Orkut","dir":"orkut","css":"el2_orku","url":"http://promote.orkut.com/preview?nt=orkut.com&tt=@@T@@&du=@@U@@&cn="},
		{"name":"Google","dir":"gbookmarks","css":"el2_gbook","url":"http://www.google.com/bookmarks/mark?op=edit&bkmk=@@U@@&title=@@T@@"},		
		{"name":"Digg","dir":"digg","css":"el2_digg","url":"http://digg.com/submit?phase=2&url=@@U@@&title=@@T@@"},		
		{"name":"Live Favorites","dir":"livefavorites","css":"el2_live","url":"https://skydrive.live.com/sharefavorite.aspx/.SharedFavorites?url=@@U@@&title=@@T@@"},
		{"name":"diHITT","dir":"dihitt","css":"el2_dihi","url":"http://www.dihitt.com.br/submit?rUrl=@@U@@&rTitle=@@T@@"},
		{"name":"Delicious","dir":"delicious","css":"el2_deli","url":"http://del.icio.us/post?url=@@U@@&title=@@T@@"},
		{"name":"Stumbleupon","dir":"stumbleupon","css":"el2_stum","url":"http://www.stumbleupon.com/submit?url=@@U@@&title=@@T@@"},
		{"name":"Blinklist","dir":"blinklist","css":"el2_blklst","url":"http://www.blinklist.com/index.php?Action=Blink/addblink.php&Url=@@U@@&Title=@@T@@&Description=@@T@@"},
		{"name":"Simpy","dir":"simpy","css":"el2_simp","url":"http://www.simpy.com/simpy/LinkAdd.do?href=@@U@@&title=@@T@@&note=@@T@@&v=6"},
		{"name":"LinkedIn","dir":"linkedin","css":"el2_lnkin","url":"http://www.linkedin.com/shareArticle?mini=true&url=@@U@@&title=@@T@@&summary=@@T@@&source=Elephant2%20Social%20Bookmarks"},
		{"name":"Reddit","dir":"reddit","css":"el2_redd","url":"http://reddit.com/submit?url=@@U@@&title=@@T@@"},		
		{"name":"Newsvine","dir":"newsvine","css":"el2_nvin","url":"http://www.newsvine.com/_tools/seed?popoff=0&u=@@U@@&h=@@T@@"},
		{"name":"Technorati","dir":"technorati","css":"el2_techno","url":"http://technorati.com/faves?sub=addfavbtn&add=@@U@@"},
		{"name":"Yammer","dir":"yammer","css":"el2_yamm","url":"xxx"}
	]},
	
	getURLService : function(key, t) {
		return this.share(key,t);
	},
	
	share : function(key, t, u) {		
		if(t == 'undefined' || t == undefined) {
			t = this.el2_pgtitle;
		}		
		for(var i=0;i<this.el2_sbs.sbs.length;i++) {
			_sbs = this.el2_sbs.sbs[i];
			if(_sbs.dir == key) {
				var _url = typeof(u) != 'undefined' ? u : this.el2_pgurl;
				var _url_service = EL2.host+"/sb/"+_sbs.dir+"?SID=" + this.el2_sid + "&pagetitle=" + encodeURIComponent(t) + "&pageurl=" + encodeURIComponent(_url);
				if(this.el2_cssfile) _url_service += "&cssfile=" + encodeURIComponent(this.el2_cssfile);
				return _url_service;
			}
		}		
		return "";
	},	
	getURL : function(str) {
		return str.replace("@@U@@", E$.el2_pgurl).replace("@@T@@", E$.el2_pgtitle);
	},
	
	writeCSSFile : function(color) {
		if(this.el2_background!="rect" && this.el2_background!="rounded") this.el2_background = "rect";
		
		var el2styles = '.el2sb * {margin:0;padding:0;border:0;outline:0;font-size:100%;vertical-align:baseline;background-color:transparent;background-image:none;line-height:100%}';
		el2styles += '.el2sb{position:relative;height:' + E$.el2_size.height + 'px}';
		el2styles += '.el2toolbar{position:absolute;display:block;background-color:#' + color + ';width:' + E$.el2_size.width + 'px;height:' + E$.el2_size.height + 'px}';
		el2styles += '.el2sbs{position:absolute;top:22px;left:0;width:325px;height:225px;background:#FCFCFC;font:lighter 12px tahoma,arial,verdana,sans;display:none;z-index:100;border:1px solid;}';
		el2styles += '.el2sbs h2{border:0;color:#AAA;font-size:14px;font-weight:700;margin:0;padding:15px 0 0 15px;background-image:none;text-align:left !important;display:block;}';
		//el2styles += '.el2sbs_divs {list-style:none;height:130px;overflow:hidden;margin:10px 10px 0 10px;padding:0}';
		el2styles += '.el2sbs ul{list-style:none;height:135px;overflow:hidden;margin:5px 10px 0 10px;padding:0}';
		el2styles += '.el2sbs ul li{list-style-type:none;margin:8px 0px;width:85px;float:left;font-size:12px;text-align:left}';
		el2styles += '.el2sbs ul li a{background-image:url(' + E$.el2I + 'el2sb_sprite_new.gif);background-repeat:no-repeat;padding-left:20px;display:block;height:16px;text-decoration:none;color:#369}';
		
		el2styles += '.el2_gsonic{background-position:0 -96px}.el2_gbuzz{background-position:0 -80px}.el2_deli{background-position:0 -112px}';
		el2styles += '.el2_stum{background-position:0 -224px}.el2_nvin{background-position:0 -304px}.el2_techno{background-position:0 -320px}';
		el2styles += '.el2_redd{background-position:0 -288px}.el2_digg{background-position:0 -192px}';
		el2styles += '.el2_magn{background-position:0 -102px}.el2_gbook{background-position:0 -128px}.el2_live{background-position:0 -208px}';
		el2styles += '.el2_ybook{background-position:0 -176px}.el2_lnkin{background-position:0 -272px}.el2_linkk{background-position:0 -48px}';
		el2styles += '.el2_ueba{background-position:0 -204px}.el2_lnkto{background-position:0 -221px}.el2_qi{background-position:0 -238px}';
		el2styles += '.el2_blklst{background-position:0 -240px}.el2_dihi{background-position:0 -144px}.el2_simp{background-position:0 -256px}';
		el2styles += '.el2_ueba{background-position:0 -160px}.el2_rec6{background-position:0 -320px}.el2_twitt{background-position:0 0px}';
		el2styles += '.el2_fbok{background-position:0 -16px}.el2_orku{background-position:0 -32px}.el2_gost{background-position:0 -64px}';
		el2styles += '.el2_thum{background-position:0 -336px}.el2_plax{background-position:0 -368px}.el2_gread{background-position:0 -400px}'
		el2styles += '.el2_yamm{background-position:0 -352px}.el2_gmai{background-position:0 -384px}';
		
		el2styles += '.el2sbs a.el2toggle{left:0;margin:5px 10px 0 !important;text-transform:uppercase;font-size:9px;text-align:center;display:block;position:absolute;top:180px;width:176px;height:16px;background:url(' + E$.el2I + 'btn_mais2.gif);color:#AAA;line-height:16px;cursor:pointer}';
		el2styles += '.el2sbs<div id="gplus_button" el2-gplus="small"></div> p{position:absolute;bottom:10px;right:10px;font-size:10px;color:#AAA}';		
		el2styles += '.el2btn_close{position:absolute;top:15px;left:80%;cursor:pointer;text-decoration:none;color:#AAA;font-weight:bold}';
		
		el2styles += '.el2btn_close img{border:none}';
		el2styles += '.el2pwd{color:#AAA;text-align:right;font-size:9px;margin-right:5px;}';
		el2styles += '.el2sb_shadow{-moz-box-shadow: 10px 10px 20px #888;-webkit-box-shadow: 10px 10px 20px #888;box-shadow: 10px 10px 20px #888;}';		
		
		$J('head').append('<style>' + el2styles + '</style>');
	},
	
	writeToolbar : function(containerID,img,w,h) {		
		if(jQuery.isArray( containerID )) {
			for(var i=0;i<containerID.length;i++) {
				var _container = $J('#'+containerID[i].id);				
				_container.append('<span class="el2toolbar"><img src="' + img + '" width="' + w + '" height="' + h + '"></span>');
				
				var _title = (containerID[i].title != undefined) ? containerID[i].title : this.el2_pgtitle;
				var _url = (containerID[i].url  != undefined) ? containerID[i].url : this.el2_pgurl;
				
				this.writeSBList(this.el2_sbs.sbs,containerID[i].id, _title,_url);
				this.setup(_container);
			}
		} else {
			var _container = $J('#'+containerID);
			_container.append('<span class="el2toolbar"><img src="' + img + '" width="' + w + '" height="' + h + '"></span>');
			this.writeSBList(this.el2_sbs.sbs,containerID,this.el2_pgtitle,this.el2_pgurl);
			this.setup(_container);
		}		
	},
	
	writeSBList : function(el2_sblist,cid,t,u) {
		var el2_li = "";
		var el2_comp = "?SID=" + this.el2_sid + "&pagetitle=" + encodeURIComponent(t) + "&pageurl=" + encodeURIComponent(u);
		if(this.el2_cssfile) el2_comp += "&cssfile=" + encodeURIComponent(this.el2_cssfile);
		
		/*for(i=0; i<el2_sblist.length; i++) {
			el2_li += '<li><a href="'+EL2.host+'/sb/' + el2_sblist[i].dir + el2_comp + '" class="' + el2_sblist[i].css + '">' + el2_sblist[i].name + '</a></li>';
		}*/
		for(i=0; i<el2_sblist.length; i++) {
			var s = el2_sblist[i];
			var htmlTag = 'a';
			var href = EL2.host+'/sb/' + el2_sblist[i].dir + el2_comp;
			if (s.htmlTag != null) {
				htmlTag = s.htmlTag;
			}
			if (s.href != null) {
				href = s.href;
			}
			el2_li += '<li><' + htmlTag + ' href="' + href + '"';
			if (s.css != null) {
				el2_li += ' class="' + el2_sblist[i].css + '"';
			}
			if (s.size != null) {
				el2_li += ' size="' + s.size + '"';
			}
			if (s.callback != null) {
				el2_li += ' callback="' + s.callback + '" '; 
			}
			if (s.count != null) {
				el2_li += ' count="' + s.count + '" '; 
			}
			el2_li += '">' + el2_sblist[i].name + '</' + htmlTag + '></li>';
		}
		
		$J('body').append('<div class="el2sbs el2sb_shadow" id="el2sbs_'+cid+'"><h2>'+E$._locale.title+'</h2><a href="javascript://" class="el2btn_close">'+E$._locale.fechar+' <img src="'+this.el2I+'closebutton.gif" /></a>'
				+ '<ul>' + el2_li + '</ul>'
				+ '<a class="el2toggle">'+E$._locale.mais+'</a><p class="el2pwd">Powered by EL<sup>2</sup></p></div>');
		$J("a[class^='el2_']").attr('target','_blank');	
	},
	setDelay : function(_obj) {
		return setTimeout(function(){
			$J(_obj).fadeOut(400);	  
		  },E$.el2_delay);
	},
	setup : function(objContainer) {
		var _el2sbs = $J("#el2sbs_" + objContainer.attr('id'));
		// var el2off = $J(objContainer).offset();
		// var top = new Number(el2off.top) + new Number(E$.el2_size.height);
		
		$J($J(objContainer).find(".el2toolbar")).hover(
			function () {				
				E$.calcPosition(this);
				var _left = $J(this).offset().left;
				var loffset = $J(_el2sbs).width() + $J(this).offset().left;		
				
				if(loffset > $J(window).width()) {		
					_left = _left - ($J(_el2sbs).width()-($J(this).width()*0.9));
					//_left = _left - ($J(_el2sbs).width()-10);
					//_left<div id="gplus_button" el2-gplus="small"></div> = (_left + 10) - $J(window).width();
				}
				
				_el2sbs.css('left', _left).css('top',E$.el2_sbs_top);
				$J('#el2sbs_' + $J(this).parent().attr('id')).fadeIn(400);
				clearTimeout(E$.el2_timer);
			}, 
			function () {				
				clearTimeout(E$.el2_timer);
				E$.el2_timer = E$.setDelay($J('#el2sbs_' + $J(this).parent().attr('id')));
			}
		);
		
		$J(_el2sbs).hover(
			function () { clearTimeout(E$.el2_timer);}, 
			function () { E$.el2_timer = E$.setDelay(this); }
		);
		
		$J($J(_el2sbs).find('.el2toggle')).click(function(){
			var ov = $J(_el2sbs).find('ul').css('overflow');
			if(ov=='hidden') {
				$J(this).html(E$._locale.menos);
				_el2sbs.find('ul').css('overflow','auto');
			} else {
				$J(this).html(E$._locale.mais);
				_el2sbs.find('ul').css('overflow','hidden').scrollTop(0);
			}
		});
		
		$J('.el2btn_close').click(function(){
			$J(_el2sbs).fadeOut(400);
		});
	},
	calcPosition : function (_tb) {
		var _p = $J(_tb).parent();
		var boxtop = $J(_tb).offset().top;
			
		
		//var beforetoolbar = boxtop-f_scrollTop();
		var aftertoolbar = this.f_clientHeight() - boxtop + this.f_scrollTop();
		var sbsboxheight = $J('#el2sbs_'+_p.attr('id')).height();		
		if((sbsboxheight > aftertoolbar) && aftertoolbar > 0) {
			E$.el2_sbs_top = boxtop - sbsboxheight;
		} else {
			E$.el2_sbs_top = boxtop + $J(_tb).height();
		}
	},
	f_clientHeight : function() {
		return this.f_filterResults (
				window.innerHeight ? window.innerHeight : 0,
				document.documentElement ? document.documentElement.clientHeight : 0,
				document.body ? document.body.clientHeight : 0
			);
	},
	f_scrollTop : function() {
		return this.f_filterResults (
			window.pageYOffset ? window.pageYOffset : 0,
			document.documentElement ? document.documentElement.scrollTop : 0,
			document.body ? document.body.scrollTop : 0
		);
	},
	f_filterResults : function(n_win, n_docel, n_body) {
		var n_result = n_win ? n_win : 0;
		if (n_docel && (!n_result || (n_result > n_docel)))
			n_result = n_docel;
		return n_body && (!n_result || (n_result > n_body)) ? n_body : n_result;
	},
	
	init : function(args) {
		EL2.core.extend(args,this);
		this._locale = EL2.sb.language.getLabels(this.el2_lang);
		this.writeCSSFile(this.el2_hex);				
		this.writeToolbar(this.el2_container,this.el2_img, this.el2_size.width, this.el2_size.height);	
		//this.writeSBList(this.el2_sbs.sbs);		
		//this.setup();
	}
};

EL2.namespace("sb.language");
EL2.sb.language = {
	ptBR : {title:'Compartilhar', mais : 'Mais op&ccedil&otilde;es',menos:'Menos op&ccedil&otilde;es',powered:'Powered by EL<sup>2</sup>',fechar:'Fechar'},
	esES : {title:'Compartir', mais : 'Ver m&aacute;s',menos:'Ver menos',powered:'Powered by EL<sup>2</sup>',fechar:'Cerrar'},	
	getLabels : function(str) {
		if(str == 'es-es') return this.esES;
		else return this.ptBR;
	}
};

function plusOneCallback(obj) {
	if (obj.state == 'on') {
		var _url_service = EL2.host+"/sb/plusone?SID=" + E$.el2_sid + "&pagetitle=" + encodeURIComponent(E$.el2_pgtitle) + "&pageurl=" + encodeURIComponent(obj.href);
		// alert(_url_service);
		if(this.el2_cssfile) _url_service += "&cssfile=" + encodeURIComponent(this.el2_cssfile);
		$.ajax({
			url: _url_service,
			type: 'GET'
		});
	}
}

var E$ = EL2.sb;

;if(jQuery)(function($) {
	jQuery.fn.gplus = function(args) {		
		var _dargs = {lang: 'pt_BR', cls : "el2_pone", callback : "plusOneCallback", count : "true", size : "small"};		
		EL2.core.extend(args,_dargs);		
		EL2.core.loadModule("https://apis.google.com/js/plusone.js",'{"lang":'+_dargs.lang+'}');
		$(this).append('<g:plusone href="http://api.elephant2.com" class="'+_dargs.cls+'" size="'+_dargs.size+'" callback="'+_dargs.callback+'" count="'+_dargs.count+'" ">+1</g:plusone>');
	}
	jQuery.fn.flike = function(args) {		
		var _dargs = {};		
		EL2.core.extend(args,_dargs);
		EL2.core.loadModule("https://apis.google.com/js/plusone.js",'{"lang":'+E$.el2_lang+'}');
		//$(this).append('<g:plusone href="http://api.elephant2.com" class="'+_dargs.cls+'" size="'+_dargs.size+'" callback="'+_dargs.callback+'" count="'+_dargs.count+'" ">+1</g:plusone>');
	}
})(jQuery);


