/** 
 * TripPanelSetup.js
 * 
 */
var Dom = YAHOO.util.Dom;
var Anim = YAHOO.util.Anim;
var Motion = YAHOO.util.Motion;
var Event = YAHOO.util.Event;
var DDM = YAHOO.util.DragDropMgr;

function initTripPanel() {

	//** Init UI Component.
	InitTripPanelUIApp.init();

	//** Init TripDetailApp.
	TripDetailApp.init();
	
	//** Init MapNearbyApp.
	new MapNearbyApp('trip_nearby', tripMap);

	//** Init TripScoreApp.
	TripScoreApp.init();	

	//** Init TripCommentApp.
	TripCommentApp.init();

	//** Init TripPhotoAlbumApp.
	TripPhotoAlbumApp.init();
	
	//** Init TripFuncBtnApp.
	TripFuncBtnApp.init();

}

//**********************************************
// Show Trip Detail App
//**********************************************
var tripDetailPanel;
var authorPanel;
var photoPanel;
var poiIntroPanel;

var tripMap;

var selectedTripId;

var selectedSchedulePoiId;

var trip;

var dayShowHide;

var loadingPanel;

TripDetailApp = {
	init: function() {
		//** Init Main Panel.
		tripDetailPanel = new YAHOO.widget.Panel('s_container', { 
			fixedcenter:true, 
			width:'886px', 
			height:'570px', 
			visible:false, 
			constraintoviewport:true, 
			draggable:false, 
			close:false, 
			underlay:'none', 
			modal:true/*, 
			effect:{
				effect: YAHOO.widget.ContainerEffect.SLIDE,
				duration: 0.5
			}*/
		});
		//var escKey = new YAHOO.util.KeyListener(document, { keys:27 }, { fn:tripDetailPanel.hide, scope:tripDetailPanel, correctScope:true }, 'keyup' ); 
		//tripDetailPanel.cfg.queueProperty('keylisteners', escKey);
		tripDetailPanel.render();

		Event.on('m_sub_title_closebtn', 'click', function(){
			tripDetailPanel.hide();
			poiIntroPanel.hide();
			authorPanel.hide();
		});

		Event.on(Dom.getElementsByClassName('showTripDetail'), 'click', this.showTripDetailPanel);
		//Event.on(Dom.getElementsByClassName('showTripDetail'), 'click', function() {
		//	alert("into showTripDetail");
		//	window.open('/wps/portal/imove/TripDetail?TRIP=87','_self', '');
		//});

		//** Init Trip Map.
		tripMap = new UMap(Dom.get('tripMap'));
		tripMap.addControl(U_FULLZOOM_CONTROL);
		tripMap.addControl(U_TYPE_CONTROL);
	
		//** Init Author Panel.
		authorPanel = new YAHOO.widget.Panel('m_layer_container2', {
			width:'268px', 
			height:'220px', 
			visible:false, 
			constraintoviewport:true, 
			draggable:false, 
			modal: false, 
			close:false, 
			underlay:'none', 
			context:['showAuthor', 'tl', 'bl']/*, 
			effect:{
				effect: YAHOO.widget.ContainerEffect.FADE,
				duration: 0.35
			}*/
		});		
		authorPanel.render();

		Event.on('showAuthor', 'click', function(){
			authorPanel.cfg.setProperty('context', ['showAuthor', 'tl', 'bl']);
			authorPanel.show();
			//** fix z-index bug.
			Dom.setStyle('m_layer_container2_c', 'z-index', Dom.getStyle('s_container_c', 'z-index') + 1);
		});
		Event.on('closeAuthorBtn', 'click', function(){authorPanel.hide();});
		Event.on('t_author_add_btn', 'click', function(){
			var postData = 'function=addMyFriend&friendId=' + trip.member.accountId;
			YAHOO.util.Connect.asyncRequest('POST', commonAjaxServlet, { success:TripDetailApp.successHandler_AddFriend, failure:TripDetailApp.failureHandler }, postData);
		});
		Event.on('t_author_search_btn', 'click', function(){
			Dom.get('PanelSearchTripAccountId').value = trip.member.accountId;
			Dom.get('PanelSearchTripForm').submit();
		});

		//** Init POI Intro Panel.
		poiIntroPanel = new YAHOO.widget.Panel('m_layer_container', {
			width:'342px', 
			height:'210px', 
			visible:false, 
			constraintoviewport:true, 
			draggable:false,
			//fixedcenter: true, 
			context:['m_sub_guide_left', 'tr', 'tl'], 
			//underlay:'none', 
			modal:false, 
			close:false
		});		
		poiIntroPanel.render();

		Event.on(Dom.getElementsByClassName('showPOIIntro'), 'click', function(){poiIntroPanel.show()});		
		Event.on('closePOIIntro', 'click', function(){poiIntroPanel.hide()});
		
		//** Prepare Loading Panel.
		loadingPanel = new YAHOO.widget.Panel('loadingPanel', {
			width:'240px', 
			fixedcenter: true, 
			close: false, 
			draggable: false, 
			modal: true, 
			visible: false
		});
		loadingPanel.setHeader('<b>讀取行程資料中, 請稍候...</b>');
		loadingPanel.setBody('<img src=/wps/themes/html/iMove/img/loading-L.gif />');
		loadingPanel.render(document.body);
		
		//** Print Trip
		Event.on('printTrip', 'click', function(){
			window.open(PortletContextPath + '/jsp/TripPrint.jsp?tripId=' + selectedTripId, 'TripPrintWindow', 'resizable=yes,scrollbars=yes,menubar=no,toolbar=no,status=no,width=700px,height=600px,screenX=10px,screenY=10px,top=10px,left=10px');
		});
		
		//** Switch Trip Tab
		Event.on('switchTripTabComment', 'click', function(){
			subTabView.set('activeIndex', 2);
		});

	},
	
	showTripDetailPanel: function(e) {
		alert("TripPanelSetup.js showTripDetailPanel()");
		//loadingPanel.show();		
		//var target = YAHOO.util.Event.getTarget(e);
		//selectedTripId = target.id.substring(5);
		//var postData = 'function=getTrip&tripId=' + selectedTripId;
		//YAHOO.util.Connect.asyncRequest('POST', ajaxServlet, { success:TripDetailApp.successHandler, failure:TripDetailApp.failureHandler }, postData);
	},
	
	show: function(tripId) {
		loadingPanel.show();
		
		selectedTripId = tripId;
		var postData = 'function=getTrip&tripId=' + selectedTripId;
		YAHOO.util.Connect.asyncRequest('POST', ajaxServlet, { success:TripDetailApp.successHandler, failure:TripDetailApp.failureHandler }, postData);
	},
	
	successHandler_AddFriend: function(o){
		if(o.responseText !== undefined){
			if (o.responseText == 'OK') {
				Dom.get('mymove_msg_text').innerHTML = '請求已送出!';
				AlertMsgPanel.show();
				Dom.setStyle('trip_author_add_friend', 'display', 'none');
			}
			else {
				alert('Add Friend Faile !');
			}
		}
	},
	
	successHandler: function(o){
		if(o.responseText !== undefined){

			trip = eval('(' + o.responseText + ')');
			
			//** Reset Score.
			TripScoreApp.reset(trip);
			
			//** Reset Comment.
			TripCommentApp.reset();
			//** Reset TabView.
			subTabView.set('activeIndex', 0, true);

			//** Compose IMG.
			var titleImgSrc = '/wps/themes/html/iMove/img/photo1.gif';
			if (trip.photoFile) {
				titleImgSrc = GetFileServlet + '?ACTION=VIEWRALBUM&FILENAME=' + trip.photoFile.fileName + '&SOURCEDIR=' + trip.photoFile.destDir;
			}
			Dom.get('titleImg').src = titleImgSrc;
			
			//** show icon.
			Dom.setStyle('icon_1', 'display', '');
			Dom.setStyle('icon_2', 'display', '');
			Dom.setStyle('icon_3', 'display', '');
			Dom.setStyle('icon_4', 'display', '');
			Dom.setStyle('icon_5', 'display', '');
			Dom.setStyle('icon_6', 'display', '');
			if (!(trip.isHotTrip)) Dom.setStyle('icon_1', 'display', 'none');
			if (!(trip.isFestivalTrip)) Dom.setStyle('icon_2', 'display', 'none');
			if (!(trip.isThemeTrip)) Dom.setStyle('icon_3', 'display', 'none');
			if (!(trip.isExpertTrip)) Dom.setStyle('icon_4', 'display', 'none');
			if (!(trip.isNvTrip)) Dom.setStyle('icon_5', 'display', 'none');
			if (!(trip.isEditorsChoice)) Dom.setStyle('icon_6', 'display', 'none');

			Dom.getElementsByClassName('m_red_text3', 'div', 'm_trip_leftbar_top_title')[0].innerHTML = trip.title;
			Dom.get('showDuration').innerHTML = trip.duration;
			Dom.get('showTag').alt = trip.tag;
			Dom.get('showTag').title = trip.tag;
			//mark by johnson on 2009/04/07
			//if (trip.member.accountId == 'imadmin1') {
			//	Dom.get('showAuthor').innerHTML = '行遍天下';			
			//}
			//else {
			//	Dom.get('showAuthor').innerHTML = trip.member.accountId;
			//}
			
			Dom.get('showAuthor').innerHTML = trip.author;
			Dom.get('tripIntroInPanel').innerHTML = trip.introduction;
			Dom.get('showCreateTime').innerHTML = trip.createdTime.substring(0, 10);
			Dom.get('showCommentCount').innerHTML = trip.commentCount;
			
			//** Score.
			Dom.get('trip_p_scoreAvg').src = '/wps/themes/html/iMove/img/star_' + Math.round(trip.scoreAvg) + '.gif';
			Dom.get('trip_p_scoreCount').innerHTML = trip.scoreCount;

			//** Journal.
			Dom.get('m_sub_diary').innerHTML = trip.journal;

			//** Comments.
			var comments = trip.comments;
			TripCommentApp.fillList(comments);

			//** Init Map point.
			tripMap.clearOverlays();
			if (trip.schedule[0][0]) {
				if (trip.schedule[0][0].type == 1) {
					var point = new ULatLng(trip.schedule[0][0].poi.latitude, trip.schedule[0][0].poi.longitude);
					tripMap.centerAndZoom(point, 6);
				}
				if (trip.schedule[0][0].type == 2) {
					var point = new ULatLng(trip.schedule[0][0].customPoi.latitude, trip.schedule[0][0].customPoi.longitude);
					tripMap.centerAndZoom(point, 6);
				}
			}
			
			//** Schedule.
			TripDetailApp.resetSchedule();

			dayShowHide = new Array(trip.schedule.length);
			
			for (var i = 0; i < trip.schedule.length; i++) {
				TripDetailApp.appendDayItem(i+1);
				TripDetailApp.appendPoiItem(i+1, trip.schedule[i]);
				dayShowHide[i] = true;
			}

			Event.on(Dom.getElementsByClassName('day_show_hide_btn'), 'click', function(e){
				var target = YAHOO.util.Event.getTarget(e);
				var dayIdx = target.id.substring(7, 8);

				var showHideItems = Dom.get('poiItems'+dayIdx).getElementsByTagName('p');
				
				if (dayShowHide[dayIdx-1]) {
					for (var i=0; i<showHideItems.length; i++) Dom.setStyle(showHideItems[i], 'display', 'none');
					//Dom.setStyle('poiItems'+dayIdx, 'display', 'none');
					target.src = '/wps/themes/html/iMove/img/sub_open.gif';
					dayShowHide[dayIdx-1] = false;
				}
				else {
					for (var i=0; i<showHideItems.length; i++) Dom.setStyle(showHideItems[i], 'display', '');
					//Dom.setStyle('poiItems'+dayIdx, 'display', '');
					target.src = '/wps/themes/html/iMove/img/sub_close.gif';
					dayShowHide[dayIdx-1] = true;
				}
			});
			
			//** Author Panel.
			Dom.get('author_panel_accountId').innerHTML=trip.author;
			
			//mark by Johnson on 2009/04/07
			//if (trip.member.accountId == 'imadmin1') {
			//	Dom.get('author_panel_accountId').innerHTML = '行遍天下';			
			//}
			//else {
			//	Dom.get('author_panel_accountId').innerHTML = trip.member.accountId;
			//}
			Dom.get('author_panel_expLevel').innerHTML = trip.member.expLevel;
			var authorIconSrc = '/wps/themes/html/iMove/img/mem_icon.gif';
			if (trip.member.photoFile) {
				authorIconSrc = GetFileServlet + '?ACTION=VIEWCALBUM&FILENAME=' + trip.member.photoFile.fileName + '&SOURCEDIR=' + trip.member.photoFile.destDir;
			}
			Dom.get('author_panel_icon').src = authorIconSrc;
			Dom.get('author_panel_shareTripCount').innerHTML = trip.member.shareTripCount;
			Dom.get('author_panel_editorChoiceTripCount').innerHTML = trip.member.editorChoiceTripCount;
			Dom.get('author_panel_expPoints').innerHTML = trip.member.expPoints;
			if (trip.isfriend) {
				Dom.setStyle('trip_author_add_friend', 'display', 'none');
			}
			else {
				Dom.setStyle('trip_author_add_friend', 'display', '');
			}
			
			//** Photo Viewer Panel.
			if (trip.photoAlbum) {
				TripPhotoAlbumApp.load(trip.photoAlbum);
			}
			else {
				TripPhotoAlbumApp.reset();
			}

			loadingPanel.hide();
			tripDetailPanel.show();
			
		}
	},

	failureHandler: function(o){
		loadingPanel.hide();
		alert('AJAX Connection Failure !!');
	}, 
	
	appendDayItem: function(dayIdx) {
		var baseEl = document.createElement('div');
		baseEl.className = 'm_day_list_item1';

		var leftEl = document.createElement('div');
		leftEl.className = 'm_day_list_item1_left';
		var dayEl = document.createElement('b');
		dayEl.className = 'm_red_text2';
		dayEl.innerHTML = 'Day ' + dayIdx + '.';
		leftEl.appendChild(dayEl);

		var btnEl = document.createElement('div');
		btnEl.className = 'm_day_list_item1_btn';
		var imgEl = document.createElement('img');
		imgEl.id = 'dayItem' + dayIdx;
		imgEl.className = 'day_show_hide_btn';
		imgEl.src = '/wps/themes/html/iMove/img/sub_close.gif';
		imgEl.width = '14';
		imgEl.height = '15';
		imgEl.border = '0';
		Dom.setStyle(imgEl, 'cursor', 'pointer');
		btnEl.appendChild(imgEl);

		baseEl.appendChild(leftEl);
		baseEl.appendChild(btnEl);

		Dom.get('m_day_list').appendChild(baseEl);
	},
	
	appendPoiItem: function(dayIdx, pois) {
		var baseEl = document.createElement('div');
		baseEl.id = 'poiItems' + dayIdx;
		baseEl.className = 'm_day_list_item1_ctn m_clearfix';

		for (var i = 0; i < pois.length; i++) {
			var pEl = document.createElement('p');
			
			var imgEl = document.createElement('img');
			imgEl.width = '19';
			imgEl.height = '19';
			imgEl.align = 'top';
			
			var aEl = document.createElement('a');
			aEl.id = 'd' + (dayIdx-1) + 'p' + i;
			aEl.className = 'showPOIIntro';
			aEl.href = 'javascript://';
			Dom.setStyle(aEl, 'cursor', 'pointer');

			if (pois[i].type == 1) {
				if (pois[i].poi.buy) imgEl.src = '/wps/themes/html/iMove/img/shop.gif';
				if (pois[i].poi.eat) imgEl.src = '/wps/themes/html/iMove/img/eat.gif';
				if (pois[i].poi.live) imgEl.src = '/wps/themes/html/iMove/img/stay.gif';
				if (pois[i].poi.travel) imgEl.src = '/wps/themes/html/iMove/img/play.gif';
				aEl.innerHTML = pois[i].poi.display;
			}
			if (pois[i].type == 2) {
				imgEl.src = '/wps/themes/html/iMove/img/custom.gif';
				aEl.innerHTML = pois[i].customPoi.name;
			}
			
			pEl.appendChild(imgEl);
			pEl.appendChild(aEl);

			baseEl.appendChild(pEl);

			//** Draw poi point on map.
			if (pois[i].type == 1) {
				var point = new ULatLng(pois[i].poi.latitude, pois[i].poi.longitude);
				var catIcon = new UIcon();
				if (pois[i].poi.buy) catIcon.image = '/wps/themes/html/iMove/img/urmap_shop.gif';
				if (pois[i].poi.eat) catIcon.image = '/wps/themes/html/iMove/img/urmap_eat.gif';
				if (pois[i].poi.live) catIcon.image = '/wps/themes/html/iMove/img/urmap_stay.gif';
				if (pois[i].poi.travel) catIcon.image = '/wps/themes/html/iMove/img/urmap_play.gif';
				catIcon.iconWidth = 19;
				catIcon.iconHeight = 19;
				catIcon.iconAnchor = new UPoint(9, 9);
				catIcon.infoWindowAnchor = new UPoint(9, 0);
	
				var marker = new UMarker(point, catIcon, pois[i].poi.display + '(' + pois[i].poi.webcatpois[0].webcat.catmst.catName + ')', pois[i].poi.introduction.substring(0, 20)+'...');
				tripMap.addOverlay(marker);
			}
			if (pois[i].type == 2) {
				var point = new ULatLng(pois[i].customPoi.latitude, pois[i].customPoi.longitude);
				var catIcon = new UIcon();
				catIcon.image = '/wps/themes/html/iMove/img/urmap_custom.gif';
				catIcon.iconWidth = 19;
				catIcon.iconHeight = 19;
				catIcon.iconAnchor = new UPoint(9, 9);
				catIcon.infoWindowAnchor = new UPoint(9, 0);
	
				var marker = new UMarker(point, catIcon, pois[i].customPoi.name, pois[i].customPoi.introduction);
				tripMap.addOverlay(marker);
			}

			//** Event Control.
			Event.on(aEl, 'click', function(e){
				var target = YAHOO.util.Event.getTarget(e);
				var dIdx = target.id.indexOf('d');
				var pIdx = target.id.indexOf('p');
				var d = target.id.substring(dIdx + 1, pIdx);
				var p = target.id.substring(pIdx + 1);
				
				var sPoi = trip.schedule[d][p];
				
				//** POI Intro Panel.
				if (sPoi.type == 1) {
					Dom.setStyle('poi_p_info', 'visibility', '');
					Dom.setStyle('poi_p_action', 'visibility', '');
					Dom.setStyle('custom_info', 'display', 'none');
					
					Dom.get('poi_p_title').innerHTML = sPoi.poi.display;
					Dom.get('poi_p_scoreAvg').src = '/wps/themes/html/iMove/img/star_' + Math.round(sPoi.poi.scoreAvg) + '.gif';
					Dom.get('poi_p_scoreCount').innerHTML = sPoi.poi.scoreCount;
					Dom.get('poi_p_intro').innerHTML = sPoi.poi.introduction;
	
					var titleImgSrc = '/wps/themes/html/iMove/img/photo1.gif';
					if (sPoi.poi.storePic) {
						titleImgSrc = GetFileServlet + '?ACTION=VIEWM&FILENAME=' + sPoi.poi.storePic.picName + '&SOURCEDIR=' + sPoi.poi.storePic.picDest;
					}
					Dom.get('poi_p_img').src = titleImgSrc;
					
					//** move map.
					selectedSchedulePoiId = sPoi.poi.poiId;
					var point = new ULatLng(sPoi.poi.latitude, sPoi.poi.longitude);
					tripMap.centerMap(point);
				}
				if (sPoi.type == 2) {
					Dom.setStyle('poi_p_info', 'visibility', 'hidden');
					Dom.setStyle('poi_p_action', 'visibility', 'hidden');
					Dom.setStyle('custom_info', 'display', '');
					
					Dom.get('poi_p_title').innerHTML = sPoi.customPoi.name;
					Dom.get('poi_p_intro').innerHTML = sPoi.customPoi.introduction;
					Dom.get('poi_p_img').src = '/wps/themes/html/iMove/img/photo1.gif';
					Dom.get('custom_info_phone').innerHTML = '電話:' + sPoi.customPoi.phone;
					Dom.get('custom_info_address').innerHTML = '地址:' + sPoi.customPoi.address;
					Dom.get('custom_info_web').innerHTML = '網址:' + sPoi.customPoi.web;
					
					//** move map.
					var point = new ULatLng(sPoi.customPoi.latitude, sPoi.customPoi.longitude);
					tripMap.centerMap(point);
				}

				poiIntroPanel.cfg.setProperty('context', [target.parentNode, 'tr', 'tl']);
				poiIntroPanel.show();
				
				//** fix z-index bug.
				Dom.setStyle('m_layer_container_c', 'z-index', Dom.getStyle('s_container_c', 'z-index') + 1);
			});
		}

		Dom.get('m_day_list').appendChild(baseEl);
	},
	
	resetSchedule: function() {
		while (Dom.get('m_day_list').hasChildNodes()) {
			Dom.get('m_day_list').removeChild(Dom.get('m_day_list').firstChild);
		}
	}
};

//**********************************************
// Comment App
//**********************************************
TripCommentApp = {
	init: function() {
		Event.on('sent_btn', 'click', this.addComment);
		Event.on('cancel_btn', 'click', function(){
			Dom.get('textarea').value = '';
		});
	},

	fillList: function(comments) {

		Dom.get('m_sub_dis_list_container').innerHTML = '';
		
		for (var i=0; i<comments.length; i++) {
			var comment = comments[i];
			TripCommentApp.appendBrick(comment);
		}
	},

	appendBrick: function(comment) {
		if (comment.status == 2) return false;
		
		var baseEl = document.createElement('div');
		baseEl.className = 'm_sub_dis_list';

		var leftEl = document.createElement('div');
		leftEl.className = 'm_sub_dis_list_left';
		var imgEl = document.createElement('img');
		var authorIconSrc = '/wps/themes/html/iMove/img/mem_icon.gif';
		if (comment.member.photoFile) {
			authorIconSrc = GetFileServlet + '?ACTION=VIEWCALBUM&FILENAME=' + comment.member.photoFile.fileName + '&SOURCEDIR=' + comment.member.photoFile.destDir;
		}
		imgEl.src = authorIconSrc;
		imgEl.width = '60';
		imgEl.height = '60';
		leftEl.appendChild(imgEl);

		var rightEl = document.createElement('div');
		rightEl.className = 'm_sub_dis_list_right';
		//** compose author info.
		var infoEl = document.createElement('p');
		var memberEl = document.createElement('b');
		memberEl.className = 'm_red_text3';
		Dom.setStyle(memberEl, 'margin-right', '5px');
		if (comment.member.accountId == 'imadmin1') {
			memberEl.innerHTML = '行遍天下';			
		}
		else {
			memberEl.innerHTML = comment.member.accountId;
		}
		
		var fontEl = document.createElement('font');
		fontEl.color = '#C97050';
		var timeEl = document.createTextNode('於' + comment.createdTime.substring(0, 19) + ' 回應:');
		fontEl.appendChild(timeEl);
		infoEl.appendChild(memberEl);
		infoEl.appendChild(fontEl);
		
		//** Delete Event.
		/*
		var delEl = document.createElement('input');
		delEl.value = 'DELETE';
		delEl.type = 'button';
		Event.on(delEl, 'click', function(){
			var postData = 'function=deleteComment&tripId=' + selectedTripId + '&tripCommentId=' + comment.id;
			YAHOO.util.Connect.asyncRequest('POST', ajaxServlet, { success:TripCommentApp.successHandler_Del, failure:TripCommentApp.failureHandler }, postData);
		});
		infoEl.appendChild(delEl);
		*/
		
		//** compose content.
		var contentEl = document.createElement('p');
		contentEl.innerHTML = comment.content;
		//** compose right block
		rightEl.appendChild(infoEl);
		rightEl.appendChild(contentEl);

		baseEl.appendChild(leftEl);
		baseEl.appendChild(rightEl);

		Dom.get('m_sub_dis_list_container').appendChild(baseEl);
	},
	
	reset: function() {
		if (Dom.get('m_sub_message_container'))	{
			Dom.get('textarea').value = '';
			Dom.setStyle('sent_btn', 'display', '');
			Dom.setStyle('cancel_btn', 'display', '');
			Dom.setStyle('commentWait', 'display', 'none');
		}
	},
	
	addComment: function() {
		Dom.setStyle('sent_btn', 'display', 'none');
		Dom.setStyle('cancel_btn', 'display', 'none');
		Dom.setStyle('commentWait', 'display', '');

		var content = Dom.get('textarea').value;
		if (content.length < 10) {
			Dom.get('mymove_msg_text').innerHTML = '請輸入超過10字!';
			AlertMsgPanel.show();
			TripCommentApp.reset();
			Dom.get('textarea').value = content;
			return false;
		}
		if (content.length > 300) {
			Dom.get('mymove_msg_text').innerHTML = '請輸入少於300字!';
			AlertMsgPanel.show();
			TripCommentApp.reset();
			Dom.get('textarea').value = content;
			return false;
		}
		var postData = 'function=addComment&tripId=' + selectedTripId + '&content=' + encodeURI(content);
		YAHOO.util.Connect.asyncRequest('POST', ajaxServlet, { success:TripCommentApp.successHandler, failure:TripCommentApp.failureHandler }, postData);
	},
	
	successHandler: function(o){
		Dom.get('mymove_msg_text').innerHTML = '新增回應完成!';
		AlertMsgPanel.show();
		TripCommentApp.reset();
		if(o.responseText !== undefined){
			var json = eval('(' + o.responseText + ')');
			TripCommentApp.fillList(json.comments);
		}
	},
	
	successHandler_Del: function(o){
		//Dom.get('mymove_msg_text').innerHTML = '刪除回應完成!';
		//AlertMsgPanel.show();
		alert('刪除回應完成!');
		TripCommentApp.reset();
		if(o.responseText !== undefined){
			var json = eval('(' + o.responseText + ')');
			TripCommentApp.fillList(json.comments);
		}
	},

	failureHandler: function(o){
		alert('AJAX Connection Failure !!');
	}
};


//**********************************************
// Score App
//**********************************************
var selectedScore;

TripScoreApp = {
	init: function() {
		Event.on(Dom.getElementsByClassName('scoreIcon'), 'click', function(e){
			var target = Event.getTarget(e);
			selectedScore = target.id.substring(5);
			for (var i=1; i<=5; i++) {
				if (i<=selectedScore) Dom.get('score'+i).src = '/wps/themes/html/iMove/img/star_on.gif';
				else Dom.get('score'+i).src = '/wps/themes/html/iMove/img/star_off.gif';
			}
			Dom.setStyle('scoreConfirm', 'display', '');
		});
		Event.on('scoreConfirm', 'click', this.addScore);
	},
	
	reset: function(trip) {
		if (Dom.get('addScoreArea')) {
			Dom.setStyle('scoreConfirm', 'display', 'none');
			Dom.setStyle('scoreWait', 'display', 'none');
			for (var i=1; i<=5; i++) Dom.get('score'+i).src = '/wps/themes/html/iMove/img/star_off.gif';
			if (trip.isScored) {
				Dom.setStyle('addScoreArea', 'display', 'none');
				Dom.setStyle('tripAddScoreMsg', 'display', '');
			}
			else {
				Dom.setStyle('addScoreArea', 'display', '');
				Dom.setStyle('tripAddScoreMsg', 'display', 'none');
			}
		}
	},
	
	addScore: function() {
		
		Dom.setStyle('scoreConfirm', 'display', 'none');
		Dom.setStyle('scoreWait', 'display', '');		
		var postData = 'function=addScore&tripId=' + selectedTripId + '&score=' + selectedScore;
		YAHOO.util.Connect.asyncRequest('POST', ajaxServlet, { success:TripScoreApp.successHandler, failure:TripScoreApp.failureHandler }, postData);
	},
	
	successHandler: function(o){
		if(o.responseText !== undefined){
			Dom.setStyle('addScoreArea', 'display', 'none');
			Dom.setStyle('tripAddScoreMsg', 'display', '');
			
			var newScore = ((trip.scoreAvg * trip.scoreCount) + parseInt(selectedScore))/(trip.scoreCount + 1);
			Dom.get('trip_p_scoreAvg').src = '/wps/themes/html/iMove/img/star_' + Math.round(newScore) + '.gif';
			Dom.get('trip_p_scoreCount').innerHTML = trip.scoreCount + 1;
		}
		Dom.get('mymove_msg_text').innerHTML = '評分完成!';
		AlertMsgPanel.show();
	},

	failureHandler: function(o){
		alert('TripScoreApp AJAX Connection Failure !!');
	}
};

//**********************************************
// Trip Photo Album App
//**********************************************
var tripPhotos;
var tripPhotosPerPage = 3;
var tripPhotosAllPage = 1;
var tripPhotosNavPage = 1;

var s_tripPhotosPerPage = 2;
var s_tripPhotosAllPage = 1;
var s_tripPhotosNavPage = 1;

TripPhotoAlbumApp = {
	init: function() {
		//** Init Photo Viewer.
		photoPanel = new YAHOO.widget.Panel('viewer_container', {
			width:'486px', 
			height:'580px', 
			visible:false, 
			constraintoviewport:true, 
			draggable:false,
			fixedcenter: true, 
			modal:true, 
			close:false/*, 
			underlay:'none', 
			effect:{
				effect: YAHOO.widget.ContainerEffect.FADE,
				duration: 0.35
			}*/
		});		
		photoPanel.render();

		Event.on('showPhoto', 'click', function(){photoPanel.show();});
		Event.on('m_view_title_closebtn', 'click', function(){photoPanel.hide();});

		Event.on('t_p_p_nav_previous', 'click', function(){
			if (tripPhotosNavPage == 1) {
				alert('已到第一頁!');
				return false;
			}
			tripPhotosNavPage = tripPhotosNavPage - 1;
			TripPhotoAlbumApp.movePage(tripPhotosNavPage);
		});

		Event.on('t_p_p_nav_next', 'click', function(){
			if (tripPhotosNavPage == tripPhotosAllPage) {
				alert('已到最後一頁!');
				return false;
			}
			tripPhotosNavPage = tripPhotosNavPage + 1;
			TripPhotoAlbumApp.movePage(tripPhotosNavPage);
		});

		Event.on(Dom.getElementsByClassName('t_p_p_chgImg'), 'click', function(e){
			var target = YAHOO.util.Event.getTarget(e);
			var brickSeq = parseInt(target.id.substring(12));
			var photoIdx = (tripPhotosNavPage - 1) * tripPhotosPerPage + brickSeq;
			Dom.get('t_p_p_showCurrentPhoto').src = GetFileServlet + '?ACTION=VIEWCALBUM&FILENAME=' + tripPhotos[photoIdx].fileName + '&SOURCEDIR=' + tripPhotos[photoIdx].destDir;
			Dom.get('t_p_p_showCurrentIntro').innerHTML = tripPhotos[photoIdx].description;
		});

		Event.on('trip_morephoto_left', 'click', function(){
			if (s_tripPhotosNavPage == 1) {
				alert('已到第一頁!');
				return false;
			}
			s_tripPhotosNavPage = s_tripPhotosNavPage - 1;
			TripPhotoAlbumApp.s_movePage(s_tripPhotosNavPage);
		});

		Event.on('trip_morephoto_right', 'click', function(){
			if (s_tripPhotosNavPage == s_tripPhotosAllPage) {
				alert('已到最後一頁!');
				return false;
			}
			s_tripPhotosNavPage = s_tripPhotosNavPage + 1;
			TripPhotoAlbumApp.s_movePage(s_tripPhotosNavPage);
		});

		Event.on(Dom.getElementsByClassName('trip_p_s_zoom_img'), 'click', function(e){
			var target = YAHOO.util.Event.getTarget(e);
			var brickSeq = parseInt(target.id.substring(12));
			var photoIdx = (s_tripPhotosNavPage - 1) * s_tripPhotosPerPage + brickSeq;
			Dom.get('t_p_p_showCurrentPhoto').src = GetFileServlet + '?ACTION=VIEWCALBUM&FILENAME=' + tripPhotos[photoIdx].fileName + '&SOURCEDIR=' + tripPhotos[photoIdx].destDir;
			Dom.get('t_p_p_showCurrentIntro').innerHTML = tripPhotos[photoIdx].description;

			//** Change NAV Photo Bar.
			tripPhotosNavPage = parseInt(photoIdx/tripPhotosPerPage) + 1;
			TripPhotoAlbumApp.movePage(tripPhotosNavPage);
			photoPanel.show();
		});

	},

	load: function(photoAlbum) {

		tripPhotos = photoAlbum.photoFiles;

		var photosCount = tripPhotos.length;

		if ((photosCount%tripPhotosPerPage) == 0) tripPhotosAllPage = photosCount/tripPhotosPerPage;
		else tripPhotosAllPage = parseInt(photosCount/tripPhotosPerPage) + 1;
		
		Dom.get('m_view_title_left').innerHTML = photoAlbum.albumName;

		Dom.setStyle('t_p_p_showCurrentPhoto', 'visibility', '');
		Dom.get('t_p_p_showCurrentPhoto').src = GetFileServlet + '?ACTION=VIEWCALBUM&FILENAME=' + tripPhotos[0].fileName + '&SOURCEDIR=' + tripPhotos[0].destDir;
		Dom.get('t_p_p_showCurrentIntro').innerHTML = tripPhotos[0].description;

		tripPhotosNavPage = 1;
		TripPhotoAlbumApp.movePage(tripPhotosNavPage);

		//** for small photo icon slider.
		if ((photosCount%s_tripPhotosPerPage) == 0) s_tripPhotosAllPage = photosCount/s_tripPhotosPerPage;
		else s_tripPhotosAllPage = parseInt(photosCount/s_tripPhotosPerPage) + 1;

		s_tripPhotosNavPage = 1;
		TripPhotoAlbumApp.s_movePage(s_tripPhotosNavPage);
	},
	
	movePage: function(page) {

		var startIdx = (page - 1) * tripPhotosPerPage;
		var brickIdx = 0;
		for (var i = startIdx; i < startIdx + tripPhotosPerPage; i++) {
			Dom.setStyle('t_p_p_navPhoto' + brickIdx, 'display', 'none');
			if (i < tripPhotos.length) {
				Dom.get('t_p_p_navImg' + brickIdx).src = GetFileServlet + '?ACTION=VIEWRALBUM&FILENAME=' + tripPhotos[i].fileName + '&SOURCEDIR=' + tripPhotos[i].destDir;
				Dom.setStyle('t_p_p_navPhoto' + brickIdx, 'display', '');
			}
			brickIdx++;
		}

	},

	s_movePage: function(page) {

		var startIdx = (page - 1) * s_tripPhotosPerPage;
		var brickIdx = 0;
		for (var i = startIdx; i < startIdx + s_tripPhotosPerPage; i++) {
			Dom.setStyle(Dom.get('trip_p_s_img' + brickIdx).parentNode, 'visibility', 'hidden');
			if (i < tripPhotos.length) {
				Dom.get('trip_p_s_img' + brickIdx).src = GetFileServlet + '?ACTION=VIEWRALBUM&FILENAME=' + tripPhotos[i].fileName + '&SOURCEDIR=' + tripPhotos[i].destDir;
				Dom.setStyle(Dom.get('trip_p_s_img' + brickIdx).parentNode, 'visibility', '');
			}
			brickIdx++;
		}

	},

	reset: function() {
		Dom.get('m_view_title_left').innerHTML = '目前無關連相簿';
		Dom.setStyle('t_p_p_showCurrentPhoto', 'visibility', 'hidden');
		Dom.get('t_p_p_showCurrentIntro').innerHTML = '';
		Dom.setStyle('t_p_p_navPhoto0', 'display', 'none');
		Dom.setStyle('t_p_p_navPhoto1', 'display', 'none');
		Dom.setStyle('t_p_p_navPhoto2', 'display', 'none');
		tripPhotosAllPage = 1;
		tripPhotosNavPage = 1;

		Dom.setStyle(Dom.get('trip_p_s_img0').parentNode, 'visibility', 'hidden');
		Dom.setStyle(Dom.get('trip_p_s_img1').parentNode, 'visibility', 'hidden');
		s_tripPhotosAllPage = 1;
		s_tripPhotosNavPage = 1;
	}
	
};


YAHOO.util.Event.onDOMReady(initTripPanel);
