// JavaScript Document

var locstring = unescape(location.search.substring(1, location.search.length));
var variable = locstring.split('=');
var tabNumber = variable[1];


function mainNavHover(tabName) {

document.getElementById(tabName + "-on").style.display = "block";
document.getElementById(tabName + "-off").style.display = "none";


}

function mainNavHoverOff(tabName) {

document.getElementById(tabName + "-on").style.display = "none";
document.getElementById(tabName + "-off").style.display = "block";


}

var num //Global variable to store page's total number of tabs

function tabSetUp(numOfTabs) {
	num = numOfTabs;
	
	for (var i=2; i< num + 1; i++){
	
		var tabNum = "tabNum" + i;
		document.getElementById(tabNum).style.display = "none";
	}

}

function switchTab(tabNumber) {
		
	for (var i=1; i< num + 1; i++){
		if (i == tabNumber) {
			document.getElementById("tabNum" + tabNumber).style.display = "block";
			document.getElementById("tab" + tabNumber).className = "selected";		
		} else {
			document.getElementById("tabNum" + i).style.display = "none";
			document.getElementById("tab" + i).className = "notSelected";
		}
	}
}

function showMore() {
	document.getElementById("moreLinks").style.display = "block";
}

function displayInfoBox(event, focusBox) {
	var left;
	var top;

	left=event.clientX + document.body.scrollLeft;
	top=(event.clientY + document.body.scrollTop);
	
	document.getElementById(focusBox).style.top = top;
	document.getElementById(focusBox).style.left = left;
	document.getElementById(focusBox).style.display = "block";

}

function closeInfoBox (focusBox) {
 	document.getElementById(focusBox).style.display = "none";
}
 
function showMore(divClass) {
	document.getElementById(divClass).style.display = "block";
}
function closeBox(divClass) {
	document.getElementById(divClass).style.display = "none";
}
function selectLang() {
		document.write ('	<select name="language" onchange="changeLanguage(this.value)">');
		document.write ('		<option value="">Choose your language</option>');
		document.write ('		<option value="EN">English</option>');
		document.write ('		<option value="FR">Fran&ccedil;ais</option>');
		document.write ('		<option value="ES">Español</option>');
		document.write ('		<option value="SC">简体中文</option>');
		document.write ('		<option value="TC">繁体中文</option>');
		document.write ('		<option value="PJ">ਪੰਜਾਬੀ</option>');
		document.write ('		<option value="AR">العربية</option>');
		document.write ('		<option value="FS">فارسی</option>');
		document.write ('	</select>');
}
function changeLanguage(langType) {
	var chngHost = '';
	
	if (langType == 'EN') {
		chngHost = 'http://scotiabank.com';
	} else if (langType == 'FR') {
		chngHost = 'http://banquescotia.com/bondebut/';
	} else if (langType == 'ES') {
		chngHost = 'http://espanol.startright.scotiabank.com';
	} else if (langType == 'SC') {
		chngHost = 'http://schinese.startright.scotiabank.com';
	} else if (langType == 'TC') {
		chngHost = 'http://trchinese.startright.scotiabank.com';
	} else if (langType == 'PJ') {
		chngHost = 'http://punjabi.startright.scotiabank.com';
	} else if (langType == 'AR') {
		chngHost = 'http://arabic.startright.scotiabank.com';
	} else if (langType == 'FS') {
		chngHost = 'http://farsi.startright.scotiabank.com';
	}
	
	if (langType == 'FR') {
		location.replace(chngHost);	
	} else {
		langURL = chngHost + location.pathname;
		location.replace(langURL);
	}
	
 }

/** Pop Up Window ***/
function popUpWin (__url, _type ) {
    if ( _type == "" ) {
        _type = 0;
    }
    var _params = "height=500,width=480,toolbar="+_type+",scrollbars=1, location=0, status=1, menubar="+_type+",resizable=1,left=100,top=60";
    window.open(__url, "BNSwindow", _params);
};


function writeNav(tabNum) {		
		
		document.write ('<ul>');
		
		if (tabNum == 1 || tabNum == 4) {
			document.write ('	<li id="startRight-off"><a href="/cda/content/0,1608,CID12955_LIDen,00.html"><img src="/startright/images/nav-moving-indicator.gif" width="241" height="35" alt="" border="0" onMouseOver="javascript:mainNavHover(\'startRight\');"></a></li>');

		} else {
			document.write ('	<li id="startRight-off"><a href="/cda/content/0,1608,CID12955_LIDen,00.html"><img src="/startright/images/nav-moving-off.gif" width="241" height="35" alt="" border="0" onMouseOver="javascript:mainNavHover(\'startRight\');"></a></li>');
		}
		document.write ('	<li id="startRight-on">');
		document.write ('		<ul onMouseOut="javascript:mainNavHoverOff(\'startRight\');" onMouseMove="javascript:mainNavHover(\'startRight\');">');
		document.write ('			<li><a href="/cda/content/0,1608,CID12955_LIDen,00.html"><img src="/startright/images/nav-moving-on.gif" width="241" height="35" alt="" border="0"></a></li>');
		document.write ('			<li class="mainnavItems"><a href="/cda/content/0,1608,CID12958_LIDen,00.html">International Account Opening for Landed Immigrants from India</a></li>');
		document.write ('			<li class="mainnavItems"><a href="/cda/content/0,1608,CID13191_LIDen,00.html">International Account Opening for Landed Immigrants from China</a></li>');
		document.write ('			<li class="mainnavItems"><a href="/cda/content/0,1608,CID12960_LIDen,00.html">International Account Opening for Immigrant Investors</a></li>');
		document.write ('			<li class="mainnavItems"><a href="/cda/content/0,1608,CID12959_LIDen,00.html">International Account Opening for International Students from India</a></li>');
		document.write ('			<li class="mainnavItems"><a href="/cda/content/0,1608,CID13542_LIDen,00.html">International Account Opening for International Students from China</a></li>');
        document.write ('			<li class="mainnavItems"><a href="/cda/content/0,1608,CID14572_LIDen,00.html">International Account Opening for International Landed Immigrants, Foreign Workers and International Students from Mexico</a></li>');
		document.write ('		</ul>');
		document.write ('		<img src="/startright/images/nav-moving-bottom.gif" width="241" height="7" alt="" border="0"><br>');
		document.write ('	</li>');	
		
		if (tabNum == 2 || tabNum == 4) {
			document.write ('	<li id="internationalAcc-off"><a href="/cda/content/0,1608,CID12956_LIDen,00.html"><img src="/startright/images/nav-already-in-canada-indicator.gif" width="241" height="35" alt="" border="0" onMouseOver="javascript:mainNavHover(\'internationalAcc\');"></a></li>');

		} else {
			document.write ('	<li id="internationalAcc-off"><a href="/cda/content/0,1608,CID12956_LIDen,00.html"><img src="/startright/images/nav-already-in-canada-off.gif" width="241" height="35" alt="" border="0" onMouseOver="javascript:mainNavHover(\'internationalAcc\');"></a></li>');
		}
		document.write ('	<li id="internationalAcc-on">');
		document.write ('		<ul onMouseOut="javascript:mainNavHoverOff(\'internationalAcc\');" onMouseMove="javascript:mainNavHover(\'internationalAcc\');">');
		document.write ('			<li><a href="/cda/content/0,1608,CID12956_LIDen,00.html"><img src="/startright/images/nav-already-in-canada-on.gif" width="241" height="35" alt="" border="0"></a></li>');
		document.write ('			<li class="mainnavItems"><a href="/cda/content/0,1608,CID12961_LIDen,00.html"><em>StartRight</em> Program for Landed Immigrants</a></li>');
		document.write ('			<li class="mainnavItems"><a href="/cda/content/0,1608,CID12962_LIDen,00.html"><em>StartRight</em> Program for International Students</a></li>');
		document.write ('			<li class="mainnavItems"><a href="/cda/content/0,1608,CID13565_LIDen,00.html"><em>StartRight</em> Program for Foreign Workers</a></li>');
		document.write ('		</ul>');
		document.write ('		<img src="/startright/images/nav-already-in-canada-bottom.gif" width="241" height="12" alt="" border="0"><br>');
		document.write ('	</li>');
			
			
		if (tabNum == 3 || tabNum == 4) {
			document.write ('	<li id="insideScotia-off"><a href="/cda/content/0,1608,CID12957_LIDen,00.html"><img src="/startright/images/nav-about-scotia-indicator.gif" width="235" height="35" alt="" border="0" onMouseOver="javascript:mainNavHover(\'insideScotia\');"></a></li>');

		} else {
			document.write ('	<li id="insideScotia-off"><a href="/cda/content/0,1608,CID12957_LIDen,00.html"><img src="/startright/images/nav-about-scotia-off.gif" width="235" height="35" alt="" border="0" onMouseOver="javascript:mainNavHover(\'insideScotia\');"></a></li>');
		}
		document.write ('	<li id="insideScotia-on">');
		document.write ('		<ul onMouseOut="javascript:mainNavHoverOff(\'insideScotia\');" onMouseMove="javascript:mainNavHover(\'insideScotia\');">');
		document.write ('			<li><a href="/cda/content/0,1608,CID12957_LIDen,00.html"><img src="/startright/images/nav-about-scotia-on.gif" width="235" height="35" alt="" border="0"></a></li>');
		document.write ('			<li class="mainnavItems"><a href="/cda/content/0,1608,CID12964_LIDen,00.html">Careers</a></li>');
		document.write ('			<li class="mainnavItems"><a href="/cda/content/0,1608,CID12966_LIDen,00.html">Newcomers Success Stories</a></li>');
		document.write ('			<li class="mainnavItems"><a href="/cda/content/0,1608,CID12967_LIDen,00.html">Resources for Newcomers</a></li>');
		document.write ('			<li class="mainnavItems"><a href="/cda/content/0,1608,CID13954_LIDen,00.html">Starting a Small Business</a></li>');
		document.write ('		</ul>');
		document.write ('		<img src="/startright/images/nav-about-scotia-bottom.gif" width="235" height="16" alt="" border="0"><br>');
		document.write ('	</li>');
		document.write ('</ul>');
		
}


function writeBanner() {
	featuredcontentglider.init({
			gliderid: "mcbBanner", //ID of main glider container
			contentclass: "glidecontent", //Shared CSS class name of each glider content
			togglerid: "p-select", //ID of toggler container
			remotecontent: "", //Get gliding contents from external file on server? "filename" or "" to disable
			selected: 0, //Default selected content index (0=1st)
			persiststate: false, //Remember last content shown within browser session (true/false)?
			speed: 500, //Glide animation duration (in milliseconds)
			direction: "rightleft", //set direction of glide: "updown", "downup", "leftright", or "rightleft"
			autorotate: true, //Auto rotate contents (true/false)?
			autorotateconfig: [8000, 2] //if auto rotate enabled, set [milliseconds_btw_rotations, cycles_before_stopping]
		})
}


var featuredcontentglider={
	csszindex: 100,
	ajaxloadingmsg: '<b>Fetching Content. Please wait...</b>',
	glide:function(config, showpage, isprev){
		var selected=parseInt(showpage)
		if (selected>=config.$contentdivs.length){ //if no content exists at this index position
			alert("No content exists at page "+(selected+1)+"! Loading 1st page instead.")
			selected=0
		}
		var $target=config.$contentdivs.eq(selected)
		//Test for toggler not being initialized yet, or user clicks on the currently selected page):
		if (config.$togglerdiv.attr('lastselected')==null || parseInt(config.$togglerdiv.attr('lastselected'))!=selected){
			var $selectedlink=config.$toc.eq(selected)
			config.$next.attr('loadpage', (selected<config.$contentdivs.length-1)? selected+1+'pg' : 0+'pg')
			config.$prev.attr('loadpage', (selected==0)? config.$contentdivs.length-1+'pg' : selected-1+'pg')
			var startpoint=(isprev=="previous")? -config.startpoint : config.startpoint
			$target.css(config.leftortop, startpoint).css("zIndex", this.csszindex++) //hide content so it's just out of view before animating it
			var endpoint=(config.leftortop=="left")? {left:0} : {top:0} //animate it into view
			$target.animate(endpoint, config.speed)
			config.$toc.removeClass('selected')
			$selectedlink.addClass('selected')
			config.$togglerdiv.attr('lastselected', selected+'pg')
		}
	},

	getremotecontent:function($, config){
		config.$glider.html(this.ajaxloadingmsg)
		$.ajax({
			url: config.remotecontent,
			error:function(ajaxrequest){
				config.$glider.html('Error fetching content.<br />Server Response: '+ajaxrequest.responseText)
			},
			success:function(content){
				config.$glider.html(content)
				featuredcontentglider.setuptoggler($, config)
			}
		})
	},

	aligncontents:function($, config){
		config.$contentdivs=$("#"+config.gliderid+" ."+config.contentclass)
		config.$contentdivs.css(config.leftortop, config.startpoint).css({height: config.$glider.height(), visibility: 'visible'}) //position content divs so they're out of view:
	},

	setuptoggler:function($, config){
		this.aligncontents($, config)
		config.$togglerdiv.hide()
		config.$toc.each(function(index){
				$(this).attr('pagenumber', index+'pg')
				if (index > (config.$contentdivs.length-1))
					$(this).css({display: 'none'}) //hide redundant "toc" links
		})
		var $nextandprev=$("#"+config.togglerid+" .next, #"+config.togglerid+" .prev")
		$nextandprev.click(function(event){ //Assign click behavior to 'next' and 'prev' links
			featuredcontentglider.glide(config, this.getAttribute('loadpage'), this.getAttribute('buttontype'))
			event.preventDefault() //cancel default link action
		})
		config.$toc.click(function(event){ //Assign click behavior to 'toc' links
			featuredcontentglider.glide(config, this.getAttribute('pagenumber'))
			event.preventDefault()
		})
		config.$togglerdiv.fadeIn(1000, function(){
			featuredcontentglider.glide(config, config.selected)
			if (config.autorotate==true){ //auto rotate contents?
				config.stepcount=0 //set steps taken
				config.totalsteps=config.$contentdivs.length*config.autorotateconfig[1] //Total steps limit: num of contents x num of user specified cycles)
				featuredcontentglider.autorotate(config)
			}
		})
		config.$togglerdiv.click(function(){
			featuredcontentglider.cancelautorotate(config.togglerid)
		})
	},

	autorotate:function(config){
		var rotatespeed=config.speed+config.autorotateconfig[0]
		window[config.togglerid+"timer"]=setInterval(function(){
			if (config.totalsteps>0 && config.stepcount>=config.totalsteps){
				clearInterval(window[config.togglerid+"timer"])
			}
			else{
				config.$next.triggerHandler('click')
				config.stepcount++
			}
		}, rotatespeed)
	},

	cancelautorotate:function(togglerid){
		if (window[togglerid+"timer"])
			clearInterval(window[togglerid+"timer"])
	},

	getCookie:function(Name){ 
		var re=new RegExp(Name+"=[^;]+", "i") //construct RE to search for target name/value pair
		if (document.cookie.match(re)) //if cookie found
			return document.cookie.match(re)[0].split("=")[1] //return its value
		return null
	},

	setCookie:function(name, value){
		document.cookie = name+"="+value
	},

	init:function(config){
		jQuery(document).ready(function($){
			config.$glider=$("#"+config.gliderid)
			config.$togglerdiv=$("#"+config.togglerid)
			config.$toc=config.$togglerdiv.find('.toc')
			config.$next=config.$togglerdiv.find('.next')
			config.$prev=config.$togglerdiv.find('.prev')
			config.$prev.attr('buttontype', 'previous')
			var selected=(config.persiststate)? featuredcontentglider.getCookie(config.gliderid) : config.selected
			config.selected=(isNaN(parseInt(selected))) ? config.selected : selected //test for cookie value containing null (1st page load) or "undefined" string	
			config.leftortop=(/up/i.test(config.direction))? "top" : "left" //set which CSS property to manipulate based on "direction"
			config.heightorwidth=(/up/i.test(config.direction))? config.$glider.height() : config.$glider.width() //Get glider height or width based on "direction"
			config.startpoint=(/^(left|up)/i.test(config.direction))? -config.heightorwidth : config.heightorwidth //set initial position of contents based on "direction"
			if (typeof config.remotecontent!="undefined" && config.remotecontent.length>0)
				featuredcontentglider.getremotecontent($, config)
			else
				featuredcontentglider.setuptoggler($, config)
			$(window).bind('unload', function(){ //clean up and persist
				config.$togglerdiv.unbind('click')
				config.$toc.unbind('click')
				config.$next.unbind('click')
				config.$prev.unbind('click')
				if (config.persiststate)
					featuredcontentglider.setCookie(config.gliderid, config.$togglerdiv.attr('lastselected'))
				config=null
				
			})
		})
	}
}


