/*
moo.fx pack, effects extensions for moo.fx.
by Valerio Proietti (http://mad4milk.net) MIT-style LICENSE
for more info visit (http://moofx.mad4milk.net).
11/01/2005
v1.0.31
*/

//text size modify, works with ems only
fx.Text = Class.create();
fx.Text.prototype = Object.extend(new fx.Base(), {
	initialize: function(el, options) {
		this.el = $(el);
		this.setOptions(options);
	},

	increase: function() {
		this.el.style.fontSize = this.now + "em";
	}
});

//combination
fx.FadeSize = Class.create();
fx.FadeSize.prototype = {
	initialize: function(el, options) {
		this.el = $(el);
		this.el.o = new fx.Opacity(el, options);
		if (options) options.onComplete = null;
		this.el.h = new fx.Height(el, options);
		this.el.w = new fx.Width(el, options);
	},

	toggle: function() {
		this.el.o.toggle();
		for (var i = 0; i < arguments.length; i++) {
			if (arguments[i] == 'height') this.el.h.toggle();
			if (arguments[i] == 'width') this.el.w.toggle();
		}
	},

	hide: function(){
		this.el.o.hide();
		for (var i = 0; i < arguments.length; i++) {
			if (arguments[i] == 'height') this.el.h.hide();
			if (arguments[i] == 'width') this.el.w.hide();
		}
	}
}

//cookies
var Remember = new Object();
Remember = function(){};
Remember.prototype = {
	initialize: function(el, options){
		this.el = $(el);
		this.days = 365;
		this.options = options;
		this.effect();
		var cookie = this.readCookie();
		if (cookie) {
			this.fx.now = cookie;
			this.fx.increase();
		}
	},

	//cookie functions based on code by ALA author Peter-Paul Koch
	setCookie: function(value) {
		var date = new Date();
		date.setTime(date.getTime()+(this.days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
		document.cookie = this.el+this.prefix+this.el.className+this.el.id+"="+value+expires+"; path=/";
	},

	readCookie: function() {
		var nameEQ = this.el+this.prefix+this.el.className+this.el.id+"=";
		var ca = document.cookie.split(';');
		for(var i=0;i < ca.length;i++) {
			var c = ca[i];
			while (c.charAt(0)==' ') c = c.substring(1,c.length);
			if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
		}
		return null;
	},

	custom: function(from, to){
		if (this.fx.now != to) {
			this.setCookie(to);
			this.fx.custom(from, to);
		}
	}
}

//customized for mooflex
fx.RememberHeight = Class.create();
fx.RememberHeight.prototype = Object.extend(new Remember(), {
	effect: function(){
		this.fx = new fx.Height(this.el, this.options);
		this.prefix = 'height';
	},
	
	toggle: function(){
		if (this.el.offsetHeight == 0) this.setCookie(this.el.scrollHeight);
		else this.setCookie(0);
		this.fx.toggle();
	},
	
	resize: function(how, max, min){
		if ((this.el.offsetHeight+how) < max && (this.el.offsetHeight+how) > min) {
			this.setCookie(this.el.offsetHeight+how);
			this.fx.custom(this.el.offsetHeight,this.el.offsetHeight+how);
		}
	},

	hide: function(){
		this.fx.hide();
	}
});

fx.RememberText = Class.create();
fx.RememberText.prototype = Object.extend(new Remember(), {
	effect: function(){
		this.fx = new fx.Text(this.el, this.options);
		this.prefix = 'text';
	}
});


//mooflex custom effects
fx.OpacityBackAndForth = Class.create();
fx.OpacityBackAndForth.prototype = {
	initialize: function(el, duration, class1, class2){
		this.el = $(el);
		this.el.opct = new fx.Opacity(this.el, {onComplete: function(){
			if (this.el.opct.now == 0) {
				this.el.opct.toggle();
				this.el.switchClassName(this.el, class1, class2);
			}
		}, duration: duration});

		this.el.switchClassName = function(element, class1, class2){
			if (Element.hasClassName(element, class1)){
				Element.removeClassName(element, class1);
				element.title = class2;
			}
			else {
				Element.addClassName(element, class1);
				element.title = class2;
			}
		}
	},

	go: function(){
		this.el.opct.toggle();
	}
}

fx.MooBlock = Class.create();
fx.MooBlock.prototype = {
	initialize: function(el, arrow, class1, class2) {
		this.el = $(el);
		this.arrow = $(arrow);
		this.el.h = new fx.RememberHeight(this.el, {duration: 450, onComplete: function(){
			if (this.el.offsetHeight > 0) this.el.style.height = "1%";
		}});
		this.arrow.o = new fx.OpacityBackAndForth(this.arrow, 220, class1, class2);
	},

	toggle: function(){
		this.el.h.toggle();
		this.arrow.o.go();
	}
}