// $Id: anim.js,v 1.6 2009/06/02 14:05:08 vojnovicn Exp $
// requires yui.animation, yui.connect, yui.dom, 


Anim = {
	loadedContent: null,
	
	fadeIn: function (obj) {
		var anim = new YAHOO.util.Anim(obj, {opacity: {to:1}}, .3);  
		anim.animate(); 
	},
	
	fadeOut: function (obj) {
		var anim = new YAHOO.util.Anim(obj, {opacity: {to:0}}, .3);
		anim.onComplete.subscribe(function(status,e,obj){
			obj.parentNode.removeChild(obj);
		},obj);
		anim.animate(); 
	},
	
	fadeOutHide: function (obj) {
		var anim = new YAHOO.util.Anim(obj, {opacity: {to:0}}, .3);
		anim.onComplete.subscribe(function(){
			obj.style.display = 'none';
		});
		anim.animate(); 
	},
	
	spotlight: function (obj, cName) {
		YAHOO.util.Dom.removeClass(obj, cName);
		
		var endColor = $D.getStyle(obj, 'background-color');
		if (endColor=='transparent') endColor = '#fff';

		$D.addClass(obj, cName);
		if (this.spotlightColor==null)
			this.spotlightColor = $D.getStyle(obj, 'background-color');

		YAHOO.util.Dom.setStyle(obj, 'background-color', this.spotlightColor);
	
		var anim = new YAHOO.util.ColorAnim(obj, {backgroundColor: {to: endColor}}, .3);  
		anim.animate();
	},
	
	scrollContent: function(id, speed, pauseOnMouseOver) {
		var content = document.getElementById(id);
		var content_2 = document.getElementById(id+'_2');
		var container = content.parentNode;
		
		var top = YAHOO.util.Dom.getRegion(content).top; 
		var bottom = YAHOO.util.Dom.getRegion(content).bottom;
		var fold = YAHOO.util.Dom.getRegion(container).top;

		if (!speed || speed>100)
			var speed = 25;

		// don't scroll if content < container
		if (bottom-top<YAHOO.util.Dom.getRegion(container).bottom-fold)
			return false;
		
		// create duplicate and append it
		if (!content_2) {
			content_2 = content.cloneNode(true);
			content_2.id = id+'_2';
   			YAHOO.util.Dom.insertAfter(content_2, content);
		}
		
		// move
		YAHOO.util.Dom.setY(content, top-1);
		YAHOO.util.Dom.setY(content_2, bottom-1);
		
		// switch contents if content moves over the fold
		if(bottom < fold+1) {
   			content_2.id = content.id;
   			content.id += '_2';
   		}

		// and one more time...		
		setTimeout("Anim.scrollContent('"+id+"', "+speed+")", 1000/speed);
	},
	
	scrollPage: function(el,speed){
		var setAttr = function(a, v, u) {
        	window.scroll(0, v);
	    };
	
	    var anim = new YAHOO.util.Anim(null,
	        { scroll : {
	            from : YAHOO.util.Dom.getDocumentScrollTop(),
	            to : ANET.utils.getOffset(document.getElementById(el)).y }
	        },
	        speed, YAHOO.util.Easing.easeOut
	    );
	    anim.setAttribute = setAttr;
	    anim.animate();
	},
	
	// refactoring ftw...
	slideContentOnLoad: function (o, container, options) {
		x = (options.x==null) ? 0 : options.x;
		y = (options.y==null) ? 0 : options.y;
		speed = (options.speed==null) ? 1 : options.speed;
		container = document.getElementById(container);
	
		var slideOutAnim = new YAHOO.util.Motion(container, {points: { by: [x, y]}}, speed, YAHOO.util.Easing.easeIn);  
		slideOutAnim.onComplete.subscribe(function() {Anim.slideBack(o, container, options)});  
		
		slideOutAnim.animate();
	},
	
	slideBack: function(o, container, options) {
		if (!YAHOO.util.Connection.isCallInProgress(o)) {
			x = (options.x==null) ? 0 : options.x;
			y = (options.y==null) ? 0 : options.y;
			speed = (options.speed==null) ? 1 : options.speed;

			container.innerHTML = this.loadedContent;
			var slideInAnim = new YAHOO.util.Motion(container, {points: { by: [-x, -y]}}, speed, YAHOO.util.Easing.easeOut);  
			slideInAnim.animate();
		}
		else {
			setTimeout(function(){Anim.slideBack(o, container, options)}, 400);
		}
	}
}
