dojo.require("dojo.fx");
dojo.require("dojo.fx.easing");

SJANIMATOR_CENTER			= 1;

SJANIMATOR_OBJECTS_ALIGN 	= new Object();
SJANIMATOR_OBJECTS_EVENT	= false;
SJANIMATOR_WINDOW_WIDTH		= 0;
SJANIMATOR_CURRENT_COUNTER	= 0;
SJANIMATOR_SCENE_COUNTER	= 0;
SJANIMATOR_SCENE_MAX		= 0;

SJANIMATOR_EFFECT_GLOW			= 1;

SJANIMATOR_EFFECT_REPLACE_SRC 	= 2;

function SJAnimatorWrapper()
{
	this.objectId  	= 'SJAnimatorWrapper';
	this.scenes 	= new Array();
	
	this.effect = function(type, props) {
		
		switch(type)
		{
			case SJANIMATOR_EFFECT_REPLACE_SRC:
				return new SJEffectReplaceSrc(props);
				break;
		}
	};
	
	this.addScene = function(scene) {
		this.scenes.push(scene);
	};
	
	this.animate = function(scene_id) {
		if(!scene_id)
		{
			scene_id = 0;
		}
		
		SJANIMATOR_SCENE_MAX 	= this.scenes.length;
		var anim 				= this.scenes[scene_id].getAnimation();
		
		dojo.connect(anim, 'onEnd', function() {			
			SJANIMATOR_SCENE_COUNTER++;
			console.log('Checking scene id: ' + SJANIMATOR_SCENE_COUNTER);
			if(SJANIMATOR_SCENE_COUNTER >= SJANIMATOR_SCENE_MAX)
			{
				return;
			}
			SJAnimator.animate(SJANIMATOR_SCENE_COUNTER);
		});
		
		anim.play();
	};
	
	this.newAnimatedObjectByNode = function(obj) {
		return new SJAnimatedObject(obj);
	}
	
	this.newAnimatedObjectByObject = function(obj) {
		var new_obj = SJAnimator.newAnimatedObjectByNode(obj.handle);
		return new_obj;
	}
	
	this.resizeWindow = function(e) {
		var obj;
		var size = SJWINDOWSIZE();
		
		SJANIMATOR_WINDOW_WIDTH = size['x'];
		for(var objectId in SJANIMATOR_OBJECTS_ALIGN)
		{
			obj  = SJANIMATOR_OBJECTS_ALIGN[objectId];
			dojo.style(obj[0], 'left', Math.floor(((size['x'] / 2) - (dojo.style(obj[0], 'width') / 2))) + 'px');
		}
	};
}


SJEFFECT_REPLACE_SRC = new Object();

// Effects
function SJEffectReplaceSrc(props)
{
	this.props = props;
	
	this.init = function(handle) {
		this.handle = handle;
		
		if(typeof props['srcName'] != 'undefined')
		{
			var oldSrc  = dojo.attr(handle, 'src');
			var splits  = oldSrc.split('/');
			splits.pop();

			SJEFFECT_REPLACE_SRC[dojo.attr(handle, 'id')] = splits.join('/') + '/' + props['srcName'];
		}
	};
	
	this.execute = function(obj) {

		alert(obj.id);
		dojo.attr(obj, 'src', SJEFFECT_REPLACE_SRC[dojo.attr(obj, 'id')]);
	};
}

function SJAnimationScene()
{
	this.objectId  = 'SJAnimationScene';
	this.duration  = 1000;
	this.objects   = new Array();
	
	this.addObject = function(object) {
		this.objects.push(object);
	};
	
	this.setDuration = function(duration) {
		this.duration = duration;
	}
	
	this.getAnimation = function() {
		
		var animations = new Array();
		for(var i = 0; i < this.objects.length; i++)
		{
			this.objects[i].setDuration(this.duration);
			animations.push(this.objects[i].getAnimation());
		}
		
		var animationArray = dojo.fx.combine(animations);
		return animationArray;
	}
}

function SJAnimatedObject(obj)
{
	this.objectId  		= 'SJAnimatedObject';
	this.handle 		= obj;
	this.id				= dojo.attr(this.handle, 'id');
	this.duration		= 1000;
	this.keyFrames		= new Array();
	this.startFrame 	= false;
	this.endFrame		= false;
	this.onBegin		= false;
	this.afterEffect	= function() { };
		
	this.setDuration = function(duration) {
		this.duration = duration;
	}
	
	this.setStartFrame = function(styles) {
		this.startFrame = styles;
	};
	
	this.setEndFrame = function(styles) {
		this.endFrame = styles;
	};
	
	this.addAfterEffect = function(effect) {
		effect.init(this.handle);
		
		this.afterEffect = effect.execute;
	};
	
	this.setRelativeBehavior = function(align) {
		SJANIMATOR_OBJECTS_ALIGN[this.id] = new Array(this.handle, align);
		
		SJAnimator.resizeWindow();
		if(SJANIMATOR_OBJECTS_EVENT == false)
		{
			SJANIMATOR_OBJECTS_EVENT = true;
			dojo.connect(window, 'onresize', null, SJAnimator.resizeWindow);
		}
	};
	
	this.getAnimation = function() {				
		
		if(this.startFrame != false)
		{
			dojo.anim(this.handle, this.startFrame, 0, dojo.fx.easing.linear);
		}
		if(typeof SJANIMATOR_OBJECTS_ALIGN[this.id] != 'undefined' && SJANIMATOR_OBJECTS_ALIGN[this.id][1] == SJANIMATOR_CENTER)
		{
			if(this.startFrame != false)
			{
				dojo.style(this.handle, 'left', ((SJANIMATOR_WINDOW_WIDTH / 2) - (dojo.style(this.handle, 'width') / 2)) + 'px');
			}
			
			this.endFrame['left'] = (SJANIMATOR_WINDOW_WIDTH / 2) - (this.endFrame['width'] / 2);
		}
		
		this.startFrame = false;
		dojo.style(this.handle, { display: 'block' });
		
		return dojo.animateProperty({ 
			node: this.handle, 
			duration: this.duration,
			properties: this.endFrame,
			easing: function(n) {
				return n;
			},
			onEnd: function(obj) {
				// alert(obj.id + ' | ' + this.afterEffect);
			},
			onBegin: function(obj) {
				
			}
		});	
	}
}

SJAnimator = new SJAnimatorWrapper();
