/**
 *	Requires:
 *	W.Request
 */
W.Miller = function(param)
{
	this.init(param);
};



W.Miller.prototype = {
	
	
	
	container	: null,
	xhr			: null,
	index		: -1,
	cookie_id	: '',
	
	
	
	init : function(param)
	{
		this.container	= W.$(param.container);
		this.xhr		= param.xhr;
		this.cookie_id	= param.cookie || this.cookie_id;
		
		var cookie		= this.get_cookie();
				
		this.request(0, param.column, param.id);
	},
	
	
	
	request : function(index, column, id)
	{
		var obj	= this;
		
		this.add(index);
		
		column	= column || '';
		id		= id || '';
		id = id.replace(/\//g, "%2F");
		
		new W.Request('GET', this.xhr + '?c=' + column + '&id=' + id).call(obj.populate.bind(obj, index));
	},
	
	
	
	add : function(index)
	{
		var divs	= this.container.getElementsByTagName('div');
		var cookie	= this.get_cookie();
		
		if (index == divs.length) {
			var div		= W.Dom.append(this.container, W.Dom.make('div', {'className': 'column column' + (index + 1)}));
			var ul		= W.Dom.append(div, W.Dom.make('ul'));
		} else {
			divs[index].innerHTML = '<ul></ul>';
			
			for (var i = divs.length; --i > index; ) {
				W.Dom.remove(divs[i]);
				cookie.pop();
			}
		}

		if (this.cookie_id) { W.Cookie.set(this.cookie_id, cookie.join('|')); }
	},
	
	
	
	populate : function(req, index)
	{
		var div	= this.container.getElementsByTagName('div')[index];
		var ul	= div.getElementsByTagName('ul')[0];
		
		ul.innerHTML = req.responseText;
		
		this.add_actions(ul, index);
		
		var cookie = this.get_cookie(this.cookie_id);
		
		if (cookie && cookie[index] !== undefined) {
			var lis	= ul.getElementsByTagName('li');
			var i	= cookie[index];
			if (lis[i]) {
				lis[i].className = 'selected';
				var link = lis[i].firstChild;
				this.select(null, link);
			}
		}
	},
	
	
	
	add_actions : function(ul, index)
	{
		var obj		= this;
		var links	= ul.getElementsByTagName('a');
		
		for (var i = links.length; --i >= 0; ) {
			links[i]._index	= index;
			links[i].setAttribute('_index', index);
			W.Event.add(links[i], 'click', function(e) { obj.select(e, this); });
		}
	},
	
	
	
	select : function(e, link)
	{
		var column	= this.columnname(link);

		if (column) {
			W.Event.stop(e);
			
			var li	= link.parentNode;
			var ul	= li.parentNode;
			
			W.Dom.removeClassName(W.Dom.getElementsByClassName('selected', ul), 'selected');
			li.className = 'selected';
		
			var index	= parseInt(link.getAttribute('_index'), 10);
			var id		= link.getAttribute('_id');
			
			if (W.Dom.isSafari) { link.innerHTML = link.innerHTML; }	// bust the Safari 3.2 cache from getElementsByClassName result otherwise over time it doesn't know which <li> tags are selected
		
			this.set_cookie(index, li);
			this.request(++index, column, id);
		}
	},
	
	
	
	get_cookie : function()
	{
		if (this.cookie_id != '') {
			var cookie	= W.Cookie.get(this.cookie_id)
			cookie = cookie ? cookie.split('|') : [];
		
			return cookie;
		}
		
		return false;
	},
	
	
	
	set_cookie : function(index, li)
	{
		if (this.cookie_id == '') { return; }
		
		var i = 0;

		while (li = li.previousSibling) { 
			if (li.nodeType == 1) { ++i; }
		}
		
		var cookie		= this.get_cookie();
		
		if (cookie) {
			cookie[index] = i;
		
			W.Cookie.set(this.cookie_id, cookie.join('|'));
		}
	},
	
	
	
	columnname : function(link)
	{
		var hash	= false;
				
		if (link) {
			if (link.hash) { hash = link.hash.substr(1); }
		} else {
			var i	= link.indexOf('#');
			
			if (i != -1) {
				hash = link.substr(i + 1);
			}
		}
		
		return hash;
	}
};

