/*
	25 may 2010: created from code in testing.html
*/

document.observe('dom:loaded', function()
{
	var zip_changer  = $('zip_changer');
	var fuel_widget2 = $('fuel_widget2');
	var zip_input    = zip_changer.down('input');
	var zip_button   = zip_changer.down('button');
	var script_src2  = fuel_widget2.down('script').readAttribute('src');
	zip_input.value  = script_src2.match(/\d+$/)[0];
	var iframe2      = null;
	zip_button.observe('click', function()
	{
		Form.Element.disable(zip_input);
		Form.Element.disable(zip_button);
		if (null === iframe2)
		{
			iframe2 = fuel_widget2.down('iframe');
			iframe2.observe('load', function()
			{
				Form.Element.enable(zip_input);
				Form.Element.enable(zip_button);
			});
		}
		var iframe2_src = iframe2.src.replace(/zip=\d+/, ('zip=' + $F(zip_input)));
		iframe2.src = iframe2_src;
	});

	var option_tweaker = $('option_tweaker');
	var fuel_widget4   = $('fuel_widget4');
	var width_input    = option_tweaker.down('input[name=width]');
	var height_input   = option_tweaker.down('input[name=height]');
	var theme_select   = option_tweaker.down('select');
	var tweaker_button = option_tweaker.down('button');
	var script_src4    = fuel_widget4.down('script').readAttribute('src');
	var width          = script_src4.match(/width=(\d+)$/);
	width_input.value  = (width ? width[1] : 150);
	var height         = script_src4.match(/height=(\d+)$/);
	height_input.value = (height ? height[1] : 200);
	var theme          = script_src4.match(/theme=([-a-z0-9]+)$/)[1];
	theme_select.down('option[value=' + theme + ']').writeAttribute('selected', true);
	var iframe4        = fuel_widget4.down('iframe');
	iframe4.observe('load', function()
	{
		Form.Element.enable(width_input);
		Form.Element.enable(height_input);
		Form.Element.enable(theme_select);
		Form.Element.enable(tweaker_button);
	});
	tweaker_button.observe('click', function()
	{
		Form.Element.disable(width_input);
		Form.Element.disable(height_input);
		Form.Element.disable(theme_select);
		Form.Element.disable(tweaker_button);
		var width  = $F(width_input);
		if (width  < 150) width  = 150;
		var height = $F(height_input);
		if (height < 200) height = 200;
		iframe4.setStyle(
		{
			'width':  width  + 'px',
			'height': height + 'px'
		});
		width_input.value  = width;
		height_input.value = height;
		var show_zipcode_button = ((height >= 245) || ((height >= 226) && (width >= 206)));
		var iframe4_src = iframe4.src.replace(/theme=[-a-z0-9]+/, ('theme=' + $F(theme_select)));
		if (iframe4_src.match(/&zipcode_button=true/))
		{
			if (!show_zipcode_button) iframe4_src = iframe4_src.replace('&zipcode_button=true', '');
		}
		else
		{
			if (show_zipcode_button) iframe4_src += '&zipcode_button=true';
		}
		iframe4.src = iframe4_src;
	});
});
