
//########################################################
var cmsContentRotator = new Class({
    
    //########################################################
    initialize: function(outerElement) {
	this.outerElement = outerElement;
	this.slideBox = outerElement.getElement("div.cmsContentRotator");
	this.slideBox.style.height = 70;

	crOptions = new Hash();
	crOptions.set('slideTime', 1000);
	crOptions.set('displayPause', 7000);
	crOptions.set('reverse', 0);
	crOptions.set('transition', '');
	crOptions.set('showBar', 1);
	crOptions.set('pad', 10);
	crOptions.set('doBottomPad', 0);
	var opts = this.slideBox.getProperty('crOpts');
	if (opts) {
	    opts.split(',').each(function(item) { 
		var kv = item.split(':');
		crOptions.set(kv[0], kv[1]);
	    });
	}

	this.displayPause = crOptions.get('displayPause').toInt();
	this.slideTime = crOptions.get('slideTime').toInt();
	this.reverse = crOptions.get('reverse').toInt();
	this.transition = crOptions.get('transition');
	this.showBar = crOptions.get('showBar').toInt();
	this.padding = crOptions.get('pad').toInt()
	this.doBottomPad = crOptions.get('doBottomPad').toInt()

	//If it's zero we want to hide the border too... easiest to just do this here.
	if (this.padding == 0) this.padding = -1;

    },

    //########################################################
    setup: function() {
	var thisObj = this;

	this.innerBox = this.slideBox.getElement(".cmsContentRotator_inner");

	this.elements = this.innerBox.getElements("div.cmsContentRotator_elem_outer");
	this.elements.each(function(elem) {
	    //Make it "visible" but out of the viewable area
	    elem.setStyle('left', (thisObj.slideBox.offsetWidth+200)+'px');
	    elem.setStyle("display", "");

	    var elemInner = elem.getElement(".cmsContentRotator_elem_inner");
	    var elemInnerBlock = elem.getElement(".cmsContentRotator_elem_innerBlock");
	    var slideWidth = thisObj.slideBox.offsetWidth - (thisObj.padding*2);
	    if (elemInnerBlock.offsetWidth && (elemInnerBlock.offsetWidth < slideWidth)) slideWidth = elemInnerBlock.offsetWidth;
	    elemInner.setStyle('width', slideWidth);

	    //var slideWidth = thisObj.slideBox.offsetWidth - (thisObj.padding*2);
	    //elem.setStyle('width', slideWidth);
	    //elem.setStyle("opacity", 0);

	    elem.addEvent('mousedown', function() { thisObj.stopRotation(); });
	    elem.addEvent('mouseover', function() { thisObj.pauseRotation(); });
	    elem.addEvent('mouseout', function() { thisObj.resumeRotation(); });

	    if (elem.getElements("object,form,input,select,textarea").length) {
		$(elem).isProblem = true;
	    }
	    
	    thisObj.hideProblems(elem);
	});

	this.elementLinks = this.outerElement.getElements("div.cmsContentRotator_elemLink");
	this.elementLinks.each(function(elem, index) {
	    elem.addEvent("click", function() { thisObj.swapSlide(index); });
	});

	this.currElem = -1;

	if (this.showBar != 0) {
	    //we need to include it for functionality but we hide it when they don't want it.
	    this.outerElement.getElement("div.cmsContentRotator_nav").setStyle("display", "");
	}
	
	this.swapSlide(0);

    },
    
    //########################################################
    stopRotation: function(e) {
	this.pauseRotation();
	this.rotationStopped = true;
    },
    
    //########################################################
    startRotation: function(e) {
	this.rotationStopped = false;
	this.resumeRotation();
    },
    
    //########################################################
    pauseRotation: function(e) {
	window.clearTimeout(this.swapTimeout);
    },
    
    //########################################################
    resumeRotation: function(e) {
	if (this.rotationStopped) return;
	window.clearTimeout(this.swapTimeout);
	this.swapTimeout = this.swapSlide_auto.delay(this.displayPause+this.slideTime, this);
    },

    //########################################################
    swapSlide_auto: function() {
	this.swapSlide((this.currElem + 1) % this.elements.length);
    },

    //########################################################
    showProblems: function(elem) {
	if (! elem.isProblem) return;
	elem.getElements(".cmsContentRotator_elem_innerBlock").each(function(problemElem) {
	    problemElem.removeClass('cmsContentRotator_hide_problems');
	});
    },

    //########################################################
    hideProblems: function(elem) {
	if (! elem.isProblem) return;
	elem.getElements(".cmsContentRotator_elem_innerBlock").each(function(problemElem) {
	    problemElem.addClass('cmsContentRotator_hide_problems');
	});
    },

    //########################################################
    swapSlide: function(nextSlideNum) {
	var thisObj = this;

	var currSlideNum = this.currElem;
	if (currSlideNum == nextSlideNum) return;
	
	var currSlide = this.elements[currSlideNum];
	var nextSlide = this.elements[nextSlideNum];
	
	var heightPad = this.padding;
	if (this.doBottomPad) heightPad *= 2;

	var nextInnerBlock = nextSlide.getElement(".cmsContentRotator_elem_innerBlock");
	var myFx = new Fx.Tween(this.slideBox, {duration:500, transition:Fx.Transitions.Sine.easeInOut});
	myFx.start('height', this.slideBox.offsetHeight, nextInnerBlock.offsetHeight+heightPad+1);

	var moveOpts = {duration:this.slideTime, transition:Fx.Transitions.Back.easeInOut};

	var trans = this.transition;
	var rev = this.reverse;
	if (trans == 'random') {
	    trans = Math.round(Math.random()*2);
	    rev = Math.round(Math.random());
	}

	if (currSlide) {
	    this.hideProblems(currSlide);
	    this.elementLinks[currSlideNum].removeClass("cmsContentRotator_navActive");
	    switch(trans) {
	    case 'fade':
	    case 0:
		var myFx = new Fx.Tween(currSlide, moveOpts);
		myFx.start('opacity', 1, 0);
		break;
	    case 'drop':
	    case 1:
		var myFx = new Fx.Tween(currSlide, moveOpts);
		myFx.addEvent('onComplete', function() { currSlide.setStyle('top', 0-currSlide.getElement(".cmsContentRotator_elem_innerBlock").offsetHeight-200); });
		if (rev) myFx.start('top', currSlide.offsetTop, 0-currSlide.offsetHeight-200);
		else myFx.start('top', currSlide.offsetTop, nextSlide.offsetHeight+200);
		break;
	    case 'slide':
	    case 2:
	    default:
		var myFx = new Fx.Tween(currSlide, moveOpts);
		myFx.addEvent('onComplete', function() { currSlide.setStyle('left', 0-currSlide.getElement(".cmsContentRotator_elem_innerBlock").offsetWidth-200); });
		if (rev) myFx.start('left', currSlide.offsetLeft, 0-currSlide.offsetWidth-200);
		else myFx.start('left', currSlide.offsetLeft, this.slideBox.offsetWidth+200);
		break;
	    }
	}
	
	var newLeft = (this.slideBox.offsetWidth - nextSlide.offsetWidth) / 2;

	switch(trans) {
	case 'fade':
	case 0:
	    nextSlide.setStyle('opacity', '0');
	    nextSlide.setStyle('top', this.padding+'px');
	    nextSlide.setStyle('left', newLeft+'px');

	    var myFx = new Fx.Tween(nextSlide, moveOpts);
	    myFx.addEvent('onComplete', function() { thisObj.showProblems(nextSlide); });
	    myFx.start('opacity', 0, 1);
	    break;
	case 'drop':
	case 1:
	    nextSlide.setStyle('top', (0-nextSlide.getElement(".cmsContentRotator_elem_innerBlock").offsetHeight-200)+'px');
	    nextSlide.setStyle('left', newLeft+'px');
	    nextSlide.setStyle('opacity', '1');

	    var myFx = new Fx.Tween(nextSlide, moveOpts);
	    myFx.addEvent('onComplete', function() { thisObj.showProblems(nextSlide); });
	    if (rev) myFx.start('top', this.slideBox.offsetHeight+200, this.padding);
	    else myFx.start('top', nextSlide.offsetTop, this.padding);
	    break;
	case 'slide':
	case 2:
	default:
	    nextSlide.setStyle('left', (0-nextSlide.offsetWidth-200)+'px');
	    nextSlide.setStyle('top', this.padding+'px');
	    nextSlide.setStyle('opacity', '1');

	    var myFx = new Fx.Tween(nextSlide, moveOpts);
	    myFx.addEvent('onComplete', function() { thisObj.showProblems(nextSlide); });
	    if (rev) myFx.start('left', this.slideBox.offsetWidth+200, newLeft);
	    else myFx.start('left', nextSlide.offsetLeft, newLeft);
	    break;
	}

	this.elementLinks[nextSlideNum].addClass("cmsContentRotator_navActive");
	this.currElem = nextSlideNum;
	
	this.startRotation();
    }
    
});

$(window).addEvent('domready', function() { 
    $$(".cmsContentRotator_outer").each(function(elem) {
	var obj = new cmsContentRotator(elem);
	obj.setup();
    });
});
    
