// client side object to display data
SEC = function(secData, secMatches) {

	// fields that set how to display the columns
	this.fields = [
		 {n:"icon",l:"",noSort:true,width:"10px"}
		,{n:"FORM_TYPE",l:"Type"}
		,{n:"DocumentDate",l:"Filing Date"}
		,{n:"CATEGORY",l:"Category",nosort:true}
		,{n:"AMENDED",l:"Amended",nosort:true,align:"alignCenter"}
	];

	this._dataPage = "secBuffer.asp";
	this._data = secData;
	this.sortby = this.fields[2].n; // sort by filing date
	this.sortasc = false;
	this.sortLinks = [];
	this.perPage = 15;
	this.firstRow = 0;
	this.matches(secMatches);
	this.pages = 0;
	this.timeFrame = "TTM";
	
	this.thead = Element.get("thead");
	this.tbody = Element.get("tbody");
	this.pagination = Element.get("pagination");		
	this.previous = Element.get("prevBar");
	this.next = Element.get("nextBar");
}


SEC.prototype.data = function() {
	if (arguments[0] !== undefined) {
		this._data = arguments[0];
	}
	return this._data;
}

SEC.prototype.matches = function() {
	if (arguments[0] !== undefined) {
		this._matches = arguments[0];
	}
	return this._matches;
}



// called on init and when sorting/paging
SEC.prototype.getData = function() {
	if (this.getDataTimer) clearTimeout(this.getDataTimer);
	var ths = this;
	this.getDataTimer = setTimeout(function() { ths.getDataDelayed() },100);
}

SEC.prototype.getDataDelayed = function() {

	Common.contentBufferAbortRequests();

	Common.loadContentBuffer({
		page:this._dataPage,
		context:this,
		data:{
			perPage: this.perPage
			,firstRow: this.firstRow
			,sort: this.sortby
			,sortasc: this.sortasc
			,timeFrame: this.timeFrame
		},
		onload:this.drawTable,
		onerror:this.handleError
	});

}


SEC.prototype.init = function() {

	// clears debug window
	try {
		//Debug.debugWindow._sdb.clear(null,null,{ pane: "requests" })
		//Debug.debugWindow._sdb.clear(null,null,{ pane: "console" })
	}
	catch(e) {}

	Events.add({element:Element.get("prevPage"), type:"click", context:this, data:{direction:-1},handler:this.navigatePage});
	Events.add({element:Element.get("nextPage"), type:"click", context:this, data:{direction:1},handler:this.navigatePage});
	

	// draw header and sorting links
	var tr = Element.create("tr",{},null,this.thead);
	var th,a;

	for (var x=0; x<this.fields.length; x++) {

		th = Element.create("th",{style:"width:"+(this.fields[x].width||"auto")},[
			a = Element.create(this.fields[x].nosort?"span":"a",{id:"sort"+this.fields[x].n,field:this.fields[x].n,href:"#"},this.fields[x].l)
		],tr);

		if (!this.fields[x].nosort) {
			a.onclick = function() {	SEC.sortField(this.getAttribute("field")); return false; }	
			this.sortLinks.push(a);
		}

	}
	
	// draw options timeframe
	this.drawTimeFrameOptions();
	this.drawTable();
}

// renders pulldown year options
SEC.prototype.drawTimeFrameOptions = function() {

	var oSelect = Element.get("secFilingTimeFrame");
	Element.removeClass(oSelect,"none");
	Element.create("option",{value:"TTM"},"Trailing 12-Months",oSelect);
	
	var now = new Date();
	//for(var i=0, yr; i < 10; i++) {
	for(var i=0, yr; i < 4; i++) {
		yr = now.getFullYear() - i;
		Element.create("option",{value:yr},"Calendar Year " + yr,oSelect);
	}

	Events.add({
		element: oSelect,
		type: "change",
		context: this,
		handler: this.changeTimeFrame
	});
}

// event handler for option change
SEC.prototype.changeTimeFrame = function(e,oSelect,a) {

	Element.removeChildNodes(this.tbody);
/*
	var tr = Element.create("tr",{},"",this.tbody);
	var td = Element.create("td",{"colspan":5,"class":"alignLeft"},"Please wait...",tr);

*/
	this.firstRow = 0;
	this.timeFrame = oSelect.options[oSelect.selectedIndex].value;
	this.getData();
	Events.cancelEvent(e);
}


// called from Content Buffer onload
SEC.prototype.drawTable = function() {
	
	var data = this.data();

	// pagination
	this.pages = Math.ceil(this.matches()/this.perPage)
	var currentPage = (this.firstRow/this.perPage)+1;
	Element.setHTML(this.pagination,"Page " + currentPage + " of " + this.pages);
	Element.setVisibility(this.previous,(currentPage>1?"visible":"hidden"));
	Element.setVisibility(this.next,(currentPage<this.pages?"visible":"hidden"));

	Element.setDisplay("pagingControls",this.pages?"block":"none")

	Element.removeChildNodes(this.tbody);

	var tr, td, a1, a2, cellClass;

	Element.setDisplay("nofilings",(data.length?"none":"block"));

	for (var i=0; i<data.length; i++) {
		//console.info(i)

		tr = Element.create("tr",{num:i},"",this.tbody);

		// Icon and Form link columns
		cellClass = ("FORM_TYPE" == this.sortby) ? "currentsymbol ":"";
		td = Element.create("td",{"class":cellClass+"alignLeft"},[
			a1 = Element.create("a",{"class":"formatIcon format" + data[i].FORMAT,href:"#"},"")
		],tr);
		td = Element.create("td",{"class":cellClass+"alignLeft"},[
			a2 = Element.create("a",{href:"#",className:"normal"},data[i].FORM_TYPE)
		],tr);

		// clicking either icon or form link gets it's parent TR's num attribute
		a1.onclick = a2.onclick = function() {
			SEC.launch(Element.getParent(this,"tr").getAttribute("num")); return false;
		}

		for (var x=2; x<this.fields.length; x++) {
			// if current sorting, add "currentsymbol" class to highlight column
			cellClass = (this.fields[x].n == this.sortby) ? "currentsymbol ":"";
			td = Element.create("td",{"class":cellClass+(this.fields[x].align||"alignLeft")},(data[i][this.fields[x].n+"_f"] || data[i][this.fields[x].n]),tr);
		}
	}

	// remove sorting classes from links
	Element.removeClass(this.sortLinks,"sort(?:asc|dsc)");

	// add sorting class to current sortyby
	Element.addClass("sort"+this.sortby,(this.sortasc?"sortasc":"sortdsc"));

	// this keeps the paging controls in the same position by making them absolutely positioned
	// it also needs to explictly set the "right" cells height so the footer doesn't collapse
	if (!this._setPagingControls) {
		var pagingControls = Element.get("pagingControls");
		var pagingPos = Element.getXY(pagingControls);
		var pagingSize = Element.getSize(pagingControls);
		Element.removeClass(pagingControls,"fright");
		document.body.appendChild(pagingControls);
		Element.setStyle(pagingControls,"visibility:visible;position:absolute;left:"+(pagingPos.x-10)+"px;top:"+pagingPos.y+"px;width:"+(pagingSize.width+18)+"px;");

		var rightTD = Element.get("right");
		var rightSize = Element.getSize(rightTD);
		Element.setStyle(rightTD,"height:"+rightSize.height+"px");

		this._setPagingControls = true;

	}

}



SEC.prototype.navigatePage = function() {
	dir = arguments[2].direction;
	// calculate first row based on direction
	if (dir == 1) {
		this.firstRow = Math.min(this.firstRow+this.perPage,this.matches()-(this.matches()%this.perPage));
	}
	else {
		this.firstRow = Math.max(this.firstRow-this.perPage,0);
	}

	this.getData();

	Events.cancelEvent(arguments[0]);
}


SEC.prototype.sortField = function(sortby) {
	if (this.sortby == sortby) {
		this.sortasc = !this.sortasc;
	}
	else {
		this.sortby = sortby;
	}

	this.firstRow = 0;
	this.getData();
}


// launch the document properties to filing.asp
SEC.prototype.launch = function(num) {
	var docparams = DataFunctions.serializer.serialize(this._data[num]);
	pdf("filing.asp?data="+docparams);
}