var c30yt_item = function(li){
	this.li = li;	
	if (li) {		
		this.media = eval($(li).find('a').attr('rel'));
	}
	
	this.prevItem = this.nextItem = null;
};

$.extend(c30yt_item.prototype, {
	li       : null,
	media    : '',
	nextItem : null,
	prevItem : null,
	
	toString : function() {
		return 'c30yt_item[li=' + this.li + ';media=' + this.media + ';hasNext=' + (this.nextItem) + ']';
	}	
});

var c30yt_ui = function(o){
	this.model   = (o && o.model)   ? o.model   : null;
	this.toolbar = (o && o.toolbar) ? o.toolbar : null;
	
	this._prev   = $(this.toolbar).find('.prev');
	this._next   = $(this.toolbar).find('.next');	
};

c30yt_ui.instances = {};

c30yt_ui.mediaUrl = '';

c30yt_ui.get = function(id) {
	return c30yt_ui.instances[id];
};

c30yt_ui.register = function(id) {
	var model = new c30yt();

	$('#' + id + ' li').each(function(){
		model.add(new c30yt_item(this));		
	});
		
	var toolbar = $('#' + id + ' .toolbar');
	
	c30yt_ui.instances[id] = new c30yt_ui({
		model   : model,
		toolbar : toolbar
	}); 
	
	c30yt_ui.instances[id].next();
}

$.extend(c30yt_ui.prototype, {
	_prev : null,
	_next : null,
	
	toolbar : null,
	model   : null,
	
	_updateToolbar : function() {		
		if (this.model.hasPrevious()) {
			$(this._prev).removeClass('inactive');
		}
		else {
			$(this._prev).addClass('inactive');
		}
		
		if (this.model.hasNext()) {
			$(this._next).removeClass('inactive');
		}
		else {
			$(this._next).addClass('inactive');
		}		
	},
	
	_updateItem : function(last) {
		var current = this.model.current;
		
		if (last && last.li) {
			$(last.li).hide();
		}
		
		$(current.li).show();
	},
	
	prev : function() {
		var last     = this.model.current;
		var prevItem = this.model.prev();
		
		this._updateToolbar();
						
		if (!prevItem) {			
			return;
		}

		this._updateItem(last);
	},
	
	next : function() {
		var last     = this.model.current;
		var nextItem = this.model.next();
		
		this._updateToolbar();		
						
		if (!nextItem) {
			return;
		}

		this._updateItem(last);
	},
	
	toString : function() {
		return 'c30yt_ui';
	}
});

var c30yt = function(){
	this._root = new c30yt_item();
	this.current = null;
};
$.extend(c30yt.prototype, {
	_root   : null,
	
	current : null,

	/*
	 * +---------------------
	 * | navigation methods
	 * +---------------------
	 */		
	
	hasPrevious : function() {
		return (this._root != this.current.prevItem);
	},
	
	getPrevious : function() {
		if (!this.hasPrevious()) {
			return null;
		}
		
		return this.current.prevItem;		
	},
		
	prev : function() {
		if (!this.current) {
			this.current = this._root;
		}
		
		if (!this.hasPrevious()) {
			return null;
		}
		
		return this.current = this.getPrevious();
	},
	
	hasNext : function() {
		return this.current.nextItem;
	},
	
	getNext : function() {
		if (!this.hasNext()) {
			return null;
		}
		
		return this.current.nextItem;		
	},
	
	next : function() {
		if (!this.current) {
			this.current = this._root;
		}
		
		if (!this.hasNext()) {
			return null;
		}
		
		return this.current = this.getNext();
	},
	
	/*
	 * +---------------------
	 * | linked list methods
	 * +---------------------
	 */
	
	getTail : function() {
		var tail = this._root;
		while (tail.nextItem) {
			tail = tail.nextItem;
		}
		
		return tail;
	},		

	/*
	 * +---------------------
	 * | item methods
	 * +---------------------
	 */		
	
	add : function(item) {
		var tail = this.getTail();
		
		tail.nextItem = item;
		item.prevItem = tail;
	},
	
	toString : function() {
		return 'c30yt model';
	}	
});

$(document).ready(function(){
	/* hide the li elements */
	$('.c30youtube_feed li').each(function(){
		$(this).hide();
	});
	
	/* but show the first */
	$('.c30youtube_feed li:first').show();
	
	/* show the feeds */
	$('.c30youtube_feed').each(function(){
		$(this).show();
	});
});