// --------------------------------
// 下拉出團日期
// --------------------------------
$('.tourdate-selector-toggle').toggle(function() {
	$(this).text('關閉');
	$(this).parent().parent().addClass('active');
}, function(){
	$(this).text('更多出團日期');
	$(this).parent().parent().removeClass('active');
});

// --------------------------------
// 設定字級 
// --------------------------------
var tripfontsize = readCookie('EURA_trip_fontsize');
if(tripfontsize==null) 
		tripfontsize = 1;
setTripFontsize(tripfontsize);
$('#font-setting a').click(function() {
	setTripFontsize($('#font-setting a').index(this) + 1);
})

function setTripFontsize(s)
{
	$('.trip-detail-cont').removeClass('fontsize1 fontsize2 fontsize3').addClass('fontsize'+s);
	$('#font-setting a').eq(s-1).addClass('on').siblings().removeClass('on');
	createCookie('EURA_trip_fontsize',s,0);
}

// --------------------------------
// DIV#trip-showcase 切換TAB
// --------------------------------
SWFAddress.addEventListener('change', onSWFAddressChange);
//$('#trip-showcase').find('.tabview-cont-wrap > div:not(:eq(0))').css('display','none');
$('#trip-showcase').find('.tabview-menu a').click(toggleShowcaseTab);

function toggleShowcaseTab(e)
{
	// hilite button
	$(this).parent().siblings().removeClass('on').end().addClass('on'); 
	// show content
	var tar = $('#trip-'+$(this).attr('href').substr(1) )
	var type = ($(this).attr('href').substr(1));
	switch(type)
	{
		case 'map':
			//
			initGMap();
		break;
	}
	tar.show().siblings().hide();
	//tar.parent().animate({height: tar.height()}, 600);
	tar.parent().css({height: tar.height() });
}

function onSWFAddressChange(e) 
{
	if(e.value=="/"||e.value=="")
		$('#trip-showcase').find('.tabview-menu a:first').click();
	else
		$('#trip-showcase').find('.tabview-menu a[href="#'+ e.value +'"]').click();
	//alert(e.value)
}

// --------------------------------
// GallerySlider
// --------------------------------
var GallerySlider = function(){
	var S = {
		slider:null, slides:null,	gRight:null, gLeft:null,	legends:null,	idname:null, cookieName:null,	sIndex:0,	sTotal:0,
		
		setup: function( elem )
		{
				S.slider = elem;
				//alert('setup() S.slider: '+ S.slider);
				S.slides = elem.find('.slide');
				S.legends = elem.find('.legend .cont');
				S.bNext = elem.find('.goright');
				S.bPrev = elem.find('.goleft');
				S.idname = elem[0].id;
				S.cookieName = 'EURA_GS_'+ S.idname;
				S.init();
		},
		
		init: function()
		{
			var index = readCookie(S.cookieName +'_sIndex');
			S.sIndex = index==null ? 0 : parseInt(index);
			//S.sIndex = 0;
			
			var ary = S.legends.find('>li').get().reverse();
			if(ary.length>0) {
				S.sTotal = ary.length;
			} else {
				//alert('idname: ' + S.idname + ' 沒內容');
				return;
			}
			S.legends.empty();
			$.each(ary, function(i) {
				S.legends.append('<li>' +ary[i].innerHTML + '</li>');
			});
			S.bNext.click(S.goNext);
			S.bPrev.click(S.goPrev);
			S.slides.css('top',-S.sIndex*240);
			S.legends.css('top',-(S.sTotal-S.sIndex-1)*76);
			S.checkNav();
			//	alert('idname: ' + S.idname+ '  index: '+S.sIndex + ' total: ' + S.sTotal);
		},
		
		goNext: function()
		{
			if(S.sIndex<S.sTotal-1) {
				S.sIndex++;
				S.slides.animate({top:-S.sIndex*240}, 700);
				S.legends.animate({top:-(S.sTotal-S.sIndex-1)*76}, 500);
				createCookie(S.cookieName +'_sIndex', S.sIndex, 0);
			//	alert('goNext() index: '+S.sIndex);
			} else {}
			S.checkNav();
		},
		
		goPrev: function()
		{
			if(S.sIndex>0) {
				S.sIndex--;
				//alert(-S.sIndex*240)
				S.slides.animate({top:-S.sIndex*240}, 700);
				S.legends.animate({top:-(S.sTotal-S.sIndex-1)*76}, 500);
				createCookie(S.cookieName +'_sIndex',S.sIndex,0);	
			//	alert('goPrev() index: '+S.sIndex);
			} else {}
			S.checkNav();
		},
		
		checkNav: function()
		{
			if(S.sIndex==0)
				S.bPrev.css('opacity',.25);
			else
				S.bPrev.css('opacity',1);
			if(S.sIndex==S.sTotal-1)
				S.bNext.css('opacity',.25);
			else
				S.bNext.css('opacity',1);
		}
	}
	return S;
}

$('.gallery-slider').each(function() {
	GallerySlider().setup( $(this) )
})
