/**
 * org.bearcatmusic -- namespace
 **/
 var org;
 if (!org) org = {}; else if (typeof org != "object") throw new Error("org already exists and is not an object");
 if (!org.bearcatmusic) org.bearcatmusic = {}; else if (typeof org.bearcatmusic != "object") throw new Error("org.bearcatmusic already exists and is not an object");
 
/**
 * ensure org.bearcatmusic.Class.define is available
 **/
if (!org.bearcatmusic.Class || !org.bearcatmusic.Class.define) throw new Error("Class.js must be included before Transition.js");

/**
 * ensure google.loader is available
 **/
if (!window['google'] || !window['google']['loader']) throw new Error("www.google.com/jsapi must be included before CalendarFeed.js");

/**
 * Transition class
 **/
org.bearcatmusic.Transition = org.bearcatmusic.Class.define({
	name: 		"Transition",

	construct:	function (currentID, otherID, horizontal)
				{
					this.currentID = currentID;
					this.otherID = otherID;
					this.horizontal = horizontal;
					this.scope = currentID;
				},
	
	statics: {
		load:	function () { google.load("prototype", "1.6"); google.load("scriptaculous", "1.8"); },
	},
	
	methods: {
		getCurrentID:	function () { return this.currentID; },
		
		getOtherID:		function () { return this.otherID; },
		
		next:			function (additionalEffects, afterSetupCallback)
						{
							this.transition(this.currentID, this.otherID, 
											 this.horizontal ? -document.getElementById(this.currentID).offsetWidth : -document.getElementById(this.currentID).offsetHeight,
											 additionalEffects, afterSetupCallback);
						},

		previous:		function (additionalEffects, afterSetupCallback)
						{
							this.transition(this.currentID, this.otherID, 
											 this.horizontal ? document.getElementById(this.currentID).offsetWidth : document.getElementById(this.currentID).offsetHeight,
											 additionalEffects, afterSetupCallback); },

		transition:		function (outgoingElementID, incomingElementID, amount, additionalEffects, afterSetupCallback)
						{
							var effects = [];
							var horizontal = this.horizontal;
							var xAmount = horizontal ? amount : 0;
							var yAmount = horizontal ? 0 : amount;
							
							effects.push(new Effect.Move(outgoingElementID, { x: xAmount, y: yAmount, mode: 'relative', delay: 0.0, duration: 1.0, sync: true }));
							effects.push(new Effect.Opacity(outgoingElementID, { from: 1.0, to: 0.0, delay: 0.0, duration: 0.1, sync: true}));
						
							effects.push(new Effect.Move(incomingElementID, { x: xAmount, y: yAmount, mode: 'relative', delay: 0.0, duration: 1.0, sync: true }));
							effects.push(new Effect.Opacity(incomingElementID, { from: 0.0, to: 1.0, delay: 0.9, duration: 0.1, sync: true}));
							
							if (additionalEffects)
								effects = effects.concat(additionalEffects);
						
							new Effect.Parallel(effects, { duration: 1.0, queue: { position: 'end', scope: this.scope }, afterSetup: function(effect) {
										// Make sure the incoming element is in the right place before we start
										document.getElementById(incomingElementID).style.left = (document.getElementById(outgoingElementID).offsetLeft - xAmount) + "px";
										document.getElementById(incomingElementID).style.top = (document.getElementById(outgoingElementID).offsetTop - yAmount) + "px";
										
										// Invoke the supplied callback (if any)
										if (afterSetupCallback) afterSetupCallback();
									}
								});
							
							// Swap the elements
							var tmp = this.currentID;
							this.currentID = this.otherID;
							this.otherID = tmp;
	
						}
	}
});

