/**
 * This file contains all the functions related to displaying the updates.
 */

// Stupid global stuff

/**
 * Because the object "postControllerObject" keeps changing, this is a way to call it no matter what it is.
 * Used by hashnavigation.js
 */
function selectPostById ( anId )
{
	postControllerObject.selectById( anId );
}


function PostController( aShowKey, name )
{

	this.name = name;
	// save parameter
	this.mySeriesKey = aShowKey;

	// pesky, global, encapsulation breaking, motherfather
	postControllerObject = this;
	
	// keep the reflexes updated with the new objects
	//myHash.addReflex("p", this.selectById, this);


	// state
	this.myPosts 	= new Array();
	this.isVisible	= true;	// default show

	// config
	this.PADDING		= 4; // how much to show on either side of selected post
	this.SELECTOR_SIZE 	= 10;

	this.currentIndex	= 0;	// array position of default selected post in our array
	this.currentId		= null;	// the selected post's id
	

	// get all posts for this show
	$.ajax({
		url:    "lib/json.php?getPostsFor=" + this.mySeriesKey,
		success: function( data ) 
		{
			postControllerObject.addPosts (data );
        },
		async:   false
    });

	// intialize update section
	this.initialize();
	
	// draw
	this.draw();

	this.myReadyState = 0;
	this.myTotal = this.myPosts.length;


}


PostController.prototype.change = function ( aPageKey )
{
	this.constructor( aPageKey );
}

PostController.prototype.getPostHandler = function ( jsonData )
{

	var myNewData = eval ("(" + jsonData + ")");

	var myPostList = "";
	for (var i in this.myPosts)
	{
		myPostList += this.myPosts[i].getId() + " ";
		if ( this.myPosts[i].getId() == myNewData.myId )
		{
			this.myPosts[i] = new Post ( myNewData.myId, myNewData.myTitle, myNewData.myBody, myNewData.myDate, myNewData.myAuthor);
		}
	}

	this.myReadiness["aPost"] = true;

	this.draw();

}


PostController.prototype.addPosts = function ( jsonData )
{

	var myData;

	myData = eval("("+jsonData+")");

	for ( var i in myData )
	{

		onePost = myData[i];
		this.myPosts.push( new Post (onePost.myId, onePost.myTitle, onePost.myBody, onePost.myDate, onePost.myAuthor) );

	}

}


PostController.prototype.initialize = function ()
{

	// Hide/show if any posts exist or not
	if ( this.myPosts.length == 0 )
	{
		$("#updateSection").hide('fast');
	} else
	{
		$("#updateSection").show('fast');
	}

}


/**
 * Updates the update section
 */
PostController.prototype.draw = function()
{

	var optionalNegativeOffset, optionalPositiveOffset;

	var myPostMenuDiv;

	var myMenuButtons, oneDotImg, oneDotA;

	var oneButtonDiv;
	var backButtonHTML, forwardButtonHTML;

	var myEdgeOffset, edgeStart, edgeEnd;

	var totalDots;

	optionalNegativeOffset = 0;
	optionalPositiveOffset = 0;

	// Remove whats there first
	$("#postMenu").empty();

	selectPostMenu = $("#postMenu");

	selectPostMenu.append("<div id=\"postMenuButtons\"></div>")

	myMenuButtons = $("#postMenuButtons");

	// currently selected post, plus pads on either side
	totalDots = 1 + (this.PADDING * 2);


	/*
	 * Back Button
	 */
	backButtonHTML = "<div id=\"backButtonDiv\" class=\"onePostButton\"><a href=\"#commandPostBack\" onclick=\"clickHandler(this); return false;\" id=\"postBackButton\" class=\"postMenuDotA\">&lt;</a></div>";

	myMenuButtons.append(backButtonHTML);


	myEdgeOffset = 0;

	// predicted begining and end points
	edgeStart	= (parseInt(this.currentIndex) - parseInt(this.PADDING));
	edgeEnd		= (parseInt(this.currentIndex) + parseInt(this.PADDING));

	// if there are too many dots and not enough actual posts
	if ( this.myPosts.length < totalDots )
	{

		// total dots are not the number of posts
		totalDots = this.myPosts.length;

		//
		//myEdgeOffset = this.PADDING - this.currentIndex;

	}

	if ( edgeStart < 0 )
	{
		myEdgeOffset -= edgeStart;
	} else if ( edgeEnd > this.myPosts.length )
	{
		
		myEdgeOffset -= (edgeEnd - this.myPosts.length) + 1;
	}

	// Do we have enough material, if not, set upper limit
	// totalDots = (totalDots > this.myPosts.length) ? this.myPosts.length : totalDots;


	for ( var i = 0; i < totalDots; i++)
	{

		// calculate where it is in the index
		myAbsolutePosition = ( this.currentIndex - this.PADDING ) + i + myEdgeOffset ;

		// give us a handle
		onePost = this.myPosts[myAbsolutePosition];

		// indicate that it's selected
		oneBackgroundImage = ( myAbsolutePosition == this.currentIndex ) ? "url(img/selected.gif)" : "url(img/unselected.gif)";


		myMenuButtons.append("<div class=\"onePostButton\"><a href=\"#p=" + onePost.getId() + "\" class=\"postMenuDotA\" style=\"background-image: " + oneBackgroundImage + ";\" onclick=\"clickHandler(this); return false;\">" + (myAbsolutePosition + 1) + "</a></div>");

	}

	myMenuButtons.append( "<div class=\"onePostButton\"><a href=\"#commandPostForward\" id=\"forwardButton\" class=\"postMenuDotA\" onclick=\"clickHandler(this); return false;\">&gt;</a></div>" );

	$("#postMenu").append( myMenuButtons );

	var currentPost = this.myPosts[this.currentIndex];

	if ( currentPost )
	{
		$("#postTitle").html( currentPost.getTitle() );
		$("#postData").html( currentPost.getDate() +  " " + currentPost.getAuthor() );
		$("#postBody").html( currentPost.getBody() + "<br /><br />" );
	} else 
	{
		$("#postBody").html( "No updates available" );	
	}

}


PostController.prototype.selectPost = function ( aPostIndex )
{
	
	this.currentIndex = aPostIndex;

	this.draw();	
	
}


PostController.prototype.selectBack = function ()
{

	if (this.currentIndex > 0 ) // see if the future is ok
	{

		this.currentIndex--;	

		this.draw();	

	}
	
	return false;

}


PostController.prototype.selectForward = function ()
{

	if ( parseInt(this.currentIndex) + 1 < this.myPosts.length ) // see if the future is ok
	{

		this.currentIndex++;

		this.draw();

	}

	return false;

}

PostController.prototype.selectById = function ( anId )
{


	this.currentId = anId;

	for (var i in this.myPosts)
	{

		if ( this.myPosts[i].getId() == anId )
		{


			this.currentIndex = i;

			this.draw();

		}

	}

} // END of selectById
