/*
Cette cr�ation est mise � disposition selon le Contrat Paternit�-Partage des Conditions Initiales � l'Identique 3.0 Unported disponible en ligne http://creativecommons.org/licenses/by-sa/3.0/ ou par courrier postal � Creative Commons, 171 Second Street, Suite 300, San Francisco, California 94105, USA.
*/
/* extension prototype */

//Object.extend(Event, {
//        wheel:function (event){
//        var delta = 0;
//        if (event.wheelDelta) {
//			delta = event.wheelDelta/120;
//			if (window.opera) delta = -delta;
//        } else if (event.detail) {
//            delta = -event.detail/3;
//        }
//		if(navigator.userAgent.indexOf('Mac') != -1) delta = -delta;
//		return Math.round(delta);
//	}
//});

var Horinaja = Class.create();
Horinaja.prototype = {
	initialize: function(capture, duree, secExecution, pagination, width, pagination_limit){
		this.capture = capture;
		this.duree = duree;
		this.secExecution = secExecution;
		this.pagination = pagination;
        this.pagination_limit = pagination_limit;
        if (pagination_limit == null)
            this.pagination_limit = 3;
        this.px = width;
		this.nCell = 0;
		this.id = 1;
		this.po = 0;
		this.f = $$('div#'+this.capture+' ul li');
        if(width == null)
		    this.px = $(this.capture).getWidth();
		this.pxH = $(this.capture).getHeight();
		this.start();
	},
	start: function(){
				this.mover = $(this.capture).firstDescendant();
				this.nCell = this.f.length;
				for(i=0;i!=this.nCell;i++){
					this.f[i].setStyle({
					  width: this.px+'px',
					  float: 'left'
					  });
				}
				$(this.capture).setStyle({
					//overflow:'hidden',
					position: 'relative'
					});

				$(this.capture).firstDescendant().setStyle({
					width:(this.px*this.nCell)+'px'
					});
				if(this.pagination){
						$(this.capture).insert({bottom:'<ol class="horinaja_pagination"></ol>'});
                        this.olPagination  = $(this.capture).firstDescendant().next();
						$(this.olPagination).setStyle({
							width: this.px+''
							});
						this.wb = Math.floor(this.px/this.f.length);
                        if(this.pagination_limit == 3)
                            {
                            for(i=1;i!=(2);i++){
                                $(this.olPagination).insert({bottom:'<li><a>'+i+'</a></li>'});
                                if(i!=this.id){
                                    $(this.olPagination).childElements()[i-1].setStyle({
                                        opacity:0.4
                                        });
                                }
                            }
                            }
                           else
                            {
                            $(this.olPagination).insert({bottom:'<li id="prev"><a>'+'</a></li>'});
                            $('prev').childElements()[0].setStyle({

                            });

                            for(i=1;i!=(this.pagination_limit+1);i++){
                                    $(this.olPagination).insert({bottom:'<li class="fade_elements"><a>'+i+'</a></li>'});
                                    if(i!=this.id){
                                        $(this.olPagination).childElements()[i].setStyle({
                                            opacity:0.4
                                            });
                                    }
                                }
                            $(this.olPagination).insert({bottom:'<li id="next"><a>'+'</a></li>'});
                            $("next").setStyle({

                            });
                            }


						$(this.olPagination).childElements()[0].setStyle({
							opacity:1
						});

				}
                if(this.pagination_limit == 3)
                    {
                    this.startPe();

                    //Event.observe($(this.capture),"mouseout", this.startPe.bind(this));
                    //Event.observe($(this.capture),"mouseover", this.stopPe.bind(this));
                    //Event.observe($(this.capture), "mousewheel", this.wheelwheel.bind(this));
                    //Event.observe($(this.capture), "DOMMouseScroll", this.wheelwheel.bind(this));
                    }
                    else
                    {
                    //var nextBtn = $("next");
                    //var prevBtn = $("prev");

                    this.startPe();
                    //Event.observe($(nextBtn),"click",this.moveToNext.bind(this));
                    //Event.observe($(prevBtn),"click",this.moveToPrev.bind(this));


                    }

    this.startOC();
	},

	startOC: function(){
	Event.observe($(this.olPagination),"click", this.moveP.bind(this));
	},

    moveToNext: function(evt){
			this.movePNext(evt);
    },

    moveToPrev: function(evt){
			this.movePPrev(evt);
    },

	startPe: function(){
	this.periodik = new PeriodicalExecuter(this.Pe.bind(this),this.secExecution);
	},
	stopPe: function(){
	this.periodik.stop();
	},
	effaceP: function(mop){
		this.mop = mop;
		if(this.pagination)
		new Effect.Fade($(this.olPagination).childElements()[this.mop-1],{duration:0.3,to:0.2})
        //{
            //alert($(this.olPagination).getElementsByClassName("fade_elements")[this.mop]);
        //new Effect.Fade($(this.olPagination).getElementsByClassName("fade_elements")[1],{duration:0.3,to:0.2})
        //}
        //new Effect.Fade($("fade_elements"),{duration:0.3,to:0.2})

	},
	move: function(xp){
		this.xp = xp;
        if(this.pagination_limit == 4)
            {
            new Effect.Move(this.mover, {
                        x: this.xp*3,
                        y: 0,
                        mode:'absolute',
                        duration: this.duree,
                        transition: Effect.Transitions.sinoidal
                    });
                    if(this.pagination){
                        //new Effect.Appear($(this.olPagination).$("fade_elements"),{duration:0.3,to:0.2})
                        new Effect.Appear($(this.olPagination).childElements()[this.id-1],{duration:0.3,to:1})
                    }
            }
        else
        {
            new Effect.Move(this.mover, {
                        x: this.xp*3,
                        y: 0,
                        mode:'absolute',
                        duration: this.duree,
                        transition: Effect.Transitions.sinoidal
                    });
                    if(this.pagination){
                        
                        //    new Effect.Appear($(this.olPagination).getElementsByClassName("fade_elements")[this.id-1],{duration:0.3,to:1});

                        //new Effect.Appear($("fade_elements"),{duration:0.3,to:0.2})
                        new Effect.Appear($(this.olPagination).childElements()[this.id],{duration:0.3,to:1})
                    }
            }
	},
	Pe: function(){
					if(this.id<this.pagination_limit){
							this.po=this.po-this.px;
							this.effaceP(this.id);
                            this.id=this.id+1;
							this.move(this.po);
						}else{
							this.po=0;
							this.effaceP(this.id);
							this.id=1;
							this.move(this.po);
						}
	},
	moveP: function(evt){
		var child = Event.element(evt);
		this.occ = parseInt(child.innerHTML);
        if(this.pagination_limit == 3)
            {
            if(this.id>this.occ){
                this.diff= this.id-this.occ;
                this.po=this.po+(this.px*this.diff);
                this.effaceP(this.id);
                this.id=this.occ;
                this.move(this.po);
            }else if(this.id<this.occ){
                this.diff= this.occ-this.id;
                this.po=this.po-(this.px*this.diff);
                this.effaceP(this.id);
                this.id=this.occ;
                this.move(this.po);
            }
            }
            else
            {
            if(this.id>this.occ){
                this.diff= this.id-this.occ;
                this.po=this.po+(this.px*this.diff);
                this.effaceP(this.id+1);
                this.id=this.occ;
                this.move(this.po);
            }else if(this.id<this.occ){
                this.diff= this.occ-this.id;
                this.po=this.po-(this.px*this.diff);
                this.effaceP(this.id+1);
                this.id=this.occ;
                this.move(this.po);
            }
            }
	},

    movePNext: function(evt){
		if(this.pagination_limit > this.id)
        {
            var child = Event.element(evt);
		    this.occ = parseInt(this.id+1);
			this.diff= this.occ-this.id;
			this.po=this.po-(this.px*this.diff);
            this.effaceP(this.id+1);
			this.id=this.occ;
			this.move(this.po);
        }
        else
        {
            var child = Event.element(evt);
		    this.occ = parseInt(1);
			this.diff= this.occ-this.id;
			this.po=this.po-(this.px*this.diff);
            this.effaceP(this.id+1);
			this.id=this.occ;
			this.move(this.po);
        }

	},

    movePPrev: function(evt){
		if(this.id > 1)
        {
            var child = Event.element(evt);
		    this.occ = parseInt(this.id-1);
			this.diff= this.occ-this.id;
			this.po=this.po-(this.px*this.diff);
            this.effaceP(this.id+1);
			this.id=this.occ;
			this.move(this.po);
        }
        else
        {
            var child = Event.element(evt);
		    this.occ = parseInt(this.pagination_limit);
			this.diff= this.occ-this.id;
			this.po=this.po-(this.px*this.diff);
            this.effaceP(this.id+1);
			this.id=this.occ;
			this.move(this.po);
        }

	},

	stopEvent:function(pE)
	{
	   if (!pE)
		 if (window.event)
		   pE = window.event;
		 else
		   return;
	  if (pE.cancelBubble != null)
		 pE.cancelBubble = true;
	  if (pE.stopPropagation)
		 pE.stopPropagation();
	  if (pE.preventDefault)
		 pE.preventDefault();
	} ,
	wheelwheel: function(e){
						this.event = e;
						this.stopPe();
						this.stopEvent(e);
						if (Event.wheel(this.event) < 0){
							if(this.id<this.nCell){
									this.po=this.po-this.px;
									this.effaceP(this.id);
									this.id=this.id+1;
									this.move(this.po);
								}
						}else{
							if(this.id!=1){
									this.po=this.po+this.px;
									this.effaceP(this.id);
									this.id=this.id-1;
									this.move(this.po);
								}
						}
	}
};
