// JavaScript Document


var SimpleCarousel = new Class({
	Implements: [Options, Events],
	options: {
//		onRotate: $empty,
//		onStop: $empty,
//		onAutoPlay: $empty,
//		onShowSlide: $empty,
		slideInterval: 5000,
		transitionDuration: 500,
		startIndex: 0,
		buttonOnClass: "selected",
		buttonOffClass: "off",
		rotateAction: "none",
		rotateActionDuration: 0,
		autoplay: true
	},
	initialize: function(container, slides, buttons, options){
		this.container = $(container);
		if(this.container.hasClass('hasCarousel')) return false;
		this.setOptions(options);
		this.container.addClass('hasCarousel');
		this.slides = $$(slides);
		this.buttons = $$(buttons);
		this.createFx();
		this.showSlide(this.options.startIndex);
		if(this.options.autoplay) this.autoplay();
		if(this.options.rotateAction != 'none') this.setupAction(this.options.rotateAction);
		return this;
	},
	toElement: function(){
		return this.container;
	},
	setupAction: function(action) {
		this.buttons.each(function(el, idx){
			$(el).addEvent(action, function() {
				this.slideFx.setOptions(this.slideFx.options, {duration: this.options.rotateActionDuration});
				if(this.currentSlide != idx) this.showSlide(idx);
				this.stop();
			}.bind(this));
		}, this);
	},
	createFx: function(){
		if (!this.slideFx) this.slideFx = new Fx.Elements(this.slides, {duration: this.options.transitionDuration});
		this.slides.each(function(slide){
			slide.setStyle('opacity',0);
		});
	},
	showSlide: function(slideIndex){
		var action = {};
		this.slides.each(function(slide, index){
			if(index == slideIndex && index != this.currentSlide){ //show
				$(this.buttons[index]).addClass(this.options.buttonOnClass);
				$(this.buttons[index]).removeClass(this.options.buttonOffClass);

				action[index.toString()] = {
					opacity: 1
				};
			} else {
				$(this.buttons[index]).removeClass(this.options.buttonOnClass);
				$(this.buttons[index]).addClass(this.options.buttonOffClass);
				action[index.toString()] = {
					opacity:0
				};
			}
		}, this);
		//this.fireEvent('onShowSlide', slideIndex);
		this.currentSlide = slideIndex;
		this.slideFx.start(action);
		return this;
	},
	autoplay: function(){
		this.slideshowInt = this.rotate.periodical(this.options.slideInterval, this);
        this.fireEvent('onAutoPlay');
        return this;
    },
    stop: function(){
        $clear(this.slideshowInt);
        this.fireEvent('onStop');
        return this;
    },
    rotate: function(nextOrPrev){
        current = this.currentSlide;
        next = (current+1 >= this.slides.length) ? 0 : current+1;
        prev = (current-1 < 0) ? this.slides.length-1 : current-1;

        /* Hacky McHackerson? */
        if(nextOrPrev == 'next' || nextOrPrev == undefined){
            this.showSlide(next);
            this.fireEvent('onRotate', next);
        }else{
            this.showSlide(prev);
            this.fireEvent('onRotate', prev);
        }
        return this;
    },
    showVideo: function(videoDiv){
        videoDiv.setStyle('display', 'block');
        this.slides.each(function(slide){
            slide.setStyles({
                'opacity': 1,
                'visibility': 'hidden'
            });
        });
    },

    hideVideo: function(videoDiv){
        videoDiv.setStyle('display', 'none');
        var current = this.currentSlide;
        this.slides.each(function(slide, index){
            if(index != current){
                slide.setStyle('opacity', 0);
            };
            slide.setStyle('visibility', 'visible');
        });
    }
});

function callMain()
{

  $('carouselMain').store('carousel', new SimpleCarousel('carouselMain', $$('#carouselMain .slide'), $$('#carouselMain .featureButton'), {
        slideInterval: 5000,
        rotateAction: 'mouseenter',
        onShowSlide: function(index) {
            this.buttons.each(function(aButton, i) {
                if (i == index) {
                    aButton.morph({'top':-10});
                    aButton.getElement('img').morph({'margin-bottom': 0});
                } else {
                    aButton.morph({'top':0});
                    aButton.getElement('img').morph({'margin-bottom': 0});
                }
            })
        }
    }));

    window.addEvent('blur', function() {
        $('carouselMain').retrieve('carousel').stop();
    });
}

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}
