//rv:114
var Data={},
	popups={},
	UrlPrefix='http://d3d60285aj0yqx.cloudfront.net/r01/',
	UrlPrefix2='http://d3d60285aj0yqx.cloudfront.net/r02/',
	UrlFindAdvisor='http://www.manulife.ca/canada/canada1.nsf/Public/find_an_advisor_home',
	UrlSurvey='http://survey.euro.confirmit.com/wix/p400425416.aspx?l=9';

function language($arg){
	var i,j,link2={},link1={
		'/en/index.html'                : ['/fr/index.html','mhome'],
		'/en/product_allocation.html'   : ['/fr/repartition_par_produit.html','mprod'],
		'/en/cases_studies.html'        : ['/fr/etudes_de_cas.html','mhelp'],
		'/en/next_step.html'            : ['/fr/prochaine_etape.html','mnext'],
		'/en/news.html'                 : ['/fr/medias.html','mnews'],
		'/en/disclaimers.html'          : ['/fr/disclaimers.html',false],
		'/en/privacy.html'              : ['/fr/privacy.html',false],

		'/en/contact_your_advisor.php'  : ['/fr/contacter_votre_conseiller.php',false],
		'/en/send_to_a_friend.php'      : ['/fr/envoyer_a_un_ami.php',false],

		'/en/video_volatility.html'     : ['/fr/video_volatilite.html',false],
		'/en/video_inflation.html'      : ['/fr/video_inflation.html',false],
		'/en/video_longevity.html'      : ['/fr/video_longevite.html',false],
		'/en/video_rsq.html'            : ['/fr/video_qdrr.html',false],
		'/en/video_rsq_high.html'       : ['/fr/video_qdrr_eleve.html',false],
		'/en/video_rsq_low.html'        : ['/fr/video_qdrr_faible.html',false],
		'/en/video_rsq_mid.html'        : ['/fr/video_qdrr_moyen.html',false],
		'/en/video_rsq_pa.html'         : ['/fr/video_qdrr_ap.html',false]
	};

	for(i in link1){
		j=link1[i][0];
		link2[i]=[j,link1[i][1]];
		link2[j]=[i,link1[i][1]];
	}
	link1=null;
	return link2[$arg];
}

function getVideoUrl()
{
	var n=Data['page'],v=false,m=UrlPrefix+'media/';
	switch(n){

		case '/en/video_volatility.html'  : v=m+'market_risk_en_0364.flv,'+m+'market_risk_en_0664.flv,'+m+'market_risk_en_0964.flv';break;
		case '/en/video_inflation.html'   : v=m+'inflation_en_0364.flv,'+m+'inflation_en_0664.flv,'+m+'inflation_en_0964.flv';break;
		case '/en/video_longevity.html'   : v=m+'longevity_en_0364.flv,'+m+'longevity_en_0664.flv,'+m+'longevity_en_0964.flv';break;
		case '/en/video_rsq.html'         : v=m+'rsq_en_0364.flv,'+m+'rsq_en_0664.flv,'+m+'rsq_en_0964.flv';break;
		case '/en/video_rsq_high.html'    : v=m+'rsq_high_0364.flv,'+m+'rsq_high_0664.flv,'+m+'rsq_high_0964.flv';break;
		case '/en/video_rsq_low.html'     : v=m+'rsq_low_0364.flv,'+m+'rsq_low_0664.flv,'+m+'rsq_low_0964.flv';break;
		case '/en/video_rsq_mid.html'     : v=m+'rsq_mid_en_001_0364.flv,'+m+'rsq_mid_en_001_0664.flv,'+m+'rsq_mid_en_001_0964.flv';break;
		case '/en/video_rsq_pa.html'      : v=m+'rsq_pa_0364.flv,'+m+'rsq_pa_0664.flv,'+m+'rsq_pa_0964.flv';break;
		case '/fr/video_volatilite.html'  : v=m+'market_risk_fr_0364.flv,'+m+'market_risk_fr_0664.flv,'+m+'market_risk_fr_0964.flv';break;
		case '/fr/video_inflation.html'   : v=m+'inflation_fr_0364.flv,'+m+'inflation_fr_0664.flv,'+m+'inflation_fr_0964.flv';break;
		case '/fr/video_longevite.html'   : v=m+'longevity_fr_0364.flv,'+m+'longevity_fr_0664.flv,'+m+'longevity_fr_0964.flv';break;
		case '/fr/video_qdrr.html'        : v=m+'rsq_fr_0364.flv,'+m+'rsq_fr_0664.flv,'+m+'rsq_fr_0964.flv';break;
		case '/fr/video_qdrr_eleve.html'  : v=m+'rsq_high_fr_0364.flv,'+m+'rsq_high_fr_0664.flv,'+m+'rsq_high_fr_0964.flv';break;
		case '/fr/video_qdrr_faible.html' : v=m+'rsq_low_fr_0364.flv,'+m+'rsq_low_fr_0664.flv,'+m+'rsq_low_fr_0964.flv';break;
		case '/fr/video_qdrr_moyen.html'  : v=m+'rsq_mid_fr_0364.flv,'+m+'rsq_mid_fr_0664.flv,'+m+'rsq_mid_fr_0964.flv';break;
		case '/fr/video_qdrr_ap.html'     : v=m+'rsq_pa_fr_0364.flv,'+m+'rsq_pa_fr_0664.flv,'+m+'rsq_pa_fr_0964.flv';break;
	}
	return v;
}

function enableCacheIE6(e)
{
	if(document.all&&document.execCommand&&!window.opera)
	try{document.execCommand('BackgroundImageCache',false,true);}catch(e){}
}

function url()
{
	var h=window.location.href,i='/index.html',p,d,f=0,url=h;
	url=url.match(/^(file|https|http?):\/+([^@#\?\&]*?)(\/?(en|fr)?\/(:?.*?))(\?.*?)?$/i)||[];

	Data['domain']=d=(url[2]||'').replace('/^(.*\.ca.*)/','$1');
	p=url[3]||i;
	if(p=='/')p=i;
	if(d.indexOf('helpmysavingslast')>=0){
		f='/en';
		Data['lang']='en';
		p=f+p;
	}else if(d.indexOf('pouruneepargnedurable')>=0){
		f='/fr';
		Data['lang']='fr';
		p=f+p;
	}else{
		url=h;
		// Enforce /html or /site or /site2 on demo/dev site
		url=url.match(/^(file|https|http?):\/+([^@#\?\&]*?)\/(?:html|site|site2)?(\/?(en|fr)?\/(:?.*?))(\?.*?)?$/i)||[];
		p=url[3]||i;
		if(p=='/')p=i;
		Data['lang']=url[4];
	}

	Data['url']=url[0]||url;
	Data['type']=url[1];
	Data['sufix']=(url[6])?url[6]:false;
	Data['local']=(url[1]!='http');
	Data['prefix']=url[2];
	Data['page']=p;
	Data['flag']=f;
}

function menu()
{
	var lang=Data['lang'],
		page=Data['page'],
		toggle=language(page)||[],
		togglelink=toggle[0]||'',
		id=toggle[1],
		local=Data['local'],
		domaine='..';

		if(id)
			$('#'+id).attr('class','active').attr('href','javascript:void(0)');
		
		//id=(lang=='fr')?'english':'francais';

		if(!local&&Data['url'].indexOf('manulifedemo')<0)domaine=(lang=='en')?'http://www.pouruneepargnedurable.ca':'http://www.helpmysavingslast.ca';
		$('a[target=_blank]').click(SetStop);

		if(!Data['flag'])
			$('#lang').attr('href',domaine+togglelink);

		$('#search a:last').attr('href','mailto:?body='+escape(location.href)+'&subject='+escape(document.title));
}

function SetStop()
{
	if($('#vid')[0]){
		setFlashFunction();
		flashMovie = swfobject.getObjectById('vid') ;
		if(typeof flashMovie!='undefined'&&flashMovie){
			if(typeof flashMovie.jsStop == 'function')
			{
				flashMovie.jsStop();
			}
			else
			{
				makeCallable(flashMovie, 'jsStop');
			}
		}
	}
}

function hasFlash(){
	var s=window.swfobject,v;
	if(s&&typeof s.getFlashPlayerVersion=='function'){
		v=s.getFlashPlayerVersion();
		if(v){v=parseInt(v.major,10)||0;if(v>8)return v;}
	}
	return 0;
}

function initFlash()
{
	if(!hasFlash())
		return;

	var d=window.Data||{},f='first',second='',lang=d['lang'],s=UrlPrefix+'swf/',t='transparent',p='../',fr=d['lang']=='fr',b='../media/';
	if($.cookie(f)==null)
		$.cookie(f,1,365);
	else
		second='_short';

	setFlashFunction();
	if($('#homebanner')[0])createFlash({swf:b+(fr?'homepage_fr_002.swf':'homepage_en_002.swf'),location:'homebanner',width:'958',height:'285',lang:lang,prefix:p,wmode:t});
	if($('#videoselector')[0])createFlash({swf:UrlPrefix2+'swf/VideoSelector.swf',location:'videoselector',width:'583',height:'199',lang:lang,prefix:p,wmode:t});
	if($('#allocationpage')[0])createFlash({swf:UrlPrefix2+'swf/AllocationPage.swf',url:false,location:'allocationpage',width:'1000',height:'410',lang:lang,prefix:p,wmode:t});
	if($('#videoembed')[0])createFlash({swf:UrlPrefix+'player/blue_player_09.swf',url:getVideoUrl(),location:'videoembed',width:768,height:432,lang:lang,prefix:p,wmode:t});
	if($('#chipgame')[0])createFlash({swf:UrlPrefix2+'swf/application.swf',location:'chipgame',width:'768',height:'472',lang:lang,prefix:p});
	if($('#calculator')[0])
	{
		var id='calculator',
			host=window.location.host,
			serverURL='http://'+host+'/clientrsq',
			swf=serverURL+'/RSQConsumerTool.swf',
			//swf=UrlPrefix2+'swf/RSQConsumerTool_en_intro.swf',
			w=796,
			h=484,
			fv={
				language:fr?'fr_FR':'en_US',
				showIntro:'true',
				LocalPlayer:d['local'],
				autoplay:1,
				width:w,
				height:h,
				serverURL:serverURL
				//,sid:0
				//,sid:'8B1E9C23A99641B6ACACED02DC108008'
			},params={
				menu:'false',
				scale:'noScale',
				allowfullscreen:'true',
				allowscriptaccess:'always',
				bgcolor:'#FFFFFF'
			};
			
		swfobject.embedSWF(swf,id,w,h,'9.0.28',UrlPrefix+'player/expressInstall.swf',fv,params,{id:id});

		//createFlash({swf:UrlPrefix2+'swf/RSQConsumerTool_en_intro.swf',location:'calculator',width:'796',height:'484',lang:lang,prefix:p});
	}
}

function createFlash(Obj)
{
	Obj=Obj||{};
	var fv={},params={menu:'false',allowfullscreen:'true',allowscriptaccess:'always',wmode:Obj['wmode']},attributes={id:'vid'},
	width=Obj['width']||768,height=Obj['height']||432,url=Obj['url'];
		fv.LocalPlayer=Data['local'];
		fv.production=true ;
		if(Obj['url']){
			if(url.indexOf(',')<0)url=UrlPrefix+'media/'+url;
			fv.width=width;
			fv.height=height;
			fv.autoplay=1;
			fv.mouseActivated='false';
			fv.mouseActivated='false';
			fv.mouseActivatedMode='appear';
			fv.controlsColor='0xffffff';
			fv.highlightColor='0x6b9121';
			fv.controlerColor='0xc9df9f,0x7d8b62';
			fv.controlsPadding=10;
			fv.autoResize='false';
			fv.skinColoration='false';
			fv.separatorType='onlyScrubBarSeparator';
			fv.skin=UrlPrefix+'player/skin_rsq.swf';
			fv.URL=url;
			height += 40;
		}else{
			fv.pdfServiceUrl = '/report/report.pdf';//url for the pdf
			fv.emailServiceURL = '/report/email';//url for the email
			fv.defaultlang = Obj['lang'] || 'en';
			fv.urlPrefix = Obj['prefix'] || '';
		}
		swfobject.embedSWF( Obj['swf'] , Obj['location'], width, height, '9.0.28', 'Player/expressInstall.swf', fv, params, attributes);
}

function trackFlash(action){
	var isProd=Data&&Data['flag']||0;
	wannabedebug=window['wannabedebug']||0;
	if(isProd)
	$.glamGA.customTrack('UA-8396535-6',{
		type:'view',
		url:action,
		debug:wannabedebug
	});
}
var trackUsage=trackFlash;

function debugbox(){
	if(typeof console=='undefined'){var d;
		$('head').append('<link rel="stylesheet" href="../css/debug.css" type="text/css" media="screen" />');
		$('body').append('<script src="../script/jquery.ui.js" type="text/javascript"></script>').append('<div id="debug"><div id="debughead">Debug Function</div><div id="debugbody">&nbsp;</div><div>');
		d=$('#debug');
		debug.draggable();
		debug.resizable();
	}
}

function tracking(){

	wannabedebug = $.cookie('zdebug')=='on'?true:false;
	var isProd=Data&&Data['flag']||0,
		H='href',
		V='javascript:void(0);',
		a=$('a');

	a.filter('[href=#]').attr(H,V);
	if(wannabedebug)debugbox();

	if(isProd)
		$.glamGA('UA-8396535-6',{
			debug: wannabedebug,
			type: 'view',
			url: window.location.href,
			localhost: false,
			trackPage: true,
			trackLinks: true,
			trackMails: true,
			trackFiles: true,
			categoryLinks: 'external',
			categoryMails: 'email',
			categoryFiles: 'download',
			titleLinks: true,
			filetypes: ['pdf','doc','xlsx','docx','xls','csv','gif','png','jpg','mp3','txt','ppt','pptx','zip','xml','flv','mp4','mpeg']
		});
}

function disclaimer(url){SetStop();window.open(url,'RSC_Disclaimers','resizable=0,scrollbars=0,height=460,width=999');}
function simpleResearch(){SetStop();var u=UrlFindAdvisor;if(Data['lang']=='fr')u+='f';window.open(u,'Manulife_Advisor_Research','resizable=0,scrollbars=0,height=624,width=982');}
function outilderecherche(){SetStop();window.open(UrlFindAdvisor+'f','Manulife_Advisor_Research','resizable=0,scrollbars=0,height=624,width=982');}
function survey(){

	/*var force=1,win=0,err,name='survey';
	$(window).unload(function(){
		if(win&&!win.closed&&win.focus){
			try{
				win.focus();
			}catch(err){}
		}
		return true;
	});

	if($.cookie(name)==null){
		$('a').click(function(){
			win=pop(UrlSurvey,name);
			if(win&&win.blur)
				try{
					win.blur();
				}catch(err){}
			$.cookie(name,1,365);
			return true;
		});
	}*/
	/*
	var force=1,win=0,err,name='survey';
	if(Data['lang']=='fr')UrlSurvey='http://survey.euro.confirmit.com/wix/p400425416.aspx?l=3084';
	if($.cookie(name)==null){
		win=pop(UrlSurvey,name);
		if(win&&win.blur)
		try{
			win.blur();
		}catch(err){}
	$.cookie(name,1,365);
	}*/
}

function pop(url,name)
{
	name=name||'popup';
	var a=',location=yes,menubar=yes,resizable=yes,scrollbars=yes,status=yes,titlebar=yes,toolbar=yes,directories=yes';
	return popupWindow(url,name,a+',width=900');
}

function popupWindow(url, id0, args) {
	var t  = new Date().getTime(),
		id = id0 || 'win_' + t,
		n  = id,
		b  = false,
		e,
		w  = popups[id];
		args = args || '';
	try {
		b = w && !w.closed;
	} catch(e) {
		b = false;
	}

	try {
		if (b && w && w.open) {
			if (w.location)
				w.location.href = url;
			else
				try {
					if (w.close) w.close();
				} catch(e) {}

		} else
			w = null;

	} catch(e) {
		b = false;
		w = null;
	}

	if (!w) {
		popups[id] = w = window.open(url, id + t, 'location=yes,status=yes,scrollbars=yes' + args);
		if (w) {
			try {
				if (!document.all && window.moveTo && w.open) w.moveTo(10, 10);
			} catch(e) {}
			if (!w.opener) w.opener = w;
		}
	}

	if (w && window.focus)
		w.focus();

	return w;
}

// Careful with these UTF8 accents...
function toANSI(s){return s?s.replace(/\%/g,'%25')
.replace(/\//g,'%2F').replace(/[ \t]/g,'%20').replace(/\+/g,'%2B').replace(/=/g,'%3D').replace(/,/g,'%2C').replace(/</g,'%3C').replace(/>/g,'%3E')
.replace(/[\'’]/g,'%27')
.replace(/[\"“”«»]/g,'%22').replace(/\?/g,'%3F').replace(/@/g,'%40').replace(/\&/g,'%26').replace(/#/g,'%23').replace(/À/g,'%C0').replace(/Á/g,'%C1').replace(/Â/g,'%C2').replace(/Ã/g,'%C3').replace(/Ä/g,'%C4').replace(/Å/g,'%C5').replace(/Æ/g,'%C6').replace(/Ç/g,'%C7').replace(/È/g,'%C8').replace(/É/g,'%C9').replace(/Ê/g,'%CA').replace(/Ë/g,'%CB').replace(/Ì/g,'%CC').replace(/Í/g,'%CD').replace(/Î/g,'%CE').replace(/Ï/g,'%CF').replace(/Ñ/g,'%D1').replace(/Ò/g,'%D2').replace(/Ó/g,'%D3').replace(/Ô/g,'%D4').replace(/Õ/g,'%D5').replace(/Ö/g,'%D6').replace(/Ù/g,'%D9').replace(/Ú/g,'%DA').replace(/Û/g,'%DB').replace(/Ü/g,'%DC').replace(/à/g,'%E0').replace(/á/g,'%E1').replace(/â/g,'%E2').replace(/ã/g,'%E3').replace(/ä/g,'%E4').replace(/æ/g,'%E6').replace(/ç/g,'%E7').replace(/è/g,'%E8').replace(/é/g,'%E9').replace(/ê/g,'%EA').replace(/ë/g,'%EB').replace(/ì/g,'%EC').replace(/í/g,'%ED').replace(/î/g,'%EE').replace(/ï/g,'%EF').replace(/ñ/g,'%F1').replace(/ò/g,'%F2').replace(/ó/g,'%F3').replace(/ô/g,'%F4').replace(/õ/g,'%F5').replace(/ö/g,'%F6').replace(/ù/g,'%F9').replace(/ú/g,'%FA').replace(/û/g,'%FB').replace(/ü/g,'%FC').replace(/ÿ/g,'%FF'):'';}

function removeAccents(v){return v?v.replace(/[<>&]/g,' ').replace(/[áâàäÁÂÀÄÃã]/g,'a').replace(/[éêèëÉÊÈË]/g,'e').replace(/[íîìïÍÎÌÏ]/g,'i').replace(/[óôòöÓÔÒÖÕõ]/g,'o').replace(/[úûùüÚÛÙÜ]/g,'u').replace(/[ñÑ]/g,'n').replace(/[çÇ]/g,'c').replace(/[ÿ]/g,'y'):'';}

function cleanEmailData(h,a,i){i=i||0;var s=i?'':' ';
if(h){h=h.replace(/\%[01][0-9A-Fa-f]/gi,s);if(a)h=h.replace(/\%[89A-Fa-f][0-9A-Fa-f]/gi,s);h=decodeURIComponent(h);h=h.replace(/<[a-z]+>/gi,s).replace(/<[a-z]+[^>@]*>/gi,s);if(a)h=h.replace(/\&[#a-zA-Z0-9]+;/gi,s);h=h.replace(/[ \r\n\t]+/gi,s);h=h.replace(/<[^>@]+>/gi,s);if(a)h=h.replace(/[^a-zA-Z0-9\.\-\_\@<>\'\" \n]+/gi,s);h=h.replace(/[<>\'\"]+/gi,s);h=h.replace(/[ \r\n\t]+/gi,s);
if(i)return h;if(h.match('@'))return h+s;}
return '';}

var emailBtn=$('input#from,input#to'),
	sendBtn=$('div.send input'),
	reEmail=/^[a-z0-9A-Z\-\_\.]+@[a-z0-9A-Z\-\_\.]+(\.[a-zA-Z0-9\-\_\.]+)*\.[a-zA-Z][a-zA-Z]+$/;

function checkEmail(c,e){
	var q=0,s=sendBtn,D='disabled';c=c||0;

	emailBtn.each(function(){
		var t=$(this),
			v=t.val(),
			w=$.trim(v),
			a=removeAccents(w).toLowerCase(),
			z=cleanEmailData(a,true,true),
			B='bad',
			G='good';

		if(c<2&&v!=z)t.val(z);

		if(reEmail.test(z)){
			if(c==1&&e==this)t.addClass(G).removeClass(B);
		}else{
			if(c==1&&e==this)t.addClass(B).removeClass(G);
			q++;
		}
	});

	q?s.attr(D,D).addClass(D):s.removeAttr(D).removeClass(D);
}

function fixEmail()
{
	emailBtn.blur(function(){checkEmail(1,this);}).keyup(function(){checkEmail(2);});
	checkEmail(0);
}

// http://code.google.com/p/doctype/wiki/ArticleFixingFlashExternalInterface
function makeCallable(instance,name){
	setFlashFunction();
	if(instance&&name)
	{
		instance[name] = function () {
		if(instance&&typeof instance.CallFunction=='function'&&typeof __flash__argumentsToXML=='function')
		return eval(instance.CallFunction("<invoke name=\""+name+"\" returntype=\"javascript\">" + __flash__argumentsToXML(arguments,0) + "</invoke>"));
		}
	}
}

function setFlashFunction(){
	var u="undefined";

	if(typeof(__flash__arrayToXML)==u||!__flash__arrayToXML)
	window.__flash__arrayToXML=function(obj){
		var s = "<array>";
		for (var i=0; i<obj.length; i++) {
				s += "<property id=\"" + i + "\">" + __flash__toXML(obj[i]) + "</property>";
		}
		return s+"</array>";
	};

	if(typeof(__flash__argumentsToXML)==u||!__flash__argumentsToXML)
	window.__flash__argumentsToXML=function(obj,index) {
		var s = "<arguments>";
		for (var i=index; i<obj.length; i++) {
				s += __flash__toXML(obj[i]);
		}
		return s+"</arguments>";
	};

	if(typeof(__flash__objectToXML)==u||!__flash__objectToXML)
	window.__flash__objectToXML=function(obj) {
		var s = "<object>";
		for (var prop in obj) {
				s += "<property id=\"" + prop + "\">" + __flash__toXML(obj[prop]) + "</property>";
		}
		return s+"</object>";
	};

	if(typeof(__flash__escapeXML)==u||!__flash__escapeXML)
	window.__flash__escapeXML=function(s) {
		return s.replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;").replace(/"/g, "&quot;").replace(/'/g, "&apos;");
	};

	if(typeof(__flash__toXML)==u||!__flash__toXML)
	window.__flash__toXML=function(value) {
		var type = typeof(value);
		if (type == "string") {
			return "<string>" + __flash__escapeXML(value) + "</string>";
		} else if (type == "undefined") {
			return "<undefined/>";
		} else if (type == "number") {
			return "<number>" + value + "</number>";
		} else if (value == null) {
			return "<null/>";
		} else if (type == "boolean") {
			return value ? "<true/>" : "<false/>";
		} else if (value instanceof Date) {
			return "<date>" + value.getTime() + "</date>";
		} else if (value instanceof Array) {
			return __flash__arrayToXML(value);
		} else if (type == "object") {
			return __flash__objectToXML(value);
		} else {
			return "<null/>"; //???
		}
	};

	if(typeof(__flash__addCallback)==u||!__flash__addCallback)
	window.__flash__addCallback=function(instance, name) {
		instance[name] = function () {
		if(instance&&typeof instance.CallFunction=='function'&&typeof __flash__argumentsToXML=='function')
		return eval(instance.CallFunction("<invoke name=\""+name+"\" returntype=\"javascript\">" + __flash__argumentsToXML(arguments,0) + "</invoke>"));
		}
	};

	if(typeof(__flash__removeCallback)==u||!__flash__removeCallback)
	window.__flash__removeCallback=function(instance, name) {
		instance[name] = null;
	};
}

$(function(){
	enableCacheIE6();
	url();
	menu();
	initFlash();
	tracking();
	//survey();
	fixEmail();

	window.__flash__removeCallback=function(i,n,e){try{if(i&&n)i[n]=null;}catch(e){}};
});

//if(document.all && parseInt($.browser.version,10)<7)document.write('<script src="../script/firebug-lite-compressed.js" type="text/javascript"'+'></'+'script>');


