//
// This script section is responsible for cycling through the coral tree partners.
//

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

Widget.DivBlender = Class.create();

Widget.DivBlender.prototype = {	
	initialize: function(div1, div2, timeS, callbackUrl) {
		this.div1 = div1;
		this.div2 = div2;
		
		$(div1).otherDiv = $(div2);
		$(div2).otherDiv = $(div1);
		
		this.timeMs = timeS * 1000;
		this.callbackUrl = callbackUrl;
		this.counter = 0;
		
		new Ajax.Updater(this.div1, this.callbackUrl(this.counter));
		
		this.onTimer();
	},
	
	onTimer: function () {
		if(this.counter % 2 == 0) {
			this.showDiv($(this.div1));
		}
		else {
			this.showDiv($(this.div2));
		}
		
		this.counter++;
		
		setTimeout (this.onTimer.bind(this), this.timeMs);
	},
	
	showDiv: function(element, value) {
		element.show();
		element.otherDiv.show();
		
		new Effect.Opacity(element, { duration: 0.4, from: 0.0, to: 1.0 });
		new Effect.Opacity(element.otherDiv, { duration: 0.4, from: 1.0, to: 0.0 });
		
		this.visibleDiv = element;
		
		setTimeout (this.moveToFront.bind(this), 400);		
	},
	
	moveToFront: function () {
		this.visibleDiv.show();
		
		this.visibleDiv.otherDiv.hide();
		new Ajax.Updater(this.visibleDiv.otherDiv.id, this.callbackUrl());
	}

};

document.observe('dom:loaded', function() {				
	new Widget.DivBlender('partners1', 'partners2', 5, function () {return 'templates/default/includes/ajaxGetNextPartners.php?' + (new Date())});
	
	window.onresize = function() {
	   [$('partners1'), $('partners2')].each (function (x) {x.setStyle({'right': '0px', 'left':'0px'})});
    };
	
	window.onresize();

});

