// JavaScript Document

var $=$j;

var mySuggest1;
var destSuggest;


/*** Input Field Hint ***/
// Yet Another Hint
// http://plugins.jquery.com/project/YAHint (access Jan 2010)
;(function($){var H=$.hint=function(txt,opts){return $(document).hint(txt,opts);},toOpts=function(t,o){if(!o&&typeof t=="object"){o=t;t=null;}
o=o||{};if(t){o.text=''+t;}
if(o.keepLabel)o.method='valueSwap';return o;};$.fn.hint=function(txt,opts){opts=toOpts(txt,opts);return this.each(function(){H.init.call(this,opts);});};$.extend(H,{version:"1.6.1",query:'input:password,input:text:not(._hintPw),textarea',on:'hint',attr:false,inline:false,text:undefined,method:'labelOver',parentCss:{position:'relative',float:'left',clear:'left'},inlineCss:{display:'inline',float:'none'},labelCss:{position:'absolute',top:'4px',left:'5px'},getElements:function(opts){var q=opts.query||H.query,inputs=this.is(q)?this:this.find(q);return(inputs.length==0?this:inputs);},init:function(opts){var $e=$(this),meta=$.metadata,Hm=meta?$e.metadata():null;H.getElements.call($e,opts).each(function(){var $i=$(this),hm=meta?$i.metadata():null,h=$.extend(true,{},H,Hm,hm,opts);$.extend(h,h[h.method]);h.create.call($i,h);});},hasValue:function(h){var v=this.val();if("txtAct" == $(this).attr("id")){var obj = this; var jobj= $(this);  obj.onbeforeactivate = function(){return false;};obj.onmouseover = function(){obj.setCapture();}; obj.onmouseout = function(){obj.releaseCapture();};obj.onfocus = function(){obj.blur();};return true;} ;return(v&&$.trim(v)!='');},start:function(){var i=$(this),h=i.data('hint');method=h.hasValue.call(i,h)?'hide':'show';if("txtAct" == i.attr("id") && "" == i.val()){i.val(i.attr("title"));};h[method].call(i,h);},end:function(){var i=$(this),h=i.data('hint');if(h&&h.hasHint.call(i,h)){h.hide.call(i,h);}},getText:function(h){return h.text||this.attr(h.attr||'title');},create:function(h){if(this.data('hint'))this.data('hint').destroy.call(this);h.setup.call(this,h);this.blur(h.start).focus(h.end).data('hint',h);h.start.call(this);},destroy:function(){var h=this.data('hint');h.teardown.call(this,h);this.unbind('blur',h.start).unbind('focus',h.end).data('hint',null);},valueSwap:{setup:function(h){var self=this,text=h.getText.call(self,h);h.kill=function(){h.destroy.call(self);};if(self.is(':password')){h.password=$('<input type="text" value="'+text+'" class="_hintPw">').focus(function(){self.show().focus();}).addClass(h.on).insertBefore(self);}else if($.browser.msie&&!self.attr('defaultValue')&&self.val()==text){self.val('');}
$(window).unload(h.kill);$(this[0].form).submit(h.kill);},hide:function(h){if(h.password){h.password.hide();this.show();}else{if(h.hasHint.call(this,h))this.val('');this.removeClass(h.on);}},show:function(h){if(h.password){this.hide();h.password.show();}else{this.addClass(h.on).val(h.getText.call(this,h));}},hasHint:function(h){if(h.password)return h.password.is(':visible');return this.hasClass(h.on)&&(this.val()==h.getText.call(this,h));},teardown:function(h){h.end.call(this);if(h.password)h.password.remove();$(window).unbind('unload',h.kill);$(this[0].form).unbind('submit',h.kill);}},labelOver:{setup:function(h){var self=this,n=this.attr('name'),l=$('label[for='+n+']'),p=self.parent();if(l.size()==0){h.newLabel=true;l=$('<label for="'+n+'">'+h.getText.call(this,h)+'</label>');self.before(l);}else if(h.text||h.attr){h.labelText=l.text();l.text(h.getText.call(this,h));}
p=self.wrap('<div></div>').before(l.remove()).parent().css(h.parentCss);if(h.inline)p.css(h.inlineCss);h.labelStyle=l.attr('style')||'';h.label=l.addClass(h.on).css(h.labelCss).click(function(){self.focus();});},hide:function(h){h.label.css('textIndent',-10000);},show:function(h){h.label.css('textIndent',0);},hasHint:function(h){return h.label.css('textIndent').charAt(0)=='0';},teardown:function(h){h.label.removeClass(h.on).attr('style',h.labelStyle);if(h.newLabel){h.label.remove();h.label=null;}else if(h.labelText){h.label.text(h.labelText);}
var p=this.parent().after(this);if(h.label){p.before(h.label);}
p.remove();}}});})(jQuery);

/* Copyright (c) 2006 Brandon Aaron (http://brandonaaron.net)
 * Dual licensed under the MIT (http://www.opensource.org/licenses/mit-license.php) 
 * and GPL (http://www.opensource.org/licenses/gpl-license.php) licenses.
 *
 * $LastChangedDate: 2007-07-21 18:45:56 -0500 (Sat, 21 Jul 2007) $
 * $Rev: 2447 $
 *
 * Version 2.1.1
 */
(function($){$.fn.bgIframe=$.fn.bgiframe=function(s){if($.browser.msie&&/6.0/.test(navigator.userAgent)){s=$.extend({top:'auto',left:'auto',width:'auto',height:'auto',opacity:true,src:'javascript:false;'},s||{});var prop=function(n){return n&&n.constructor==Number?n+'px':n;},html='<iframe class="bgiframe"frameborder="0"tabindex="-1"src="'+s.src+'"'+'style="display:block;position:absolute;z-index:-1;'+(s.opacity!==false?'filter:Alpha(Opacity=\'0\');':'')+'top:'+(s.top=='auto'?'expression(((parseInt(this.parentNode.currentStyle.borderTopWidth)||0)*-1)+\'px\')':prop(s.top))+';'+'left:'+(s.left=='auto'?'expression(((parseInt(this.parentNode.currentStyle.borderLeftWidth)||0)*-1)+\'px\')':prop(s.left))+';'+'width:'+(s.width=='auto'?'expression(this.parentNode.offsetWidth+\'px\')':prop(s.width))+';'+'height:'+(s.height=='auto'?'expression(this.parentNode.offsetHeight+\'px\')':prop(s.height))+';'+'"/>';return this.each(function(){if($('> iframe.bgiframe',this).length==0)this.insertBefore(document.createElement(html),this.firstChild);});}return this;};})(jQuery);



// page init
$j(function(){
	
	
	/* hover delay - menu / tooltip */
	initHoverDelay();
	

	/* auto field hints initialization */
	$j.hint({method:'valueSwap'});
	
	
	/* loop through all buttons and attach the ending span for round corner */
	var inputBtn = $j("input.btn, input.btnRed");
	inputBtn.each ( function () {
		if ($j(this).hasClass('btnRed')) {
			$j(this).after("<span class='btnEndingRed'></span>");
		} else {
			$j(this).after("<span class='btnEnding'></span>");
		}
	});


	$j("a[title],input[title]").each(function(){


		   var me=$j(this);
			if ((me.attr("title")!="")&&(me.attr("name")!="")){
	   
			   if ($j("div[title="+me.attr("title")+"]").length>0){
					var xwin=$j("div[title="+me.attr("title")+"]");
					var txt=$j("#"+me.attr("name"));
	
	
					var action=function(){
						$j("div.search-pop").hide();
						var css={};
						css.top=(Math.max(me.offset().top+me.height(), txt.offset().top+txt.height()))+"px";
						css.left=txt.offset().left+"px";
						css.width=(50+me.width()+txt.width()+parseInt(txt.css("margin-right"))+parseInt(txt.css("padding-right"))+parseInt(me.css("padding-left"))+parseInt(me.css("padding-right"))+parseInt(txt.css("padding-left")))+"px";
						
						xwin.css(css).show().bgiframe();
						xwin.children(".btn-close").click(function(){
								txt.val("");
								xwin.find("input[type=checkbox][checked=true]").siblings("span").each(function(){
											   if (txt.val()==""){
												   if ($j(this).html()!="\u5168\u90E8"){
														txt.val($j(this).html());
													}
											   }else{
													if ($j(this).html()!="\u5168\u90E8"){
														txt.val(txt.val()+", "+$j(this).html());
													}
											   }
										   }
									);
								xwin.hide();
								txt.removeClass("hint");
								if (txt.val()==""){
									txt.blur();
								}
						 });
					}
					
					
					if(me[0]==txt[0]){
						me.focus(action);
					}else{
						me.click(action);
					}
	
			   }
			}
	});





	var options={
		"firstlevel":{
			noall:true,
			nocheckbox:true,
			cls:"firstlevel"
		},
		"init":{
			closed:true,
			cls:"jQtree"
		}
	}

	if (typeof(destList)!="undefined"){

		$("#destList").append(buildTree(destList, options)).find("li").each(function(){
			var me=$(this);
			var ul=me.children("ul");
			if (ul.length>0){
				me.children("span, div.ico").click(function(){
					if (me.hasClass("closed")){
						me.removeClass("closed");
					}else{
						me.addClass("closed");
					}
					IE6MultiClass(me);
				});
			}
		});
	}

//CPPZUC ADD FOR PAX.0636 20110811 START	
  init_destination();
  init_acticity();
//CPPZUC ADD FOR PAX.0636 20110811 END

	init_keywordsuggest();
	init_destsuggest();
});








// open-close init
function initHoverDelay() {
	var _navItems = $j('#main-nav > li:has(ul)');
	_navItems.hoverDelay({
		opener:'>a',
		popup:'>ul',
		delay: 500
	});
	$j('#main-nav > li').mouseover(function(){
			$j(this).siblings().removeClass("hover").children('li > ul').hide();
	});		


	var _toolTips = $j('a.ico-hints');
	_toolTips.hoverDelay({
		opener:'',
		popup:'div.tips-hints-content',
		delay: 500,
		align: 'right'
//		pageReference:'#doc2',
	});
	_toolTips.parent().each(function(_ind){$j(this).css('zIndex',_toolTips.length - _ind)});

	var _toolTips = $j('a.b1-link');
	_toolTips.clickLink();
//	_toolTips.hoverDelay({
//		opener:'',
//		popup:'div.hints-content',
//		delay: 500
//		pageReference:'#doc2',
//	}); 

	
}

// click link plusin
jQuery.fn.clickLink = function(){

return this.each(function(){
  if ($(this).attr("title")=="") return function(){};
  var _link = jQuery(this);   
  _link.click(function(){
     var s = _link.parent();
     var benefit=$j("div.hints-content" +"[title="+$(this).attr("title")+"]");
     s.html(benefit.html());
  }); 
}); 
}

// hover delay plugin
jQuery.fn.hoverDelay = function(_options){
	var _options = jQuery.extend({
		hoverClass: 'hover',
		opener:'>a',
		popup:'>div',
		delay: 200,
		align:"left"
	},_options);

	return this.each(function(){
		// options

		if ($(this).attr("title")=="") return function(){};

	
		var _holder = jQuery(this);
		var _reactTime = _options.delay;
		var _hoverClass = _options.hoverClass;
		var _popup = (_options.popup.indexOf(">")===0)?jQuery(_options.popup, _holder).hide():$j(_options.popup+"[title="+$(this).attr("title")+"]").hide().css({left:"-4000px",top:"-4000px",position:"absolute","z-index":"10000"});
		var _opener = (_options.opener!=="")?jQuery(_options.opener, _holder):_holder;
		var _timer;
		
		if (_options.popup.indexOf(">")!==0){
			$(_options.popup+"[title="+$(this).attr("title")+"]").attr("title","");
			$(this).attr("title","");
		}


		// gallery control
		_holder.hover(function(){
			if(_timer) clearTimeout(_timer);
			_holder.addClass(_hoverClass);
			_timer = setTimeout(function(){
				_popup.bgiframe();
				_popup.show();
				if(_options.popup.indexOf(">")!==0){
					var left=_holder.offset().left;
					var top=_holder.offset().top+_holder.height();
					if (_options.pageReference){
						left-=$(_options.pageReference).offset().left;
					}
					if (_options.align!="left"){
						left-=_popup.width()+_holder.width();
					}
					_popup.css({left:left+"px",top:top+"px",position:"absolute","z-index":"10000"});
				}
				_popup.mouseover(function(){if(_timer) clearTimeout(_timer);})
				_popup.mouseout(function(){
									if(_timer) clearTimeout(_timer);
									_timer = setTimeout(function(){
										_popup.hide();
										_holder.removeClass(_hoverClass);
									},_reactTime);
							 })
			},_reactTime);
		},function(){
			if(_timer) clearTimeout(_timer);
			_timer = setTimeout(function(){
				_popup.hide();
				_holder.removeClass(_hoverClass);
			},_reactTime);
		});

		// click behaviour
		_opener.click(function(){
						   
			if(_timer) clearTimeout(_timer);
			_holder.addClass(_hoverClass);
			_popup.show();
			return false;
		})
	});
}

//CPPZUC ADD FOR PAX.0636 20110811 START
/*======================inint dest===================*/
function init_destination(){
    $j(".search-pop").find("input[type=checkbox][id]").each(function(){
			var destString = this.id;
			var fullName;
			var logogram;
			if(destString.indexOf("(") > -1){
				fullName = destString.substring(0,destString.indexOf("("));
				logogram = destString.substring(destString.indexOf("(") + 1, destString.indexOf(")"));
			} else {
				fullName = destString;
				logogram = destString;
			}
			
      if(typeof(dest_list) != 'undefined'){     
        if(dest_list.indexOf(fullName) > -1 || dest_list.indexOf(logogram) > -1){
			    this.checked = true;
			  }
			}
		});
}

function init_acticity(){
    $j(".search-pop-short").find("input[type=checkbox][id]").each(function(){
			var actString = this.id;
			var fullName;
			var logogram;
			if(actString.indexOf("(") > -1){
				fullName = actString.substring(0,actString.indexOf("("));
				logogram = actString.substring(actString.indexOf("(") + 1, actString.indexOf(")"));
			} else {
				fullName = actString;
				logogram = actString;
			}
			
      if(typeof(activity_list) != 'undefined'){     
        if(activity_list.indexOf(fullName) > -1 || activity_list.indexOf(logogram) > -1){
			    this.checked = true;
			  }
			}
		});
}


//CPPZUC ADD FOR PAX.0636 20110811 END
/*======================tree===================*/
function buildTree(obj, options){
	var ul=$("<ul/>");
	if (options.init.cls) {
		ul.addClass(options.init.cls);
	}
	
	if (!options.firstlevel.noall) {
		var li=$("<li/>").appendTo(ul);
		li.append($("<div/>").addClass("ico"));
		li.append($("<input/>").attr({"type":"checkbox","title":"ALL"}).click(checkAll));
		li.append($("<span/>").html("\u5168\u90E8"));
		IE6MultiClass(li);
	}
	for(var i=0;i<obj.length;i++){
		if (typeof(obj[i])=="string"){
			var li=$("<li/>").appendTo(ul);
			if (options.firstlevel.cls) {
				li.addClass(options.firstlevel.cls);
			}
			li.append($("<div/>").addClass("ico"));
			li.append($("<input/>").attr({"type":"checkbox", "title":obj[i], "id":obj[i]}).click(uncheckAll));
			li.append($("<span/>").html(obj[i], $.extend({}, options, {"firstlevel":{}})));
			IE6MultiClass(li);
		}else{
			for(var j in obj[i]){
				var li=$("<li/>").appendTo(ul).addClass("hasChildren");
				li.append($("<div/>").addClass("ico"));
				if (options.firstlevel.cls) {
					li.addClass(options.firstlevel.cls);
				}
				if (!options.firstlevel.nocheckbox) {
					li.append($("<input/>").attr({"type":"checkbox", "title":j, "id":j}).click(checkGroup));
				}
				li.append($("<span/>").html(j));
				var subul=buildTree(obj[i][j], $.extend({}, options, {"firstlevel":{}}));
				subul.appendTo(li);
				if (options.init.closed) {
					li.addClass("closed");
				}
				IE6MultiClass(li);
			}
		}
	}
	li.addClass("last");
	IE6MultiClass(li);
	return(ul);
}
function IE6MultiClass(li){
	var subclass=[];

	subclass["firstlevel"]=(li.hasClass("firstlevel"))?"firstlevel":"";
	subclass["last"]=(li.hasClass("last"))?"last":"";
	subclass["hasChildren"]=(li.hasClass("hasChildren"))?"hasChildren":"";
	subclass["closed"]=(li.hasClass("closed"))?"closed":"";

	var mclass="";
	mclass=subclass["firstlevel"]+" "+subclass["last"]+" "+subclass["hasChildren"]+" "+subclass["closed"];
	mclass=mclass.replace("   ", " ");
	mclass=mclass.replace("  ", " ");
	mclass=mclass.replace(/(^\s*)|(\s*$)/g, "");
	mclass=mclass.replace(" ", "_");
	mclass=mclass.replace(" ", "_");
	li.removeClass();
	//alert(mclass);
	
	for(var i in subclass){
		if (subclass[i]!=""){
			li.addClass(i);
		}
	}
	li.addClass(mclass);

}

function checkAll(){
	var chkbox=$(this);
	if(chkbox.attr("checked")){
		chkbox.parents("li:eq(1), ul:eq(0)").find("input[type=checkbox]").attr("checked", true);
	}else{
		chkbox.parents("li:eq(1), ul:eq(0)").find("input[type=checkbox]").attr("checked",false);
	}
}
function uncheckAll(){
	var chkbox=$(this);
	if(!chkbox.attr("checked")){
		chkbox.parents("ul:eq(0)").children("li").children("input[type=checkbox][title=ALL]").attr("checked",false);
	}
}
function checkGroup(){
	var chkbox=$(this);
	if(!chkbox.attr("checked")){
		chkbox.parents("li:eq(0)").find("input[type=checkbox]").attr("checked", false);
	}else{
		chkbox.parents("li:eq(0)").find("input[type=checkbox]").attr("checked", true);
	}
}



/*******************keyword suggest*******************/

function init_keywordsuggest(){
/*
	var input = getParameter('keyword');
	if (input != "") {
		input = input.replace(/\+/g, " ")+(" "); 	
		$("#txtSearch").val(utf8decode(unescape(input)));
	}
	if(document.getElementById("txtSearch")){
		mySuggest1 = new Suggest('mySuggest1', document.getElementById("txtSearch"), 'autocomplete', 'inc/suggest.php?q=', 'HK', utf8decode(unescape(input)));
	}
*/
}

function init_destsuggest(){
/*

	if(document.getElementById("txtDest")){
		destSuggest = new Suggest('destSuggest', document.getElementById("txtDest"), 'autocomplete', 'inc/suggest.php?q=', 'HK', "");
	}
*/
}




/* iframe */
// http://herr-schuessler.de/blog/overlapping-select-field-bug-in-ie6-solved-jquery-style/

jQuery.fn.activeXOverlap = function() { 

    $(this).each(function(i){
        var h   = $(this).outerHeight();
        var w   = $(this).outerWidth();
		/*
        var iframe  = '<!--[if IE 6]>' +
                      '<iframe src="javascript:false;" style="height: ' +
                      h +
                      'px; width: ' +
                      w +
                      'px" class="selectOverlap">' +
                      '</iframe>' +
                      '<![endif]-->'
		*/
		var iframe=$("<iframe/>").attr({
										   src:"",
										   id:"bgIframe"
									   }).css({
										   height:h+"px",
										   width:w+"px",
										   left:$(this).offset().left+"px",
										   top:$(this).offset().top+"px",
										   position:"absolute",
										   "z-index":$(this).css("z-index")-1,
										   	border: 0,
											margin: 0,  
											padding: 0
									   });
		iframe.insertAfter($(this));
        //$(this).prepend(iframe);
    });
}


