function updatePreview(){
	var settings = [];
	settings[0] = $('#numberUpdates').attr('value');
	settings[1] = $('#showDate').attr('value');
	settings[2] = $('#linksBehavior').attr('value');
	settings[3] = $('#width').attr('value');
	settings[4] = $('#backgroundColor').attr('value').substr(1, 6);
	settings[5] = $('#backgroundTransparent').attr('value');
	settings[6] = $('#borderColor').attr('value').substr(1, 6);
	settings[7] = $('#foregroundColor').attr('value').substr(1, 6);
	var code = '<!-- BEGIN: Twitter for Web widget (http://twitterforweb.com) -->'
					+'\n'+'<div style="width:'+settings[3]+'px;font-size:8px;text-align:right;"><script type="text/javascript">'
					+'//<![CDATA['
					+'\n'+'document.write(unescape("%3Cscript src=\'http://twitterforweb.com/twitterforweb.js?_rnd="+(new Date()).getTime()+"&username='+escape($('#username').attr('value'))+'&settings='+settings.join(',')+'\' type=\'text/javascript\'%3E%3C/script%3E"));'
					+'\n'+'//]]>'
					+'</script>Created by: <a href="http://twitterforweb.com" title="Twitter for Web">Twitter for Web</a></div>'
					+'\n'+'<!-- END: Twitter for Web widget (http://twitterforweb.com) -->';
	try {
		document.getElementById('code').value=code;
	} catch(e) {
		document.getElementById('code').innerHTML=code.replace(/\</g, '&lt;').replace(/\>/g, '&gt;');
	}
	
	$('#previewContent').html(' ').attr('class', 'loading');
	$.getScript('twitterforweb.js?_rnd='+(new Date()).getTime()+'&username='+escape($('#username').attr('value'))+'&settings='+settings.join(',')+'&from=twitterforweb');
	return false;
};

var $_memoryValue = '';

$(document).ready(function() {
	updatePreview();
	$('#backgroundColor').ColorPicker({
		onSubmit: function(hsb, hex, rgb, el) {
			$(el).val('#'+hex);
			$(el).ColorPickerHide();
			if ($('#backgroundColor').attr('value') != $_memoryValue) {
				$_memoryValue = $('#backgroundColor').attr('value');
				updatePreview();
			}
		},
		onChange: function(hsb, hex, rgb, el) {
			$('#backgroundColor').val('#'+hex);
		},
		onBeforeShow: function () {
			$_memoryValue = this.value;
			$(this).ColorPickerSetColor(this.value);
		},
		onHide: function (hsb) {
			if ($('#backgroundColor').attr('value') != $_memoryValue) {
				$_memoryValue = $('#backgroundColor').attr('value');
				updatePreview();
			}
		}
	});
	$('#borderColor').ColorPicker({
		onSubmit: function(hsb, hex, rgb, el) {
			$(el).val('#'+hex);
			$(el).ColorPickerHide();
			if ($('#borderColor').attr('value') != $_memoryValue) {
				$_memoryValue = $('#borderColor').attr('value');
				updatePreview();
			}
		},
		onChange: function(hsb, hex, rgb, el) {
			$('#borderColor').val('#'+hex);
		},
		onBeforeShow: function () {
			$_memoryValue = this.value;
			$(this).ColorPickerSetColor(this.value);
		},
		onHide: function (hsb) {
			if ($('#borderColor').attr('value') != $_memoryValue) {
				$_memoryValue = $('#borderColor').attr('value');
				updatePreview();
			}
		}
	});
	$('#foregroundColor').ColorPicker({
		onSubmit: function(hsb, hex, rgb, el) {
			$(el).val('#'+hex);
			$(el).ColorPickerHide();
			if ($('#foregroundColor').attr('value') != $_memoryValue) {
				$_memoryValue = $('#foregroundColor').attr('value');
				updatePreview();
			}
		},
		onChange: function(hsb, hex, rgb, el) {
			$('#foregroundColor').val('#'+hex);
		},
		onBeforeShow: function () {
			$_memoryValue = this.value;
			$(this).ColorPickerSetColor(this.value);
		},
		onHide: function (hsb) {
			if ($('#foregroundColor').attr('value') != $_memoryValue) {
				$_memoryValue = $('#foregroundColor').attr('value');
				updatePreview();
			}
		}
	});
	$('#backgroundTransparent').bind('change', function() {
		$('#backgroundColor').attr('disabled', this.value == 1);
		return updatePreview();
	});
	$('#numberUpdates, #showDate, #linksBehavior').bind('change', updatePreview);
	$('#username, #width').bind('focus', function() {
		$_memoryValue = this.value;
	}).bind('blur', function() {
		if (this.value != $_memoryValue) {
			return updatePreview();
		}
	});
	$('#code').bind('focus', function() {
		try {
			this.select();
		} catch (e){};
	});
	$('#twitterForm').bind('submit', function() {
		return updatePreview();
	});
	$('#resetButton').bind('click', function() {
		$('#numberUpdates').attr('value', 7);
		$('#showDate').attr('value', 1);
		$('#linksBehavior').attr('value', 2);
		$('#width').attr('value', 240);
		$('#backgroundColor').attr('value', '#f4f4f4').attr('disabled', false);
		$('#backgroundTransparent').attr('value', 0);
		$('#borderColor').attr('value', '#c4c4c4');
		$('#foregroundColor').attr('value', '#000000');
		return updatePreview();
	});
});