// JavaScript Document
function isBadScroll(){var ua=navigator.userAgent.toUpperCase(),r=/Blackberry[\s\:\-\/\#]*([0-9]+)/i,m=ua.match(r),v=parseInt(m&&m[1]||0,10);
//return (v==9530);
//return (v>=9500&&v<=9590);
return (v>7000);
}
function gid(x){return document.getElementById(x);}
function intval(x){return parseInt(x||0,10);}

	/**
	 * Contact Tab Panel Swapping function
	 *
	 * @variable mc     <String>    The Montreal button CSS class name style (selected or not)
	 * @variable md     <String>    The Montreal panel display value (block or none)
	 * @variable tc     <String>    The Toronto  button CSS class name style (selected or not)
	 * @variable td     <String>    The Toronto  panel display value (block or none)
	 */
	function contactcard(mc,md,tc,td){
	var mon =gid('mon'),
		mond=gid('mond'),
		tor =gid('tor'),
		tord=gid('tord');

		mon.className=mc;
		mond.style.display=md;
		tor.className=tc;
		tord.style.display=td;
	}
	


/**
 * Contact Tab Panel Trigger and Swapping
 *
 * @variable mon    <DOMElement>    The Montreal button
 * @variable mond   <DOMElement>    The Montreal panel
 *
 * @variable tor    <DOMElement>    The Toronto  button
 * @variable tord   <DOMElement>    The Toronto  panel
 */
function contact()
{
	var mon =gid('mon'),
		monc=gid('monc'),
		mond=gid('mond'),
		tor =gid('tor'),
		torc=gid('torc'),
		tord=gid('tord');

	var fm=function(){contactcard('','block','des','none');return false;},
	ft=function(){contactcard('des','none','','block');return false;};

//	if(mon)mon.onclick=fm;
//	if(tor)tor.onclick=ft;

	if(monc)monc.onclick=fm;
	if(torc)torc.onclick=ft;
}

/**
 * Stop the current animation
 *
 * @variable timer  <Interval>      Interval object
 * @variable timer2 <Interval>      Interval object
 * @variable timer3 <Timer>         Timer object
 * @variable timer4 <Timer>         Timer object
 *
 * @variable dx     <Integer>       Delta   x (How much do we move by)
 * @variable c      <Boolean>       Did we received a click event to abort the initial animation flow
 * @variable adt    <Integer>       Animation delta time for the initial flow animation
 */
var timer   = 0,
	timer2  = 0,
	timer3  = 0,
	timer4  = 0,
	dx      = 15,
	dy      = 15*8, //15*3, //15*2
	adt     = 2000,
	c       = 0;

var badanim=0;//isBadAnim();

function stop(e){
	try{
		if(timer)clearInterval(timer);
		if(timer2)clearInterval(timer2);
		if(timer3)clearTimeout(timer3);
		if(timer4)clearTimeout(timer4);
	}catch(e){}

	timer=0;
	timer2=0;
	timer3=0;
	timer4=0;
}


/**
 * Carrousel Image Animation with Button handlers
 *
 * @variable next   <DOMElement>    Next button DOM Element
 * @variable prev   <DOMElement>    Previous button DOM Element
 * @variable anim   <DOMElement>    Animation container DOM Element
 * @variable li     <Array>         Array of list item DOM Elements
 * @variable w      <Integer>       Image width
 *
 * Animation variable:
 *
 * @variable a      <Function>      Animation callback function (used for the initial flow, so it is timer recursive)
 * @variable x      <Integer>       How much pixel to shift the DIV container by?
 * @variable mx     <Integer>       Maximum x (Total width minus one image)
 * @variable ix     <Integer>       Initial and Current x (Where do we start the initial animation from and where are we at now?)
 * @variable tx     <Integer>       Target  x (What is the desired target)
 */
function carrousel(){
	var next = gid('next'),
		prev = gid('prev'),
		anim = gid('anim'),
		car  = gid('carrous'),
		text,
		li   = anim.childNodes,
		n    = li.length,
		m    = 0,
		lif  = li[0],
		w,
		h,
		mx,
		ix   = 0,
		x;

	// Fix Symbian OS, DOM text insertion problem
	while(n--)
	{
		if(li[n].tagName){m++;lif=li[n];}
	}

	w    = parseInt(lif.offsetWidth,10);
	h    = parseInt(lif.offsetHeight,10);
	mx   = w*(n=m-1);

	if(badanim)dx=w;

	if(mx<0)mx=0;

	/**
	 * Animation step going left (Moving to the next picture)
	 */
	function animLeft(tx,a){
		if(ix<tx){
			ix+=dx;
			if(ix>tx)ix=tx;
		}else{
			stop();
			if(a&&!c)
				timer3=setTimeout(function(e){
					a(e,a);
				},adt);
		}

		x=-ix;
		if(badanim)
			anim.style.top=intval(x/w*h)+'px';
		else
			anim.style.left=x+'px';
	}

	/**
	 * Animation step going right (Moving to the previous picture)
	 */
	function animRight(tx,a){
		if(ix>tx){
			ix-=dx;
			if(ix<tx)ix=tx;
		}else{
			stop();
			if(a&&!c)
				timer3=setTimeout(function(e){
					a(e,a);
				},adt);
		}

		x=-ix;
		if(badanim)
			anim.style.top=intval(x/w*h)+'px';
		else
			anim.style.left=x+'px';
	}

	/**
	 * Start the animation loop moving to the next picture
	 */
	function goNext(e,a){
		var tx=ix+w;
		if(!a){c=1;stop();}
		if(tx>mx){
			tx=0;
			/*
			if(badanim)
			{
				anim.style.left='0px';
				alert('left:0');
			}
			else
			*/
				timer=setInterval(function(){
					animRight(tx,a);
				},1);
		}else{
			/*
			if(badanim)
				anim.style.left=(-tx)+'px';
			else
			*/
				timer=setInterval(function(){
					animLeft(tx,a);
				},1);
		}
	}

	/**
	 * Start the animation loop moving to the previous picture
	 */
	function goPrev(e,a){
		var tx=ix-w;
		if(!a){c=1;stop();}
		if(tx<0){
			tx=mx;
			/*
			if(badanim)
			{
				alert('left:'+(-tx)+'px');
				anim.style.left=(-tx)+'px';
			}
			else
			*/
				timer=setInterval(function(){
					animLeft(tx,a);
				},1);
		}else{
			/*
			if(badanim)
			{
				alert('left:'+(-tx)+'px');
				anim.style.left=(-tx)+'px';
			}
			else
			*/
				timer=setInterval(function(){
					animRight(tx,a);
				},1);
		}
	}

	if(!next){
		next=document.createElement('a');
		next.setAttribute('id','next');
		next.setAttribute('href','javascript:void(0);');
		//next.setAttribute('style','text-decoration:none;');
		text=document.createTextNode('\u00A0');
		next.appendChild(text);
		car.appendChild(next);
	}

	if(!prev){
		prev=document.createElement('a');
		prev.setAttribute('id','prev');
		prev.setAttribute('href','javascript:void(0);');
		//prev.setAttribute('style','text-decoration:none;');
		text=document.createTextNode('\u00A0');
		prev.appendChild(text);
		car.appendChild(prev);
	}

	/**
	 * Bind click triggers for arrows
	 */
	if(next)next.onclick=goNext;
	if(prev)prev.onclick=goPrev;

	/**
	 * Start the slow initial animation flow
	 */
	if(!badanim)
		timer4=setTimeout(function(e){
			goNext(e,goNext);
		},adt);
}

/**
 * Animation step going left (Moving to the next picture)
 */
function animDown(ty){
	iy=parseInt(window.scrollY||window.pageYOffset||document.body.scrollTop||document.body.parentNode.scrollTop,10)||0;
	var done=0;
	if(iy<ty){
		iy+=dy;
		if(iy>ty)iy=ty;
	}else{
		//alert('stop');
		//console.log('animDown: stop');
		stop();
		done=1;
	}

	//console.log('animDown:',iy,'>',ty,dx);

	var f=window.scrollTo||window.scroll;
	f(0,iy);
}

/**
 * Animation step going right (Moving to the previous picture)
 */
function animUp(ty){
	iy=parseInt(window.scrollY||window.pageYOffset||document.body.scrollTop||document.body.parentNode.scrollTop,10)||0;
	var done=0;
	if(iy>ty){
		iy-=dy;
		if(iy<ty)iy=ty;
	}else{
		//alert('stop');
		//console.log('animUp: stop');
		stop();
		done=1;
	}

	//console.log('animUp:',iy,'>',ty,dx);
	var f=window.scrollTo||window.scroll;
	f(0,iy);
}

function goDown(iy,y){
	stop();
	//console.log('goDown: stop');
	if(y<iy){
		timer2=setInterval(function(){
			animUp(y);
		},100);
	}else{
		timer2=setInterval(function(){
			animDown(y);
		},100);
	}
}

/**
 * Top menu animation
 *
 * @variable a      Array of id for top menu buttons
 * @variable n      Number of top menu buttons and auto-decrementing loop counter
 * @variable b      The top menu button DOM Element
 *
 * @variable id     The id of the target, which is the id of the top menu button without the 'm' prefix
 * @variable to     The target DOM element to be scroll to

 * @variable iy     The window current Y position (Initial Y for the animation)
 * @variable y      The window Y position of that target element to be scroll to
 */
function menu()
{
	var a=['mc','mw','ms'],
		n=a.length,
		b;

	while(n--)
	{
		b=gid(a[n]);
		b.onclick=function(){
			var id=(this.id||'').substring(1),
				to=gid(id),
				y=parseInt(to.offsetTop,10),
				iy=parseInt(window.scrollY||window.pageYOffset||document.body.scrollTop||document.body.parentNode.scrollTop,10)||0;
			//console.log(iy+':'+y);
			//window.scroll(0,y);
			goDown(iy,y);
		};
		b.setAttribute('href','javascript:void(0);');
	}
}

function project(){

}

/**
 * When the page loads...
 */
function loader(){
	contact();
	carrousel();

	if(!isBadScroll())
	menu();

/*
	if(!badanim)
	{
	}
	else
	{
		var c=gid('p');
		if(c)c.className='badanim';
	}
*/

	project();
}

window.onload=loader;


