function createCode() {
	
	var code = '<script type="text/javascript">\n'
		+ '//<![CDATA[\n'
		+ 'var BRB_protocol = (document.location.protocol == "https:" ? "https" : "http");\n'
		+ 'document.write(unescape("%3Cscript src=\\"" + BRB_protocol + "://browser.fusonic.net/script/BrowserRecommendationBar.js\\" type=\\"text/javascript\\"%3E%3C/script%3E"));\n'
		+ '//]]>\n'
		+ '</script>\n'
		+ '<script type="text/javascript">\n'
		+ '//<![CDATA[\n';
		
	// Color settings
	var textColor = $("#textColor").attr("value");
	var linkColor = $("#linkColor").attr("value");
	var backgroundColor = $("#backgroundColor").attr("value");
	if(textColor.toUpperCase() != "#000000")
		code += 'BRB.textColor = "' + textColor + '";\n';
	if(linkColor.toUpperCase() != "#0000C0")
		code += 'BRB.linkColor = "' + linkColor + '";\n';
	if(backgroundColor.toUpperCase() != "#FFFEE0")
		code += 'BRB.backgroundColor = "' + backgroundColor + '";\n';
		
	// Set colors for preview
	$("#BRB_bar").css("color", textColor);
	$("#BRB_bar").css("background-color", backgroundColor);
	$("#BRB_bar span.link").css("color", linkColor);
	
	// Other settings
	var infoLink = $("#infoLink").attr("selectedIndex") == 0;
	if(!infoLink) {
		$("#BRB_info").css("display", "none");
		code += 'BRB.showInfoLink = false;\n';
	}
	else
		$("#BRB_info").css("display", "");
		
	// Actions
	var ie0Action = $("#ie0Action").attr("options")[$("#ie0Action").attr("selectedIndex")].value;
	var ie7Action = $("#ie7Action").attr("options")[$("#ie7Action").attr("selectedIndex")].value;
	var op0Action = $("#op0Action").attr("options")[$("#op0Action").attr("selectedIndex")].value;
	var op9Action = $("#op9Action").attr("options")[$("#op9Action").attr("selectedIndex")].value;
	var ff0Action = $("#ff0Action").attr("options")[$("#ff0Action").attr("selectedIndex")].value;
	if(ie0Action != "recommend_ff3")
		code += 'BRB.action_ie0 = "' + ie0Action + '";\n';
	if(ie7Action != "recommend_ff3")
		code += 'BRB.action_ie7 = "' + ie7Action + '";\n';
	if(op0Action != "update")
		code += 'BRB.action_op0 = "' + op0Action + '";\n';
	if(op9Action != "")
		code += 'BRB.action_op9 = "' + op9Action + '";\n';
	if(ff0Action != "update")
		code += 'BRB.action_ff0 = "' + ff0Action + '";\n';
		
	// Texts
	var updateText_en = $("#updateText_en").attr("value");
	var recommendationText_en = $("#recommendationText_en").attr("value");
	var updateText_de = $("#updateText_de").attr("value");
	var recommendationText_de = $("#recommendationText_de").attr("value");
	if(updateText_en != "A newer version of your web browser is available. Download the new {link}.")
		code += 'BRB.text["en"]["textUpdate"] = "' + updateText_en + '";\n';
	if(recommendationText_en != "You are using a browser which doesn't support modern web-technologies correctly. Try {link}!")
		code += 'BRB.text["en"]["textUpgrade"] = "' + recommendationText_en + '";\n';
	if(updateText_de != "Es steht eine neue Version Ihres Browsers zum Download zur Verfügung. Testen Sie den neuen {link}!")
		code += 'BRB.text["de"]["textUpdate"] = "' + updateText_de + '";\n';
	if(recommendationText_de != "Sie verwenden einen Browser, der moderne Web-Standards nicht korrekt unterstützt. Testen Sie {link}!")
		code += 'BRB.text["de"]["textUpgrade"] = "' + recommendationText_de + '";\n';
		
	code += 'BRB.init();\n'
		+ '//]]>\n'
		+ '</script>';
		
	$("#code").attr("value", code);
	
}

var colorPickerChoosing = null;
var dontHidePicker = false;

function showColorPicker(type) {
	
	dontHidePicker = true;
	colorPickerChoosing = type;
	
	var input = $("#" + type).get(0);
	
	var posX = input.getBoundingClientRect().left + input.offsetWidth;
	var posY = input.getBoundingClientRect().top;
	
	$("#colorPicker").css("left", posX + "px");
	$("#colorPicker").css("top", posY + "px");
	$("#colorPicker").css("display", "block");
	
}

function hideColorPicker() {

	if(dontHidePicker) {
		dontHidePicker = false;
		return;
	}
	
	$("#colorPicker").get(0).style.display = "none";
	colorPickerChoosing = null;
	
}

function hideColorPicker1() {
	
	window.setTimeout("hideColorPicker()", 10);
	
}

function colorChoosen(color) {
	
	if(colorPickerChoosing == null) {
		hideColorPicker();
		return;
	}
	
	$("#" + colorPickerChoosing).attr("value", color);
	$("#" + colorPickerChoosing + "Preview").css("background-color", color);
	
	createCode();
	
}

$(document).click(hideColorPicker1);

$(document).ready(function() {
	
	/*
	 * Recalculate background positions
	 */
	
	if(document.documentElement) {
		var scrollX = document.documentElement.scrollLeft;
		var scrollY = document.documentElement.scrollTop;
	}
	else {
		var scrollX = window.scrollX;
		var scrollY = window.scrollY;
	}
	
	var allBoxes = $("div.box");
	var rootLeft = $("#colorsBox").offset().left;
	var rootTop = $("#colorsBox").offset().top;
	$("div.box").each(function() {
		
		var box = $(this);
		var posX = box.offset().left - rootLeft;
		var posY = box.offset().top - rootTop;
		box.css("backgroundPosition", -posX + "px " + -posY + "px");
		
	});
	
	
	/*
	 * Create color pickers
	 */
	
	$("#colorPickerInner").farbtastic(colorChoosen);
	
	
	/*
	 * Translation
	 */
	
	if(window.location.href.match(/lang=([a-z]{2})/)) {
		var targetLang = RegExp.$1;
	}
	else if(navigator.language != null)
		var targetLang = navigator.language.substring(0, 2);
	else if(navigator.browserLanguage != null)
		var targetLang = navigator.browserLanguage.substring(0, 2);
	else
		var targetLang = "";
	
	var lang = {
		"de" : {
			"<small>1)</small> Settings" : "<small>1)</small> Einstellungen",
			"<small>2)</small> Actions" : "<small>2)</small> Aktionen",
			"<small>4)</small> Your code" : "<small>4)</small> Dein Code",
			"Text color" : "Textfarbe",
			"Link color" : "Linkfarbe",
			"Background color" : "Hintergrundfarbe",
			"Recommendation" : "Empfehlung",
			"Insert this code somewhere between &lt;body&gt; and &lt;/body&gt; on your site." : "Fügen Sie diesen Code irgendwo zwischen &lt;body&gt; und &lt;/body&gt; auf Ihrer Seite ein.",
			"The Variable <i>{link}</i> is replaced by a link to the download page of the browser, e.g." : "Die Variable <i>{link}</i> wird durch einen Link zur Downloadseite des Browsers ersetzt, z.B.",
			"Don't show BRB" : "BRB nicht anzeigen",
			"Recommend Firefox 3" : "Firefox 3 empfehlen",
			"Recommend Internet Explorer 7 Update" : "Internet Explorer 7 Update empfehlen",
			"Recommend Opera 9 Update" : "Opera 9 Update empfehlen",
			"Recommend Firefox 3 Update" : "Firefox 3 Update empfehlen",
			"Imprint" : "Impressum",
			"This is a preview bar! Edit settings and see the effect." : "Dies ist eine Beispielleiste, die sich an deine Einstellungen anpasst.",
			"What's this all about?" : "Um was geht's eigentlich?"
		}
	};
	
	var replaceInnerHTML = function(i) {
		if(lang[targetLang][this.innerHTML] != undefined)
			this.innerHTML = lang[targetLang][this.innerHTML];
	}
	
	if(lang[targetLang] != undefined) {
	
		$("h1").each(replaceInnerHTML);
		$("div.left").each(replaceInnerHTML);
		$("span").each(replaceInnerHTML);
		$("option").each(replaceInnerHTML);
		$("a").each(replaceInnerHTML);
	
	}
	
	
	/*
	 * Create code
	 */
	
	createCode();
	
});

