/**************************************************************************************
*File: cashe.js
*Contents: Cashe Class.
*All rights reserved.
**************************************************************************************/

function Cashe(locator,pagesFrame,columnIds,maxEntriesCol,lng) {
	this.locator = locator;
	this.columnIds = columnIds;
	this.maxEntriesCol = maxEntriesCol;
	this.pagesFrame = pagesFrame;  
	this.lng = lng;

	this.menu_bar_fl = new YAHOO.widget.MenuBar("menu_bar_fl");
	this.menu_bar_pn = new YAHOO.widget.MenuBar("menu_bar_pn");

	this.menu_bar_fl.render();
	this.menu_bar_pn.render();
	this.menu_bar_fl.show();
	this.menu_bar_pn.show();

	this.searchRequest = null;
	this.sid = null;
	this.curS = 0;
	this.alterS = null;
	this.currentPage = null; //Remote
	this.count = 0;

	this.aFrame = new Array();
	this.bFrame = new Array();
	this.curFrame = this.aFrame;
	this.alterFrame = this.bFrame;
	this.pagePointer = 1;

	this.nCount = 0;
	this.pCount = 0;
	this.maxMoves = 2
	this.valid = false; //Relates to the contents of alterFrame

	this.loadCallback = {
		success: Cashe.prototype.loadCB,
		failure: Cashe.prototype.requestFailure,
		scope: this
	};

	this.loadOnFirstFaultCallback = {
		success: Cashe.prototype.loadOnFirstFaultCB,
		failure: Cashe.prototype.requestFailure,
		scope: this
	};

	this.loadOnLastFaultCallback = {
		success: Cashe.prototype.loadOnLastFaultCB,
		failure: Cashe.prototype.requestFailure,
		scope: this
	};

	this.loadInitCallback = {
		success: Cashe.prototype.loadInitCB,
		failure: Cashe.prototype.requestFailure,
		scope: this
	};
}

//Load types
Cashe.prototype.LOAD_INIT = 0;
Cashe.prototype.LOAD_FIRST_FAULT = 1;
Cashe.prototype.LOAD_LAST_FAULT = 2;
Cashe.prototype.LOAD_PRE = 3;

Cashe.prototype.request = CasheRequest;
Cashe.prototype.load = loadFrame;
Cashe.prototype.loadPage = loadPage;
Cashe.prototype.swap = swapFrames;
Cashe.prototype.firstPageInCashe = firstPageInCashe;
Cashe.prototype.lastPageInCashe = lastPageInCashe;
Cashe.prototype.parse = CasheParse;
Cashe.prototype.fillColumn = fillColumn;
Cashe.prototype.fillPage = fillPage;
Cashe.prototype.goToPage = goToPage;
Cashe.prototype.firstPage = firstPage;
Cashe.prototype.lastPage = lastPage;
Cashe.prototype.nextPage = nextPage;
Cashe.prototype.previousPage = previousPage;
Cashe.prototype.refreshNavigationBar = refreshNavigationBar;


function loadPage(pageNum,loadType) {
	var s = (pageNum - 1)*(this.columnIds.length * this.maxEntriesCol);
	this.load(s,loadType);
}

//asyn is a Boolean that tells if the request should be sent asynchronously or not.
function loadFrame(s,loadType) {
	if(this.alterS == s) //We already have the block in alterFrame
		return;
	this.valid = false; //Invalidate the contents of alterFrame

	var cb = new Array();
	cb[this.LOAD_INIT] = this.loadInitCallback;
	cb[this.LOAD_FIRST_FAULT] = this.loadOnFirstFaultCallback;
	cb[this.LOAD_LAST_FAULT] = this.loadOnLastFaultCallback;
	cb[this.LOAD_PRE] = this.loadCallback;
	
	var url = this.searchRequest + "&s=" + s + "&c=" + (this.columnIds.length * this.maxEntriesCol * this.pagesFrame);
	this.alterS = s;
	if(this.sid != null)
		url += "&sid=" + this.sid;
	YAHOO.util.Connect.asyncRequest('GET', url, cb[loadType]);
}

Cashe.prototype.loadCB = function(o) {
	var xmlDoc = o.responseXML;
	this.parse(xmlDoc); 
	this.valid = true;
}

Cashe.prototype.loadInitCB = function(o) {
	var textNone = {"eng": "Search produced no results", "fr": "Aucun r&#233;sultat"};
	var xmlDoc = o.responseXML;
	this.parse(xmlDoc); 
	this.valid = true;

	if(this.count > 0) {
		this.swap(1);
		this.goToPage(this.pagePointer);
	}
	else 
		error(this.lng, textNone[this.lng]);
}

Cashe.prototype.loadOnFirstFaultCB = function(o) {
	var xmlDoc = o.responseXML;
	this.parse(xmlDoc); 
	this.valid = true;
	this.swap(1);
	this.nCount = 0;
	this.pCount = 0;
	this.pagePointer = 1;
	this.goToPage(1);
}

Cashe.prototype.loadOnLastFaultCB = function(o) {
	var xmlDoc = o.responseXML;
	this.parse(xmlDoc); 
	this.valid = true;
	this.swap(1);
	this.nCount = 0;
	this.pCount = 0;
	this.pagePointer = Math.ceil(this.curFrame.length/(this.maxEntriesCol*this.columnIds.length));
	this.goToPage(this.pagePointer);
}

Cashe.prototype.requestFailure = function(o) {
	var message = {"eng": "Request failure", "fr": "Échec de requête"};
	error(message[this.lng],this.lng);
}


function CasheParse(xmlDoc) {
	this.sid = getOptionalProperty(xmlDoc.documentElement, 'sid');
	this.count = parseInt(xmlDoc.documentElement.getElementsByTagName('count')[0].firstChild.nodeValue);

	var xmlNode = xmlDoc.documentElement.getElementsByTagName(this.locator.businessXmlTag);
	this.alterFrame.length = 0;   

	for(var i = 0;i < xmlNode.length;i++) {
		this.alterFrame[i] = new this.locator.businessClass(xmlNode[i]);
		this.alterFrame[i].createMarker();
	}
}


function fillColumn(colNum,firstIndex) {
	var html = new String();
	var i = firstIndex;
	var label;

	while(i < this.curFrame.length && i - firstIndex + 1 <= this.maxEntriesCol) {
		label = this.curFrame[i].getName()
		html += "<a class=\"result\" onclick=\"" + this.locator.name + ".cashe.curFrame[" + i + "].goTo();return false;\" href=\"#\" onmouseover=\"return true;\" title=\"" + this.curFrame[i].getAddress() + "\">&#8250; " + this.curFrame[i].getName() + "</a><br/>";
		i++;
	} 

	document.getElementById(this.columnIds[colNum]).innerHTML = html;
	return i - 1;
}


function fillPage(firstIndex) {
	var l = firstIndex - 1;

	for(var i = 0; i < this.columnIds.length; i++)
		l = this.fillColumn(i, l + 1);
}

function goToPage(pageNum) {
	var pageCount = Math.ceil(this.count/(this.maxEntriesCol*this.columnIds.length));
	var min = this.firstPageInCashe();

	this.currentPage = min + pageNum - 1;
	this.fillPage((pageNum - 1)*this.maxEntriesCol*this.columnIds.length);
	this.refreshNavigationBar();
}

function refreshNavigationBar() {
	var pageCount = Math.ceil(this.count/(this.maxEntriesCol*this.columnIds.length));
	var html = "Page";

	if(this.lng == 'fr')
		html += " ";
	html += ": " + this.currentPage + "/" + pageCount;

	document.getElementById("page_num").innerHTML = html;

	var mItemPrevious = this.menu_bar_pn.getItem(0);
	var mItemNext = this.menu_bar_pn.getItem(1);
	var mItemFirst = this.menu_bar_fl.getItem(0);
	var mItemLast = this.menu_bar_fl.getItem(1);

	if(pageCount == 1) {
		mItemPrevious.cfg.setProperty('disabled',true);
		mItemNext.cfg.setProperty('disabled',true);
		mItemFirst.cfg.setProperty('disabled',true);
		mItemLast.cfg.setProperty('disabled',true);
	}
	else {
		if(this.currentPage == 1) {
			mItemPrevious.cfg.setProperty('disabled',true);
			mItemNext.cfg.setProperty('disabled',false);
			mItemFirst.cfg.setProperty('disabled',true);
			mItemLast.cfg.setProperty('disabled',false);	
		}
		else if(this.currentPage == pageCount) {
			mItemPrevious.cfg.setProperty('disabled',false);
			mItemNext.cfg.setProperty('disabled',true);
			mItemFirst.cfg.setProperty('disabled',false);
			mItemLast.cfg.setProperty('disabled',true);
		}
		else {
			mItemPrevious.cfg.setProperty('disabled',false);
			mItemNext.cfg.setProperty('disabled',false);
			mItemFirst.cfg.setProperty('disabled',false);
			mItemLast.cfg.setProperty('disabled',false);
		}
	}
}


function firstPage() {
	var min = this.firstPageInCashe();
	if(min != 1) 
		this.loadPage(1,this.LOAD_FIRST_FAULT);
	else {
		this.pagePointer = 1;
		this.goToPage(1);
	}
}

function lastPage() {
	var lastPage = Math.ceil(this.count/(this.maxEntriesCol*this.columnIds.length));
	var max = this.lastPageInCashe();
	if(max != lastPage) 
		this.loadPage(lastPage - this.pagesFrame + 1,this.LOAD_LAST_FAULT);
	else {
		this.pagePointer = Math.ceil(this.curFrame.length/(this.maxEntriesCol*this.columnIds.length));
		this.goToPage(this.pagePointer);
	}
		
}

function nextPage() {
 	var lastPage = Math.ceil(this.count/(this.maxEntriesCol*this.columnIds.length));
 	if(this.currentPage == lastPage)
 		return;

	var max = this.lastPageInCashe();

	this.nCount++;
	this.pCount--;
   
	if(this.nCount == this.maxMoves && max != lastPage) {
		this.loadPage(max + 1,this.LOAD_PRE);
	}

	if(this.pagePointer == this.pagesFrame) {
		if(!this.swap(1)) {
			this.nCount--;
			this.pCount++;
		}
	}
	else
		this.pagePointer++;

	if(this.pCount < 0)
		this.pCount = 0;

	this.goToPage(this.pagePointer);
}

function previousPage() {
	if(this.currentPage == 1)
		return;
	var min = this.firstPageInCashe();
	this.pCount++;
	this.nCount--;

	if(this.pCount == this.maxMoves && min != 1) {
		this.loadPage(min - this.pagesFrame,this.LOAD_PRE);
	}

	if(this.pagePointer == 1) {
		if(!this.swap(this.pagesFrame)) {
			this.pCount--;
			this.nCount++;
		}
	}
	else
		this.pagePointer--;

	if(this.nCount < 0)
		this.nCount = 0;

	this.goToPage(this.pagePointer);
}

function swapFrames(newPointer) {
	if(this.valid) {
		var temp = this.curFrame;
		this.curFrame = this.alterFrame;
		this.alterFrame = temp;
		temp = this.curS;
		this.curS = this.alterS;
		this.alterS = temp;
		this.nCount = 0;
		this.pCount = 0;
		this.pagePointer = newPointer;
		return true;
	}
	else
		return false;
}

function firstPageInCashe() {
	return Math.ceil(this.curS/(this.maxEntriesCol*this.columnIds.length)) + 1;
}

function lastPageInCashe() {
	return Math.ceil(this.firstPageInCashe() + this.curFrame.length/(this.maxEntriesCol*this.columnIds.length)) - 1;
}

function CasheRequest(req) {
	this.searchRequest = req;
	this.sid = null;
	this.pagePointer = 1;
	this.curS = -1;
	this.alterS = -1;
	this.locator.hide();
	this.loadPage(1,this.LOAD_INIT);
}

function changeClass(id, newClass) {
	var node = document.getElementById(id);
	node.className = newClass;
}





