// JavaScript Document

function oListing(eList,options) {
	var parent = this;
	// local variables
	this.modeUL	= (options.modeUL==null)?false:options.modeUL;
	this.eList 	= eList;
	this.ePage	= (options.page==null)?"#dPage":options.page;
	this.ePages	= (options.pages==null)?"#dPages":options.pages;
	this.eRecs	= (options.recs==null)?"#dRecords":options.recs;
	this.eSearch= (options.search==null)?"#iSearch":options.search;
	this.eNext	= (options.next==null)?"#dNext":options.next;
	this.ePrev	= (options.prev==null)?"#dPrev":options.prev;
	
	this.url	= (options.url==null)?"":options.url;
	this.listHolder	= "";
	this.vars		= Array();
	this.json	= (options.json==null)?true:options.json;
	this.totalRecords	= 0;
	this.totalPages		= 0;
	this.form	= (options.form==null)?false:options.form;
	
	this.init	= init;
	function init() {
	this.listHolder = (this.modeUL)?this.eList:this.eList+" tbody";
	this.vars["sort"] 	= "id";
	this.vars["order"] 	= "DESC";
	this.vars["page"] 	= "1";
	this.convertNav();
	this.convertTH();
	this.convertSearch();
	}
	
	this.convertSearch = convertSearch;
	function convertSearch() {
	$(this.eSearch).bind("keyup",function(){parent.vars["search"]=$(this).val(); parent.load();});
	}
	
	this.convertNav = convertNav;

	function convertNav() {
	p = $(this.ePrev); n = $(this.eNext);
	p.bind("click",function(){parent.prev()});
	n.bind("click",function(){parent.next()});
	}
	
	this.prev = prev;

	function prev() {
	current = this.vars["page"];
		if(current==1) return false;
		else {
		this.vars["page"] = --current;
		this.load();
		}
	}
	
	this.next = next;
	function next() {
	current = this.vars["page"];
		if(current>=this.totalPages) return false;
		else {		
		this.vars["page"] = (current*1)+1;
		this.load();
		}
	}
	
	this.convertTH = convertTH;
	function convertTH() {
	parent = this;
	$(this.eList+" th").each( function() { 
		th = $(this);
		axis = th.attr("axis");
			if(axis!="") {
			th.bind("click",function(){parent.sort($(this).attr("axis"));}).css({cursor:"pointer"});
			if(parent.vars["sort"]==axis) th.addClass("sorted");
			}
		});
	}
	
	this.load = load;
	function load() {
	message = new Message;
	if(! message.isAJAX() ) message.showAJAX("Loading...");
		sData = this.loadData();
		obj = $.ajax({
			type: "POST",
			url: this.url+"&",
			data: sData,
			async: false
		});

		this.clear();
		table = $(this.listHolder);

		if(this.json) {
		json = $.parseJSON(obj.responseText);
		html = json["html"];
		this.totalPages = json["totalPages"];
		this.totalRecords = json["totalRecords"];
		this.page = (this.totalPages>0)?json["page"]:0;
		$(this.ePages).html(this.totalPages);
		$(this.eRecs).html(this.totalRecords);
		$(this.ePage).html(this.page);
			if(json["totalRecords"]==0) {
			table.append("<tr><td colspan='99'></td></tr>");
			table = $(this.listHolder+" tr td");
			}
		if(this.page>1) $(this.ePrev).removeClass("disabled"); else $(this.ePrev).addClass("disabled");
		if(this.page<this.totalPages) $(this.eNext).removeClass("disabled"); else $(this.eNext).addClass("disabled");
		} else {
		html = obj.responseText;
		$(this.ePage).html(this.vars["page"]);
		}
		table.append(html);
		message.showAJAX("Complete",500);
		$(this.eList+" a[rel=boxy]").boxy();
		$(this.eList+" a[rel=iboxy]").iboxy();
		this.alternateRow();
	}

	this.loadData = loadData;
	function loadData() {
	vars = this.vars;
	data = (this.json)?"json=true&":"";
		for(var i in vars) 
		data+=i+"="+vars[i]+"&";
	return data;
	}

	this.clear = clear;
	function clear() {
		$(this.listHolder).empty();
	}
	
	this.sort = sort;
	function sort(field) {
	message.showAJAX("Sorting...");
	this.vars["page"] = 1;
	if(this.vars["sort"]==field) this.vars["order"]=(this.vars["order"]=="DESC")?"ASC":"DESC";
	else this.vars["sort"]=field;
	$(this.eList+" th").removeClass("sorted").each( function(){
		th = $(this); if(th.attr("axis")==field) th.addClass("sorted");
		});
	this.load();
	}
	
	this.set = set;
	function set(variable,value) {
	this.vars[variable] = value;
	return true;
	}
	
	this.act = act;
	function act(action) {
	if(!this.form) return false;
	form = $(this.form);
	url = form.attr("action");
	data = form.serialize()+"&a="+action;
	b = $.ajax({
	  type: 'POST',
	  url: url,
	  data: data,
	  async: false
	}).responseText;
	b = $.parseJSON(b);
	message.show(b.message,b.type);
	this.load();
	return false;
	}
	
	this.alternateRow = alternateRow;
	function alternateRow() {
	$(this.eList+" tr").bind("mouseover",function(){ $(this).addClass("trhover");}).bind("mouseout",function(){$(this).removeClass("trhover")});
	}

	// initialize
	this.init();
}

function Message() {
this.ajax = "#fAJAX";
this.message = "#fMessage";

	this.showAJAX = showAJAX;
	function showAJAX(msg) {
	ajax = $(this.ajax);
	out = (arguments.length>1)?arguments[1]:0;
	ajax.html(msg).fadeIn(500);
	if(out>0) setTimeout(this.hideAJAX,out);
	}
	
	this.hideAJAX = hideAJAX;
	function hideAJAX() {
	ajax = (this.ajax);
	ajax.hide(); ajax.html("");
	}
	
	this.isAJAX = isAJAX;
	function isAJAX() {
	ajax = $(this.ajax);
	return(ajax.html()!="")?true:false;
	}

	this.show = show;
	function show(msg,type) {
	message = $(this.message);
	message.html(msg).slideDown().attr("class","message "+type);
	}
	
	this.hide = hide;
	function hide() {
	message = $(this.message);
	message.html("").hide().attr("class","message");
	}
}
var message;
message = new Message;

$(document).ready(function(){
$("#ajax").bind("click",function(){$(this).fadeOut(300);});						   
});

