//user content page

ywUtil.ScheduleHandler( ywContent );

function ywContent() {
	var content = arguments.callee; //can't use 'this' here
	content.contentId = ywUtil.Get_GET( 'id' );
	content.parseData();
}
ywContent.setTableSize = function( rows, cols ) {
	this.tableSize = rows * cols;
	this.numCols = cols;
};
ywContent.setDivId = function( id ) {
	this.divId = id;
};
ywContent.setHref = function( relative_url ) {
	this.href = relative_url;
};
ywContent.setNavId = function( id ) {
	this.navId = id;
};
ywContent.setData = function( responseText ) {
	//must put paren.s around response to prevent invalid label error!
	this.content = eval( '(' + responseText + ')' );
};
ywContent.makeTemplate = function( markup ) {
	this.rowTemplate = markup.replace(/\f/g, "\n");	
};
ywContent.parseData = function() {
	if( !this.content.length ) {
		return;
	}

	this.divParent = document.getElementById( this.divId );	
	this.imgSrc = ywUtil.GetGlobal( 'imagesrc' ) + '/';

	var navBar = this.getNavBar();
	if (navBar )
		this.display( navBar.firstItem(), navBar.lastItem() );
	else
		this.display( 0, this.content.length - 1 );
};
ywContent.getNavBar = function( currentItem ) {
	if (this.content.length > this.tableSize) {
		var callback = 'ywContent.redisplay';
		
		//navigate to search result item (if applicable)
		if (!arguments.length) {
			currentItem = 0;
			if (this.contentId != null) {
				for (var i = 0; i < this.content.length; i++) {
					if (this.content[i].id == this.contentId) {
						currentItem = i;
						break;
					}
				}
			}
		}
		ywNavigation( 0, this.content.length - 1, currentItem, this.tableSize, callback, this.href );
		document.getElementById( this.navId ).innerHTML = ywNavigation.makeNavBar();
		return ywNavigation;
	}
	else {
		document.getElementById( this.navId ).innerHTML = '';
		return null;
	}
};
ywContent.display = function( first, last ) {
//display selected content

	var html = '';
	for (var i = first; i <= last; i++) {
		html += this.displayDiv( i );
	}
	this.divParent.innerHTML = html;
	ywUtil.ScrollHere();
};
ywContent.redisplay = function( first, last ) {
	var temp = this.getNavBar( first );
	this.display( first, last );
};
ywContent.displayDiv = function( index ) {
	var text = this.content[index].description.replace( /\n/g, "</p><p>" );
	var html = this.rowTemplate.replace( '$description', text);
	
	html = html.replace( '$title', this.content[index].title );

	if (this.content[index].image)
		html = html.replace( '$image', this.imgSrc + this.content[index].image );
	else
		html = html.replace( '<img src="$image" class="floatImg" />', '' );
	
	return html;
};