/* The copy right is belong to idealhome.com.tw
	 Author= Arthur Chang	 (張榮庭)
	 this document created day 2008/06/02
 */

function PageIndexNavigationBar(parentTagName, pageIndexs, container, navigationOption){
	// clean existing elements for container;
	container.innerHTML = "";
	this.navigationOption = navigationOption;
	this.presentInstance = document.createElement(parentTagName);
	this.presentInstance.pageIndexs = pageIndexs;
	this.pageIndexs = pageIndexs;
	this.presentInstance.container = container;
	this.container = container;	
	container.appendChild(this.presentInstance);
	this.initailDefaultActionObj();
}
/*
 * multiLine: 要分幾列
 */
PageIndexNavigationBar.create = function(elementsMainContainer, navigationBarMainContainer,  numberOfElementsOfEachNavigationPage, elements, navigationOption, multiLine){
	if (elements == null || elements.length ==0){
		return null;
	}
	//original var tmpNavigationPageControoledDivContainer = document.createElement('div');
	var tmptable = document.createElement('table');
	var pageIndexs = [];
	if(navigationOption == null){
		navigationOption = new NavigationOption();
	}	
	var tmpNavigationBarContainer = new PageIndexNavigationBar("div", pageIndexs, navigationBarMainContainer, navigationOption);
	var k = 1;
	var doForMultiLine = false;
	if(typeof multiLine == "number"){
		var amountOfElementsInALine = numberOfElementsOfEachNavigationPage / multiLine;
		doForMultiLine = true;
	}
	for (var i = 0 ; i < elements.length; i= i + numberOfElementsOfEachNavigationPage, k++){
		// Room of elements
		//for table row
		var tr= tmptable.insertRow(k-1);
		tr.style.display = "none";
		// navigation page
		var pageIndex = new NavigationPageIndex("a", tmpNavigationBarContainer, k, tr, (k -1), null, navigationOption.pageIndexOption);
		pageIndexs.push(pageIndex);
		var	td;
		var countOfRowInInnerTable = 0;
		var countOfColumnInInnerRow = amountOfElementsInALine;		//make sure to create a row
		// include elements to its own room.
		for (var j = 0 ; (j < numberOfElementsOfEachNavigationPage + 1) && ((i + j) < elements.length)  ; j++){
			//original tmpDiv.appendChild(elements[i + j]);
			if(navigationOption.direction==NavigationOption.VERTICAL){
				if(j == 0){
					td = tr.insertCell(j);
				}
				td.appendChild(elements[i + j]);
			}else{
				if(doForMultiLine){					 
					if(j == 0){
						td = tr.insertCell(j);
						var innerTable = document.createElement('table');
						td.appendChild(innerTable);
					}
					if(countOfColumnInInnerRow > amountOfElementsInALine -1){
						var innerTr= innerTable.insertRow(countOfRowInInnerTable);
						countOfColumnInInnerRow =0;
						countOfRowInInnerTable++;
					}

					var innerTd = innerTr.insertCell(countOfColumnInInnerRow);
					innerTd.appendChild(elements[i + j]);
					countOfColumnInInnerRow++;
				}else{
					td = tr.insertCell(j);
					td.appendChild(elements[i + j]);
				}
			}
			
					
		}
	}
	tmpNavigationBarContainer.filledCompleted();
	//clear elements
	elementsMainContainer.innerHTML ="";
	
	elementsMainContainer.child = tmptable;//original  tmpNavigationPageControoledDivContainer;
	navigationBarMainContainer.child = tmpNavigationBarContainer.presentInstance;
	tmpNavigationBarContainer.mainControlledContainer = elementsMainContainer;
	tmpNavigationBarContainer.mainNavigationBarContainer = navigationBarMainContainer;
	elementsMainContainer.appendChild(tmptable); // original  tmpNavigationPageControoledDivContainer);
	return tmpNavigationBarContainer;	
}


PageIndexNavigationBar.prototype.appendChild = function(child){
	this.presentInstance.appendChild(child);	
}

PageIndexNavigationBar.prototype.removeAllFromParentContainer = function(){
	this.mainControlledContainer.removeChild(this.mainControlledContainer.child);
	this.mainNavigationBarContainer.removeChild(this.mainNavigationBarContainer.child);
}



PageIndexNavigationBar.prototype.initailDefaultActionObj = function(){
	if(this.navigationOption.hasFirstPageBtn){
		var first = new NavigationActionButton("a","第一頁", this);
		first.onclick(function(){this.container.goFirstPage()});
	}
	if(this.navigationOption.hasPriorPageBtn){
		var prior = new NavigationActionButton("a","上一頁", this);
		prior.onclick(function(){this.container.goPriorPage()});
	}
	
}

PageIndexNavigationBar.prototype.filledCompleted = function(){
	this.index = 0;	
	if(this.navigationOption.hasNextPageBtn){
		var next = new NavigationActionButton("a","下一頁", this);
		next.onclick(function(){this.container.goNextPage()});
	}
	if(this.navigationOption.hasLastPageBtn){
		var last =  new NavigationActionButton("a","最後一頁", this);
		last.onclick(function(){this.container.goLastPage()});
	}
	this.goFirstPage(); // default will show page 1	
}

PageIndexNavigationBar.prototype.setCurrentpage = function(thepage){
	this.currentPage = thepage;
	this.index = thepage.tabOrder;
}

PageIndexNavigationBar.prototype.goPriorPage =function(){
	if(this.index > 0){
		var pageIndex = this.pageIndexs[this.index -1];
		fireClickEvent(pageIndex.presentInstance);
	}	
}

PageIndexNavigationBar.prototype.goNextPage = function(){
	if(this.index < this.pageIndexs.length -1 ){
		var pageIndex = this.pageIndexs[this.index +1];
		fireClickEvent(pageIndex.presentInstance);
	}
}

PageIndexNavigationBar.prototype.goFirstPage = function(){
	var pageIndex = this.pageIndexs[0];
	fireClickEvent(pageIndex.presentInstance);
}

PageIndexNavigationBar.prototype.goLastPage = function(){
	var pageIndex = this.pageIndexs[this.pageIndexs.length -1];
	fireClickEvent(pageIndex.presentInstance);
}

PageIndexNavigationBar.prototype.showCurrentNavigationPage = function(thePage){	
	if (thePage != thePage.container.currentPage && thePage.container.currentPage != null){
		thePage.container.currentPage.controledDiv.style.display = "none";
		thePage.container.currentPage.style.backgroundColor ="#FFFFFF";
	}
	thePage.style.backgroundColor ="#BBBBAA";
	thePage.container.setCurrentpage(thePage);
	thePage.controledDiv.style.display = "";
}


/*
 *  event has not be implemented
 */
function NavigationPageIndex(parentTagName, container, name, controledDiv, tabOrder, event, pageIndexOption){
	this.presentInstance = document.createElement(parentTagName);


	this.presentInstance.container = container;
	this.container = container;
	this.presentInstance.innerHTML = name;
	this.name = name ;
	this.presentInstance.controledDiv = controledDiv;
	this.controledDiv = controledDiv;
	this.presentInstance.tabOrder = tabOrder;
	this.tabOrder = tabOrder;
	this.presentInstance.onclick = function(){this.container.showCurrentNavigationPage(this);};  // default behavior of click event 
	var wrapper = document.createElement("span");  // created for css active, link, hover, css can not work fine if we do only use a tag while using MSIE and FF
	wrapper.appendChild(this.presentInstance);
	if(pageIndexOption){
		wrapper.className = pageIndexOption.className;
	}
	container.appendChild(wrapper);  // default to add elements to container immediately.
}

function NavigationActionButton(parentTagName, name, container){
    this.presentInstance = document.createElement(parentTagName);
    this.presentInstance.style.cursor="pointer";
	this.presentInstance.innerHTML = name;
	this.presentInstance.container = container;
	this.container = container;
	container.appendChild(this.presentInstance);
}

NavigationActionButton.prototype.onclick = function(func){
	this.presentInstance.onclick = func;
}

function NavigationOption(){
}

NavigationOption.VERTICAL="vertical";
NavigationOption.HORIZONTAL="horizontal";

NavigationOption.prototype.hasFirstPageBtn = false;
NavigationOption.prototype.hasLastPageBtn = false;
NavigationOption.prototype.hasPriorPageBtn = false;
NavigationOption.prototype.hasNextPageBtn = false;
NavigationOption.prototype.direction = NavigationOption.VERTICAL;
NavigationOption.prototype.pageIndexOption = null;

function NavigationPageIndexOption(){
}

NavigationPageIndexOption.prototype.className = "";


function fireClickEvent(obj){
	if(navigator.appName == 'Netscape'){
		var   e   =   document.createEvent('MouseEvent');   
		e.initEvent('click',false,false);   
		obj.dispatchEvent(e);   
	}else{
		obj.click();
	}
}

function getXMLNodeText(oNode) {

    var sText = "";

    for (var i = 0; i < oNode.childNodes.length; i++) {

       if (oNode.childNodes[i].hasChildNodes()) {

           sText += getText(oNode.childNodes[i]);

       } else {

           sText += oNode.childNodes[i].nodeValue;

       }

    }

    return sText;

}