/*
 * Cellblock general-purpose management console, a component of
 * the Gloto Platform.  See www.gloto.com for more information,
 * terms, and licensing.
 *
 * Copyright (C) 2005-2010, Gloto Corp.
 * http://www.cellblock.com/terms.htm
 */

function Feeds( resources ){
	var feeds = this;
	this.resources = resources;
	this.currPage = 0;
	this.perPage = 5;
	this.offset = 0;
	this.numFrames = 0;
	this.aka = resources.aka;
	this.target;

	this.embedFeeds = function( )
	{
		if( feeds.aka != null ){
			feeds.currPage = 0;
			feeds.numFrames = 0;
			feeds.getFeedContent();
		}
		else{
			alert("No Feed Source Defined");
		}
	};

	this.changePage = function( page )
	{
		feeds.currPage = page;
		feeds.offset = feeds.currPage * feeds.perPage;
		feeds.getFeedContent();
	};

	this.getFeedContent = function( )
	{		
		util.clear( feeds.resources.container );
		api.numFrames( feeds.aka, feeds.setFeedContent );

	};

	this.setFeedContent = function(data)
	{
		if( util.valid( data ) ){ 
			feeds.numFrames = data.count;
			api.frameInfo( feeds.aka, feeds.offset, feeds.perPage, feeds.showFeedContent );
		}else{
			util.error( data );
		}
	};

	this.hasPreviousPage = function()
	{
		return ( ( feeds.currPage - 1 ) * feeds.perPage >= 0 );
	};

	this.hasNextPage = function()
	{
		return ( ( feeds.currPage + 1 ) * feeds.perPage < feeds.numFrames );
	};

	this.createLaunchPopup = function( id, action )
	{
		return function(){
			var navigation = false;
			view.launchPopup( id, action, navigation );
		};
	};

	this.showFeedContent = function( data )
	{
		if( util.valid( data ) ){
			var table = util.createElement({
				type: "table",
				parentNode: feeds.resources.container
			});

			var tbody = document.createElement("tbody");
			table.appendChild( tbody );

			var row = util.createElement({
				type: "tr",
				parentNode: tbody
			});

			var prev = util.createElement({
				type: "td",
				text: "&lt;",
				id: "feedsNavPrev",
				parentNode: row
			});

			if( feeds.hasPreviousPage() ){	
				prev.onclick = util.closure( feeds.currPage - 1, feeds.changePage );
				prev.setAttribute("class", "pointer");
				prev.setAttribute("className", "pointer");
			}

			var feedContent = util.createElement({
				type: "td",
				id: "feedContent",
				parentNode: row							 
			});

			function createClick( frame ){
				return function(){ if( feeds.resources.click != null ){ feeds.resources.click(frame.id) } };
			};

			var frames = data.frames;
			for( var i = 0; i < frames.length; i++ ){
				var frame = frames[i];

				//var thumb = "";
				//var id = "feedItem" + frame.id;

				var span = util.createElement({
					type: "span",
					className: "thumbnail",
					parentNode: feedContent
				});

				var div = util.createElement({
					type: "div",
					id: "thumbnail",
					parentNode: span
				});

				var table = util.createElement({
					type: "table",
					width: "100%",
					height: "100%",
					border: "0",
					cellpadding: "0",
					cellspacing: "0",
					parentNode: div
				});

				var tbody = util.createElement({
					type: "tbody",
					parentNode: table
				});

				var tr = util.createElement({
					type: "tr",
					parentNode: tbody
				});

				var td = util.createElement({
					type: "td",
					align: "center",
					valign: "middle",
					parentNode: tr
				});

				var a = util.createElement({
					type: "a",
					href: util.noClick(),
					onclick: createClick( frame ),
					parentNode: td
				});

				var img = util.createElement({
					type: "img",
					id: "thumbnail",
					src: frame.medium,
					parentNode: a
				});
			}

			var next = util.createElement({
				type: "td",
				text: "&gt;",
				id: "feedsNavNext",
				parentNode: row
			});

			if( feeds.hasNextPage() ){	
				next.onclick = util.closure( feeds.currPage + 1, feeds.changePage );
				next.setAttribute("class", "pointer");
				next.setAttribute("className", "pointer");
			}
		}else{
			util.error( data );
		}
	};
}