// Copyright (c) Ekinoxe Origin 2009
// Tous droits réservés

var accordion;

// ----- INITIALISATION

var init = function() {

  if($$('.acc_toggler').length>0)
    accordion = new Fx.Accordion($$('.acc_toggler'), $$('.acc_element'), { display: -1, alwaysHide: true });
  
}

// ----- PAGE ASYNCHRONES (AJAX)

var page = function(catid) {

  var remote = new Remote({ name:'categories' , command:'getCategoryPage' , params:{catid:catid} });

  remote.addEvent('complete', function(r) {
    var p = 'contenu';
    if($(p)) {
    
      $(p).set('html', r.getResponseData());
      
      if(typeof Milkbox != 'undefined')
        new Milkbox();
      
      myMooFlowPage.start();
    }
  }.bind(this));
  
  remote.addEvent('error', function(r) {
    W('Erreur pendant le chargement de la page, veuillez réessayez SVP.');
  }.bind(this));
  
  remote.call();

}

// --------- DIAPORAMA MOOFLOW

 var myMooFlowPage = {

	 start: function(){
		$$('.MooFlowieze').each(function(mooflow){
	  		 var milk_box = new Milkbox();		
			 var mf = new MooFlow(mooflow, {
				 startIndex: 1,
				 useSlider: true,
				 useCaption: true,
				 useResize: false,
				 useMouseWheel: true,
				 useKeyInput: true
			 	 /*onClickView: function(obj){
			 	 	D(obj.href);
			 	 	D(obj);
					var img = milk_box.loadImage(obj.href);
					D(img);
				 } */
			 });
		});
	 }

 };
 
// ----- RESEAUX SOCIAUX

/*
window.addEvent('domready', function() {
 SqueezeBox.initialize({ 'size': { 'x':800 , 'y':600 }}).assign($$('a.boxed'), { parse: 'rel' });
});
*/

// ----- DIAPORAMAS

window.addEvent('domready', function() {
  $$('.diaporama').each(function(diapo) {
    
    elements = {}

    elements.images    = diapo.getElements('div.picture img');
    elements.slider    = diapo.getElements('div.slider')[0];
    elements.sliderDiv = diapo.getElements('div.slider > div')[0];
    elements.toLeft    = diapo.getElements('div.left a')[0]; // $$('.slider')[0].scrollTo(100,100)
    elements.toRight   = diapo.getElements('div.right a')[0];
    
    to = 330;
    max = (elements.images.length)*to;
    a = elements.slider.getScrollLeft();
    this.step = 0;
    this.maxStep = elements.images.length-1;
    this.myFx = new Fx.Scroll(elements.slider, { transition:Fx.Transitions.Quad.easeInOut , duration:300 });
    
    elements.sliderDiv.setStyles({'width':max+to});

    elements.toLeft.addEvent('mouseup', function(diapo, elements, imgSize, max) {
      /*
      a = elements.slider.getScrollLeft();
      x = ((a-imgSize)<0) ? max-imgSize : a-imgSize;
      this.myFx.cancel().start(x,0);
      */
      if(this.step<=0) {
        this.step = this.maxStep;
        x = imgSize*this.step;
      } else {
        this.step--;
        x = imgSize*this.step;
      }
      this.myFx.cancel().start(x,0);
    }.bind(this, [diapo, elements, to, max]));
    
    elements.toRight.addEvent('mouseup', function(diapo, elements, imgSize, max) {
      if(this.step>=this.maxStep) {
        this.step = 0;
        x = 0;
      } else {
        this.step++;
        x = imgSize*this.step;
      }
      this.myFx.cancel().start(x,0);
    }.bind(this, [diapo, elements, to, max]));
    
  });
});

// ----- JUST FOR FUN. :)

var Konomi = new Class({
code: ["up","up","down","down","left","right","left","right","b", "a","enter"],
step: 0,
initialize: function(func1,func2,func3){
if($defined(func2))
this.func2 = func2;
if($defined(func3))
this.func3 = func3;
this.func = func1;
window.addEvent('keyup',this.keystroke.bind(this));
},
keystroke: function(e){
ev = new Event(e);
if(ev.key == this.code[this.step]){
if($defined(this.func2))
this.func2.attempt(this.step);
this.step++;
if(this.step == this.code.length){
this.step = 0;
this.func.attempt();
}
} else {
this.step = 0;
if($defined(this.func3))
this.func3.attempt();
}
}  
});
new Konomi(function() {
var canvas,ctx,canvContainer,outerDiv,canvasW=960,canvasH=276,friction=0.96,numMovers=160,movers=[],isMouseDown=false,
mouseX=0,mouseY=0,mouseVX=0,mouseVY=0,prevMouseX=0,prevMouseY=0,quartCirc=Math.PI*0.5;
function bullshit(){canvas=document.getElementById("mainCanvas");if(canvas.getContext){setup();setInterval(run,25);}}
function setup(){outerDiv=document.getElementById("outer");outerDiv.style.height="276px";canvContainer=document.getElementById("canvasContainer");ctx=canvas.getContext("2d");for(var b="$SITEKINOX".split(""),i=numMovers;i--;){var e=new Mover;e.x=Math.random()*
canvasW;e.y=Math.random()*canvasH;e.vX=60-Math.random()*120;e.vY=60-Math.random()*120;e.size=2;e.lett=b[i%b.length];movers[i]=e}document.onmousedown=onDocMouseDown;document.onmouseup=onDocMouseUp;document.onmousemove=onDocMouseMove}
function run(){ctx.globalCompositeOperation="source-over";ctx.fillStyle="rgba(8,8,12,.65)";ctx.fillRect(0,0,canvasW,canvasH);ctx.globalCompositeOperation="lighter";mouseVX=mouseX-prevMouseX;mouseVY=mouseY-prevMouseY;prevMouseX=mouseX;prevMouseY=mouseY;for(var b=canvasW/1.65,i=canvasW/8,e=canvasW/2,l=numMovers;l--;){var f=movers[l],j=f.x,m=f.y,g=f.vX,h=f.vY,d=j-mouseX,a=m-mouseY,c=Math.sqrt(d*d+a*a);a=Math.atan2(a,d);d=Math.cos(a);a=Math.sin(a);if(isMouseDown)if(c<e){var k=(1-c/e)*14;g+=d*k+0.5-Math.random();
h+=a*k+0.5-Math.random()}if(c<b){k=(1-c/b)*canvasW*0.0019;g-=d*k;h-=a*k}if(c<i){c=(1-c/i)*canvasW*2.5E-4;g+=mouseVX*c;h+=mouseVY*c}g*=friction;h*=friction;c=(Math.abs(g)+Math.abs(h))*0.5*1.75;c=Math.min(c,72);c=Math.max(c,2);d=j+g;a=m+h;if(d>canvasW){d=canvasW;g*=-1}else if(d<0){d=0;g*=-1}if(a>canvasH){a=canvasH;h*=-1}else if(a<0){a=0;h*=-1}f.vX=g;f.vY=h;f.x=d;f.y=a;j=Math.atan2(a-m,d-j)+quartCirc;ctx.save();ctx.translate(d,a);ctx.rotate(j);ctx.fillStyle=f.color;ctx.font=c+"pt Arial";ctx.fillText(f.lett,
0,0);ctx.restore()}}function onDocMouseMove(b){b=b?b:window.event;mouseX=b.clientX-outerDiv.offsetLeft-canvContainer.offsetLeft;mouseY=b.clientY-outerDiv.offsetTop-canvContainer.offsetTop}function onDocMouseDown(){isMouseDown=true;return false}function onDocMouseUp(){return isMouseDown=false}function Mover(){this.color="rgb("+Math.floor(Math.random()*255)+","+Math.floor(Math.random()*255)+","+Math.floor(Math.random()*255)+")";this.size=this.vY=this.vX=this.x=this.y=0;this.lett=""}
function rect(b,i,e,l){ctx.beginPath();ctx.rect(b,i,e,l);ctx.closePath();ctx.fill()}function trace(b){document.getElementById("output").innerHTML=b}bullshit();
});

// ----- EXECUTION DOMREADY

window.addEvent('domready', init );
window.addEvent('domready', myMooFlowPage.start); 
window.addEvent('domready', function(){
	if($('webcallback_submit')){
		$('webcallback_submit').addEvent('click', function(){
			var reg = new RegExp("^([0-9]{10,})$");
			//clean
			var tel = $('webcallback_tel').value;
			tel = tel.clean();
			tel = tel.replace(/ /g,'');
			$('webcallback_tel').value = tel;
			if(reg.exec($('webcallback_tel').value))
				$('webcallback_form').submit();
			else
				alert('veuillez saisir un numéro de téléphone correct');
		});
	}
});
