var tfb = {};
tfb.allowedLabels = ["follow-me","follow-us","follow","my-twitter"];
tfb.defaultTop = 78;
tfb.defaultColor = "#35ccff";

tfb.gebi= function(id) {
	return document.getElementById(id);
}

tfb.isInArray = function(str, ar) {
	if (ar.length < 1) return;
	for (var i = 0; i < ar.length;  i++) {
		if (ar[i] == str) {
			return true;
			break;
		}
	}
	return false;
}


tfb.showbadge = function() {

	if (!window.XMLHttpRequest) { return; } //IE6 - not supported;

	var bodyEl = document.getElementsByTagName('body')[0];
	if (document.getElementById('twitterFollowBadge')) {
		bodyEl.removeChild(document.getElementById('twitterFollowBadge'));
	}

	if (tfb.top < 0 || tfb.top > 1000 || isNaN(tfb.top)) {
		tfb.top = tfb.defaultTop;
	}

	if (!tfb.isInArray(tfb.label, tfb.allowedLabels)) {
		tfb.label = tfb.allowedLabels[0];
	}
	var validColorPattern = /^#([a-f]|[A-F]|[0-9]){3}(([a-f]|[A-F]|[0-9]){3})?$/;
	if (!validColorPattern.test(tfb.color) || (tfb.color.length != 4 && tfb.color.length != 7)) {
		tfb.color = tfb.defaultColor;
	};
	if (tfb.side != 'l') {
		tfb.side = 'r';
	}

	tfb.mainStyleCode = 'position:fixed;' +
						'top:' + tfb.top + 'px;' +
						'width:30px;' +
						'height:119px;' +
						'z-index:10000;' +
						'cursor:pointer;' +
						'background:' + tfb.color + ' url(http://files.go2web20.net/twitterbadge/1.0/bg-badge/' + tfb.label + '.png);' +
						'background-repeat:no-repeat;';

	tfb.aboutStyleCode = 'position:absolute;' +
						'bottom:1px;' +
						'width:10px;' +
						'height:11px;' +
						'background:url(http://files.go2web20.net/twitterbadge/1.0/icon-about.png);' +
						'background-repeat:no-repeat;';

	if (tfb.side == 'l') {
		tfb.mainStyleCode += 'left:0; background-position:right top;';
		tfb.aboutStyleCode += 'left:0;';
	} else {
		tfb.mainStyleCode += 'right:0; background-position:left top;';
		tfb.aboutStyleCode += 'right:0;';
	}

	tfbMainDiv = document.createElement('div');
	tfbMainDiv.setAttribute('id', 'twitterFollowBadge');
	tfbMainDiv.setAttribute('style', tfb.mainStyleCode);
	tfbMainDiv.onclick = function() {
		window.open('http://www.twitter.com/' + tfb.account);
	}

	tfbAboutDiv = document.createElement('div');
	tfbAboutDiv.setAttribute('id', 'tfbAbout');
	tfbAboutDiv.setAttribute('style', tfb.aboutStyleCode);
	tfbAboutDiv.onclick = function() {
		window.open('http://www.go2web20.net/twitterFollowBadge/');
	}
	tfbMainDiv.appendChild(tfbAboutDiv);
	bodyEl.appendChild(tfbMainDiv);
}