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);
	}
};
 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":"Orkut","dir":"orkut","css":"el2_orku","url":"http://promote.orkut.com/preview?nt=orkut.com&tt=@@T@@&du=@@U@@&cn="},	    
	    {"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":"Gostei!","dir":"gostei","css":"el2_gost","url":"http://gostei.abril.com.br/post/new?t=@@T@@&u=@@U@@"},
	    {"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":"Delicious","dir":"delicious","css":"el2_deli","url":"http://del.icio.us/post?url=@@U@@&title=@@T@@"},
	    {"name":"Google","dir":"gbookmarks","css":"el2_gbook","url":"http://www.google.com/bookmarks/mark?op=edit&bkmk=@@U@@&title=@@T@@"},	     		
	    {"name":"diHITT","dir":"dihitt","css":"el2_dihi","url":"http://www.dihitt.com.br/submit?rUrl=@@U@@&rTitle=@@T@@"},
	    {"name":"Ueba","dir":"ueba","css":"el2_ueba","url":"http://ueba.com.br/NovoLink?url=@@U@@&titulo=@@T@@&origem=EL2SB"},		
		{"name":"Y!Bookmarks","dir":"ybookmarks","css":"el2_ybook","url":"http://myweb2.search.yahoo.com/myresults/bookmarklet?u=@@U@@&t=@@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":"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@@"}		
	]},
	
	getURLService : function(key, t) {
		return this.share(key,t);
	},
	
	share : function(key, t) {		
		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_service = EL2.host+"/sb/"+_sbs.dir+"?SID=" + this.el2_sid + "&pagetitle=" + encodeURIComponent(t) + "&pageurl=" + encodeURIComponent(this.el2_pgurl);
				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:#FFF url(' + E$.el2I + 'background-' + this.el2_background + '.png);font:lighter 12px Arial, sans-serif;display:none;z-index:100}';
		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:11px;text-align:left}';
		el2styles += '.el2sbs ul li a{background-image:url(' + E$.el2I + 'sbs_new.gif);background-repeat:no-repeat;padding-left:20px;display:block;height:16px;text-decoration:none;color:#369}';
		el2styles += '.el2_gsonic{background-position:0 -417px}.el2_gbuzz{background-position:0 -400px}.el2_deli{background-position:0 0px}.el2_digg{background-position:0 -17px}.el2_stum{background-position:0 -34px}.el2_nvin{background-position:0 -51px}.el2_techno{background-position:0 -68px}.el2_redd{background-position:0 -85px}.el2_magn{background-position:0 -102px}.el2_gbook{background-position:0 -119px}.el2_live{background-position:0 -136px}.el2_ybook{background-position:0 -153px}.el2_lnkin{background-position:0 -170px}.el2_linkk{background-position:0 -187px}.el2_ueba{background-position:0 -204px}.el2_lnkto{background-position:0 -221px}.el2_qi{background-position:0 -238px}.el2_blklst{background-position:0 -255px}.el2_dihi{background-position:0 -272px}.el2_simp{background-position:0 -289px}.el2_ueba{background-position:0 -304px}.el2_rec6{background-position:0 -320px}.el2_twitt{background-position:0 -336px}.el2_fbok{background-position:0 -352px}.el2_orku{background-position:0 -368px}.el2_gost{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 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:#CCC}';
		el2styles += '.el2btn_close img{border:none}';
		$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>';
		}
		$J('body').append('<div class="el2sbs" id="el2sbs_'+cid+'"><h2>'+E$._locale.title+'</h2><a href="javascript:void();" 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 = (_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;
	}
};
var E$ = EL2.sb;


