// facilita chamada ao widget de rt

// avoiding document.write() -- you will see (and if you can see you can copy)
// iframe to look "just" like buzz boss want

// TODO: a lot

(typeof __buzzScriptCounter != 'number') ? __buzzScriptCounter = 0 : ++__buzzScriptCounter;
(function(){
	var dev = window.location.href.match(/(localhost:|localhost\/\~)/);
	var src = dev ? window.location.href.split('/').slice(0, 3).join('/') + "/widgets/retweet/retweet.html" : "http://buzzvolume.com/widgets/retweet/retweet.html";
	
	// create default buzzvolume badge
	var url = (typeof buzzvolume_url=="string") ? buzzvolume_url : window.location.href;
    var title = (typeof buzzvolume_title=="string") ? buzzvolume_title : document.title;
	var source = (typeof buzzvolume_source=="string") ? buzzvolume_source : false;
	var style = (typeof buzzvolume_style=="string")? buzzvolume_style : false;
	var alias = (typeof buzzvolume_alias=="string") ? buzzvolume_alias : false;
    var background = (typeof buzzvolume_background=="string") ? buzzvolume_background : false;
    var color = (typeof buzzvolume_background=="string") ? buzzvolume_color : false;
	var x_login = (typeof buzzvolume_x_login=="string") ? buzzvolume_x_login : false;
    var x_apiKey = (typeof buzzvolume_x_apiKey=="string") ? buzzvolume_x_apiKey : false;
	
	var params = [
		["?url", url], ["&source", source], ["&alias", alias],
		["&style", style], ["&title", title], ["&background", background],
		["&x_login", x_login], ["&x_apiKey", x_apiKey], ["&color", color]
	];
	for (param in params) {
		var param = params[param];
		if (param[1]) {
			src = [src, param[0], '=', encodeURIComponent(param[1])].join('');
		}
	}
	
	// find (this) script node
	var buzzScripts = [];
	var scriptsNodes = document.getElementsByTagName("script");
	for (var i=0; i < scriptsNodes.length; i++) {
		if (dev) {
			(scriptsNodes[i].getAttribute('src') && scriptsNodes[i].getAttribute('src').match(/\/buzzwidget\.js/)) ? buzzScripts.push(scriptsNodes[i]) : null;
		} else {
			(scriptsNodes[i].getAttribute('src') && scriptsNodes[i].getAttribute('src').match(/buzzvolume.com\/[^\/]*\/buzzwidget\.js/)) ? buzzScripts.push(scriptsNodes[i]) : null;
		}
	}
	var scriptNode = buzzScripts[__buzzScriptCounter];
	
	var iframe = document.createElement('iframe');
	iframe.setAttribute('src', src);
	iframe.setAttribute('width', (style == "compact") ? 99 : 50);	
	iframe.setAttribute('height', (style == "compact") ? 19 : 57);
	iframe.setAttribute('frameborder', 0);
	iframe.setAttribute('scrolling', 'no');
	
	// ninja huh?
	scriptNode.parentNode.insertBefore(iframe, scriptNode);
	
	buzzvolume_url=null;
	buzzvolume_source=null;
	buzzvolume_style=null;
	buzzvolume_alias=null;
	buzzvolume_title=null;
})();