var Toolbar = function(o) {
    if (typeof o == "object") {
        for (var R in o) {
            if (typeof o[R] != "function") {
                this[R] = o[R];
            }
        }
    }

      
    this.el_totalDocuments = document.getElementById(this.id_totalDocuments);
    this.el_container = document.getElementById(this.id_container);

    if(o.pageSize==undefined) this.pageSize=8;
	else this.pageSize=o.pageSize;
    if(o.agentViewCount==undefined) this.agentViewCount=this.viewName;
    this.sUrl = '/' + this.dbPath + '/' + this.viewName + '?OpenView&Start=1&Count=' + this.pageSize;
}
Toolbar.prototype = {
//    pageSize:8,
    dbPath:'',
    viewName:'',
 
    id_totalDocuments:'totalDocuments',
   
    id_container:'lastnews',
    agentViewCount:'',
    currentPage : 1,
    
    msg:function() {
        alert(this.pages);
    },
    countDocs:function() {
        var object = this;
        YAHOO.util.Connect.asyncRequest('GET', 'count?OpenAgent&db=' + this.dbPath + '&v=' + this.agentViewCount + '&k=1', {
            success: function(o) {
                if (o.responseText !== undefined) {
                    object.pages = Math.ceil(parseInt(o.responseText) / object.pageSize);
					object.count_pages=object.pages;
					object.currentPage=1;
					object.outHrefPages();
                }
            },
            failure: function(o) {
                if (o.responseText !== undefined) {
                }
            }
        }
                )
    },
    doLoad : function(tmp_url) {
        var object = this;
        YAHOO.util.Connect.asyncRequest('POST', tmp_url, {
            success: function(o) {
                if (o.responseText !== undefined) {
                    object.el_container.innerHTML = o.responseText;
					object.outHrefPages();
                }
            },
            failure: function(o) {
                if (o.responseText !== undefined) {
                }
            }
        })
    },
    onClick : function(which) {
		var startCount=1;
        switch (which) {
	            case "first":
                this.currentPage = 1;
				startCount=1;
                this.sUrl = '/' + this.dbPath + '/' + this.viewName + '?OpenView&Start=1&Count=' + this.pageSize;
                this.doLoad(this.sUrl);
                break;
            case "prev":
                this.currentPage = (this.currentPage - 1);
                if (this.currentPage == 0) this.currentPage = 1;
				startCount=(this.currentPage*this.pageSize)-this.pageSize;
				if(startCount==0)startCount=1;
				else startCount++;
                this.sUrl = '/' + this.dbPath + '/' + this.viewName + '?OpenView&Start=' + startCount + '&Count=' + this.pageSize;
                this.doLoad(this.sUrl);             
                break;
            case "next":
                this.currentPage = (this.currentPage + 1);
                if (this.currentPage > this.pages) this.currentPage = this.pages;
				startCount=(this.currentPage*this.pageSize)-this.pageSize;
				if(startCount==0)startCount=1;
				else startCount++;
                this.sUrl = '/' + this.dbPath + '/' + this.viewName + '?OpenView&Start=' + startCount + '&Count=' + this.pageSize;
                this.doLoad(this.sUrl);             
                break;
            case "last":
                this.currentPage = this.pages;
				startCount=(this.pages*this.pageSize)-this.pageSize;
			    if(startCount==0)startCount=1;
		        else startCount++;
                this.sUrl = '/' + this.dbPath + '/' + this.viewName + '?OpenView&Start=' + startCount + '&Count=' + this.pageSize;
                this.doLoad(this.sUrl);
                break;
		  default:		  
  		    this.currentPage = parseInt(which);
			startCount=(this.currentPage*this.pageSize)-this.pageSize;
			if(startCount==0)startCount=1;
		    else startCount++;
		    this.sUrl = '/' + this.dbPath + '/' + this.viewName + '?OpenView&Start=' + startCount + '&Count=' + this.pageSize;
		    this.doLoad(this.sUrl);             
		    break;
        }
    },
	outHrefPages:function(){
        var object = this;
		var limit=15;
		var start = 0;
        var str_count = '';		
		if(object.currentPage>1) str_count = '<a href="#" onClick="dp.onClick(\'first\'); return true;">Первая</a>&nbsp;&nbsp;<a href="#" onClick="dp.onClick(\'prev\'); return true;">Предыдущая</a>&nbsp;&nbsp;';		
		else str_count = '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;';		
        if(object.pages>limit) {
			if(object.currentPage>=limit){
				start = (object.currentPage-7);
				limit=start+limit;
			}
        } else  limit = object.pages;
		if (limit >= object.pages )  limit = object.pages;
        for (var i = start; i < limit; i++) {
				if (object.currentPage != (i + 1)) 	str_count +='&nbsp;&nbsp;<a href="#" onClick="dp.onClick(' + (i + 1) + '); return true;">' + (i + 1) + '</a>';
				else str_count += "&nbsp;&nbsp;" + (i + 1);
        }
		if(object.pages>limit) str_count +='&nbsp;&nbsp;...';
		if(object.currentPage<object.pages) str_count +='&nbsp;&nbsp;&nbsp;&nbsp;<a href="#" onClick="dp.onClick(\'next\'); return true;">Следующая</a>&nbsp;&nbsp;<a href="#" onClick="dp.onClick(\'last\'); return true;">Последняя</a>';		
        object.el_totalDocuments.innerHTML = str_count;		
	}
};
String.prototype.trim = function() {
    var A = /^\s+|\s+$/g;
    return function() {
        return this.replace(A, "")
    }
}();