/*
Copyright (c) 2009 Victor Stanciu - http://www.victorstanciu.ro

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.
*/

Box = Class.create(Abstract, {
	initialize: function (scroller, slides, controls, options) {
		this.scrolling	= false;
		this.scroller	= $(scroller);
		this.slides		= slides;
		this.controls	= controls;
		this.i = 0;
		this.growFirstLaunch = 1;
		this.goCircle = false;
		this.options    = Object.extend({
            duration:           1,
            auto:               false,
            frequency:          3000,
            visibleSlides:      1,
            controlClassName:   'carousel-control',
            jumperClassName:    'carousel-jumper',
            disabledClassName:  'carousel-disabled',
            selectedClassName:  'carousel-selected',
            circular:           false,
            wheel:              true,
            effect:             'scroll',
            transition:         'sinoidal',
            direction:          'left_to_right',
            imageWidth:          0,
            imageHeight:         0,
			nextPrev:			 'next',
			imageSmallWidth:	 0
        }, options || {});
        

		if(this.options.effect == 'morph' || this.options.effect == 'grow' || this.options.effect == 'fold') {
			if(document.getElementById('carousel-control-wrapper')) {
				$('carousel-control-wrapper').hide();
			}
		}

        if (this.options.effect == 'fade') {
            this.options.circular = true;
        }

		this.slides.each(function(slide, index) {
			slide._index = index;
        });

		if (this.controls) {
            this.controls.invoke('observe', 'click', this.click.bind(this));
        }
        
        if (this.options.wheel) {            
            this.scroller.observe('mousewheel', this.wheel.bindAsEventListener(this)).observe('DOMMouseScroll', this.wheel.bindAsEventListener(this));;
        }

        if (this.options.auto) {
            this.start();
        }

		if (this.options.initial) {
			var initialIndex = this.slides.indexOf($(this.options.initial));
			if (initialIndex > (this.options.visibleSlides - 1) && this.options.visibleSlides > 1) {               
				if (initialIndex > this.slides.length - (this.options.visibleSlides + 1)) {
					initialIndex = this.slides.length - this.options.visibleSlides;
				}
			}
            this.moveTo(this.slides[initialIndex]);
		}
	},

	click: function (event) {
		this.stop();

		var element = event.findElement('a');
		
		if(element.rel == '') {
			return true;
		}

		if (!element.hasClassName(this.options.disabledClassName)) {
			if (element.hasClassName(this.options.controlClassName)) {
				eval("this." + element.rel + "()");
            } else if (element.hasClassName(this.options.jumperClassName)) {
                this.moveTo(element.rel);
                if (this.options.selectedClassName) {
                    this.controls.invoke('removeClassName', this.options.selectedClassName);
                    element.addClassName(this.options.selectedClassName);
                }
            }
        }

		this.deactivateControls();

		event.stop();
    },

	moveTo: function (element) {
		if (this.options.beforeMove && (typeof this.options.beforeMove == 'function')) {
			this.options.beforeMove();
        }

		this.previous = this.current ? this.current : this.slides[1];
		this.current  = $(element);

		var scrollerOffset = this.scroller.cumulativeOffset();
		var elementOffset  = this.current.cumulativeOffset();

		if (this.scrolling) {
			this.scrolling.cancel();
		}

        switch (this.options.effect) {
			case 'scroll':
            default:
                var transition;
                switch (this.options.transition) {
                    case 'spring':
                        transition = Effect.Transitions.spring;
                        break;
                    case 'sinoidal':
                    default:
                        transition = Effect.Transitions.sinoidal;
                        break;
                    case 'linear':
                        transition = Effect.Transitions.linear;
                        break;
                    case 'flicker':
                        transition = Effect.Transitions.flicker;
                        break;
                    case 'wobble':
                        transition = Effect.Transitions.wobble;
                        break;
                    case 'pulse':
                        transition = Effect.Transitions.pulse;
                        break;
                }
				if(this.goCircle) {
					var minus = 0;
				} else {
					if(this.options.nextPrev == 'prev') {
						var minus = 0;
					} else if(this.options.nextPrev == 'next') {
						var minus = this.options.imageWidth - this.options.imageSmallWidth;
					}
				}
                this.scrolling = new Effect.SmoothScroll2(this.scroller, {
                    duration: this.options.duration,
					imageWidth: this.options.imageWidth,
					imageHeight: this.options.imageHeight,
					imageSmallWidth: this.options.imageSmallWidth,
					current: this.current,
					previous: this.previous,
					x: (elementOffset[0] - scrollerOffset[0] - minus),
                    y: (elementOffset[1] - scrollerOffset[1]),
                    transition: transition,
                    afterFinish: (function () {
                        if (this.controls) {
                            this.activateControls();
                        }
                        if (this.options.afterMove && (typeof this.options.afterMove == 'function')) {
                            this.options.afterMove();
                        }                        
                        this.scrolling = false;
                    }).bind(this)});
				
            break;
        }

		return false;
	},

	prev: function () {
		if (this.current) {
			var currentIndex = this.current._index;
			var prevIndex = (currentIndex == 0) ? (this.options.circular ? this.slides.length - 1 : 0) : currentIndex - 1;
        } else {
            var prevIndex = (this.options.circular ? this.slides.length - 1 : 0);
			var currentIndex = 1;
        }
		if (currentIndex == 1 && this.options.circular && this.options.effect == 'scroll') {
			this.scroller.scrollLeft = (((this.slides.length-1)/2)) * this.options.imageSmallWidth;
			this.scroller.scrollTop  = 0;
			prevIndex = ((this.slides.length-1)/2);
			this.current = this.slides[((this.slides.length-1)/2)+1];
			
			this.slides[(((this.slides.length-1)/2)+1)].childElements()[0].childElements()[0].setStyle({width: this.slides[1].childElements()[0].childElements()[0].getWidth() + "px", height: this.slides[1].childElements()[0].childElements()[0].getHeight() + "px"});

			this.slides[1].childElements()[0].childElements()[0].setStyle({width: this.slides[0].childElements()[0].childElements()[0].getWidth() + "px", height: this.slides[0].childElements()[0].childElements()[0].getHeight() + "px"});

        }

		this.slides[currentIndex].childElements()[0].setAttribute('rel', 'next');
		this.slides[prevIndex].childElements()[0].setAttribute('rel', '');
		this.slides[prevIndex-1].childElements()[0].setAttribute('rel', 'prev');
//		carousel-control
		this.options.nextPrev = 'prev';
		this.moveTo(this.slides[prevIndex]);
	},

	next: function () {
		if (this.current) {
			var currentIndex = this.current._index;
			var nextIndex = (this.slides.length - 1 == currentIndex) ? (this.options.circular ? 0 : currentIndex) : currentIndex + 1;
        } else {
            var nextIndex = 2;
			var currentIndex = 1;
		}

		this.goCircle = false;
		if (nextIndex == (((this.slides.length-1)/2)+2) && this.options.circular && this.options.effect == 'scroll') {
			this.scroller.scrollLeft = 0;
			this.scroller.scrollTop  = 0;
			nextIndex = 2;
			this.current = this.slides[1];
			this.slides[((this.slides.length-1)/2)+1].childElements()[0].childElements()[0].setStyle({width: this.slides[0].childElements()[0].childElements()[0].getWidth() + "px", height: this.slides[0].childElements()[0].childElements()[0].getHeight() + "px"});
			this.goCircle = true;
		}
		this.slides[currentIndex].childElements()[0].setAttribute('rel', 'prev');
		this.slides[nextIndex+1].childElements()[0].setAttribute('rel', '');
		this.slides[nextIndex+1].childElements()[0].setAttribute('rel', 'next');

		this.options.nextPrev = 'next';
		this.moveTo(this.slides[nextIndex]);
	},

	first: function () {
		this.moveTo(this.slides[0]);
    },

	last: function () {
		this.moveTo(this.slides[this.slides.length - 1]);
    },

	toggle: function () {
		if (this.previous) {
			this.moveTo(this.slides[this.previous._index]);
        } else {
            return false;
        }
    },

	stop: function () {
		if (this.timer) {
			clearTimeout(this.timer);
		}
	},

	start: function () { 
        this.periodicallyUpdate();
    },

	pause: function () {
		this.stop();
		this.activateControls();
    },

	resume: function (event) {
		if (event) {
			var related = event.relatedTarget || event.toElement;
			if (!related || (!this.slides.include(related) && !this.slides.any(function (slide) { return related.descendantOf(slide); }))) {
				this.start();
            }
        } else {
            this.start();
        }
    },

	periodicallyUpdate: function () {
		if (this.timer != null) {
			clearTimeout(this.timer);
			if(this.options.direction == 'right_to_left') {
				this.next();
			} else {
				this.prev();
			}
        }
		this.timer = setTimeout(this.periodicallyUpdate.bind(this), this.options.frequency);
    },
    
    wheel: function (event) {
        event.cancelBubble = true;
        event.stop();
        
		var delta = 0;
		if (!event) {
            event = window.event;
        }
		if (event.wheelDelta) {
			delta = event.wheelDelta / 120; 
		} else if (event.detail) { 
            delta = -event.detail / 3;	
        }        
       
        if (!this.scrolling) {
            this.deactivateControls();
            if (delta > 0) {
                this.prev();
            } else {
                this.next();
            }            
        }
        
		return Math.round(delta); //Safari Round
    },

	deactivateControls: function () {
		this.controls.invoke('addClassName', this.options.disabledClassName);
    },

	activateControls: function () {
		this.controls.invoke('removeClassName', this.options.disabledClassName);
    }
});

Effect.SmoothScroll2 = Class.create();
Object.extend(Object.extend(Effect.SmoothScroll2.prototype, Effect.Base.prototype), {
	initialize: function (element) {
		this.element = $(element);
		var options = Object.extend({ x: 0, y: 0, mode: 'absolute' } , arguments[1] || {});
		this.start(options);
    },

	setup: function () {
		if (this.options.continuous && !this.element._ext) {
			this.element.cleanWhitespace();
			this.element._ext = true;
			this.element.appendChild(this.element.firstChild);
        }

		this.originalLeft = this.element.scrollLeft;
		this.originalTop  = this.element.scrollTop;

		if (this.options.mode == 'absolute') {
			this.options.x -= this.originalLeft;
//			this.options.y -= this.originalTop;
        }
    },

	update: function (position) {
		if(this.originalLeft < this.options.imageSmallWidth) {
			this.element.scrollLeft = this.options.x * position + this.originalLeft - this.options.imageSmallWidth;
		} else {
			this.element.scrollLeft = (this.options.x - this.options.imageSmallWidth) * position + this.originalLeft;
		}
//		$('infoBox').update(this.originalLeft);
//		this.element.scrollTop  = this.options.y * position + this.originalTop;
		position2 = this.options.imageSmallWidth/this.options.imageWidth;
		position2 = position + position2;
		if(this.options.imageWidth * position2 < this.options.imageWidth + 1) {
			this.options.current.childElements()[0].childElements()[0].setStyle({width: this.options.imageWidth * position2 + "px", height: this.options.imageHeight * position2 + "px"});
		} else {
			this.options.current.childElements()[0].childElements()[0].setStyle({width: this.options.imageWidth + "px", height: (this.options.imageWidth/this.options.imageWidth)*this.options.imageHeight + "px"});
		}
		if(this.options.imageWidth * (1-position) > this.options.imageSmallWidth) {
			this.options.previous.childElements()[0].childElements()[0].setStyle({width: this.options.imageWidth * (1-position) + "px", height: this.options.imageHeight * (1-position) + "px"});
		} else {
			this.options.previous.childElements()[0].childElements()[0].setStyle({width: this.options.imageSmallWidth + "px", height: (this.options.imageSmallWidth/this.options.imageWidth)*this.options.imageHeight + "px"});
		}
	}
});
