var messageStack = stdClass.extend({
	//constructor
	constructor: function(el, settings) {
		this.base();
		// initialize settings
		Object.extend(this.s, {
			timer: null,
			waitTime: 4000,
			effectTime: 2000,
			closeSelector: ".close"
			/* put extensions to collections here */
		});
		
		// initialize nodes
		Object.extend(this.n, {
			el: el,
			childEl: [],
			tempContainer: document.createElement('div')
			/* put extensions to nodes here */
		});
		
		// initialize collections
		Object.extend(this.c, {
			messageFunctions: {
				success: this.successMessage.bind(this),
				error: this.errorMessage.bind(this)
			}
			/* put extensions to collections here */
		});
		var eles = this.n.el.getElementsBySelector('ul');
		
		for(var x=0; x<eles.length; x++){
			var childEl = {
				el: eles[x],
				timer: null
			}
			this.startTimer(childEl);
			this._attachEvents(childEl);
		}
	},
	closeClicked: function(childEl){
		this.removeMessage(childEl);
	},
	addMessage: function(message, type){
		if(!this.n.el){
			return;
		}
		if(typeof(this.c.messageFunctions[type]) == "function"){
			var html = this.c.messageFunctions[type](message);
		}else{
			return;
		}
		
		this.n.tempContainer.innerHTML = html;
		var childEl = {
			el:this.n.tempContainer.firstChild,
			timer:null
		}
		this.n.el.appendChild(childEl.el);
		this._attachEvents(childEl);
		Element.show(this.n.el);
		if(window.Effect){
			Effect.BlindDown(childEl.el,{
				duration: (this.s.effectTime/1000),
				afterFinishInternal: function(effect) {
					effect.element.undoClipping();
					effect.element.style.height = '';
				}
			});
		}else{
			Element.show(childEl.el);
		}
		this.startTimer(childEl);
	},
	removeMessage: function(childEl) {
		this.endTimer(childEl);
		if(window.Effect){
			Effect.Fade(childEl.el, {
				duration: (this.s.effectTime/1000)
			});
			/*Effect.BlindUp(childEl.el,{
				duration: (this.s.effectTime/1000),
				afterFinishInternal: function(effect) {
					effect.element.undoClipping();
					effect.element.style.height = '';
					Element.hide(effect.element);
				}
			});*/
		}else{
			Element.hide(this.n.el);
		}
	},
	startTimer: function(childEl) {
		this.endTimer(childEl);
		this.s.timer = setTimeout(this.removeMessage.bind(this, childEl), this.s.waitTime);
	},
	endTimer: function(childEl) {
		if(childEl.timer != null){
			clearTimeout(childEl.timer);
			childEl.timer = null;
		}
	},
	successMessage: function(message){
		var html = '<ul class="success">';
		html += '<li class="title">Success</li>';
		html += '<li>' + message + '</li>';
		html += '</ul>';
		return html;
	},
	errorMessage: function(message){
		var html = '<ul class="error">';
		html += '<li class="title">Error</li>';
		html += '<li>' + message + '</li>';
		html += '<li class="close">Close me</li>';
		html += '</ul>';
		return html;
	},
	_attachEvents: function(childEl) {
		var eles = document.getElementsBySelector(this.s.closeSelector, (childEl.el||this.n.el));
		for(var x=0; x<eles.length; x++){
			this.eObserve(eles[x], 'click', this.closeClicked.bind(this, childEl));
		}
	}
});
Behaviour.register({
	'div#messagestack' : function(el){
		new messageStack (el);
	}
});var makeLoginBox = function(node, e) {
	new FormDialog({
		'+zones': {
			heading: 'Loading...',
			text: 'Loading...'
		},
		position: {  
			exemplarAnchor: 'bottom right',
			selfAnchor: 'top right',
			exemplar: node,
			offsetY: 0,
			offsetX: 0
		},
		groupId: 'AuthLogin',
		groupLimit: 1,
		startHidden: true,
		moduleName: 'AuthLogin',
		parentThis: this,
		classNames: ['kDialog2', 'authLogin']
	}, node);
	if(e){
		Event.stop(e);
	}
	return false;
}
EventSelectors.register({
    'a.loginBox:click': function(node, e) {
		if(String(node.onclick).indexOf('makeLoginBox') == -1){
			makeLoginBox(node,e);
		}
	}
});var aUpdateRecentBlogs = stdClass.extend({
	//constructor
	constructor: function(el, settings) {
		this.base();
		// initialize settings
		Object.extend(this.s, {
			waitTime: 10000,
			waitOffset: null,
			timer: null,
			type: 'users',
			tagsString: null,
			moduleName: 'BlogsRecentBlogs',
			json: {
				ver: '0.1',
				meta: {},
				data: {
					requests: []
				},
				ele: document.createElement('div')
			},
			lastupdated: null,
			itemSelector: 'div.item',
			maxShown: 8
			/* put extensions to collections here */
		});
		Object.extend(this.s, settings);
		
		// initialize nodes
		Object.extend(this.n, {
			el: el
			/* put extensions to nodes here */
		});
		
		// initialize collections
		Object.extend(this.c, {
			requests: [],
			items: []
			/* put extensions to collections here */
		});
		
		Element.parseClasses(this.n.el);
		
		this.s.lastupdated = this.n.el.vars.lastupdated;
		
		this.s.type = this.n.el.vars.blogType;
		
		this.s.tagsString = this.n.el.vars.tagsString;
		
		//this.runInterval();
		window.aUpdateRecentBlogsObject.instances.push(this);
		//alert(window.aUpdateRecentBlogsInstances.length);
		//setInterval(this.runInterval.bind(this), 10000)
		//this._startTimer();
		return;
		if(this.s.waitOffset != null && this.s.waitOffset != 0){
			setTimeout(this._startTimer.bind(this), this.s.waitOffset);
		}else{
			//this._startTimer();
			setInterval(this.runInterval.bind(this), 10000)
		}
	},
	insertBlog: function() {
		if(this.c.items.length <= 0){
			return;
		}
		var blog = this.c.items[0];
		
		this.c.items.splice(0,1);
		var cur_blogs = document.getElementsBySelector(this.s.itemSelector, this.n.el);
		if(cur_blogs.length > 0){
			cur_blogs[0].parentNode.insertBefore(blog, cur_blogs[0]);
		}else{
			var cont = document.getElementsBySelector('div.box_content', this.n.el)[0];
			cont.insertBefore(blog, cont.firstChild);
		}
		blog.style.display = "none";
		Effect.BlindDown(blog, {duration:.5});
		new Effect.Highlight(blog, {duration: 3, startcolor: '#DFEEF0'});
		
		if(cur_blogs.length >= this.s.maxShown){
			Effect.BlindUp(cur_blogs[cur_blogs.length-1], {duration:.5});
			//cur_blogs[cur_blogs.length-1].parentNode.removeChild(cur_blogs[cur_blogs.length-1]);
		}
	},
	getUpdatedBlogs: function() {
		this.addRequest('getUpdatedBlogs', {
			lastupdated: this.s.lastupdated,
			type: this.s.type,
			tagsString: this.s.tagsString
		});
	},
	runInterval: function() {
		this.getUpdatedBlogs();
	},
	
	addRequest: function(requestName, dataObject) {
		this.c.requests.push({
			id: this.c.requests.length,
			type: requestName,
			data: dataObject
		});
		this.s.timesUpdated ++;
		this.JsonOut();
	},
	//json methods
	JsonOut: function() {
		//construct the post
		this.s.json.data.requests = this.c.requests;
		var data = '__json=' + this.s.moduleName + '&data=' + Object.toJSON(this.s.json);
		this.c.requests = [];
		//send out the request
		var myAjax = new Ajax.Request(window.location,
		{
			method: 'post',
			parameters: data,
			onSuccess: this.JsonIn.bind(this)
		});
	},
	
	JsonIn: function(t) {
		var result = t.responseText.evalJSON();
		(result.responses.length).times(function(i) {
			if(result.responses[i].type == 'updatedBlogs'){
				var jsonele = document.createElement('div');
				jsonele.innerHTML = result.data.html;
				var eles = document.getElementsBySelector(this.s.itemSelector, jsonele);
				if(eles.length > 0){
					for(var x=eles.length-1; x>=0; x--){
						this.c.items.push(eles[x]);
					}
					Element.parseClasses(jsonele.firstChild);
					if(jsonele.firstChild.vars){
						this.s.lastupdated = jsonele.firstChild.vars.lastupdated;
					}
				}
				this.insertBlog();
			}
		}.bind(this));
		this._startTimer();
	},
	
	_startTimer: function() {
		return;
		setTimeout(this.runInterval.bind(this), 10000);
		//setTimeout(this._delayTimer.bind(this), this.s.waitTime*30);
	},
	_delayTimer: function() {
		clearInterval(this.s.timer);
		this.s.timer = setInterval(this.runInterval.bind(this), this.s.waitTime*30);
	}
});
window.aUpdateRecentBlogsObject = {
	instances: [],
	num: 0,
	interval: function() {
		if(window.aUpdateRecentBlogsObject.instances[window.aUpdateRecentBlogsObject.num]){
			window.aUpdateRecentBlogsObject.instances[window.aUpdateRecentBlogsObject.num].runInterval();
		}
		if(parseInt(window.aUpdateRecentBlogsObject.num) >= window.aUpdateRecentBlogsObject.instances.length){
			window.aUpdateRecentBlogsObject.num = 0;
		}else{
			window.aUpdateRecentBlogsObject.num++;
		}
	},
	timer: null,
	startTimer: function() {
		if(window.aUpdateRecentBlogsObject.timer == null){
			window.aUpdateRecentBlogsObject.timer = setInterval('window.aUpdateRecentBlogsObject.interval()', 3333)
		}
	}
}
EventSelectors.register({
	'div.aUpdateRecentBlogs': function(el, index) {
		new aUpdateRecentBlogs(el);
		window.aUpdateRecentBlogsObject.startTimer();
	}
}, true);
