/**
 * carousel.js v1.0
 * 
 * ©Copyright 2009 François Monette <francois@quali-t-innovation.com>
 * 
 * Trigosoft inc. <info@trigosoft.net>, hereby disclaims all copyright interest 
 * in the program “carousel.js” written by François Monette.
 * 
 *  Samuel Sirois <samuel@trigosoft.net>, 12 October 2009
 *  Samuel Sirois, President of Trigosoft inc.
 *    
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
 var nitems = 0;
 var size = []; 
 var pos = []; 
// var center = [400,200,500];
 var center = [225, 100, 500];
 var angle = [];
 var radius = 125; // rayon du cercle dont le centre est "center"
 var destangle = 0;
 var movevel = 0;
 var startupstate = 0;
 var startupimg = 0;
 
 function Transform(onum)
  {
	var obj = document.getElementById("img" + onum);
	var ratio = size[onum][1] / size[onum][0];
	
	// Effacer l'image si elle est "derriere nous"
	if(pos[onum][2] <= 0)
	 obj.style.display = "none";
	else
	 obj.style.display = "inline";
	 
	var height = size[onum][1] / (pos[onum][2] / 400);
	var width = height / ratio;
	
	var xpos = pos[onum][0] - width / 2;
	var ypos = pos[onum][1] - height /2;
	
	
	obj.style.width = width + "px";
	obj.style.height = height + "px";
	obj.style.left = xpos + "px";
	obj.style.top = ypos + "px";
	
	obj.style.zIndex = Math.floor(1000 - pos[onum][2]);	
  }
 
 function SetPos(onum)
  {
	pos[onum][0] = center[0] + radius * Math.cos(angle[onum] * Math.PI/180);	
    pos[onum][1] = center[1];
	pos[onum][2] = center[2] + radius * Math.sin(angle[onum] * Math.PI/180);
    
  }
 
 function TransformAll()
  {
	 for(var i=0;i < nitems;i++)
	  {
		SetPos(i);
		Transform(i);
      }
  }
  
 function SetInitialPos()
  {
	var step = 360 / nitems;
	var pos = 0;
	
	for(var i=0;i< nitems;i++,pos+=step)
	 {
	  angle[i] = pos;
     }
  }
  
 function StepAll(vec)
  {
    for(var i=0;i < nitems;i++)
	 {
      angle[i] += vec;
     }
  }
 
 function DoMove()
  {
   StepAll(movevel);
   TransformAll();
      
   if(movevel > 0)
    {
     if(angle[0] < destangle)
      { 
       setTimeout(DoMove,1);
      }
     else
      movevel = 0;
    }
   else
    if(angle[0] > destangle)
     {
	   setTimeout(DoMove,1);
     }
    else
     movevel = 0;
  }
    
      
 function TurnLeft()
  {
	 if(movevel == 0)
    {
     destangle = angle[0] + 360 / nitems;
     movevel = 2;
   
     setTimeout(DoMove,1);
    }
  }
 
 function TurnRight()
  {
	 if(movevel == 0)
    {
     destangle = angle[0] - 360 / nitems;
     movevel = -2;
   
     setTimeout(DoMove,1);
    }
  }

 function InitCarousel()
  {
	 addEventListener(document.getElementById("left"), "click", TurnLeft);
	 addEventListener(document.getElementById("right"), "click", TurnRight);
	 
	 nitems = $("#recentProjects li").length;
   
   $("#recentProjects li img").each(function ()
    {
	  size.push([this.width,this.height]);
	  pos.push([]);
	  angle.push(0);
    });
	  
   SetInitialPos();
   TransformAll();
  }

 addLoadEvent(InitCarousel);