// script.aculo.us scriptaculous.js v1.8.0, Tue Nov 06 15:01:40 +0300 2007

// Copyright (c) 2005-2007 Thomas Fuchs (http://script.aculo.us, http://mir.aculo.us)
// 
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
// 
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
// For details, see the script.aculo.us web site: http://script.aculo.us/


if(typeof(AC)=="undefined"){AC={};}

AC.produitdemo={
productSlider:null,
sliderVal:0,
animationId:false,
viewportWidth:946,
contentWidth:946,
categories:[
{id:'pb-cat1',offset:0},
{id:'pb-cat2',offset:0.32}
],
isIPhone:AC.Detector.isiPhone(),
arrowScrollAmount:0.24,
iPhoneScrollAmount:0.22,
iPhoneContainerWidth:834,
isSliding:false,
lastX:0.32,
isMouseDown:false,
dif:0,
overlap:0,
offsetImageWidth:127,
sliderOffset:291,
offsetContentWidth:-946,
clicked:false,
startIndex:0,

isIpScroll:false,
hasIpDragged:false,
init:function(setupArgs){
if(typeof(setupArgs.categories)!='undefined')this.categories=setupArgs.categories;
if(typeof(setupArgs.imageOverlap)!='undefined')this.overlap=setupArgs.imageOverlap;
if(typeof(setupArgs.sliderCentering)!='undefined')this.sliderOffset=setupArgs.sliderCentering;
if(typeof(setupArgs.initialCategory)!='undefined')this.startIndex=setupArgs.initialCategory;
if(typeof(setupArgs.arrowScrollAmount)!='undefined')this.arrowScrollAmount=setupArgs.arrowScrollAmount;
if(typeof(setupArgs.iPhoneCategories)!='undefined'){this.iPhoneCategories=setupArgs.iPhoneCategories;}
if(typeof(setupArgs.iPhoneScrollAmount)!='undefined')this.iPhoneScrollAmount=setupArgs.iPhoneScrollAmount;
if(typeof(setupArgs.iPhoneContainerWidth)!='undefined')this.iPhoneContainerWidth=setupArgs.iPhoneContainerWidth;

$('produitscrolltrack').style.visibility="visible";
$('produitdemocontent').style.overflow="hidden";

this.viewportWidth=$('produitdemocontent').getWidth();
this.offsetImageWidth=$$('#produitscroll .produitimage')[0].getWidth()-this.overlap;
this.contentWidth=this.offsetImageWidth*$$('#produitscroll .produitimage').length;
this.offsetContentWidth=-1*(this.contentWidth-this.viewportWidth);

this.productSlider=new Control.Slider('produitscrollhandle','produitscrolltrack',{
axis:'horizontal'
});

if(AC.produitdemo.isIPhone){
this.categories=this.iPhoneCategories;
this.arrowScrollAmount=this.iPhoneScrollAmount;
$('produitscrolltrack').style.visibility="none";
$('pb-leftarrow').style.visibility="none";
$('pb-rightarrow').style.visibility="none";
$('produitdemocontent').style.width=this.iPhoneContainerWidth+"px";

var leftArrow=document.createElement('div');
leftArrow.id="pb-iphone-leftarrow";

var rightArrow=document.createElement('div');
rightArrow.id="pb-iphone-rightarrow";

$('produitdemo').appendChild(leftArrow);
$('produitdemo').appendChild(rightArrow);

Event.observe($(leftArrow),'click',function(){
AC.produitdemo.left();
});

Event.observe($(rightArrow),'click',function(){
AC.produitdemo.right();
});
}

AC.produitdemo.animateSlide(this.categories[this.startIndex].offset);


this.productSlider.options.onChange=function(value){
$('produitscrollhandleimage').style.left=$('produitscrollhandle').style.left;

if(AC.produitdemo.isThrow&&!AC.produitdemo.isSliding){
AC.produitdemo.isSliding=true;
AC.produitdemo.isThrow=false;

var mod=value+AC.produitdemo.throwMod;
if(mod<0)mod=0;
if(mod>1)mod=1;
AC.produitdemo.animateSlide(mod);
}
else if(!AC.produitdemo.isSliding&&value){
AC.produitdemo.isSliding=true;
AC.produitdemo.animateSlide(value);
}
};

this.productSlider.options.onSlide=function(value){
$('produitscrollhandleimage').style.left=$('produitscrollhandle').style.left;
if(value&&!AC.produitdemo.isSliding){
AC.produitdemo.isSliding=true;
AC.produitdemo.isThrow=false;
if(AC.produitdemo.isMouseDown){
AC.produitdemo.dif=value-AC.produitdemo.lastX;
AC.produitdemo.lastX=value;


if(AC.produitdemo.dif>0.05){
AC.produitdemo.isThrow=true;
AC.produitdemo.throwMod=0.2;
}
else if(AC.produitdemo.dif<-0.04){
AC.produitdemo.isThrow=true;
AC.produitdemo.throwMod=-0.2;
}
}

var w=AC.produitdemo.offsetContentWidth;
$('produitscroll').style.left=w*value+"px";

this.sliderVal=value;
AC.produitdemo.lastX=value;
AC.produitdemo.colorCats();
AC.produitdemo.isSliding=false;
}

Element.setStyle($('produitdemocontent'),{overflow:"hidden"});
};

Event.observe('produitscrolltrack','mousedown',function(e){
var o=e.offsetX||e.layerX;
if(Event.element(e).id=='produitscrolltrack'&&o<100)AC.produitdemo.animateSlide(0);
});
Event.observe('pb-leftarrow','mousedown',function(){
AC.produitdemo.left();
});
Event.observe('pb-rightarrow','mousedown',function(){
AC.produitdemo.right();
});

Event.observe('produitscrollhandle','mousedown',function(){
AC.produitdemo.isMouseDown=true;
$('produitscrollhandle').style.zIndex="5";
});

Event.observe('produitscrollhandle','mouseup',function(){
AC.produitdemo.isMouseDown=false;
});

AC.produitdemo.categories.each(function(c){
Event.observe($(c.id),'mouseup',function(e){
AC.produitdemo.animateSlide(c.offset);
});
});
},

animateSlide:function(toX){

if(toX>1)toX=1;
if(toX<0)toX=0;
AC.produitdemo.sliderVal=toX;
window.clearInterval(AC.produitdemo.animationId);
var w=AC.produitdemo.offsetContentWidth;
var stopPoint=w*toX;

var sliderStopPoint=(Math.round(AC.produitdemo.viewportWidth-AC.produitdemo.sliderOffset)*toX);

AC.produitdemo.isSliding=true;
AC.produitdemo.animationId=window.setInterval(function(){
var sliderPos=parseInt($('produitscroll').getStyle('left'))||0;
var handlePos=parseInt($('produitscrollhandle').getStyle('left'))||0;
var x=AC.produitdemo.calculateDecel(sliderPos,stopPoint);
var sx=AC.produitdemo.calculateDecel(handlePos,sliderStopPoint);
$('produitscroll').style.left=x+"px";
$('produitscrollhandle').style.left=sx+"px";
$('produitscrollhandleimage').style.left=sx+"px";
AC.produitdemo.colorCats();

if(x==stopPoint){
window.clearInterval(AC.produitdemo.animationId);
AC.produitdemo.isSliding=false;
}
},30);

},

colorCats:function(){
var sliderX=parseInt($('produitscrollhandle').getStyle('left'))+(($('produitscrollhandle').getWidth()-20)/2);
AC.produitdemo.categories.each(function(c){
var left=parseInt($(c.id).getStyle('left'))
var clr=Math.ceil((Math.min(sliderX,left)/Math.max(sliderX,left))*10);
$(c.id).className='pb-catclass'+clr;
});
},

left:function(){
AC.produitdemo.animateSlide(AC.produitdemo.sliderVal-AC.produitdemo.arrowScrollAmount);
},

right:function(){
AC.produitdemo.animateSlide(AC.produitdemo.sliderVal+AC.produitdemo.arrowScrollAmount);
},

calculateDecel:function(from,to){
var n=from-Math.floor((from-to)*.4);
if(Math.abs(from-to)<4)return to;
else return n;
}
};
