// DisplayPanels v101
// (C)Erik Gorton June 2010 > Ethical Design www.ethicaldesign.com.au
// Displays pre-formatted articles
// Requires the PanelMaker plugin (by ethicaldesign)

// Create the obj_Article Object for storing values and updating the pagenum
	function obj_Article(theArticleNum, theNumColumns, theCurrentColumn){
		this.articleNum = theArticleNum;
		this.numColumns = theNumColumns;
		this.currentColumn = 1;
		this.updatePageNum = function(theCurrent) {
			// ceiling rounds UP to the nearest integer
			if (theCurrent){this.currentColumn = theCurrent;}
			var theOutput = "<strong>"+ (Math.floor(this.currentColumn/2)+1);
			theOutput += "</strong> of "+ Math.ceil(this.numColumns/2);
			document.getElementById("pagenum_" + this.articleNum).innerHTML = theOutput;
			if( Math.ceil(this.numColumns/2) == 1 ){ document.getElementById("pagenum_" + this.articleNum).innerHTML = ""; }
			//alert(this.currentColumn);
		}
	};
	// Create global array of all articles and their properties - set indice 0 to 0 so we can avoid problem of starting at index 0
	global_Article = [new obj_Article(0)];
	
$(function(){ //Document Ready Functions
	//alert(global_Article);
	DisplayPanels();
});

function DisplayPanels(){
	vArticleNum=0;
	do {
		vArticleNum++;
		vArticle=document.getElementById("articlebox_"+vArticleNum);
		if(!vArticle){ vArticleNum--; break; }
		
		var vNumPanels = $("#articlebox_"+vArticleNum).attr("title");
		$("#articlebox_"+vArticleNum).attr("title","");
		global_Article[vArticleNum]=new obj_Article(vArticleNum,vNumPanels); // add the article to the list
		global_Article[vArticleNum].updatePageNum();
	
		jQuery(document).ready(function(){//Setup the slideshow carousel
			jQuery('#blogpost_'+vArticleNum).jcarousel({
					scroll:2,
					animation:500,
					easing:'easeOutCubic',
					itemFirstInCallback: blog_itemFirstInCallback
			});
		});
		
	}while(vArticle);
}


/* This is the callback function which receives notification when an item becomes the first one in the visible range. */
function blog_itemFirstInCallback(carousel, item, idx, state) {
		var theArticleNum = carousel.container.parent().parent()[0].id;
		theArticleNum = parseInt( theArticleNum.replace(/articlebox_/, "") ); // Remove the leading 'blogpost_' to leave the number
		global_Article[theArticleNum].updatePageNum(idx);
};
