jQuery.fn.extend({
		tableSorter: function(options){
		
			if(options==undefined)
				options = {};
			
			if(!jQuery.isFunction(options.onComplete))
				options.onComplete = function(e){};
			
			var mainEl = $(this);
			var rows = $(this).find('tbody > tr').get();
		
			mainEl.data("tableSorterCol", null);
			mainEl.data("tableSorterOrder", "asc");
			mainEl.data("tableSorter", {
				rows: rows,
				onComplete: options.onComplete,
				sortColumn: function(col){
					// BUILD COL ARRAY FOR SORTAGE
					var sortOrder = "asc";
					if(mainEl.data("tableSorterCol")==col)
					{
						if(mainEl.data("tableSorterOrder")=="asc")
						{
							sortOrder = "desc";
						}
					}
					mainEl.data("tableSorterOrder",sortOrder);
					
					mainEl.find("thead tr td").removeClass("sortasc").removeClass("sortdesc");
					mainEl.find("thead tr td").eq(col).addClass("sort"+sortOrder);
					
					mainEl.data("tableSorter").rows.sort(function(a, b) {
						if(sortOrder=="asc")
						{
							var keyA = $(a).children('td').eq(col).html().toUpperCase()
							var keyB = $(b).children('td').eq(col).html().toUpperCase()
						} else {
							var keyB = $(a).children('td').eq(col).html().toUpperCase()
							var keyA = $(b).children('td').eq(col).html().toUpperCase()
						}
						if (keyA < keyB) return -1;
						if (keyA > keyB) return 1;
						return 0;
					});
					mainEl.data("tableSorterCol",col);
					mainEl.data("tableSorter").buildTable();
				},
				buildTable: function(){
					$.each(rows, function(index, row) {
						mainEl.children('tbody').append(row);
					});
					mainEl.data("tableSorter").onComplete(mainEl);
				}
			});
		
			// FIND WHAT COLS TO READ
			$.each(mainEl.find("thead tr td"), function(index) {
				if($(this).hasClass("sort"))
				{
					$(this).click(function(){
						if(mainEl.data("tableSorter").rows.length>1)
						{
							mainEl.data("tableSorter").sortColumn(index);
						}
					});
				}
			});
	
		
		},
		tooltip: function(text,options){
			$(this).each(function(e){
				$(this).removeTooltip();
				
				if(options==undefined)
					options={};
				if(options.move==undefined)
					options.move=true;
			
				$(this).css("position","relative");
				var tooltipText = text;
				var tooltip = $("<div>").addClass("tooltip");
				var tooltipLeft = $("<div>").addClass("tooltip_left");
				var tooltipMiddle = $("<div>").addClass("tooltip_bg");
				var tooltipRight = $("<div>").addClass("tooltip_right");
				tooltipMiddle.append($("<span>").append(tooltipText));
				tooltip.append(tooltipLeft).append(tooltipMiddle).append(tooltipRight)
				var tooltipWidth = Math.ceil(tooltipText.length*8)-14;
				tooltipMiddle.width(tooltipWidth);
				tooltip.width(tooltipWidth+36);
				$(this).append(tooltip);
			
				$(this).data("tooltip", {
					e: tooltip,
					text: tooltipText,
					show:function(e){tooltip.show();},
					hide:function(e){tooltip.hide();},
					move:function(e){
						var x = e.pageX-(this.offsetLeft+21);
						var y = e.pageY-(this.offsetTop+35);
						tooltip.css({left:x,top:y});
					}
				});
		 
				tooltip.hide();
				$(this).mouseenter($(this).data("tooltip").show);
				$(this).mouseleave($(this).data("tooltip").hide);
				if(options.move==true)
					$(this).mousemove($(this).data("tooltip").move);
				
			});
		},
		
		removeTooltip: function(){
			if($(this).data("tooltip")!=undefined)
			{
				// REMOVE ALL EVENTS
				$(this).unbind("mouseenter",$(this).data("tooltip").show);
				$(this).unbind("mouseleave",$(this).data("tooltip").hide);
				$(this).unbind("mousemove",$(this).data("tooltip").move);
				// REMOVE ELEMENT
				$(this).data("tooltip").e.remove();
			
				$(this).removeData("tooltip");
			}
		},
		
		autoHeight: function(maxHeight){
			$(this).data("autoHeight", {
				height:$(this).height(),
				width:$(this).width()
			});
			$(this).keyup(function(){
				var limitPerLine = Math.ceil(($(this).width())/7);
				var calcHeight = 0;
				var break_lines = 0;
				var text = $(this).val();	
				
				if(rows<1)
					rows = 1;
				
				var lineText = text.split("\n");
				var break_lines = lineText.length;
				if(break_lines<1)
					break_lines=1;
				
				var count = 0;
				var rows = 0;
				for(i=0; i<lineText.length; i++)
				{
					var chars = lineText[i].split("");				
					for(o=0; o<chars.length; o++)
					{
						if(count>limitPerLine)
						{
							rows++;
							count=0;
						}
						count++;
					}
				}
				
				calcHeight+=break_lines*18;
				calcHeight+=rows*18;
				
				if(calcHeight >= maxHeight)
					calcHeight = maxHeight;
				$(this).height(calcHeight);
			});
		},
		
		simpleForm: function(input,options) {
			if(options==undefined)
				options={};
			if(options.tooltip==undefined)
				options.tooltip="";
			if(options.width==undefined)
				options.width=100;
			if(options.maxHeight==undefined)
				options.maxHeight=0;
			
			
			if(!jQuery.isFunction(options.onClick))
				options.onClick = function(e){};
				
			$(input).hide();
			var text = $(this).text();
			var elc = $("<div>");
			
			var tagName = $(input)[0].tagName;
			//alert(tagName);
			if(tagName == "TEXTAREA")
			{
				$(this).addClass("simpleForm_editOver");
				$(input).addClass("mainTextarea")
						.height($(this).height())
						.width($(this).width())
						.autoHeight(options.maxHeight);
			}
			if(tagName == "INPUT" && $(input).attr("type")=="text")
			{
				$(this).width(options.width);
				$(this).addClass("simpleForm_editOver_text");
				$(input).width($(this).width())
						.addClass("mainInputText");
			}
			
			if(tagName == "SELECT")
			{
				$(this).width(options.width);
				$(this).addClass("simpleForm_editOver_text");
				$(input).width($(this).width()+34)
						.addClass("mainSelect");
			}
			
			$(this).data("simpleform", {
				e:elc,
				height:$(this).height(),
				width:$(this).width(),
				text:text,
				input:input
			});
			
			if(options.tooltip!="" || options.tooltip!=Undefined)
				$(this).tooltip(options.tooltip);
			
			$(this).click(function(){
				$(this).removeTooltip();
				$(input).show();
				$(this).hide();
				$(this).unbind("click");
				$(input).focus();
				
				options.onClick($(this));
			});
		},
		
		columnsView: function(showFirst,options){

			if(options==undefined)
				options = {};

			if(!jQuery.isFunction(options.onClick))
				options.onClick	 = function(e){};

			if(!jQuery.isFunction(options.extend))
				options.extend	 = function(e){};

			var mainEl = $(this);
			mainEl.addClass("columnsView");
			var columns = $(this).find("td");
			columns.each(function(i){
				// STURCTURE
				var thisColumn = $(this);
				thisColumn.addClass("column");
				var nextColumn = thisColumn.next();
				var thisColumnIndex = i;
				// EVENTS
				thisColumn.find("a").each(function(o){
					$(this).click(function(){

						columns.filter(":gt("+thisColumnIndex+")").find("div").hide();
						columns.filter(":gt("+thisColumnIndex+")").hide();

						thisColumn.find("a").removeClass("columnsViewSelected");
						$(this).addClass("columnsViewSelected");
						var ncDivs = nextColumn.find("div");
						ncDivs.find("a").removeClass("columnsViewSelected");
						ncDivs.hide();
						ncDivs.eq(o).show();
						nextColumn.show();
						mainEl.scrollLeft(100000);
						return false;
					});
				});
			});

			columns.find("div").hide();
			columns.filter(":first").find("div").show();

			if(showFirst)
				mainEl.find("a:first").click();

			columns.filter(":last").find("a").click(options.onClick);
			options.extend(columns);
		}
});
