var t;

$(function()
{
	$('#flash_data').slideDown();
	t = window.setTimeout(hideFlashData, 5000);
	
});

function hideFlashData()
{
	clearTimeout(t);
	$('#flash_data').slideUp();
}

function addTableRow(table)
{
	var clonedRow = $("tbody tr:first-child", table).clone();
	$(table).append(clonedRow);
	$(clonedRow).addClass('row');
	$(clonedRow).fadeIn();

	$("tbody tr", table).each(function(i) {
		$("input, select, textarea", this).each(function() {
			var temp = $(this).attr('name');

			temp = temp.replace(/\[(\d*)\]/g, '[' + i + ']');

			$(this).attr('name', temp);
		});
		
	});
}

function removeTableRow(elem)
{
	var thisElement = $(elem).parent().parent();
	$(thisElement).fadeOut();
	$("input.delete_row", thisElement).attr('checked', 'checked');
}

function draw_viz_bar(element, width, height, capacity, release, pax)
{
	var paper = Raphael(element, width, height);
	var r = paper.rect(0.5, 0.5, (((width - 2) / capacity) * release), height - 1, 5);
	r.attr("fill", "90-#eeeeee-#cccccc");
	r.attr("stroke-width", "0");
	
	var the_width = ((width - 4) / capacity) * pax;
	
	var the_fill = "90-#3e8414-#b1d53f";
	var the_stroke = "#79ae2a";
	
	if(release > 0 && pax > 0)
	{
		
		if ((pax / release) < 0.8)
		{
			the_fill = "90-#ff8600-#ffd600";
			the_stroke = "#ffb100";
		}

		if ((pax / release) < 0.4)
		{
			the_fill = "90-#b31b1a-#f04848";
			the_stroke = "#e22e2f";
		}
	}
	
	if(pax > 0)
	{
		var s = paper.rect(0, 0, the_width + 2, height - 2, 5)
		s.attr("stroke", "#bbb");

		var b = paper.rect(1, 1, the_width, height - 4, 4);
		b.attr("stroke", the_stroke);
		b.attr("fill", the_fill);
	}
	
	if(pax >= release)
	{
		var ts = paper.text(((the_width + 2) / 2) - 0.5, (height / 2) + 0.5, "FULL");
		ts.attr("fill", "#fff");
		ts.attr("opacity", "0.6");
		var t = paper.text((the_width + 2) / 2, height / 2, "FULL");
		t.attr("fill", "#222");
	}
	
	var c = paper.rect(0, 0, width - 2, height - 2, 5);
	c.attr("stroke", "#aaa");
}


// iPhone style switch
$.fn.sliderfy = function(switched_on_callback, switched_off_callback, options)
{
	// define default settings
	var settings = {
		mouse_over: 'pointer',
		mouse_out:  'default',
		switch_path: static_path + 'images/iphone_switch_on_off.jpg',
		switch_disabled_path: static_path + 'images/iphone_switch_on_off_disabled.jpg',
		container_height: 20,
		container_width: 69,
		toggle_width: 29,
	};

	if(options) {
		$.extend(settings, options);
	}
	
	return this.each(function()
	{
		var ckbox = $(this);
		var container;
		var image;
		
		$(ckbox).hide();
		
		var the_path = settings.switch_path;
		
		if($(ckbox).is(':disabled'))
		{
			the_path = settings.switch_disabled_path;
		}
		
		// make the container
		container = $('<div class="sliderfy_container" style="height:'+settings.container_height+'px; width:'+settings.container_width+'px; position: relative; overflow: hidden;background-image:url('+the_path+'); background-repeat:none; background-position:'+($(ckbox).is(':checked') ? 0 : (settings.toggle_width - settings.container_width))+'px"></div>');
		
		$(ckbox).after(container);
		
		$(container).mouseover(function(){
			$(container).css("cursor", settings.mouse_over);
		});

		$(container).mouseout(function(){
			$(container).css("background", settings.mouse_out);
		});
		
		$(container).click(function()
		{
			if($(ckbox).is(':disabled'))
			{
				
			} else
			{
				if($(ckbox).is(':checked'))
				{
					$(container).animate({backgroundPosition: (settings.toggle_width - settings.container_width)}, "slow");
					$(ckbox).attr('checked', '');
					if(switched_off_callback != undefined)
					{
						switched_off_callback();
					}

				} else
				{
					$(container).animate({backgroundPosition: 0}, "slow");
					$(ckbox).attr('checked', 'checked');
					if(switched_on_callback != undefined)
					{
						switched_on_callback();
					}
				}
			}
			
		});
		
	});
}
