// Global ThinkPop.com JS definitions
// Created: Dmitri Smirnov, Wildbit LLC

var ThinkPop = {
	initialize: function(){
		Nifty("div.block-entry-wrap");
		Nifty("div.block-user");
		Nifty("form.search");
		Nifty("div.rounded");

		this.formThought = $('form-thought');
		if(this.formThought){
			this.counter = this.formThought.down('.count strong');
			this.field = this.formThought.down('.field-thought');
			this.hidden = this.formThought.down('input[type="hidden"]');
			this.current = this.formThought.down('p.current span');
			this.name = '';
//			Event.observe(this.field, 'keyup', function(event){ 
//				if(event.keyCode == Event.KEY_RETURN) ThinkPop.submitThought();
//			})
			Event.observe(this.field, 'keyup', function(){ ThinkPop.updateThought(); })
			this.formThought.insert({bottom: '<div class="statusmsg-thought"></div>'})
			var msg = this.formThought.down('.statusmsg-thought');
			this.statusMsg = new Notificator(msg, 2000);
		}
		$$('.action-reply').invoke("observe", "click", function(el) { ThinkPop.reply(this); return false }, false);


	},

	updateThought: function(event){
		var len = this.field.value.length;
		if(len > 140) {
			this.field.value = this.field.value.substring(0,140);
			len = 140;
		}
		this.counter.update(len);
	},

	submitThought: function(){
		if(this.field.value.strip() == ''){
			this.field.value = '';
			this.field.focus();
			this.statusMsg.showMessage('Please, type anything', 'error');
		}
		else {

			// Do AJAX Submit here
			Update(this.field.value, this.hidden.value);
			$$('.action-reply').invoke("observe", "click", function(el) { ThinkPop.reply(this) }, false);
			var current = this.field.value.strip();
			this.hidden.value = '-1';
			this.field.value = '';
			this.statusMsg.showMessage('Update posted!', 'ok');
			var obj = this;

			Nifty("div.block-entry-wrap");
			setTimeout(function(){
				obj.current.update(current).highlight();
				obj.name = '';
				ThinkPop.updateThought();
			}, 2000)

		}
		return false;
	},

	reply: function(el){
		if(this.formThought){
			this.name = '@'+$(el).previous('a.user-name').innerHTML;
			this.field.value = this.name + ' ';
			this.hidden.value = $(el).up('.block-entry').id;
			if(this.field.setSelectionRange) {
				this.field.focus();
				this.field.setSelectionRange(140,140);
			}
			else {
				if(this.field.createTextRange) {
					range=this.field.createTextRange();
					range.collapse(true);
					range.moveEnd('character',140);
					range.moveStart('character',140);
					range.select();
				}
			} 
			this.updateThought();
		}
	},

	addSpinner: function(el) {
		if(!$(el).next('img.spinner'))$(el).insert({after: '<img src="../img/spinner_small.gif" alt="" class="spinner" />'})
	},

	removeSpinner: function(el) {
		$(el).next('img.spinner').remove();
	}
}




/* Notificator Class */
var Notificator = Class.create();

Notificator.prototype = {
	
	initialize: function(obj, timeout) {
		this.objPointer = $(obj);
		if(!this.objPointer) return;
		
		this.timeout = timeout;
		this.initialized = false;
	},
	
	showMessage: function(msg, type) {
		if(this.initialized) return false; // Don't show multiple notifications
		var borderCol;
		
		// Building DOM elements
		this.msg = document.createElement('div');
		var p = document.createElement('p');
		p.innerHTML = msg;
		this.msg.appendChild(p);
		this.msg.className = "statusmsg";
		this.msg.style.display = "none";
		
		if(type.indexOf("error") > -1)
			Element.addClassName(this.msg, "status-error");
		else if(type.indexOf("ok") > -1)
			Element.addClassName(this.msg, "status-ok");
		else
			Element.addClassName(this.msg, "status-info");

		this.objPointer.appendChild(this.msg);
		Element.toggle( this.msg );
		Nifty("div.statusmsg");

		this.initialized = true;
		if(this.timeout != 0) setTimeout( this.hideMessage.bind(this), this.timeout );
	},
	
	hideMessage: function() {
		var self = this;
		Effect.toggle( this.msg, "appear", { duration: 0.5, afterFinish: function(effect) 
		   {
				Element.remove(self.msg);
				self.initialized = false;
		   }
		} );
	}
};


/* Add browser and OS info to html tag */
var cssFix = function(){
  var u = navigator.userAgent.toLowerCase(),
  is = function(t){return (u.indexOf(t)!=-1)};
  $$("html")[0].addClassName([
    (!(/opera|webtv/i.test(u))&&/msie (\d)/.test(u))?('ie ie'+RegExp.$1)
      :is('firefox/2')?'gecko ff2'
      :is('firefox/3')?'gecko ff3'
      :is('gecko/')?'gecko'
      :is('opera/9')?'opera opera9':/opera (\d)/.test(u)?'opera opera'+RegExp.$1
      :is('konqueror')?'konqueror'
      :is('applewebkit/')?'webkit safari'
      :is('mozilla/')?'gecko':'',
    (is('x11')||is('linux'))?' linux'
      :is('mac')?' mac'
      :is('win')?' win':''
  ].join(''));
}();


document.observe("dom:loaded", function(){
	ThinkPop.initialize();
});
