// JavaScript Document
/*dreamweaver js function*/

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}


function MM_openBrWindow(theURL,winName,features) { //v2.0
  window.open(theURL,winName,features);
}

function chLang(lang) {
	var urlroot = window.location.protocol + "//" + window.location.host,
			currentPath = window.location.pathname;
	var switchTc='/cn/';
	var switchEn='/en/';
	lang = '/'+lang+'/';

	switch (lang){
		case '/en/':
			currentPath=currentPath.replace(switchTc, switchEn);
			break;
		case '/cn/':
			currentPath=currentPath.replace(switchEn, switchTc);
			break;
		default:
	}

	document.location = urlroot + currentPath + window.location.search;
}

/**
 * ========================================
 *
 * ajax
 * ----------------------------------------
 *
 */
	
$(window).load(function(){
});

$(function(){	
	gameBg();
	accordion();
	subMenuBg();
});

$(document).ready(function() {
			 
	   $('a.fb').bind('click', function(e){
			var url = window.location.href, shareURI = '';
			shareURI = $(this).attr('href')+'?u='+encodeURIComponent(url);
			if (document.title != undefined) shareURI = shareURI+'&t='+encodeURIComponent(document.title);
			var facebookWin = window.open(shareURI, 'facebookwin', 'width=960, height=304');
			facebookWin.focus();
			if (e != null) e.preventDefault();
		});

	   $('.m1').click(function(e){
		   e.preventDefault();
	   })
	   
	   $('.pop-div .btn-pop-close').click(function(){
	   		$('.pop-div').hide();
	   });
	  
	   $('.m1').each(function(){
		   $(this).hover(
			  function(){$(this).next('.tab').stop(true, true).css('opacity',1)},
			  function(){$(this).next('.tab').stop(true, true).css('opacity',0)}
		   );
	   })
	   
	   
	  $('.submenu').hover(
		  function(){$(this).prev('.tab').stop(true, true).css('opacity',1);},
		  function(){$(this).prev('.tab').stop(true, true).css('opacity',0);}
	  );
	  
	  navActiviate();
	  
	  
      //On Hover Over
      function hoverOn(){
        $(this).find(".submenu").stop().show().animate({'marginTop':'0'}, '20'); //Find sub and fade it in
        $(this).addClass("hover");
      }
      //On Hover Out
      function hoverOff(){
        var h = $(this).find(".submenu").css('height');
        $(this).find(".submenu").stop().animate({'marginTop':'-'+h}, '20', function() { //Fade to 0 opactiy
    	    $(this).hide();  //after fading, hide it
      	});
      	$(this).removeClass("hover");
      }
      
      //Set custom configurations
      var config = {
        sensitivity: 2, // number = sensitivity threshold (must be 1 or higher)
        interval: 0, // number = milliseconds for onMouseOver polling interval
        over: hoverOn, // function = onMouseOver callback (REQUIRED)
        timeout: 0, // number = milliseconds delay before onMouseOut
        out: hoverOff // function = onMouseOut callback (REQUIRED)
      };
      
      $("ul#menu li .submenu").each(function() {
        var h = $(this).height();
        $(this).css({'marginTop':-h, 'height':h});
      }); //Fade sub nav to 0 opacity on default
      $("ul#menu li").hoverIntent(config); //Trigger Hover intent with custom configurations
    
      // On Hover Over
      function expOn() {
        var p = $(this).parent();
        var cur = $(".current", p);
        var target = $(this);
        $(target).addClass('current');
        $(target).siblings().stop(true).animate({width:'112px'}, {queue: false});
        $(target).stop(true).animate({width:'274px'}, {queue: false});
        $('ul', target).stop(true).animate({marginLeft:'12px',paddingLeft:'96px'}, {queue: false});
        $('ul', $(target).siblings()).stop(true).animate({marginLeft:'12px',paddingLeft:'122px'}, {queue: false});
        if( cur.length > 0 ) cur.not(target).removeClass('current');
		$(this).prev().stop().css('opacity',1);
      }
      // On Hover Out
      function expOff() {
        var p = $(this).parent();
        var cur = $(".current", p); 
        if ( cur.length > 0 && $(this).hasClass("current") ) {
          $('div ul', p).stop().animate({marginLeft:'32px',paddingLeft:'122px'}, {queue: false});
          $(cur).stop().animate({width:'154px'}, {queue: false});
          $(cur).siblings().stop().animate({width:'154px'}, {queue: false});
          $(cur).removeClass('current');
		  $(this).prev().stop().css('opacity',0);
        }
      }
      
      // Set custom configurations
      var expCfg = {
           sensitivity: 4, // number = sensitivity threshold (must be 1 or higher)
           interval: 100, // number = milliseconds for onMouseOver polling interval
           over: expOn, // function = onMouseOver callback (REQUIRED)
           timeout: 200, // number = milliseconds delay before onMouseOut
           out: expOff // function = onMouseOut callback (REQUIRED)
      };
	  
	  $('.current').live('mouseover',function(e){
		  e.stopPropagation();
		  $(this).css('opacity',1);
	  });
	  
	   
	   var i;
	   for (i in document.images) {
		   if (document.images[i].src) {
			   var imgSrc = document.images[i].src;
			   if (imgSrc.substr(imgSrc.length - 4) === '.png' || imgSrc.substr(imgSrc.length - 4) === '.PNG') {
				   document.images[i].style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(enabled='true',sizingMethod='crop',src='" + imgSrc + "')";
			   }
		   }
	   }
      

});

$(window).resize(function() {
	gameBg();	
});

function gameBg(){
//	var fillH = $('.wrapper').height();
//	var misc = $('body').height()-fillH;
//	var bgH = misc+fillH;
//	$('.wrapper').css({height:bgH});
}

function pop(id){
	$('#'+id).show();
}

function navActiviate(){
	if(window.location.href.indexOf("/welcome/")>0){
		menuHl('m1:eq(0)')
	}
	if(window.location.href.indexOf("/benefits/")>0){
		menuHl('m1:eq(1)')
	} 
	if(window.location.href.indexOf("/fun/")>0){
		menuHl('m1:eq(2)')
	}
	if(window.location.href.indexOf("/inspiration/")>0){
		menuHl('m1:eq(3)')
	}
	if(window.location.href.indexOf("/love/")>0){
		menuHl('m1:eq(4)')
	}
}

function menuHl(el){
	var hlMenu = $('.'+el);
	hlMenu
	hlMenu.next().removeClass('tab').addClass('highlight');
}

function accordion(){
	toplevel=$(".ac-open").find("dd:first").stop(true,true).slideDown();
	$(".accordion:eq(2) > dl").removeClass("ac-open");
	$('dd',toplevel)
	
	$(".accordion").toggle(
		function() {
		  $(this).removeClass("ac-open");
		  $('dd:first',this).stop(true,true).slideUp();
		}, 
		function() {
		  $(this).addClass("ac-open");
		  $('dd:first',this).stop(true,true).slideDown();
		}
	);
	
	$("dl dd > .accordion").toggle(
		function() {
		  $(this).addClass("ac-open");
		  $('dd:first',this).stop(true,true).slideDown();
		},
		function() {
		  $(this).removeClass("ac-open");
		  $('dd:first',this).stop(true,true).slideUp();
		}
	);
	
	$(".accordion dd").click(function(event){
	  event.stopPropagation();
	}); 
	
}

function subMenuBg(){
	   $('.sub-bg').each(function(){
		   
		   var subBgH = $(this).parents().height()+17;
		   $(this).css('height',subBgH)
			//alert(subBgH)
	   });	   
}

//
// --- by OgilvyOne ---
//
$(document).ready(function(){
	if ( sourcecode.length > 0 || cpid.length > 0 ) {
		var ign = ['americanexpress.com.hk','cathaypacific.com','asiamiles.com','cxholidays.com','amexnetwork.com','facebook.com'],
				ex = ign.join('|').replace(/\./g, "\\."), re = new RegExp(ex);
		$("a[href]").not("[href^='javascript']").each(function() {
			var u = $(this).attr("href"), s = u, p, ext = ($(this).attr("rel") == "external" || $(this).attr("class") == "fb");
			if ( ! ext ) {
				if ( sourcecode.length > 0 ) {
					s = u.replace(/(\?|&)sourcecode\=(&|$)/,'$1'+'sourcecode='+sourcecode+'$2');
					if ( u == s ) {
						p = ( s.lastIndexOf("?") > 0 ) ? "&" : "?";
						s = s.replace(/([^#]+)(#.+)?/gi, '$1'+p+'sourcecode='+sourcecode+'$2');
					}
					u = s;
				}
				if ( cpid.length > 0 ) {
					u = s.replace(/(\?|&)cpid\=(&|$)/,'$1'+'cpid='+cpid+'$2');
					if ( u == s ) {
						p = ( u.lastIndexOf("?") > 0 ) ? "&" : "?";
						u = u.replace(/([^#]+)(#.+)?/gi, '$1'+p+'cpid='+cpid+'$2');
					}
				}
				$(this).attr("href", u);
			}
		});
	}
});

