var left_var = 0;
var right_frame_limit;
var gblIntID = '';
var gblInitID = '';
var started = false;
var gbgalID_right = '';
var gbgalID_left = '';
var img_show = 2;
var photo_img_length = 0;
var photoID = '';
if (lang == 'en'){
	arrow_title = 'More';
}else if (lang == 'tc'){
	arrow_title = '更多';	
}else if (lang == 'sc'){
	arrow_title = '更多';
}

$(document).ready(function(){
	$(document).pngFix(); //IE6
	$('.gallery').children('.left').addClass('disable');
	if ($('.gallery_con').children('ul').children('li').length <= 4){
		$('.gallery').children('.right').addClass('disable');
	}
	
	$('.'+currFontSize+'').children('a').addClass('selected');
	
	$('img.btn_over').mouseover(function(){
		if ($(this).parent('a').hasClass('selected')){
		}
		else
			$(this).attr('src', $(this).attr('src').toString().replace('.gif','_over.gif').replace('.jpg','_over.jpg').replace('.png','_over.png'));
	}).mouseout(function(){
		if ($(this).parent('a').hasClass('selected')){}
		else
			$(this).attr('src', $(this).attr('src').toString().replace('_over.gif','.gif').replace('_over.jpg','.jpg').replace('_over.png','.png'));
	});
	
	$('img.btn_over').each(function(){
		if ($(this).parent('a').hasClass('selected')){
			$(this).attr('src', $(this).attr('src').toString().replace('.gif','_over.gif').replace('.jpg','_over.jpg').replace('.png','_over.png'));
		}
	});
	
	if ($('#left_nav').innerHeight(true) > $('#right_zone').innerHeight(true))
		$('#right_zone').height($('#left_nav').innerHeight(true));
		
	$('#top_nav').children('ul').children('li').hover(
		function(){
			$(this).children('ul').stop(true, true).fadeIn(300);
		},
		function(){
			$(this).children('ul').stop(true, true).fadeOut(300);
		}
	)
	
	$('#left_nav').children('ul').children('li').hover(
		function(){
			$(this).addClass('over');
			$(this).children('ul').stop(true, true).fadeIn(300);
		},
		function(){
			$(this).removeClass('over');
			$(this).children('ul').stop(true, true).fadeOut(300);
		}
	)
	
	
	gblInitID = setTimeout('loop_autoscroll();',3000);
	$('.scroller2_wrap').hover(
		function(){
			if(started){
				stop_autoscrollcontent();
			}else{
				clearTimeout(gblInitID);
			}
		},
		function(){			
				loop_autoscroll();			
		}

	); 
	
	$('.scroller').jScrollPane({showArrows:true, verticalDragMinHeight :39, verticalDragMaxHeight : 39, reinitialiseOnImageLoad: true, arrowButtonSpeed: 1, arrowRepeatFreq : 5, mouseWheelSpeed: 4});
	$('.scroller2').jScrollPane({showArrows:true, verticalDragMinHeight :14, verticalDragMaxHeight : 14, reinitialiseOnImageLoad: true, arrowButtonSpeed: 1, arrowRepeatFreq : 70, mouseWheelSpeed: 4});
	$('.scroller3').jScrollPane({showArrows:true, verticalDragMinHeight :14, verticalDragMaxHeight : 14, reinitialiseOnImageLoad: true, arrowButtonSpeed: 1, arrowRepeatFreq : 5, mouseWheelSpeed: 4});
	
	
	$('.gallery').children('.right').mousedown(function(){
		var t = setInterval('scroll_photo_right();',1);
		gbgalID_right = t;
	}).mouseup(function(){
		clearInterval(gbgalID_right);
	});
	
	$('.gallery').children('.left').mousedown(function(){
		var t = setInterval('scroll_photo_left();',1);
		gbgalID_left = t;
	}).mouseup(function(){
		clearInterval(gbgalID_left);
	});
	
	photo_img_length = $('#photo li').length;
	setPhotoControl();
	
	var photo_t;
	if (photo_img_length>1)	{
		photo_t = setInterval('showImg();',6000);
	}
	photoID = photo_t;
	// alert(lang);
	$('.search_box').children('input').bind('focus',function(){
		if (lang == 'en'){
			if($(this).val() == 'SEARCH')
				$(this).val("");
		}else if (lang == 'tc'){
			if($(this).val() == '搜寻')
				$(this).val("");
		}else if (lang == 'sc'){
			if($(this).val() == '搜寻')
				$(this).val("");
		}
	}).bind('blur',function(){
		if (lang == 'en'){
			if($(this).val() == '')
				$(this).val("SEARCH");
		}else if (lang == 'tc'){
			if($(this).val() == '')
				$(this).val("搜寻");
		}else if (lang == 'sc'){
			if($(this).val() == '')
				$(this).val("搜寻");
		}
	});
	
	$('.search_box_index').children('input').bind('focus',function(){
		if($(this).val() == 'SEARCH 搜寻')
			$(this).val("");
	}).bind('blur',function(){
		if($(this).val() == '')
			$(this).val("SEARCH 搜寻");
	});
	if ($(window).height() >= $(document).height()){
		$('#wrapper').height($(window).height());
	
	}
	
	$('#content').children('table').each(function(){
		if ($(this).attr('width') == "549"){
			$(this).attr('width', '100%');
		}
	})
})

$(window).load(function(){ // use window.load because of google chrome and Safari
	var img_length = 0;
	$('.gallery_con').children('ul').children('li').each(function(){
		img_length += ($(this).innerWidth() + 10);
	});
	
	$('.gallery_con').children('ul').width(img_length + 10);
	right_frame_limit = - (img_length - 119*6);
	
	$('#right_arrow').attr('title', arrow_title);
});

function setPhotoControl() {
	var html='';
	html += '<ul>';
	for (var i=0; i<photo_img_length; i++) {
		if (i>0) {
			html += '<li> | </li>';
		}
		html += '<li><a href="javascript:showImg('+(i+1)+');">'+(i+1)+'</a></li>';
	}
	html += '</ul>';
	if (document.getElementById('photo_control')) {
		document.getElementById('photo_control').innerHTML = html;
	}
}

function showImg(image_no){
	//alert(image_no);
	var stop = false;
	if (typeof (image_no) == "undefined"){
		image_no = img_show;
	}else{
		clearInterval(photoID);
		stop = true;
	}
	if (image_no <= photo_img_length){
		$('#photo li.image').stop(true, true).fadeOut(500);
		$('#photo li.image_'+image_no).stop(true, true).fadeIn(500);
		img_show = image_no+1;
		//alert(img_show);
	}else{
		//alert('');
		$('#photo li.image').stop(true, true).fadeOut(500);
		$('#photo li.image_1').stop(true, true).fadeIn(500);
		img_show = 2;
	}
	
	if (stop){
		var photo_t = setInterval('showImg();',6000);
		photoID = photo_t;
	}
}

function scroll_photo_right(){
	var position = parseInt($('.gallery_con').children('ul').css('left'));
	var left = position-5;
	if ($('.gallery').children('.right').hasClass('disable')){
		return;
	}
	/* no cycle */
	$('.gallery').children('.left').removeClass('disable');
	if (left <= right_frame_limit){
		$('#right_arrow').removeAttr('title');
		$('.gallery').children('.right').addClass('disable');
		$('.gallery_con').children('ul').css('left', right_frame_limit); 
		clearInterval(gbgalID_right);
		return;
	}else{
		$('#left_arrow').attr('title', arrow_title);
		$('.gallery').children('.right').removeClass('disable');
	}
	
	
	$('.gallery_con').children('ul').css('left', left); 
		
}

function scroll_photo_left(){
	var position = parseInt($('.gallery_con').children('ul').css('left'));
	var left = position+5;
	if ($('.gallery').children('.left').hasClass('disable')){
		return;
	}
	/* no cycle */
	$('.gallery').children('.right').removeClass('disable');
	if (left >= left_var){
		$('#left_arrow').removeAttr('title');
		$('.gallery').children('.left').addClass('disable');
		$('.gallery_con').children('ul').css('left', 0); 
		clearInterval(gbgalID_left);
		return;
	}else{
		$('#right_arrow').attr('title', arrow_title);
		$('.gallery').children('.left').removeClass('disable');
	}
	$('.gallery_con').children('ul').css('left', left); 
}

// function photo_left(){
	// var position = parseInt($('.gallery_con').children('ul').css('left'));
	// if (position >= left_var)
	// {
		//$('.gallery_con').children('ul').stop(true,true).animate({left: right_frame_limit},600,'easeOutBounce'); 
	// }
	// else{
		// var left = position+119;
		
		// /* no cycle */
		// $('.gallery').children('.right').css('visibility', 'visible');
		// if (left >= left_var){
			// $('.gallery').children('.left').css('visibility', 'hidden');
		// }else{
			// $('.gallery').children('.left').css('visibility', 'visible');
		// }
		
		
		// $('.gallery_con').children('ul').stop(true,true).animate({left: left},600,'easeOutQuart'); 
		
	// }
// }

// function photo_right(){
	// var position = parseInt($('.gallery_con').children('ul').css('left'));
	// if (position <= right_frame_limit)
	// {
		//$('.gallery_con').children('ul').stop(true,true).animate({left: 0},600,'easeOutBounce'); 
	// }
	// else{
		// var left = position-119;
		
		// /* no cycle */
		// $('.gallery').children('.left').css('visibility', 'visible');
		// if (left <= right_frame_limit){
			// $('.gallery').children('.right').css('visibility', 'hidden');
		// }else{
			// $('.gallery').children('.right').css('visibility', 'visible');
		// }
		
		
		// $('.gallery_con').children('ul').stop(true,true).animate({left: left},600,'easeOutQuart'); 
		
	// }
// }

function loop_autoscroll(){
	var t = setInterval('autoscrollcontent();',100);
	started = true;
	gblIntID = t;
}
var time = 0;

function autoscrollcontent(){
	//var value = $('#auto_scroll_wrap').css('top');
	$('.scroller2').find('.jspPane').css('top', function(index, value){return (parseInt(value) - 1);});
	//$('.scroller2').find('.jspDrag').css('top', function(index, value){return (parseInt(value) + 1);});
	var height = $('.scroller2').find('.jspPane').innerHeight();
	var scroll_height = $('.scroller2').find('.jspDrag').innerHeight();
	var radio = height / scroll_height;
	radio = parseInt(radio);
	var go_top = parseInt(58 / radio);
	if (go_top == 0)
		go_top = 1;
	time += 1;
	if ( (-(parseInt($('.scroller2').find('.jspPane').css('top'))))%radio == 0 ){
		//alert('');
		var top = parseInt($('.scroller2').find('.jspDrag').css('top'));
		if(jQuery.browser.version == '6.0' || jQuery.browser.version == '7.0' || jQuery.browser.version == '8.0'){
				top = top + go_top;
		}else{
			// if (first)
				top = top + go_top;
			// else
				// top = top + 5;
		}
		if (top >= 24)
			top = 24;
		var scr_time = time*100;
		var t = $('.scroller2').find('.jspDrag').stop(true,true).animate({top: top}, scr_time, 'linear'); 
		time = 0;
	}
	// else if ((-(parseInt($('.scroller2').find('.jspPane').css('top'))))%radio == 2){
		// $('.scroller2').find('.jspDrag').css('top', function(index, value){
		// var top = parseInt(value) + 2;
			// if (top >= 36)
				// top = 36;
			// return (top);
	// });
	// }else if ((-(parseInt($('.scroller2').find('.jspPane').css('top'))))%radio == 1){
		// if(jQuery.browser.version == '6.0' || jQuery.browser.version == '7.0' || jQuery.browser.version == '8.0'){
			// $('.scroller2').find('.jspDrag').css('top', function(index, value){
			// var top = parseInt(value) + 1;
			// if (top >= 36)
				// top = 36;
			// return (top);
			// });
		// }
	// }
	
	if(parseInt($('.scroller2').find('.jspPane').css('top')) < - ($('.scroller2').find('.jspPane').innerHeight(true) - 58)){
		if(jQuery.browser.version == '6.0'){
			$('.scroller2').find('.jspPane').hide(function(){
				$('.scroller2').find('.jspPane').css('top',10);
				$('.scroller2').find('.jspDrag').stop(true,true).animate({top: 0}, 100, 'linear'); 
				$('.scroller2').jScrollPane({showArrows:true, verticalDragMinHeight :14, verticalDragMaxHeight : 14, reinitialiseOnImageLoad: true, arrowButtonSpeed: 1, arrowRepeatFreq : 70, mouseWheelSpeed: 4});
				$('.scroller2').find('.jspPane').show();
				
			});
		}
		else{
			$('.scroller2').find('.jspPane').fadeOut(700, function(){
				$('.scroller2').find('.jspPane').css('top',10);
				$('.scroller2').find('.jspDrag').stop(true,true).animate({top: 0}, 100, 'linear'); 
				$('.scroller2').jScrollPane({showArrows:true, verticalDragMinHeight :14, verticalDragMaxHeight : 14, reinitialiseOnImageLoad: true, arrowButtonSpeed: 1, arrowRepeatFreq : 70, mouseWheelSpeed: 4});
				$('.scroller2').find('.jspPane').fadeIn(800);
				
			});
		}
	}
}

function pop_up_map(filename){
	var newwindow=window.open(filename,'name','height=658,width=658');
	if (window.focus) {newwindow.focus()}
}

function stop_autoscrollcontent(){
	clearInterval(gblIntID);
}

$.fn.haschild = function(selector){
  return ($("> " + selector, this).length > 0);
}

function flashIndexStop(){
	$('#mobile').css('display', 'block');
}
