
// pagination("hot-section", "internal-content-list2")

function pagination(pageClass, rootClass, pagesContainer) {
	this.getElementsByClassName = function(str_class,tag,obj_element){
		tag = tag || "*";
		obj_element = obj_element || document;
		var obj_collection = obj_element.getElementsByTagName(tag);
		
		if(!obj_collection.length &&  tag == "*" &&  obj_element.all) obj_collection = obj_element.all;
		
		var arr = new Array();
		var delimeter = str_class.indexOf("|") != -1  ? '|' : ' ';
		var classes = str_class.split(delimeter);
		
		for(var i=0, j=obj_collection.length; i<j; i++){
			var obj_classes = obj_collection[i].className.split(' ');
			if(delimeter == ' ' && classes.length > obj_classes.length) continue;
		
			var c = 0;
	comparisonLoop:
			for(var k=0, l=obj_classes.length; k<l; k++){
				for (var m=0, n=classes.length; m<n; m++){
					if(classes[m] == obj_classes[k]) c++;
					if((delimeter == "|" && c == 1) || (delimeter == " " && c == classes.length)){
						arr.push(obj_collection[i]);
						break comparisonLoop;
					}
				}
			}
		}
		return arr;
	}
	var container = document.getElementById(pagesContainer);
	var items = this.getElementsByClassName(rootClass, "div", container);
//	alert(items);
	for (var i = 0; i < items.length; i++) {
		var pages = this.getElementsByClassName(pageClass, "ul", items[i]);
		var p = new Paginator(pages, items[i]);
		items[i].appendChild(p.root);
	}
}

function Paginator(items, container) {
	var root = document.createElement("div");
	var ul = document.createElement("ul");
	root.appendChild(ul);
	this.maxPagesDisplayed = 5;
	
	root.setAttribute("class", "paginator");
	root.className =  "paginator";
	this.currentIndex = 0;
	this.anchors = new Array();
	this.items = items;
	
	this.pageCount = this.items.length;
	this.root = root;
	
	if (this.pageCount == 1)
		return;

	this.getStartPage = function() { 
		if (this.maxPagesDisplayed >= this.pageCount) { 
			return 0;
		} else { 
			if (this.pageCount <= this.currentIndex + parseInt(this.maxPagesDisplayed/2)) {
				return this.pageCount - this.maxPagesDisplayed;
			} else if (this.currentIndex > parseInt(this.maxPagesDisplayed/2) ) {
				return this.currentIndex - parseInt(this.maxPagesDisplayed/2);
			} else {
				return 0;
			}
		}
	}
	this.adjustPagination = function(){ 
		if (this.maxPagesDisplayed < this.pageCount) { 
			for ( var i = 0; i < this.anchors.length; i++) {
				this.anchors[i].className = "hidden";
			}
			var start = this.getStartPage();
			for ( var i = start; i < start + this.maxPagesDisplayed; i++) {
				this.anchors[i].className = "";
			}
		} else { 
			for ( var i = 0; i < this.pageCount; i++) {
				this.anchors[i].className = "";
			}
		}
		this.anchors[this.currentIndex].className = "selected";
	}
	this.go = function (index) {
		this.currentIndex = index;
//		alert("current Index: " + this.currentIndex +", Items:" + this.items);
		for (var j = 0; j < this.items.length; j++) {
			if(index != j && this.items[j].className && this.items[j].className.indexOf("hidden") == -1) {
				this.items[j].className += "hidden"; 
			}
		}
		var className = this.items[index].className;
		this.items[index].className = className.replace("hidden", ""); 
		if (this.currentIndex == 0) {
			this.prev.className = "hidden";
			this.next.className = "";
		} else if (this.currentIndex == this.pageCount - 1) {
			this.next.className = "hidden";
			this.prev.className = "";
		} else { 
			this.next.className = "";
			this.prev.className = "";
		}
		this.adjustPagination();
		return false;
	}
	this.getGoToNext = function() {
		var _this = this;
		return function(){
			return _this.go((_this.currentIndex + 1) % _this.pageCount);
		}
	}
	this.getGoToI = function(index) {
		var _this = this;
		return function(){
			return _this.go(index);
		}
	}
	this.getGoToPrev = function() {
		var _this = this;
		return function(){
			return _this.go((_this.currentIndex + _this.pageCount - 1) % _this.pageCount);
		}
	}
	this.prev = document.createElement("a");
	this.prev.innerHTML = "<<";
	this.prev.href = "#";
	this.prev.onclick= "return false;"
	this.prev.onclick= this.getGoToPrev();
	this.prev.className = "hidden";
	var li = document.createElement("li");
//	li.className = "prev";
	li.appendChild(this.prev);
	ul.appendChild(li);
	for (var i = this.items.length - 1; i >= 0; i--) {
		var anchor = document.createElement("a");
		anchor.innerHTML = i+1;
		anchor.href = "#";
		anchor.onclick= "return false;"
		anchor.onclick= this.getGoToI(i);
		this.anchors[i] = anchor;
		
		li = document.createElement("li");
		li.appendChild(anchor);
		ul.appendChild(li);
//		ul.appendChild(anchor);
	}
//	alert("current Index: " + this.currentIndex +", Items:" + this.items);
	this.next = document.createElement("a");
	this.next.innerHTML = ">>";
	this.next.href = "#";
	this.next.onclick= "return false;"
	this.next.onclick= this.getGoToNext();

	li = document.createElement("li");
//	li.className = "next";
	li.appendChild(this.next);
	ul.appendChild(li);
//	ul.appendChild(this.next);

	this.anchors[this.currentIndex].className = "selected";
	this.adjustPagination();
}
addLoadEvent(function(){
	pagination("page", "hot-section", "hot-issues-details-main");
});

