$(function() {	
		
	/* show the header check and  toggle all checkboxes and selected class as header check changes  */
	$('.header_check').css('display','inline').change(function() { 
		$('.table_check')
			.attr('checked', $(this).attr('checked'))	
			.parents('tr')
				.toggleClass("selected-row",$(this).attr('checked'));
	}); 	
	
	
	/* highlight ones already selected on load (happens after refresh) */
	$('.table_check').each(function() {
		$(this).parents('tr').toggleClass("selected-row",$(this).attr('checked'));
	});
	
	
	/* toggle class on row as checkbox changes  */
	$('.table_check').change(function() {
		$(this).parents('tr')
			.toggleClass("selected-row",$(this).attr('checked'));
	});
	
	
	/*
	 * for any generaltable with a .click-to-edit cell - when the user clicks 
	 * we find the first parent row and then the first contained a tag, and 
	 * set the location to it's href
	 */
	$('.general-table .click-to-edit').click(function() {
		location.href = $(this).parents('tr').find('a').attr('href');
	});
	
	$('.general-table .dblclick-to-edit').dblclick(function() {
		var href = $(this).parents('tr').find('a').attr('href');
		if (href!=null)
			location.href = href; 
	});

	$('.general-table .click-to-check').click(function() {
		checkbox = $(this).parents('tr').find('input:checkbox');
		checkbox.attr('checked', !checkbox.attr('checked'))	
		checkbox.parents('tr').toggleClass("selected-row",checkbox.attr('checked'))
	});

	$('.general-table td').hover(
		function() { 
			var td=$(this);
			if (td.attr('rowspan')==1)
				td.parent().addClass('hover'); 
		},
		function() { $(this).parent().removeClass('hover'); }
	);


	/* focus on first input control of general-forms */
	/*var field = $('.general-form input:text,.general-form input:radio,.general-form select')[0];
	if (field!=null) {
		try {field.focus();} catch (e) {}
		try {field.select();} catch (e) {}
		}
	*/
	
	
	/* focus on first input control of general-forms */
	/*var field = $('.small-form input:text,.small-form input:radio,.small-form select')[0];
	if (field!=null) {
		try {field.focus();} catch (e) {}
		try {field.select();} catch (e) {}
		}
	*/
	
	
	
	//put a value in grey in an in put box with no focus or value
	jQuery.fn.no_val_val = function(message, cssclass) {
		return this.each(function(){
			var field = $(this);
			field
				.focus(function() { if (field.hasClass(cssclass)) { field.val(''); field.removeClass(cssclass); } } )
				.blur(function() { if (field.val()=='') { field.addClass(cssclass); field.val(message); } } )
				//.change(function() { console.log("out")})
				.parents('form')
					.submit(function() { if (field.hasClass(cssclass)) { field.val(''); field.removeClass(cssclass); }});
			if (field.val()==message)
				field.val('');
			if (field.val()=="") {
				field.addClass(cssclass);
				field.val(message);
			}			
		});
	};
	
	/* the search field should show 'search' instead of being empty */
	var search_field = $('.search-field, .sidebar-search-field')
	var search_txt = search_field.attr('alt');
	search_field.no_val_val(search_txt, 'search_message');
	
	/* any phonenumber fields should use the jquery mask plugin */
	$('.phone_number_edit').mask('(999) 999-9999');
	
	//$('.status_number_edit').mask('99999-99999');
	
	/* filter out invalid changes to the urlname after it's changed */
	$('.url_name_edit').change(function() {  	
		$(this).val($(this).val()
		.toLowerCase()
		.replace(/[^a-z0-9_-]+/g,'-')
		.replace(/[_-]+$/,'')
		.replace(/^[_-]+/,'')
		.replace(/-{2,}/,'-')
		.replace(/_{2,}/,'_'));  
	});
	
});
	    
	    
function generatePieFromTable(table_selector, pie_selector, pie_options) {
	//loads series data from a properly marked table
	var skip_rows = 0; //num of rows of table that are header

	function getPieDataFromTable(table_selector) {
		var result = [];
		var table = $(table_selector);
		if (table.size()==0) return [];
		table.find('tr').each(function() {
			var row = $(this);
			var label = row.find(".label-col").html();
			var data = row.find(".data-col").html();
			if (data==null || data=='')
				data = '0';
			if (label!=null) 
				result.push({label:label, data: parseFloat(data.replace(',',''))});
			else
				skip_rows++;
		});
		return result;
	}
   
	//pushes legend colors back into standard table (where .legend-col exists)
	function updateTableLegend(table_selector,the_plot) {
		var legend_cells = [];
		var colors = [];

		$(table_selector).find('tr').each(function() {
			var row = $(this);
			var legend_cell = row.find(".legend-col");
			if (legend_cell.size()>0) {
				legend_cell.html("<div style='border:1px solid #ccc; display:block; width:11px; height:11px; margin:auto;'></div>");
				legend_cells.push(legend_cell.find("div"));
			}
		});
		var series = the_plot.getData();
		for (var i = 0; i < series.length; ++i)
			colors.push(series[i].color);

		for (var i=0; i<Math.min(legend_cells.length, colors.length); i++) {
			legend_cells[i].css({'width':'12px','background-color':colors[i]});
		}
	}
	
	function addPieHighlighters(table_selector,plot) {
		var series_index = 0;
		$(table_selector).find('tr').each(function() {
			var row = $(this);
			if (row.find(".legend-col").size()==0) return;
			row.find('td').each(function() {
				var td = $(this);
				var ind = series_index;
				if (td.attr('rowspan')==1)
					td.hover(function(){ plot.highlight(ind,0);  },function() { plot.unhighlight(ind,0); });
			});
			series_index++;
		});
	}
	
	try {
		var previous_row = -1;
		var data =  getPieDataFromTable(table_selector);
		var plot = $.plot($(pie_selector), data, {series:{pie: pie_options}, legend: {show:false}, grid: {hoverable: true}})
		updateTableLegend(table_selector,plot);
		//addPieHighlighters(table_selector,plot);
	
		$(pie_selector).bind("plothover", function(event, pos, obj)	{
			var rows = $(table_selector).find('tr');
			if (previous_row>-1) {
				$(rows[previous_row]).removeClass('hover');
				previous_row = -1;
			}
			previous_row = obj.seriesIndex + skip_rows;
			$(rows[previous_row]).addClass('hover');
		});
		
		$(pie_selector).bind("mouseout", function(event) {
			plot.unhighlight();
			if (previous_row>-1) {
				var rows = $(table_selector).find('tr');
				$(rows[previous_row]).removeClass('hover');
				previous_row = -1;
			}
		});
	} catch (e) {
		if (console && console.log) console.log("Error adding chart for "+table_selector+": "+e);
	}
	
}
	


