Cufon.replace('.sari', {hover: true});
function hover(row) {
	$(row).addClass('hover')
}
function out(row) {
	$(row).removeClass('hover')
}
function toggleColor(row) { 
	$(row).toggleClass('selected')
}

function toggle(link) {
	$(link).closest('li').toggleClass('open');
	return false;
}
function expandAll() {
	$("#results").children().addClass('open');
	return false;
}
function collapseAll() {
	$("#results").children().removeClass('open');
	return false;
}

var Modal = {
	show_error: function(msg){
		var errorDiv = $(".error", this.modal);
		if (!errorDiv.length) {
			 errorDiv = $('<div class="error"></div>').prependTo(".bd", this.modal);
		}
		errorDiv.html('<p>'+msg+'</p>');
		this.fixIE();
	}, 
	
	fixIE: function(){		
		$(".ft", this.modal).css({position:'absolute'}).css({position:'static'})
	},
	
	overlay: null,
	_fixHeight: function(){
		var h = $(document).height();
		$(".overlay", this.modal).height(h);
	},
	fetch: function(settings){
		if (!settings.url) { throw 'no url supplied'; return false; }
		var self = this;
		$.ajax({
			type: 'get',
			url: settings.url,
			dataType: 'html',
			success: function(html){
				self.show({
					hd: settings.title,
					bd: html,
					className: settings.className
				});
				if (settings.callback && typeof(settings.callback) == 'function') {
					settings.callback();
				}
			},
			error: function(){
				self.show_error('Network error')
			}
		});
	},
	show: function(settings){
		if (!this.modal) {
			this.modal = $("#modal");
		}
		this.errorDiv = null;
		$(".hd h3", this.modal).html(settings.hd);
		$(".bd", this.modal).html(settings.bd);
		$(".ft", this.modal).html(settings.ft);
		this.modal.addClass(settings.className)
		this._fixHeight();
		this.modal.show();
	},
	hide: function(){
		if (!this.modal) {
			this.modal = $("#modal")
		}
		this.modal.hide();
	}
}

var Rates = {
	type: null,
	
	search_by_product: function(){
		var self = this;
		Modal.fetch({
			url: 'modals/search-by-product.html',
			title: 'Search for a rate by product',
			callback: function(){
				if (self.product) {
					$('#product_name option[value='+self.product+']').attr({selected: 'selected'});
				}
			}
		});
		this.type = 'product'
	},
	
	save_product: function(){
		this.product = $("#product_name").val();
	},
	
	select_state: function(){
		var self = this;
		
		if (!this.product) {
			Modal.show_error('Please select a product');
			return;
		} 
		
		Modal.fetch({
			url: 'modals/select-state.html',
			title: 'Search for a rate by product',
			callback: function(){				
				if (self.states && self.states.length) {
					for (var i=0, l=self.states.length; i<l; i++) { 
						$("#"+self.states[i]).attr({checked: 'checked'})
					}
				}
			}
		});
	},
	
	save_states: function(){		
		this.states = [];
		var self = this;
		$("input[type=checkbox]:checked").each(function(){
			self.states.push(this.value)
		});
	},
	
	select_timeframe: function(){
		var self = this;
		
		if (this.type == 'product') {
			if (!this.states || !this.states.length) {
				Modal.show_error('Please select a state');
				return;
			}
			
			Modal.fetch({
				url: 'modals/select-timeframe.html',
				title: 'Search for a rate by product',
				callback: function(){				
					if (self.timeframe) {
						$("input[value="+self.timeframe+"]").attr({checked: 'checked'})
					}
				}
			});
		}
		else {
			
			if (!this.code) {
				Modal.show_error('Please enter interest code');
				return;
			}
			
			Modal.fetch({
				url: 'modals/select-timeframe2.html',
				title: 'Search for a rate by interest code',
				callback: function(){				
					if (self.timeframe) {
						$("input[value="+self.timeframe+"]").attr({checked: 'checked'})
					}
				}
			});
		}
	},
	
	save_timeframe: function(){
		this.timeframe = $("input[type=radio]:checked").val();
	},
	
	search_or_date: function(){
		if (this.timeframe == 'historical') {
			this.select_dates();
		}
		else if(this.timeframe == 'current') {
			this.search();
		}
		else {			
			Modal.show_error('Please pick timeframe');
		}
	},
	
	select_dates: function(){
		if (!this.timeframe) {
			Modal.show_error('Please pick timeframe');
			return;
		}
		var self = this;
		if (this.type == 'product') {
			Modal.fetch({
				url: 'modals/select-dates.html',
				title: 'Search for a rate by product',
				callback: function(){
					self.setup_calendar();
				}
			});
		}
		else {
			
			if (!this.code) {
				Modal.show_error('Please enter interest code');
				return;
			}
			
			Modal.fetch({
				url: 'modals/select-dates2.html',
				title: 'Search for a rate by interest code',
				callback: function(){			
					self.setup_calendar();
				}
			});
		}
	},
	
	setup_calendar: function(){
		var self = this;
		$("#date_start").datepicker({
			dateFormat: 'dd/mm/yy',
			maxDate: self.date_end,
			buttonImage: 'img/calendar.png',
			showOn: 'both',
			onSelect: function(dateText, inst){
				self.date_start = dateText; 
				$("#date_end").datepicker('option', 'minDate', new Date(inst.selectedYear, inst.selectedMonth, inst.selectedDay))
			}
		});
		$("#date_end").datepicker({
			dateFormat: 'dd/mm/yy',
			minDate: self.date_start,
			buttonImage: 'img/calendar.png',
			showOn: 'both',
			onSelect: function(dateText, inst){
				self.date_end = dateText;
				$("#date_start").datepicker('option', 'maxDate', new Date(inst.selectedYear, inst.selectedMonth, inst.selectedDay))
			}
		});
	},
	
	search: function(){
		window.location.href = 'results.html';
	},
	
	search_by_code: function(){		
		var self = this;
		Modal.fetch({
			url: 'modals/search-by-code.html',
			title: 'Search for a rate by interest code',
			callback: function(){
				if (self.code) {
					$('#interest_code').val(self.code);
				}
			}
		});		
		this.type = 'code';
	},
	
	save_code: function(){
		this.code = $('#interest_code').val();
	}	
}

/* admin tools */
var Admin = {
	
	login_panel: function(){
			Modal.fetch({
				url: 'modals/login.html',
				title: 'Admin Login',
				callback: function(){
					$("input", Modal.modal).focus();
				}
			});
	},
	
	login: function(){
		window.location.href = 'admin.html';
	},
	
	add_code: function(product_code){	
		if (!product_code) {
			var self = this;
			Modal.fetch({
				url: 'modals/add-code.html',
				title: 'Add an interest code',
				callback: function(){
					$("input", Modal.modal).focus();
				}
			});
		}
		else {
			this.code = product_code;
			this.edit_code();
		}
	},
	
	save_new_code: function(){
		this.product = $('#product_name').val();
		if (!this.product) {			
			Modal.show_error('Please select product');
			return;
		}
		window.location.href = 'add-product.html';
	},
	
	pick_code: function(){
		Modal.fetch({
			url: 'modals/pick-code.html',
			title: 'Edit an interest code'
		});		
	},
	
	set_code: function(){
		this.code = $("#interest_code").val();
	},
	
	edit_code: function(){
		if (!this.code) {
			Modal.show_error('Please enter interest code');
			return;
		}
		var self = this;
		Modal.fetch({
			url: 'modals/edit-code.html',
			title: 'Edit an interest code',
			callback: function(){
				// fetch data related to this interest code and populate the form (?)
				$("#interest_code").val(self.code)
			}
		});		
	},
	
	save_code: function(){
		// update interest code info and go to summary page
		window.location.href = 'edit-product.html'
	},
	
	add_product: function(code, subcode){	
		Modal.fetch({
			url: 'modals/add-product.html',
			title: 'Add a product',
			callback: function() {
				if (code) {
					// fetch data and populate form
				}
			}
		});		
	},
	
	save_product: function(){
		window.location.href = 'add-product.html'
	},
	
	pick_product: function(){		
		Modal.fetch({
			url: 'modals/pick-product.html',
			title: 'Edit a product'
		});		
	},
	
	edit_product: function(){
		Modal.fetch({
			url: 'modals/add-product.html',
			title: 'Edit a product',
			callback: function(){
				// populate data
			}
		});	
	},
	
	view_reports: function(){
		
	}
	
}



/** START editable table **/

$.table = { // event handlers
	defaults: {
		table_cnt_class: 'dynatable'
	},
	edit: function(btn){ 
		var table_cnt = $(btn).closest('.'+this.defaults.table_cnt_class), dyntable;
		if (!table_cnt.data('dyntable')) {
			dyntable = new DynTable(table_cnt);
			table_cnt.data('dyntable', dyntable);
		}
		else {
			dyntable = table_cnt.data('dyntable');
		}
		dyntable.edit();
	},
	save: function(btn){
		var table_cnt = $(btn).closest('.'+this.defaults.table_cnt_class);
		if (dyntable = table_cnt.data('dyntable')) {
		//	dyntable.save();
			alert('saving')
		}
	},
	remove: function(btn){
		var table_cnt = $(btn).closest('.'+this.defaults.table_cnt_class);
		if (!table_cnt.data('dyntable')) {
			dyntable = new DynTable(table_cnt);
			table_cnt.data('dyntable', dyntable);
		}
		else {
			dyntable = table_cnt.data('dyntable');
		}
		dyntable.remove();
	},

	cancel: function(btn){
		var table_cnt = $(btn).closest('.'+this.defaults.table_cnt_class);
		if (dyntable = table_cnt.data('dyntable')) {
			dyntable.cancel();
		}
	}
};



function DynTable(el){ 
	this.root = el; // jquery DOM element
	var table = this.table = $('table', el); 
	this._cached_html = el.html();
	this.info = {
		columns: $('tbody tr:first td', table).length,
		rows: $('tbody tr', table).length,
		table_id: table.attr('id').replace(DynTable.defaults.sctn_pfx, ''),
		title: $('.table_title', el),
		draggable: true
	};
	this.data = {
		id: this.info.table_id.replace('table_', ''),
		resource_id: el.closest('.content').attr('id').replace(/^table_/, '')
	};
}
DynTable.defaults = {
	sctn_pfx: 'section_',
	minwidth: 33,
	padding: 6,
	wysiwyg_settings: {
		theme : "advanced",
		setup : function(ed) {
			ed.onInit.add(function(ed){ed.focus();});
		},
		theme_advanced_buttons1 : "bold,italic,underline,strikethrough,|,undo,redo,|,forecolor,|,bullist,numlist,|,link,unlink",
		theme_advanced_buttons2: "",
		theme_advanced_buttons3: "",
		theme_advanced_buttons4: "",
		theme_advanced_more_colors : false,
		theme_advanced_toolbar_location : "top",
		theme_advanced_toolbar_align : "left",
		theme_advanced_statusbar_location : "none"
	},
	html : {
		row_ctrl: '<td class="control ctrl_left"><a href="javascript:void(0)" class="ico ico_delete remove_row" title="remove row">delete row</a></td>',
		col_ctrl: '<a href="javascript:void(0)" class="ico ico_delete remove_col" title="remove column">remove col</a>',
		head_html: 'add a column name</th>',
		flyout_html: '<div class="container shadow w300"><div class="module bg_white"><div class="wrapper"><div class="head"><h2>Define the fixed width of a column</h2></div><div class="content mixed_wrapper"><p class="bottomSpace">Column width will be set as a %.</p><form action="#" method="post"><fieldset><ul class="details"><li class="clearfix"> <label for="col_w" style="width: auto;">Column width</label> <input id="col_w" type="text" class="text" style="width: 130px;" maxlength="5" /></li><li class="action"> <input class="btn btn_cancel" type="reset" value="" /> <input class="btn btn_ok" type="submit" value="" /></li></ul></div><div class="foot"></div></div></div>',
		edit_modal_html: '<div class="wrapper"><dl class="form"><dd><textarea id="cell_content" name="cell_content" style="width:432px;height:130px;"></textarea></dd></dl></div><div class="clearfix ctrl"><input class="btn btn_ok btn_l toRight" type="submit" value="" /><input class="btn btn_cancel btn_l toLeft" type="reset" value="" /></div>',
		header_modal_html: '<div class="wrapper"><dl class="form"><dd><input id="col_title" class="text short" type="text" /></dd></dl></div><div class="ctrl clearfix"><input class="btn btn_ok btn_l toRight" type="submit" value="" /><input class="btn btn_l btn_cancel toLeft" type="reset" value="" /></div>',
		table_ctrl: '<div class="r table_ctrl clearfix top20"><a href="javascript:void(0)" onclick="$.table.cancel(this)" class="btn_green table_cancel">Cancel</a> <a href="javascript:void(0)" onclick="$.table.save(this)" class="btn_green table_save">Save</a></div>'
	}
};

DynTable.prototype = {

	settings: {
		editableColumns: false,
		editableRows: true,
		draggableColumns: false,
		sortableRows: false,
		editableHeaders: false
	},
	
	edit: function(){ 
		this._setup(); 
	},
	
	save: function(){
		this._synchronise();
		save_table(this.data);
		this._cleanup();
	},
		
	cancel: function(){
		this._cleanup();
		this.root.html(this._cached_html);		
	},

	remove: function(){
	  show_modal('please confirm','<div class="wrapper"><p class="top20">Are you sure you want to delete this table?</p></div><div class="ctrl clearfix"><a href="javascript:void(0)" onclick="hide_modal();" class="btn btn_l btn_cancel toLeft" title="cancel">cancel</a><a href="javascript:void(0)" onclick="delete_table(\''+this.table.attr('id')+'\')" class="btn btn_l btn_delete toRight" title="delete">delete</a></div>',1,'w300');
		return false;
	},
	
	_cleanup: function(){
		var info = this.info, root = this.root, table = this.table; var settings = this.settings;
		// unbind all
		$("table#"+info.table_id+" > tbody > tr > td:not(.control)").die('click');
		$("table#"+info.table_id+" > thead > tr:not(.control) > th:not(.control)").die('click');
		
		if (settings.draggableColumns) {
			this._draggable_destroy();
		}
		
		$('.add_col', root).unbind('click.editable');
		$('.add_row', root).unbind('click.editable');
		$("table#"+info.table_id+" .remove_row").die('click.editable');
		$("table#"+info.table_id+" .remove_col").die('click.editable');
		
		$('col:last').remove();
		$('tfoot', table).remove();
		$('.control', table).remove();
		$('.add_col, .add_row, .table_ctrl', root).remove();

		info.title.html(this.data.name || info.table_title);

		this.root.removeClass('edit_mode');
		this.root.removeData('dyntable');
		this._ready = false;
	},
	
	recalculate: function(cell){
		var all = cell.siblings(".rate");
		var total = 0;
		all.each(function(){ 
			total += (parseFloat($(this).text()) ? parseFloat($(this).text()) : 0 );
		});
		if (cell.is('.rate')) {
			total += parseFloat(cell.text())
		}
		
		cell.siblings('.total').text(total+'%')
	},
	
	// private methods 
	_cell_edit_inplace: function(cell){
		var c = $(cell), content = parseFloat(c.text()), self = this;
		c.html('<input type="text" class="text" value="'+content+'" />');
		c.find('input').select().bind('blur', function(e){
			e.stopPropagation();
			var t = $(this).val();
			$(this).unbind().remove();
			c.text(parseFloat(t)? parseFloat(t)+'%' : '0.00%');
			self.recalculate(c);
		})
			.bind('click', function(e){
			e.stopPropagation();
		})
			.bind('keydown', function(e){
			switch (e.keyCode) {
				case 48: 
				case 49: 
				case 50: 
				case 51: 
				case 52: 
				case 53: 
				case 54: 
				case 55: 
				case 56: 
				case 57: 
				case 9: 
				case 96: 
				case 97: 
				case 98: 
				case 99: 
				case 100: 
				case 101: 
				case 102: 
				case 103: 
				case 104: 
				case 105: 
				case 8: 
				case 46: 
				case 110: 
				case 190: break;			
				case 13: 	$(this).blur();
				default:  e.preventDefault();
			}
		});
	},
	
	_cell_edit: function(cell){
		//Edit table cell contents				
		if (!this._edit_modal_visible) {

			var cell = $(cell),
				content = cell.html(),
				offset = cell.offset(),
				css = {position: 'absolute', top: offset.top-35, left: '50%', 'margin-left': '-150px', 'z-index': 10001},
				self = this,
				editor;
			
			var save_to_cell = function(e){
				e.preventDefault();
				if (editor) {
					cell.html(editor.getContent());
					editor.remove();
				}
				else {
					cell.html($("#cell_content").val());
				}
				hide_modal();
				self._edit_modal_visible = false;
			};
			show_modal('Edit cell contents', DynTable.defaults.html.edit_modal_html, '', 'w480', {scroll: false});
			$('.btn_cancel', app.modal).bind('click', function(e){
				e.preventDefault();
				$('.btn_ok, .btn_cancel', app.modal).unbind('click');
				hide_modal();
				self._edit_modal_visible = false;
			});
			$('.btn_ok', app.modal).bind('click', save_to_cell);
			$("#cell_content", app.modal).val(content);
			app.modal.css(css);

			var editor = new tinymce.Editor('cell_content', DynTable.defaults.wysiwyg_settings);							
			editor.render();

			this._edit_modal_visible = true;
		}
	},
	
	_header_edit: function(cell){
		
		if (!this._header_modal_visible) {
			
			var cell = $(cell),
				content = cell.html(),
				offset = cell.offset(),
				css = {position: 'absolute', top: offset.top-35, left: '50%', 'margin-left': '-150px'},
				self = this;
			
			var save_header = function(e){
				e.preventDefault();				
				$('.btn_ok, .btn_cancel', app.modal).unbind('click');
				cell.html($("#col_title").val());				
				hide_modal();
				self._header_modal_visible = false;
			};
			
			show_modal('Edit heading', DynTable.defaults.html.header_modal_html, '', 'w300', {scroll: false});
			$('.btn_cancel', app.modal).bind('click', function(e){
				e.preventDefault();
				$('.btn_ok, .btn_cancel', app.modal).unbind('click');
				hide_modal();
				self._header_modal_visible = false;
			});
			$('.btn_ok', app.modal).bind('click', save_header);			
			$("#col_title", app.modal).val(content);
			app.modal.css(css);
			self._header_modal_visible = true;
		}
	},
	
	_column_add: function(){
		var defaults = DynTable.defaults, 
			table = this.table,
			padding = defaults.padding,
			minwidth = defaults.minwidth;
		// loop over rows and make room for next column
		var w_temp, index;
		
		for (var i=0, ob=$('col', table), l=ob.length; i<=l-1; i++ ) {
			var el = ob.eq(i);
			if ( parseInt(el.width(),10) - padding/2 > 2 * minwidth) {
				w_temp = parseInt(el.width(),10);
				index = i;
			};
		}
		if (index !== 'undefined' &&  w_temp - padding/2 > 2 * minwidth) {
			
			var new_w = minwidth - padding/2;
			
			$('col', table).eq(index).css('width', w_temp - minwidth - padding/2);
			$('col:last', table).before('<col style="width:'+new_w+'px" />');
			
			$('tbody tr td:last-child', table).before('<td></td>');
			$('tfoot tr td:last-child', table).before('<td>'+defaults.html.col_ctrl+'</td>');
			$('thead tr th:last-child',table).before('<th>'+defaults.html.head_html);
			
			this.info.columns++;
			
			if (settings.draggableColumns) {
				this._draggable_reset();
			}
			
			table.trigger('modify');
		}
		else {
			alert('no more space');
		}
	},
	_column_remove: function(index) {
		var table = this.table, 
			to_remove = $('col', table).eq(index),
			w_temp = parseInt(to_remove.width(), 10);
			
		if (index > 1) { // merge with previous cell
			var prev = to_remove.prev();
			prev.width(parseInt(prev.width(), 10) + w_temp);
			to_remove.remove();
		}
		else {
			var next = to_remove.next();
			next.width(parseInt(next.width(), 10) + w_temp);
			next.prev().remove();
		}
		$('tr', table).each(function(){
			$('td', this).eq(index).remove();
			$('th', this).eq(index).remove();
		});
		
		this.info.columns--;
		
		if (settings.draggableColumns) {
			this._draggable_reset();
		}

		table.trigger('modify');
	},
	
	_row_add: function(){
		$("tr:last", this.table).removeClass('lastRow');
		var html = '<tr class="lastRow">';
		var classNames = ['', 'rate', 'rate', 'total'];
		for (var i=0, l=this.info.columns; i<l; i++) {
			html += '<td class="'+classNames[i]+'"></td>';
		}
		html += DynTable.defaults.html.row_ctrl+'</tr>';
		this.table.append(html);
		this.info.rows++;
		this.table.trigger('modify');
	},
	_row_remove: function(elem) {
		$(elem).parents('tr').eq(0).remove();
		this.info.rows--;
		this.table.trigger('modify');
	},
	
	_draggable_init: function(){
		var table = this.table,
			padding = DynTable.defaults.padding,
			minwidth = DynTable.defaults.minwidth,
			self = this;
		
		var resizeCol = function(column, width) {
			column.css('width', width);
		}
		// add handles
		var dragHTML = "<i></i>",
			dragCSS = {
				position: 'absolute',
				cursor: 'w-resize'
			},
			col = $("col", table),
			dr_widths = [],
			handles = this._handles = [];
			
		table.parent().css('position','relative');
		col_total = col.length;
		col.each(function(i){
			var el = $(this),
				data = {};
			data.offset = i > 0 ? dr_widths[i-1].width + dr_widths[i-1].offset : -12;
			data.width = el.width();
			
			
			dr_widths.push(data);
			
			if (i == 0 || i == col_total-1) {
				data.apply = false;
			}
			else {
				data.apply = true;
				var drag = $(dragHTML).insertBefore(table).css(dragCSS).css('left',data.offset -3).css('top', -15).attr('cLeft',i-1).attr('cRight',i);
				handles.push(drag);
			}
		});
		if (handles.length) {
			$(handles).each(function(){

				var handle = $(this),
					initial = parseInt(handle.css('left'),10),
					leftCol = $("col", table).eq( parseInt(handle.attr('cLeft'),10)),
					rightCol = leftCol.next('col'),
					leftWidth = parseInt(leftCol.width(),10),
					rightWidth = parseInt(rightCol.width(),10),
					len = rightWidth + leftWidth + padding + parseInt(leftCol.css('border-left-width'),10) + parseInt(leftCol.css('border-right-width'),10) + padding + parseInt(rightCol.css('border-left-width'),10) + parseInt(rightCol.css('border-right-width'),10) - minwidth*2,
					index = handle.attr('cLeft'),
					rest = handle.prev().length ? parseInt(handle.prev().css('left'),10) : dr_widths[0].offset;
					
				var left = initial-leftWidth+minwidth + (table.offset()).left,
					right = initial+rightWidth-minwidth + (table.offset()).left;
				handle.draggable('destroy');
				handle.draggable({
					axis: 'x',
					cursorAt: { left: 0, top: 0 },
					drag: function(event, ui){
						resizeCol(rightCol, rightWidth - ui.position.left + initial);
						resizeCol(leftCol, leftWidth + ui.position.left - initial);										
					},
					containment: [left, parseInt(handle.css('top'),10), right, parseInt(handle.css('top'),10)],
					stop: function(event, ui){
						self._draggable_reset();
					}
				});
			});
		}
	},
	_draggable_destroy: function(){
		$(this._handles).each(function(){ 
			$(this).draggable('destroy').unbind().remove();
		})
	},
	_draggable_reset: function(){
		this._draggable_destroy();
		this._draggable_init();
	},
	
	_read_head: function(){
		var data = this.data, table = this.table;
		data.table_head = [];
		var temp_head = [];
		var cols = $('col:not(:last)', table);
		$('thead tr th:not(.control)', table).each(function(i){
			temp_head.push({content: $(this).text(), width: cols.eq(i).width() + 'px'});
		});		
		data.table_head.push(temp_head); 
	},
	
	_read_body: function(){
		var data = this.data, table = this.table;
		data.table_body = [];
		$('tbody tr', table).not('.mceLayout tr').each(function(){
			var temp_row = [], 
				elems = $('td:not(.control)', this);
				
			elems.each(function(){
				temp_row.push({ content: $(this).html()});
			});
			if (temp_row.length) {
				data.table_body.push(temp_row);
			}
		});
	},
	
	_synchronise: function(e){ 
		var self = e? e.data.self : this;
		var data = self.data, table = self.table, info = self.info;
		self._read_body();
		self._read_head();
		col_total = info.columns;
		row_total = info.rows;
		if (row_total == 1) {
			// single row left 
			$('a.remove_row', table).hide();
		}
		else {
			$('a.remove_row', table).show();						
		}
		if (col_total == 1) {
			// single column left 
			$('a.remove_col', table).hide();
		}
		else {
			$('a.remove_col', table).show();						
		}
		data.name = $('input', info.title).val() || info.table_title;

	},
	
	_setup: function(){
		if (this._ready) { 
			return false;
		}
		else {
			var settings = this.settings;
			var table = this.table,
				root = this.root,
				info = this.info,
				self = this;
			this._ctrl = $(DynTable.defaults.html.table_ctrl).insertAfter(table);

			table.closest('.dynatable').addClass('edit_mode');

			if (!$('col', table).length) { // append <col> elems (no widths yet set)
				var total = info.columns, html = '';
				for (var i=0; i<total; i++) {
					html += '<col no="'+i+'" />';
				}
				table.prepend(html);
			}
			
			var widths = [];
			$('col', table).each(function(){ // set default width
				var w  = $(this).css('width', $(this).width());
				widths.push(w.width());
			});
			if (settings.editableColumns) {
				if ($('tfoot tr', table).length) { 
					$('tfoot tr', table).append("<td></td>").show();
				}
				else {
					var h = '<tfoot><tr>'
					for (var i=0, l=info.columns; i<l; i++) {
						h+='<td>'+DynTable.defaults.html.col_ctrl+'</td>';
					}
					h+='<td></td></tr></tfoot>';
					$('thead', table).after(h);
				}
			}
			$('thead',table).before('<col style="width:0px;" />');
			
			$('thead tr:not(.control)', table).append('<th class="control ctrl_left"></th>');
			$('tbody tr', table).append(DynTable.defaults.html.row_ctrl);
			
			var ctrlHTML = '<div class="toL">';
			
			if (settings.editableColumns) {
				'<a href="javascript:void(0)" class="btn_green_add add_col">Add a column</a> ';
			}
			if (settings.editableRows) {
				ctrlHTML += '<a href="javascript:void(0)" class="btn_green_add add_row">Add a row</a>'
			}
			table.after(ctrlHTML+='</div>');
			
			if (settings.editableColumns) {
				$('.add_col', root).bind('click.editable', function(e){
					e.preventDefault();
					self._column_add();
				});
			}
			if (settings.editableRows) {
				$('.add_row', root).bind('click.editable', function(e){
					e.preventDefault(); 
					self._row_add();
				});
			}
			if (settings.editableRows) {
				$("table#"+info.table_id+" .remove_row").live('click.editable', function(e){
					e.preventDefault(); 
					self._row_remove(this);
				});
			}
			if (settings.editableColumns) {
				$("table#"+info.table_id+" .remove_col").live('click.editable', function(e){
					e.preventDefault(); 
					var index = $('tfoot tr td', table).index($(this).closest('td'));
					self._column_remove(index);
				});
			}
			
			info.table_title = info.title.text();
			info.title.html('<input type="text" value="'+info.title.text()+'" />');
			
			// bind all cells to open popup
			$("table#"+info.table_id+" > tbody > tr > td:not(.control, .total)").live('click', function(e){
			//	self._cell_edit(this);
				self._cell_edit_inplace(this);
			});
			
	
			if (settings.editableHeaders) {
				// bind all headers to open popup
				$("table#"+info.table_id+" > thead > tr:not(.control) > th:not(.control)").css({cursor: 'pointer'}).live('click', function(e){
					self._header_edit(this);
				});
			}

			//$('tbody', table).attr('title','Click to edit cell contents');
				
			if (settings.sortableRows) {
				$('tbody', table).sortable({ 
					items: 'tr',
					update: function(){
						table.trigger('modify')
					} 
				});
				$('tr.even', table).removeClass('even');
			}
			$("tr:last", table).addClass('lastRow');
			
			this._synchronise();
			
			table.bind('modify', {self: this}, this._synchronise);
			
			if (settings.draggableColumns) {
				this._draggable_init();
			}
			this._ready = true;
		}		
	}
	
};

/** END editable table **/