// Calls the UpdateMessageBoardMessage service request.

function SetCurrentPageNumber(start, count, pageCount)
{
    var pageNbr;
    
    if (((start*-1)+count) % count > 0)
        pageNbr = parseInt((-start+count) / count) + 1;     //((-start+count) / count) + 1;
    else
        pageNbr = parseInt((-start+count) / count);         //((-start+count) / count);
    
    if (document.thread.ascending==false)
    {
        pageNbr = pageCount - pageNbr + 1;
    }
    document.thread.pageNumber = pageNbr;
    //alert(document.thread.pageNumber);
    //return pageNbr;
}   

function DisplayPagination(start, count, pageCount, curCount)
{
    var pageNumber = 0;
    if(start == 0)
    {
        pageNumber = 1;
    } 
    else 
    {
        pageNumber = (((start * -1) + 10) / 10)
        if(pageNumber % 1 > 0)
        {
            pageNumber = pageNumber + 1;
        }
    }
    
    //alert("in DisplayPagination. pageCount="+pageCount +", pageNumber="+pageNumber);

    //alert("document.thread.pageNumber="+document.thread.pageNumber +", pageCount="+pageCount);
    //alert("PageNbr="+ GetCurrentPageNumber(start, count, pageCount));
    
    showPageNav(start, 'pageNavigation', pageCount, pageNumber);
}

function updateMessageBoardMessage(op, start)
{
    alert(op + ' ' + start);
    
    var isVisible;
    var isHighlighted;
    var Id;
    var updateElement;
        
    if (op=="hide")
    {
        updateElement = document.getElementById("hide");
        isVisible = updateElement.checked;
        if (isVisible==true) isVisible=0;
        else isVisible=1;
        Id = updateElement.value;
    }
    else if(op=="highlight")
    {
        updateElement = document.getElementById("highlight");
        isHighlighted = updateElement.checked;
        if (isHighlighted==true) isHighlighted=0;
        else isHighlighted=1;
        Id = updateElement.value;
    }
    alert(updateElement.checked);
    alert(updateElement.value);
        
    var resp = Tremor.Ajax.Call("panel","UpdateMessageBoardMessage",{messageId:Id, visible:isVisible, highlighted:isHighlighted},{sync:true});
    if(resp != null)
    {
        //Tremor.Content.LoadControl($('adminMessageList').parentNode,'../template/messageBoard_Thread_Page.thtml');
        ReloadContent(start);

    } else {}
}

function quoteMessage(quoteId)
{
    quote = document.getElementById("quotedMessageId");
    quote.value = quoteId;
    
    el = document.getElementById("text");
    el.value = "<p>test</p>";
}

function showPageNav(start, positionId, pageCount, pageNumber)
{
	pageCount = Math.round(pageCount);
	pageNumber = Math.round(pageNumber);
    
    if(pageCount > 1)
    {
   	    var element = document.getElementById(positionId);
   	    var element1 = document.getElementById(positionId + '1');
        var pagerHtml;
        var k = pageNumber + 5;
        var i = pageNumber;
	
		pagerHtml = '<a href="" onclick="Prev();return false;" style="margin-right:30px;">< PREV </a>';
	

    
        /*
        if((k > pageCount) && (pageCount > 5))
        {
            i = pageCount - 4;
            pagerHtml += '...| ';
        }
    
        for (i; i <= pageCount; i++)
        { 
            if(i == pageNumber)
            {
                pagerHtml += i + ' | ';
            }
            else if(i < k){
                pagerHtml += '<a href="#" onclick="showPage(' + i + ', ' + pageCount + ');return false;">' + i + '</a> | ';
            } else {
                pagerHtml += '...| ';
                break;
            }
        }    
        */
        
        
	pagerHtml += '<a href="" onclick="Next();return false;"> NEXT ></a>';
	

	
	element.innerHTML = pagerHtml;
      element1.innerHTML = pagerHtml;
    }
}


    
function showPage(pageNumber, pageCount) 
{

    alert("entering showPage");

    if(pageNumber == 1)
    {
        First();
    } 
    else if(pageNumber == pageCount) 
    {
        Last();
    } 
    else 
    {
        pageStart = pageNumber * -entryCount + entryCount
        firstEntry = pageStart;
        ReloadContent(firstEntry);
    }
    
    alert("leaving showPage. firstEntry=" + firstEntry);

}   

	function ReloadContent(start)
	{
		start = start-1;
		//alert("ReloadContent.start="+start +", entryCount="+entryCount+", document.thread.totalThreadCount="+document.thread.totalThreadCount+", document.thread.pageCount="+document.thread.pageCount+", document.thread.curCount="+document.thread.curCount );
		//alert("start="+start+", count="+count +", pageCount="+pageCount+", curCount="+curCount+", document.thread.totalThreadCount="+document.thread.totalThreadCount);
		document.thread.pageCount = document.thread.pageCount;
		document.thread.curCount = document.thread.curCount;

		document.getElementById('reactionsBox_content').innerHTML = "<p style='font-size:14px; margin:0;'>loading...</p>";
		Tremor.Content.LoadControl('reactionsBox_content','template/messageBoard_thread_page.thtml',{start:start, count:entryCount, searchText:searchText, reloaded:true});
	}
		
	function Prev_Old() { 
	    if(firstEntry > -entryCount)
	    {
	        firstEntry = -entryCount;
	    }
  	    if (firstEntry < entryCount)
	    {
			firstEntry +=entryCount;
    	}
	    ReloadContent(firstEntry);
	}
	
		
	function Next_Old() 
	{ 
    	if (firstEntry > -document.thread.totalThreadCount + 10)
    	{	    
	        firstEntry = firstEntry - entryCount; 
	        ReloadContent(firstEntry);
		}
	}
	
	function Prev() 
	{ 
        //alert("Prev_new. firstentry="+firstEntry +", entryCount="+entryCount + ". document.thread.ascending="+document.thread.ascending);
	    if(document.thread.ascending==true)
	    {
	        if(firstEntry > -entryCount)
	        {
	            //alert("firstEntry > -entryCount")
	            firstEntry = -entryCount;
	        }
  	        if (firstEntry < entryCount)
	        {
			    firstEntry +=entryCount;
    	    }
    	    ReloadContent(firstEntry);	    
        }
        else //descending
        {
	        if (firstEntry > -document.thread.totalThreadCount + entryCount)
	        {
  	            if (firstEntry < entryCount)
	            {
                    //alert("Prev_new. firstentry="+firstEntry +", entryCount="+entryCount + ". document.thread.ascending="+document.thread.ascending);	            
			        firstEntry -=entryCount;
                    if (firstEntry >0) firstEntry=0;	
                    if (firstEntry < -document.thread.totalThreadCount + entryCount) firstEntry=-document.thread.totalThreadCount + entryCount;
        	        ReloadContent(firstEntry);        		    
    	        }  
    	    }      
        }
	}
		
		
function Next(){ 

        	if(document.thread.ascending){	
    	    		if (firstEntry > -document.thread.totalThreadCount + entryCount){	    
	            	firstEntry = firstEntry - entryCount; 
                		//alert("Next_new. firstentry="+firstEntry +", entryCount="+entryCount + ". document.thread.ascending="+document.thread.ascending);
	            	ReloadContent(firstEntry);	            
	   		}
        	}
        	else
        	{
	  		//alert('firstEntry: '+firstEntry+' entryCount: '+entryCount);
			//descending: same as Prev ascending
	        if (firstEntry < 0)
	        {	        
	            if(firstEntry > -entryCount)
	            {
	                firstEntry = 0;
	                
	            }
  	            else if (firstEntry < entryCount)
	            {
			        firstEntry +=entryCount;
		            if (firstEntry >0) firstEntry = 0;
    	        }
                //alert("Next_new. firstentry="+firstEntry +", entryCount="+entryCount + ". document.thread.ascending="+document.thread.ascending);
	            ReloadContent(firstEntry);	  
	        }                          
		}          
 	}

	
	function userPosted(){
		$('#reactionsForm').css({ display:"none" });
		$('#reactionsThanks').css({ display:"block" });

		//add a tick to GA
		urchinTracker('/kashibar/reactions_thanks');
	}
	function First()
	{
	    if(document.thread.ascending)
	    {		
	        firstEntry = 0;
	        ReloadContent(firstEntry);
	    }
	    else
	    {
	        firstEntry = -document.thread.totalThreadCount + entryCount;
		ReloadContent(firstEntry);	    
	    }
		
	}
	
	function Last()
	{
	    if(document.thread.ascending)
	    {			
	        firstEntry = -document.thread.totalThreadCount + entryCount;
	        ReloadContent(firstEntry);
	    }
	    else
	    {
	        firstEntry = 0;
	        ReloadContent(firstEntry);
	    }

	}
