/** 
 * ExpertTrip.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 init() {

	//** Init UI Component.
	InitUIApp.init();
	
	//** Init memu component.
	TripFuncBtnApp.init();
	
	//** Init DD Source Object.
	TripDDSourceApp.init('m_fcs_pphoto');
	TripDDSourceApp.init('m_ts_brk_photo');

}

//**********************************************
// Init UI Component
//**********************************************
var expNavPage = 1;
var expNavPageAll = 1;
var exps;
var perPage = 6;

InitUIApp = {
	init: function() {

		exps = Dom.getElementsByClassName('showExpert');
		var expsCount = exps.length;	

		if ((expsCount%perPage) == 0) expNavPageAll = expsCount/perPage;
		else expNavPageAll = parseInt(expsCount/perPage) + 1;

		InitUIApp.moveExpertPage(expNavPage);
		
		Event.on('moveExpertPrevious', 'click', function(){
			if (expNavPage == 1) {
				alert('已到第一筆資料!');
				return false;
			}
			expNavPage = expNavPage - 1;
			InitUIApp.moveExpertPage(expNavPage);
		});

		Event.on('moveExpertNext', 'click', function(){
			if (expNavPage == expNavPageAll) {
				alert('已到最後一筆資料!');
				return false;
			}
			expNavPage = expNavPage + 1;
			InitUIApp.moveExpertPage(expNavPage);
		});
		
		Event.on(Dom.getElementsByClassName('showTripDetail'), 'click', function(e) {
			var isLoginUser=Dom.get('ExpertTripForm').isLoginUser.value;
			var target = YAHOO.util.Event.getTarget(e);
			selectedTripId = target.id.substring(5);
			
			if (isLoginUser=='F')
				window.open('/wps/portal/imove/TripDetail?TRIP='+selectedTripId,'_self', '');
			else 
				window.open('/wps/myportal/imove/TripDetail?TRIP='+selectedTripId,'_self', '');
		});

	},
	
	moveExpertPage: function(goPage) {
		
		for (var i = 0; i < exps.length; i++) {
			Dom.setStyle(exps[i], 'display', 'none');
		}

		var startIdx = (goPage - 1) * perPage;
		for (var i = startIdx; i < startIdx + perPage; i++) {
			if (i < exps.length) Dom.setStyle(exps[i], 'display', '');
		}

	},
	
	changePage: function(targetPage) {
		Dom.get('ExpertTripForm').currentPage.value = targetPage;
		Dom.get('ExpertTripForm').submit();
	},
	
	changeExpert: function(expertId) {
		Dom.get('ExpertTripForm').command.value = 'select';
		Dom.get('ExpertTripForm').expertId.value = expertId;
		Dom.get('ExpertTripForm').currentPage.value = '1';
		Dom.get('ExpertTripForm').submit();
	}
};
