$(document).ready(function() {
	   
	   $('input').each(function(i) {
		  $(this).focus(function(){			   
           	$(this).select();
		   });
		});
	   
	   
	   
	   
	 $('#floatdiv a').click(function(e){	
			
		e
	   $("#container").animate( { scrollTop: 0 }, 'slow' );
	   
	});

	   
	   
/////////////////////////////////////////////////////////////////////// 
// handle side menu
///////////////////////////////////////////////////////////////////////   




	




 	$('.side_menu_submenu_title a').each(function(i) {
		  if($(this).hasClass('current')){
			  $(this).addClass("no_need");
		  }
	});

	
    $('.side_menu_submenu_item a').mouseover(function(e){	
		
	 	//$(this).attr('class','data_over');
		//alert($(this).parent().parent().parent().attr('side_menu_submenu_title_class'));
		var side_menu_submenu_title_class = $(this).parent().parent().parent().attr('side_menu_submenu_title_class');
		
		
		if($("."+side_menu_submenu_title_class).hasClass('no_need')){
			//$("."+side_menu_submenu_title_class).addClass("no_need");
		}else{
			$("."+side_menu_submenu_title_class).removeClass("current");
			$("."+side_menu_submenu_title_class).addClass("current");
		}
		
		
		
		$(this).stopTime("timer_check_mouse_over");
		$("."+side_menu_submenu_title_class).removeClass("mouse_overing");
		$("."+side_menu_submenu_title_class).addClass("mouse_overing");
		$("."+side_menu_submenu_title_class).oneTime(500, "timer_check_mouse_over", function() {
    		//alert(side_menu_submenu_title_class);
				//side_menu_submenu_title_class = $(this).parent().parent().parent().attr('side_menu_submenu_title_class');
				if($(this).hasClass('no_need')){
					//$("."+side_menu_submenu_title_class).removeClass("no_need");
				}else if(!$(this).hasClass('mouse_overing')){
					$(this).removeClass("current");
					$(this).removeClass("mouse_overing");
				}
			
 		});
		
		//$("#close-button").stopTime("hide");
	});
	
	
	
	
	
	$('.side_menu_submenu_item a').mouseout(function(e){
		var side_menu_submenu_title_class = $(this).parent().parent().parent().attr('side_menu_submenu_title_class');	
		
		$("."+side_menu_submenu_title_class).removeClass("mouse_overing");
		//alert(side_menu_submenu_title_class);
		////if($("."+side_menu_submenu_title_class).hasClass('no_need')){
		//}else{
			
		//}
	});
	/*
	 $('.side_menu_submenu_item a').mouseout(function(e){	
	 	//$(this).attr('class','data_over');
		side_menu_submenu_title_class = $(this).parent().parent().parent().attr('side_menu_submenu_title_class');
		if($("."+side_menu_submenu_title_class).hasClass('no_need')){
			$("."+side_menu_submenu_title_class).removeClass("no_need");
		}else{
			$("."+side_menu_submenu_title_class).removeClass("current");
		}
	});
	*/
///////////////////////////////////////////////////////////////////////   



















/////////////////////////////////////////////////////////////////////// 
// top menu
/////////////////////////////////////////////////////////////////////// 
    $('.have_pulldown').mouseout(function(e){	
		  $(this).stopTime();
	 })
    $('.have_pulldown').mouseover(function(e){	
  $(this).oneTime(200, function() {
	 	
		$(".menuPull").hide();
		$(".have_pulldown").removeClass("current");
		$(this).addClass("current");
		////////////////////////////////////////////////////////////////////////////////
		var showPulldown = $(this).attr('showPulldown');
		var showPulldown_width = $("#"+showPulldown).width();
		////////////////////////////////////////////////////////////////////////////////
		var top_menu_area_pos = $("#top_menu_area").offset();  
		var top_menu_area_width = $("#top_menu_area").width();
		var top_menu_area_left = top_menu_area_pos.left + 6;
		var top_menu_right_bound = top_menu_area_left + top_menu_area_width - 27;
		var top_menu_left_bound = top_menu_area_left;
		//alert(top_menu_left_bound+":"+top_menu_right_bound);
		////////////////////////////////////////////////////////////////////////////////
		var pos = $(this).offset();  
		var reduce_x = 180;
		var target_pos = pos.left - reduce_x;
		////////////////////////////////////////////////////////////////////////////////
		if(target_pos + showPulldown_width > top_menu_right_bound ){
			//alert("right");
			target_pos = top_menu_right_bound - showPulldown_width;
		}
		if(target_pos  < top_menu_left_bound ){
			//alert("left");
			target_pos = top_menu_left_bound;
		}
		////////////////////////////////////////////////////////////////////////////////
		$("#"+showPulldown).css( { "left": (target_pos) + "px" } );
		$("#"+showPulldown).show();
		/*
		//get the position of the placeholder element
 		 var pos = $("#placeholder").offset();  
  		var width = $("#placeholder").width();
  		//show the menu directly over the placeholder
  		$("#menu").css( { "left": (pos.left + width) + "px", "top":pos.top + "px" } );
  		$("#menu").show();
		*/
		
		
	
	});
	});
	
	

	
	
	$(".menuPull").mousemove(function(e){
			/*
			var this_pos = $(this).offset();  
			var this_width = $(this).width();
			var this_height = $(this).height();
			
			
			var top_bound = this_pos.top-3;
			var bottom_bound = this_pos.top + this_height+10;
			var left_bound = this_pos.left+3 ;
			var right_bound = this_pos.left + this_width+10;
			
			
			if(e.pageX<=left_bound || e.pageX>=right_bound || e.pageY <= top_bound || e.pageY >= bottom_bound){
				$(this).hide();
			}
			*/
			//110
			//345
			//$("#test").html(top_bound+":"+bottom_bound+":"+left_bound+":"+right_bound+"="+e.pageX+":"+e.pageY);				  
	});
	
	
	
	
	$("#header").mousemove(function(e){

			$(".have_pulldown").removeClass("current");
			$(".menuPull").hide();
					  
	});
	
	
	$("#main_content_container").mousemove(function(e){

			$(".have_pulldown").removeClass("current");
			$(".menuPull").hide();
					  
	});
	
	
///////////////////////////////////////////////////////////////////////   

	
	//search function
	$('#searchBtn').click(function(){
		var oForm = $('#searchForm');
		var keyword = $('#keyword').val().replace(/\s/g, '');
		if(keyword != '' && keyword != 'Keywordsearch'){
			oForm.submit();
		}
	});

	$('.tag_link').click(function(){
		var oForm = $('#searchForm');
		var keyword = $('span', this).html();
		$('#keyword').val(keyword);
		oForm.submit();
	});
	   
	   
});


function switchLang(lang,ver){
	//alert("lang:" + lang + ", ver:" + ver);
	var url = window.location.href;
	
	//handler in text version
	if(lang == 'txt'){
		//handle transfer to images version
		if(ver == 'img'){
			if(url.indexOf('?txt=y&') != -1)
				window.location.href = url.replace('txt=y&','');
			else if(url.indexOf('?txt=y') != -1)
				window.location.href = url.replace('?txt=y','');
			else if(url.indexOf('&txt=y') != -1)
				window.location.href = url.replace('&txt=y','');		
		}else{
			transferLang(ver);
		}
	//handler in images version
	}else{
		//handle transfer to text version
		if(ver == 'txt'){
			if(url.indexOf('txt') != -1)
				return;
			(url.indexOf('?') == -1 && url.slice(-3) == '.do')
				? window.location.href += '?txt=y'
				: window.location.href += '&txt=y';
		}else{
			transferLang(ver);
		}
	}
	
	//handle transfer languages
	function transferLang(_lang){		
		if(_lang == 'hk') _lang = 'tc';
		else if(_lang == 'cn') _lang = 'sc';
		else if(_lang == 'eng') _lang = 'en';
		
		var url = window.location.href;
		//alert("url:" + url + ", lang:" + _lang);
		if(url.indexOf('/tc/') != -1)
			window.location.href = url.replace('/tc/', '/' + _lang + '/');
		else if(url.indexOf('/sc/') != -1)
			window.location.href = url.replace('/sc/', '/' + _lang + '/');
		else if(url.indexOf('/en/') != -1)
			window.location.href = url.replace('/en/', '/' + _lang + '/');	
	}
}

function blank(a) { if(a.value == a.defaultValue) a.value = ""; }
function unblank(a) { if(a.value == "") a.value = a.defaultValue; }

function clearContents(element) {
 if (element.value == element.defaultValue) element.value = "";
}

function showContents(element) {
  if(element.value == "") element.value = element.defaultValue;
}
