var JissenNews;

(function($){

	JissenNews = function() {
		this.init.apply(this, arguments);
	};

	JissenNews.prototype = {
		url: '',
		data: '',
		isLoaded: false,
		callback: undefined,

		init: function() {},

		/*
		 * returns news item array(jQuery object).
		 *
		 * @param {string} filtering text if you need.
		 * @param {int} max count of array.
		 */
		getItems: function(category, maxCount) {
			var items = [];

			if (this.isLoaded) {
				if (category) {
					var cat = category;
					if (category.indexOf("!") >= 0) {
						cat = cat.slice(1);
						items = $('item', this.data).filter(function(){
							return $(this).find('category').text() !== cat;
						});
					}
					else {
						items = $('item', this.data).filter(function(){
							return $(this).find('category').text() === cat;
						});
					}
				}
				else {
					items = $('item', this.data);
				}
			}

			if (maxCount) {
				return items.slice(0, maxCount);
			}
			else {
				return items;
			}
		},

		/*
		 * returns news item count.
		 *
		 * @param {string} filtering text if you need.
		 */
		getCount: function(category) {
			return this.getItems(category).length;
		},

		/*
		 * load xml data & apply callback function.
		 */
		load: function() {
			this.isLoaded = false;
			var this_ = this;
			$.ajax({
				url: this.url,
				dataType: 'xml',
				ifModified: true,
				/*error: function(xhr, status) {
				},*/
				success: function(data) {
					this_.data = data;
					this_.isLoaded = true;
					this_.callback.apply(this_, arguments);
				}
			});
		},

		/*
		 * build news list in <ul.jissen_news>.
		 *
		 * @param {object} container element to append the list.
		 * @param {string} filtering text if you need.
		 * @param {int} max count of array.
		 */
		build: function(target, category, maxCount) {
			var this_ = this;
			$(document).ready(function(){
				if (this_.isLoaded) {
					var list = this_.__createList(category, maxCount);
					$(target)
						// append news.
						.append('<ul class="jissen_news">'+ list.join('\n') +'</ul>')
							.find('ul.jissen_news')
							// popup handler.
							.find('a[rel=popup]')
								.click(function(){
									window.open(this.href, 'jissen', 'menubar=yes,toolbar=no,location=yes,status=yes,scrollbars=yes,resizable=yes,width=600,height=600');
									return false;
								})
							.end()
							// external link handler.
							.find('a[rel=other]')
								.click(function(){
									window.open(this.href, '_blank');
									return false;
								})
							.end()
						.end()
					;
				}
			});
		},

		__createList: function(category, maxCount) {
			var list = [];
			$(this.getItems(category, maxCount)).each(function(i){
				var date = $(this).find('entrydate').text();
				var title = $(this).find('title').text();
				var type = $(this).find('link').attr('type');

				// set link type & icon.
				var rel = '';
				var icon =  '';
				switch (type) {
					case 'article':
						rel = 'popup';
						icon = '<span class="external">&nbsp;</span>';
						break;
					case 'inner':
						rel = 'inner';
						break;
					case 'other':
						rel = 'other';
						icon = '<span class="external">&nbsp;</span>';
						break;
					case 'document':
						rel = 'pdf';
						icon = '<span class="pdf">&nbsp;</span>';
						break;
					default:
						break;
				}

				// set post category.
				var cat = '';
				switch ($(this).find('category').text()) {
					case 'news':
						cat = 'news';
						break;
					case 'examination':
						cat = 'exam';
						break;
					case 'diary':
						cat = 'diary';
						break;
					case 'other':
						cat = 'other';
						break;
					case 'important':
					case 'o_important':
						cat = 'important';
						break;
					default:
						break;
				}

				// add link & icon to title text.
				if (type !== 'none') {
					var url = $(this).find('link').text();
					title = '<a href="'+ url +'" rel="'+ rel +'">'+ title +'</a>' + icon;
				}

				list.push('<li class="'+ cat +'"><h3>'+ date +'</h3><p>'+ title +'</p></li>');
			});
			return list;
		}
	};

})(jQuery);


