Ext.ns('Ext.ux');

Ext.ux.AnimatedFoldingChain = Ext.extend(Ext.util.Observable, {
	els: [],
	items: [],
	cls: 'x-anim-chain',
	startWait: 5000,
	cookie: '',
	hasActivatedByCookie: false,
	constructor: function(config)
	{
		config = config || {};
		Ext.apply(this, config);

		Ext.ux.AnimatedFoldingChain.superclass.constructor.call(this, config);

		this.initCookie();
		this.initMarkup();
		this.initTask();
	},

	initMarkup: function()
	{
		if (this.hasActivatedByCookie) return;
		
		Ext.each(Ext.getBody().query('.' + this.cls), function(i){
			var el = Ext.get(i);
			this.els.push(el);
			this.items.push(new Ext.ux.AnimatedChainItem(el));
		}, this);
	},

	initTask: function()
	{
		if (this.hasActivatedByCookie) return;
		
		var tick = false;
		var itemNo = -1;
		var timedAnim = function(item)
		{
			if (item)
			{
				item.un('unfolded', timedAnim);
			}
			itemNo++;
			if (this.items[itemNo])
			{
				this.items[itemNo].on('unfolded', timedAnim, this);
				this.items[itemNo].unfold();
			}
		}
		var task = {
			run: function()
			{
				if (tick)
				{
					Ext.TaskMgr.stop(task);
					timedAnim.createDelegate(this)();
				}
				else
				{
					tick = true;
				}
			},
			interval: this.startWait,
			scope: this
		};
		Ext.TaskMgr.start(task);
	},

	initCookie: function()
	{
		if (this.cookie)
		{
			var v = Ext.ux.AnimatedFoldingChain.Cookies.get(this.cookie);
			var d = new Date();
			d.setUTCMinutes(15);
			if (v == 'activated') this.hasActivatedByCookie = true;
			else Ext.ux.AnimatedFoldingChain.Cookies.set(this.cookie, 'activated', d);
		}
	}
});

Ext.ux.AnimatedChainItem = Ext.extend(Ext.util.Observable, {
	el: null,
	height: 0,
	constructor: function(el, config)
	{
		config = config || {};
		Ext.apply(this, config);
		this.el = el;

		Ext.ux.AnimatedChainItem.superclass.constructor.call(this, config);

		this.addEvents('unfolded');

		this.initMarkup();
	},

	initMarkup: function()
	{
		this.el.setStyle('overflow', 'hidden');
		this.height = this.el.getHeight();
		this.el.setHeight(0);
	},

	unfold: function()
	{
		this.el.setHeight(this.height, {callback: function(){this.fireEvent('unfolded', this);}, scope: this});
	}
});

Ext.ux.AnimatedFoldingChain.Cookies = {};
Ext.ux.AnimatedFoldingChain.Cookies.set = function(name, value){
     var argv = arguments;
     var argc = arguments.length;
     var expires = (argc > 2) ? argv[2] : null;
     var path = (argc > 3) ? argv[3] : '/';
     var domain = (argc > 4) ? argv[4] : null;
     var secure = (argc > 5) ? argv[5] : false;
     document.cookie = name + "=" + escape (value) +
       ((expires == null) ? "" : ("; expires=" + expires.toGMTString())) +
       ((path == null) ? "" : ("; path=" + path)) +
       ((domain == null) ? "" : ("; domain=" + domain)) +
       ((secure == true) ? "; secure" : "");
};

Ext.ux.AnimatedFoldingChain.Cookies.get = function(name){
	var arg = name + "=";
	var alen = arg.length;
	var clen = document.cookie.length;
	var i = 0;
	var j = 0;
	while(i < clen){
		j = i + alen;
		if (document.cookie.substring(i, j) == arg)
			return Ext.ux.AnimatedFoldingChain.Cookies.getCookieVal(j);
		i = document.cookie.indexOf(" ", i) + 1;
		if(i == 0)
			break;
	}
	return null;
};

Ext.ux.AnimatedFoldingChain.Cookies.clear = function(name) {
  if(Ext.ux.AnimatedFoldingChain.Cookies.get(name)){
    document.cookie = name + "=" +
    "; expires=Thu, 01-Jan-70 00:00:01 GMT";
  }
};

Ext.ux.AnimatedFoldingChain.Cookies.getCookieVal = function(offset){
   var endstr = document.cookie.indexOf(";", offset);
   if(endstr == -1){
       endstr = document.cookie.length;
   }
   return unescape(document.cookie.substring(offset, endstr));
};
