// jsTables - relies on jquery
// Written By Marc Castles Small Dog Design

image_path = "/puppy/images/";

preload_images(image_path + "table_header_over1.jpg",image_path + "arrow_ASC.png",image_path + "arrow_DESC.png");

var jsTables = new Array();
$(function() {
	//if js tables exist in page make them
	for(var i=0; i<jsTables.length; i++) {
		jsTables[i].build();
	}
});
function jsTable(objName) {
    this.objName = objName;
	this.sortdir = "ASC";
	this.sorted = null;
	this.sortby = null;
	this.filterValue = "";
	this.page = "";
	this.limit = "";
	jsTables.push(this);
	var self = this;
	var req = null;
	this.build = function() {
		
		var html = '<table class="jsTable"><tr>';
		var selectedNum = null;
		for(var i=0; i<this.cols.length; i++) {
			if(this.cols[i].headclass) {
				css = ' class="'+this.cols[i].headclass+'"';
			} else {
				css = "";
			}
			if(self.cols[i].show == null || typeof(self.cols[i].show) == "boolean" && self.cols[i].show == true || typeof(self.cols[i].show) == "function" && self.cols[i].show() != false) {
				show = true;	
			} else {
				show = false;
			}
			if(this.cols[i].width) {
				html += '<th style="width:'+this.cols[i].width+'px'+(show?"":";display:none;")+'"'+css+show+'>';
			} else {
				html += '<th'+css+(show?"":' style="display:none;"')+'>';
			}
			if(this.sortby == this.cols[i].field) {
				selectedNum = i;
			}
			if(this.cols[i].label) {
				if(this.cols[i].sort == true) {
					html += '<a href="#" onclick="'+this.objName+'.sort(\''+this.cols[i].field+'\',this); return false;" onmousedown="'+this.objName+'.clearSelection();" onmouseup="'+this.objName+'.clearSelection();">'+this.cols[i].label+"</a>";
				} else {
					html += this.cols[i].label;
				}
			}
			if(this.cols[i].headDraw) {
				html += this.cols[i].headDraw($(this));
			}
			html += '</th>';
		}
		html += "</tr></table>";

		$("#"+this.div).append(html);
		
		if(selectedNum != null) {
			btn = $("#"+this.div+" tr th a")[selectedNum];
			$(btn).parent().css("background","url("+image_path+"table_header_over1.jpg)");
			$(btn).css("background","url("+image_path+"arrow_"+this.sortdir+".png) no-repeat 3px center");
			$(btn).css("padding-left","20px");
			this.sorted = btn;
		}
		if(this.loadFunc) {
			this.loadFunc();
		}
		if ( req ) req.abort();
		req = $.post(this.xml,this.postvars() , function(xml) {	
			$("#"+self.div+" table tr:has(td)").remove(); //remove all previous rows
			self.draw(xml);
			if(self.completeFunc) {
				self.completeFunc();
			}
		});
	}
	
	this.draw = function(xml) {
		$(xml).find('row').each(function(i){	
			var rows = '<tr onmouseover="$(this).toggleClass(\'hover\');" onmouseout="$(this).toggleClass(\'hover\');">';
			var leftshown = false; //to apply left css
					
			for(var i=0; i<self.cols.length; i++) {
				var css = "";
				if(self.cols[i].css) {
					css = " "+self.cols[i].css;
				}
				if(self.cols[i].show == null || typeof(self.cols[i].show) == "boolean" && self.cols[i].show == true || typeof(self.cols[i].show) == "function" && self.cols[i].show() != false) {
					show = true;	
				} else {
					show = false;
				}
				if(i==0 || leftshown == false) {
					rows += '<td class="left'+css+'"'+(show?"":' style="display:none;"')+'>';
				} else  if(i == (self.cols.length-1)) {
					rows += '<td class="right'+css+'"'+(show?"":' style="display:none;"')+'>';
				} else {
					rows += (css == "" ? '<td'+(show?"":' style="display:none;"')+'>' : '<td class="'+css+'"'+(show?"":' style="display:none;"')+'>');
				}
				if(show) {
					leftshown = true;
				}
				if(self.cols[i].cellDraw) {
					rows += self.cols[i].cellDraw($(this));
				} else if(self.cols[i].field && $(this).attr(self.cols[i].field) != undefined) {
					rows += $(this).attr(self.cols[i].field);
				}
				rows += '</td>';
			}
			rows += '</tr>';
			
			$("#"+self.div+" table").append(rows);
		});
		
		$("#"+this.div+" tr:even").attr('className','row');
		$("#"+this.div+" tr:odd").attr('className','row1');

		
		var pages = Math.ceil($(xml).find("data").attr("total")/this.limit);
		if(pages > 1) {
			var start = 0;
			var end = (pages < 20? pages: 20);
			
			if(pages > 10 && this.page >= 10 && end < pages) {
				start = this.page - 9;
				end = this.page + 10;
			}
			
			if(this.page > start) {
				var html = '<a href="#" onclick="'+this.objName+'.get('+(this.page-1)+'); return false;">previous</a>';
			} else {
				var html = "";
			}
	
			for(var i=start; i<end; i++) {
				html += '<a href="#" onclick="'+this.objName+'.get('+i+'); return false;"';
				if(i==this.page) {
					html += ' class="selected"';
				}
				html += '>' + (i+1) + '</a>';
			}
			if(this.page < (end-1)) {
				html += '<a href="#" onclick="'+this.objName+'.get('+(this.page+1)+'); return false;">next</a>';
			}
			
			html += '<div style="clear:both;"></div>';
			
			if($("#"+self.div).children().size() == 1) {
				$("#"+self.div).append('<div class="jsTablePages">'+html+'</div>');
			} else {
				$("#"+self.div).children("div").html(html);
			}
		} else if($("#"+self.div).children().size() > 0) {
			$("#"+self.div).children("div").remove();
		}
	}
	
	this.sort = function(col,btn) {
		this.sortby = col;
		if(this.sorted == btn ) {//only change sort direction on current col
			this.sortdir = (this.sortdir=="DESC"?"ASC":"DESC");
		} else {
			this.sortdir = "ASC";
		}
		if(this.sorted != null) {
			$(this.sorted).parent().css("background","url("+image_path+"table_header1.jpg)");
			$(this.sorted).css("background","");
			$(this.sorted).css("padding-left","5px");
		} 
		$(btn).parent().css("background","url("+image_path+"table_header_over1.jpg)");
		$(btn).css("background","url("+image_path+"arrow_"+this.sortdir+".png) no-repeat 3px center");
		$(btn).css("padding-left","20px");
		this.sorted = btn;
		this.clearSelection();
		if(this.loadFunc) {
			this.loadFunc();
		}
		if ( req ) req.abort();
		req = $.post(this.xml, this.postvars() , function(xml) {	
			$("#"+self.div+" table tr:has(td)").remove(); //remove all previous rows
			self.draw(xml);
			if(self.completeFunc) {
				self.completeFunc();
			}
		});
	}
	
	this.clearSelection = function () {
		if (document.selection) {
			document.selection.empty();
		} else if (window.getSelection) {
			window.getSelection().removeAllRanges();
		}
	}
	
	this.showhide = function() {
		for(var i=0; i<self.cols.length; i++) {
			
			if(self.cols[i].show == null || self.cols[i].show() != false) {
				$("#"+self.div+" table tr th:eq("+i+")").show();
			} else {
				$("#"+self.div+" table tr th:eq("+i+")").hide();
			}
		}
	}
	
	this.trash = function (id) {
		//send to trash
		if(this.loadFunc) {
			this.loadFunc();
		}
		var postvars = this.postvars();
		postvars.action = "trash";
		postvars.id = id;
		if ( req ) req.abort();
		req = $.post(this.xml, postvars, function(xml) {
			$("#"+self.div+" table tr:has(td)").remove(); //remove all previous rows
			self.draw(xml);
			if(self.completeFunc) {
				self.completeFunc();
			}
		});
	}
	
	this.get = function() {
		if(arguments.length > 0) {
			this.page = arguments[0];
		}
		if(this.loadFunc) {
			this.loadFunc();
		}
		if ( req ) req.abort(); 
		req = $.post(this.xml, this.postvars() , function(xml) {	
			$("#"+self.div+" table tr:has(td)").remove(); //remove all previous rows
			self.showhide();
			self.draw(xml);
			if(self.completeFunc) {
				self.completeFunc();
			}
		});
	}
	
	this.postvars = function() {
		var postvars = {action:"get",sortby:this.sortby, sortdir:this.sortdir, filter:this.filterValue, limit:this.limit, page:this.page, name:this.objName};
		for (var custom in this.customVars) {
			if(typeof(this.customVars[custom]) == "function") {
				postvars[custom] = this.customVars[custom]();
			} else {
				postvars[custom] = this.customVars[custom];
			}
		}
		return postvars;
	}
	
	this.filter = function(value) {
		this.page = 0;
		this.filterValue = value;
		
        if(this.loadFunc) {
			this.loadFunc();
		}
		if ( req ) req.abort();
		req = $.post(this.xml, this.postvars() , function(xml) {	
			$("#"+self.div+" table tr:has(td)").remove(); //remove all previous rows
			self.showhide();
			self.draw(xml);
			if(self.completeFunc) {
				self.completeFunc();
			}
		});
	}
}
