

I4Portal.BulletinBoard = function(windowDiv,contentDiv,publicationId) {
		this.windowDiv=windowDiv;
		this.jWindowDiv = "#"+windowDiv;
		this.contentDiv=contentDiv;
		this.jContentDiv = "#"+contentDiv;
		this.publicationId = publicationId;
		
		this.postWindowUrl = "CMSPortal/bulletin_board/board_window.php";
		this.postDetailUrl="CMSPortal/bulletin_board/show_post.php";
		this.allPostSummariesUrl="CMSPortal/bulletin_board/all_post_summaries.php";
		
		this.boardCssLoaded=false;
		this.boardCssUrl="CMSportal/bulletin_board/bulletin_board.css";
};


I4Portal.BulletinBoard.prototype = {
		
		renderContent:function(){
			var t =this;
			if(!this.boardCssLoaded) {
				$('<link rel="stylesheet" type="text/css" href="'+this.boardCssUrl+'" >').appendTo("head");
				this.boardCssLoaded=true;
			}
			
			
			$.post(this.postWindowUrl,
					{"pub_id":this.publicationId},
					function(data){t.onReplyPostWindow(data);},
					"json");
		},
		
		
		
		onReplyPostWindow:function(response){
			//alert(response['result_string']);
			if(response['result_code'] != 1)  return false;
			
			var contentHtml = '	<div id="board_header"> </div>'; 
			
			var posts = response["posts"];
			
			for(var i=0; i < posts.length;i++){
				contentHtml += '<div class="post" post_id="' + posts[i]["post_id"] +'">' +
							   '  <span class="post_time">'+posts[i]["post_date"]  +'</span> ' + posts[i]["summary"] +
							   '<a href="#">details..</a>'+
							   ' </div> ';
			}
			
			contentHtml +='<div id="board_more"> 	<a href="#"> More announcements....</a> </div>';
	
			$(this.jWindowDiv).html(contentHtml);
			
			var t = this;
			$(".post a").click( function(event){ 
								t.requestPostDetail($(this).parent().attr("post_id")); 
							  });
			$("#board_more").click( function(event){ 
				t.requestAllSummaries(0); 
			  });
			
			return true;
		
		},
		
		requestPostDetail:function(postId){
			var t=this;
			$.post(this.postDetailUrl,
					{"post_id":postId},
					function(data){t.onReplyPostDetail(data);},
					"json");
			
		},
		
		onReplyPostDetail:function(response) {
			if(response['result_code'] != 1) 
				return false;
		
			var contentHtml =   '  <div id="post_time">'+response["post_date"]  +'</div> ' +
							'	<div id="post_summary">'+ response["summary"] +
			                  '</div>'; 
			 contentHtml += ' <div id="post_content">'+ response["content"] +'</div>'; 
			
			 $(this.jContentDiv).html(contentHtml);
			return true;
			
		},
		
		requestAllSummaries:function(offset){
			var t=this;
			$.post(this.allPostSummariesUrl,
					{"pub_id":this.publicationId, "offset":offset},
					function(data){t.onResponseAllSummaries(data);},
					"json");
		},
		
		onResponseAllSummaries:function(response){
			if(response['result_code'] != 1) 
				return false;
			
			var posts = response["posts"];
			
			var contentHtml='';
			for(var i=0; i < posts.length;i++){
				contentHtml += '<div class="det_post" post_id="' + posts[i]["post_id"] +'">' +
							   '  <span class="det_post_time">'+posts[i]["post_date"]  +'</span> ' + posts[i]["summary"] +
							   '<a href="#">details..</a>'+
							   ' </div> ';
			}
			
			var pagingHtml=this.build_paging(response["total_posts"], response["page_post_count"], response["offset"]);
			contentHtml +='<div id="post_paging">'+ pagingHtml+'</div>';
	
			$(this.jContentDiv).html(contentHtml);
			
			var t = this;
			$(".det_post a").click( function(event){ 
								t.requestPostDetail($(this).parent().attr("post_id")); 
							  });
			
			$(".page_index").click( function(event){ 
				t.requestAllSummaries($(this).attr("page_index")); 
			  });
			
			return true;
		},
		
		
	    build_paging:function(totalResults, resultsPerPage, startIndex){
			//console.log("total:"+totalResults+"\tPerpage:"+resultsPerPage+"\tstart"+startIndex);
		    if (totalResults <= 0 || resultsPerPage <= 0) {
		        return '&nbsp;';
		    }
		    totalResults = totalResults - 0.003;
		   var pageStart = 1;
		   var pageEnd = 9;
		   var prevButton = true;
		   var nextButton = true;
		    
		    maxPage = parseInt(totalResults / resultsPerPage) + 1;
		    minPage = 1;
		    currPage = parseInt(startIndex / resultsPerPage) + 1;
		    
		    if (currPage > 5) {
		        pageStart = currPage - 4;
		        pageEnd = currPage + 4;
		       // prevButton = true;
		    }
		    
		    if (pageEnd > maxPage) {
		        pageEnd = maxPage;
		        pageStart = maxPage - 8;
		       // nextButton = false;
		    }
		    
		    if (pageStart < minPage) {
		        pageStart = minPage;
		      // prevButton = false;
		    }
			
			if(currPage == minPage) prevButton=false;
			if(currPage == maxPage) nextButton=false;
		    
		    var footerHTML = '';
		    
		    if (pageStart == pageEnd) {
		        return '&nbsp;';
		    }
	
		    
		    
		    resOffset = (currPage - 2) * resultsPerPage;
		    if (prevButton) {
		        footerHTML += ' <span class="page_index" page_index="'  + resOffset + '" > &lt;&lt;Prev </span>';
		    }
		    
		    for (i = pageStart; i <= pageEnd; i++) {
		        resOffset = (i - 1) * resultsPerPage;
		        if (i == currPage) {
		            footerHTML += '<span  class="page_current">' + i + '</span>  ';
		        }
		        else {
		            footerHTML += '<span  class="page_index" page_index="' + resOffset + '" >' + i + '</span>  ';
		        }
		        
		    }
		    
		    if (nextButton) {
		        resOffset = currPage * resultsPerPage;
		        footerHTML += '<span  class="page_index" page_index="' +  resOffset + '" >Next&gt;&gt; </span>';
		    }
		    

		    
		    return footerHTML;
		    
		}

		
		
};
