var remoteHost = 'http://www.javezz.com';

function doPage(pg){

	$('data_view_div').hide();
	$('data_list_div').show();

	$('MESSAGES').page.value = pg;

	new Ajax.Request(remoteHost +'/msbi3/data/list.asp',
					{
					method:'get',
					asynchronous:true,
					evalScripts:true,
					onFailure:function(request){alert(request.responseText);}, 
					onSuccess:function(request){	

						try{

							var messages = $A(request.responseXML.getElementsByTagName('message'));

							if(messages.size() > 0){

								var temp = $('data_list_table'), link = null;
								temp.update('');
								temp.appendChild(Builder.node('div', {style:'width:100%;float:left;height:5px;'}));
								messages.each(function(item, idx){
									temp.appendChild(Builder.node('div', {style:'width:7%;float:left;height:25px;padding-top:5px;text-align:center;', 'class':'td_font6'}, item.getAttribute('number')));
									temp.appendChild(Builder.node('div', {style:'width:66%;float:left;height:25px;padding-top:5px;text-align:left;', 'class':'td_font6'}, link = Builder.node('a', {href:'#'}, item.getAttribute('title'))));
									temp.appendChild(Builder.node('div', {style:'width:13%;float:left;height:25px;padding-top:5px;', 'class':'td_font7'}, item.getAttribute('category')));
									temp.appendChild(Builder.node('div', {style:'width:14%;float:left;height:25px;padding-top:5px;text-align:center;', 'class':'td_font6'}, item.getAttribute('create').substr(0, 10)));

									if(idx < (messages.size() - 1)){
										temp.appendChild(Builder.node('div', {style:'width:100%;float:left;text-align:center;padding:1px 0px;'}, Builder.node('img', {src:'_img/dot.gif', width:'566', height:'1', alt:''})));
									}

									Event.observe(link, 'click', function(event){
										doShow(item.getAttribute('id'));
										Event.stop(event);
									});
								});
								temp.appendChild(Builder.node('div', {style:'width:100%;float:left;height:5px;'}));

								var pagination = request.responseXML.getElementsByTagName('pagination')[0];
								//alert(pagination.innerText);
								$('data_pagination').update(pagination.childNodes[0].nodeValue);

							}else{

								var objForm = $('MESSAGES');

								if(objForm.keyword.value != '')alert("'"+ objForm.cate.options[objForm.cate.selectedIndex].text +"' 카테고리를 '"+ objForm.keyword.value +"'으로(로) 검색된 자료가 없습니다.");

							}

						}catch(e){}

					},
					parameters:$A($('MESSAGES').elements).collect(function(item){return item.name +'='+ escape(item.value);}).join('&')
					}
					);

}



function doShow(messageId){

	$('data_list_div').hide();
	$('data_view_div').show();

	var objForm = $('MESSAGES');
	objForm.id.value = messageId;

	new Ajax.Request(remoteHost +'/msbi3/data/show.asp',
					{
					method:'get',
					asynchronous:true,
					evalScripts:true,
					//onFailure:function(request){alert(appMessage.$301.replace('?', request.status));alert(request.responseText);}, 
					onSuccess:function(request){

						try{

							var message = request.responseXML.getElementsByTagName('message')[0];

							$('data_category').update('['+ message.getAttribute('category') +']');
							$('data_created').update(message.getAttribute('create').substr(0, 10));
							$('data_title').update(message.getAttribute('number') +'. '+ message.getAttribute('title'));
							$('data_content').update(message.getElementsByTagName('content')[0].childNodes[0].nodeValue);

							var attachmentDiv = $('data_attachments');
							attachmentDiv.update('');
							var attachments = $A(request.responseXML.getElementsByTagName('attachment'));
							if(attachments.size() > 0){
								var attachmentList = Builder.node('ul', {style:'width:593px;padding:0px;margin:5px 0px;list-style:none;'});

								attachments.each(function(item){

									attachmentList.appendChild(Builder.node('li', {style:'text-align:left;padding:3px 0px;'}, [
											Builder.node('img', {src:'images/add_icon.gif', alt:'', align:'absmiddle', hspace:'5'}),
											Builder.node('a', {href:'/msbi3/attachments/download.asp?id='+ item.getAttribute('id')}, item.getAttribute('name') +"."+ item.getAttribute('extension'))
										])
									)

								});

								attachmentDiv.appendChild(attachmentList);
							}

							$('pn_table').hide();
							$('pn_div').update('');
							var next = request.responseXML.getElementsByTagName('next')[0];
							var pnTable = Builder.node('table', {width:'95%', border:'0', cellspacing:'0', cellpadding:'0'}, 
									Builder.node('tbody')
								);
							var temp = null;
							if(next){
								pnTable.childNodes[0].appendChild(Builder.node('tr', {}, [
										Builder.node('td', {width:'6%', align:'center'}, 
											Builder.node('img', {src:'images/arrow_top.gif', alt:'', width:'9', height:'5'})
										),
										Builder.node('td', {width:'94%', 'class':'td_font9'}, 
											temp = Builder.node('a', {href:'#'}, 
												next.getAttribute('number') +'. ['+ next.getAttribute('category') +']'+ next.getAttribute('title') +' '+ next.getAttribute('create').substr(0, 10)
											)
										)
									]));

								Event.observe(temp, 'click', function(){
									doShow(next.getAttribute('id'));
								});

								$('pn_table').show();
							}

							var prev = request.responseXML.getElementsByTagName('prev')[0];
							if(prev){

								pnTable.childNodes[0].appendChild(Builder.node('tr', {}, [
										Builder.node('td', {width:'6%', align:'center'}, 
											Builder.node('img', {src:'images/arrow_bot.gif', alt:'', width:'9', height:'5'})
										),
										Builder.node('td', {width:'94%', 'class':'td_font9'}, 
											temp = Builder.node('a', {href:'#'}, 
												prev.getAttribute('number') +'. ['+ prev.getAttribute('category') +']'+ prev.getAttribute('title') +' '+ prev.getAttribute('create').substr(0, 10)
											)
										)
									]));

								Event.observe(temp, 'click', function(){
									doShow(prev.getAttribute('id'));
								});

								$('pn_table').show();
							}

							$('pn_div').appendChild(pnTable);

							$('view_box').scrollTop = 0;

						}catch(e){}

					},
					parameters:$A(objForm.elements).collect(function(item){return item.name +'='+ escape(item.value);}).join('&')
					}
					);

}