
(function($){

	$(document).ready(function(){

		// set articles.
		$.ajax({
			url: './search/notice.php',
			dataType: 'xml',
			success: function(xml) {
				$('articles', xml).children().each(addAtricles);
				addArticleEvents();
			}/*,
			,error: function(xhr, stat, e) { alert(stat); }*/
		});

		// set drawer.
		$("#drawer").accordion({
			header: "h3",
			event: "mouseover",
			autoHeight: false
		});
	});

	function addAtricles(index, elm) {
		// append list container to each category.
		$('#dd0' + (index + 1))
			.append('<ul class="ul01">')
			.append('<br class="clear" />');

		// iterate articles.
		$(elm).children().each(function(i){
			var title = $('title', this).text();
			var image = $('image', this).text();
			var desc = $('description', this).text();
			var url = $('url', this).text() || '';
			//alert(['title: '+ title, 'url: '+ url, 'image: '+ image, 'desc: '+ desc].join('\n'));

			// create link.
			if (url != '') {
				var a = $('<a />')
					.attr('href', url)
					.html(title)
					.click(function(){ window.open(this.href); return false; });
			} else {
				var a = $('<span />');
			}

			// create article info.
			var article = $('<dl />');
			if (url != '') {
				//$(article).append($('<dt />').html(a))	// doesn't work.
				$(article).append($('<dt />').css('cursor', 'pointer').text(title).click(function(){ window.open(url); }));
			} else {
				$(article).append($('<dt />').text(title));
			}
			$(article).append('<dd><p>'+ desc +'</p></dd>');

			// append article set.
			$('<li class="li0'+ (i % 2 + 1) +'">')
				.append($(a).html('<img src="'+ image +'" />'))
				.append(article)
				.appendTo('#dd0'+ (index + 1) +' > ul');
		});
	}

	function addArticleEvents() {
		var colors = [ "#f0ffcc", "#fee8ea", "#e6fffb", "#f5f9df", "#fff4e6" ];

		$('#content01 dl.dl02 dd.dl02 div.tab01 dl.tab > dt').each(function(i) {
			$(this)
				.click(function(){
					// hide current visible contents.
					var current = $(this).siblings('dd:visible').prev('dt');
					var curIndex = $('#content01 dl.dl02 dd.dl02 div.tab01 dl.tab > dt').index(current);
					$(current)
						.css('backgroundPosition', '-'+ (curIndex * 90) +'px 0px')
						.next('dd').hide();

					// show clicked contents.
					$(this)
						.css('backgroundPosition', '-'+ (i * 90) +'px -35px')
						.next('dd').show();
				})
				.hover(
					function() {
						$(this).css('backgroundColor', colors[i]);
					},
					function() {
						$(this).css('backgroundColor', 'transparent');
					}
				);
		});
	}

})(jQuery);
