var clientAccountList = new Component("clientAccountList");
app.registerComponent(clientAccountList);
clientAccountList.list = function(currentPage){
    var proprtie = "";
    var direction = "";
    if(this.proprtie){
        proprtie = this.proprtie;
    }
    if(this.direction){
        direction = this.direction;
    }
    return clientAccountList.render("template/client_account_list.html", 
        "clientAccountListZone", "businessPartners?asOfDate=" + app.selectDateValue + "&page=" + currentPage +
        "&proprtie=" + proprtie + "&direction=" + direction).done(function (template, data){
                $("#clientAccountSelectAsofdate").val(app.selectDateValue);
                clientAccountList.listCallBack(template, data);
                $("#clientAccountSelectAsofdate").die().live("change",function(){
                	app.trigger("changeAsOfDate", "clientAccountList", $("#clientAccountSelectAsofdate").val());
                });
            }
        );  
};

clientAccountList.getPaginationFooter = function (pagination, callback){
    if(pagination == null){
        return "";
    }
    var totalCount = parseInt(pagination.totalCount);
    var currentPage = parseInt(pagination.page);
    var recordsPerPage = parseInt(pagination.recordsPerPage);
    var pageCount = parseInt((totalCount - 1)/recordsPerPage + 1);
    var begin = 1,end = 10, num = 10;
    if(totalCount == 0 || pageCount == 1){
        return "";
    }
    begin = Math.floor((currentPage - 1)/num) * num + 1 ;
    end = begin + num - 1;
    if(end > pageCount){
        end = pageCount;
    }
    
    var htmlBuffer = new StringBuffer();
    
    if(currentPage <= num){
        htmlBuffer.append("<a page> << </a>");
        htmlBuffer.append("<a page> < </a>");
    }else{
        htmlBuffer.append("<a page href='javascript:void(0);' onclick='", callback, "(", 1, ");'>");
        htmlBuffer.append("<<");
        htmlBuffer.append("</a>");
        htmlBuffer.append("<a page href='javascript:void(0);' onclick='", callback, "(", begin - num, ");'> < </a>");
    }    
    
    for (var i = begin; i <= end; i++){
        if(i == currentPage){
            htmlBuffer.append("<a page href='javascript:void(0);' class='current'>");
        }else{
            htmlBuffer.append("<a page href='javascript:void(0);' onclick='", callback, "(", i, ");'>");
        }
        htmlBuffer.append(i);
        htmlBuffer.append("</a>");
    }
    var lastBegin = Math.floor((pageCount - 1)/num) * num + 1;
    if(currentPage >= lastBegin){
        htmlBuffer.append("<a page> > </a>");
        htmlBuffer.append("<a page> >> </a>");
    }else{
        htmlBuffer.append("<a page href='javascript:void(0);' onclick='", callback, "(", begin + num, ");'> > </a>");
        htmlBuffer.append("<a page href='javascript:void(0);' onclick='", callback, "(", lastBegin, ");'>");
        htmlBuffer.append(">>");
        htmlBuffer.append("</a>");
    }
    
    return htmlBuffer.toString();
};

clientAccountList.listCallBack = function(template, data){
    if(this.sortA){
        var iconId = $(this.sortA).attr("id");
        var icon = $("#" + iconId);
        if(icon.hasClass("sortDownIcon")){
            icon.removeClass("sortDownIcon").addClass("sortDownIcon_click");
        }else if(icon.hasClass("sortUpIcon")){
            icon.removeClass("sortUpIcon").addClass("sortUpIcon_click");
        }
    }
    if(data.pagination == null || data.pagination.totalCount == 0){
        var noAvailableHtml = "<tr class='alt'><td colspan='3'>No BP available</td></tr>";
        $("#positionDTable").append(noAvailableHtml);
    }
    var html = this.getPaginationFooter(data.pagination, "clientAccountList.list");
    $("#portfolioPagination1").empty().append(html);
    $("#portfolioPagination2").empty().append(html);

};

clientAccountList.sort = function(proprtie, direction, a){
    this.proprtie = proprtie;
    this.direction = direction;
    this.sortA = a;
    clientAccountList.list(1);
    
};

clientAccountList.bpkeyClick = function(bpkey){
    rmBpKeySelectMenu.bpKeyInput.val(bpkey);
    app.selectBpKey = bpkey;
//    app.trigger('load', 'portfolioSummary', bpkey, "portfolioSumarryZone");
    app.trigger("load", "initialMainContent");
    app.trigger("call", 'rmBpKeySelectMenu', 'bpkeyChangeMessage', bpkey);
    app.trigger("bpKeyChange", null, bpkey, true);
    
    try{
        bpkeyValue = bpkey;
    }catch(e){
        
    }
};

clientAccountList.on( "load", function(isReloadSelf){
	
    if(isReloadSelf){
        return clientAccountList.list(1);
    }else{
        this.proprtie = "";
        this.direction = "";
        this.sortA = "";
        
//        var html = "<div id='clientAccountListZone' style='min-height:300px;'></div><div id='portfolioSumarryZone'></div>";
        var html = "<div id='clientAccountListZone' style='min-height:300px;'></div>";
        $("#" + app.mainComponentDivId).empty().append(html);
        return $.when(clientAccountList.list(1)
//        	,app.trigger("load", "portfolioSummary", null, "portfolioSumarryZone")
        	)
        	.done( function(){
				$("div.bannerImg").css({"display":"block"});
				$("#bannerContent").css({"height":"78px"});        		
        	});
    }

    
});
