if(typeof Prototype=='undefined')
	throw "Cellpex requires Prototype to be loaded."; 
	
var Posts = Class.create();
Posts.prototype = {
	initialize: function(element,url1,url2,options) {
		element = $(element);
		this.element = element;
		if(this.element) {
			this.url1 = url1;
			this.url2 = url2;
		    this.options = Object.extend({hover_color : "#D6DEFF", _loading_mess: $('loading_mess')}, options || {});
		    
			/* add event listener */
			this.mouseoverListener = this.hover.bindAsEventListener(this);
			this.mouseoutListener = this.lose_hover.bindAsEventListener(this);
			this.clickListener = this.togglePosts.bindAsEventListener(this);
		    Event.observe(this.element, 'mouseover', this.mouseoverListener);
		    Event.observe(this.element, 'mouseout', this.mouseoutListener);
		    Event.observe(this.element, 'click', this.clickListener);
		}
		this.overlibTitle = null;
	},
	hover: function(evt) {
		if(Event.element(evt).tagName != 'TABLE') {
			var tr = Event.findElement(evt, 'TR');
			if($(tr).down().tagName!='TH')
				$(tr).setStyle({backgroundColor:this.options.hover_color});
			var a = Event.findElement(evt, 'A');
			if (typeof a != 'undefined') {
				if (a.tagName.match(/a/i)) {
					var price = null;
					var currency = null;
					var comments = null;
					var title = null;
					if(a.name.match(/converter/i)) {
						a.rel.sub(/([0-9.]+);(EUR|USD|GBP)/, function(match){price=match[1];currency=match[2];})
					} else if(a.name.match(/post info/i)) {
						comments			 = a.rel;
						this.overlibTitle	 = a.title;
						a.title				 = "";
					}
					if (price && currency)
						overlib(CurrencyValue(price, currency), SHADOW, SHADOWY, 3,SHADOWX, 3, SHADOWOPACITY, 80, CAPTION, 'Currency Converter:', VAUTO, CENTER);
					else if(comments) {
						overlib(comments, SHADOW, SHADOWY, 3,SHADOWX, 3, SHADOWOPACITY, 80, CAPTION, this.overlibTitle, VAUTO, CENTER, WIDTH, 300);
					}
				}
			}
		}
	},
	lose_hover: function(evt) {
		if(Event.element(evt).tagName != 'TABLE') {
			var tr = Event.findElement(evt, 'TR');
			if($(tr).down().tagName!='TH')
				$(tr).setStyle({backgroundColor:''});
			var a = Event.findElement(evt, 'A');
			if (typeof a != 'undefined') {
				if (a.tagName == "A") {
					if(a.name.match(/converter/i) || a.name.match(/info/i))
					{
						a.title = this.overlibTitle;
						nd();
					}
				}
			}
		}
	},
	togglePosts: function(evt) {
		var this_elem = this;
		var a = Event.findElement(evt, 'A');
		if (typeof a != 'undefined') {
			if (a.tagName.match(/a/i) && a.id.match(/([a-z0-9_]+)/i)) {
				this_elem.element.getElementsBySelector("tr."+a.id).each(
					function(item) {
						if(item.hasClassName('expandable')) {
							item.removeClassName('expandable');
							a.removeClassName('collapse');
							a.addClassName('expand');
							this_elem.toggleReplies(item);
							if (a.id.match(/reply_([0-9]+)/)) new Ajax.Request(this_elem.url1,{parameters: 'id='+a.id+'&lvl=2'});
							else new Ajax.Request(this_elem.url1,{parameters: 'id='+a.id});
						} else {
							item.addClassName('expandable');
							a.removeClassName('expand');
							a.addClassName('collapse');
							this_elem.toggleReplies(item);
							if (a.id.match(/reply_([0-9]+)/))  new Ajax.Request(this_elem.url2,{parameters: 'id='+a.id+'&lvl=2'});
							else new Ajax.Request(this_elem.url2,{parameters: 'id='+a.id});
						}
					}
				);
			}
		}
	},
	toggleReplies: function(elem) {
		var this_elem = this;
		elem.descendants().each(
			function(item) {
				if (item.tagName.match(/a/i) && item.id.match(/reply_([0-9]+)/)) {
					if(item.className.match('expand')) {
						this_elem.element.getElementsBySelector("tr."+item.id).each(
							function(elem) {
								if(elem.hasClassName('expandable')) {
									elem.removeClassName('expandable');
								} else {
									elem.addClassName('expandable');
								}
							}
						);
						new Ajax.Request(this_elem.url2,{parameters: 'id='+item.id+'&lvl=2'});
					}
				}
			}
		);
	},
	showLoadingMess: function() {
		this.options._loading_mess.show();
	},
	hideLoadingMess: function() {
		this.options._loading_mess.hide();
	},
	collapseAll: function(evt) {
		var this_elem = this;
		var options = {
			onLoading: function() {
				this_elem.showLoadingMess();
			},
			onComplete: function() {
				this_elem.element.getElementsBySelector('[class="expand"]').each(
					function(item) {
						var nr_elems = this_elem.element.getElementsBySelector('tr.'+item.id).each(
							function(elem) {
								elem.addClassName('expandable');
							}
						);
						item.removeClassName('expand');
						item.addClassName('collapse');
					}
				);
				this_elem.hideLoadingMess();
			}
		};
		new Ajax.Request(this_elem.url2, options);
	},
	expandAll: function(evt) {
		var this_elem = this;
		var options = {
			onLoading: function() {
				this_elem.showLoadingMess();
			},
			onComplete: function() {
				this_elem.element.getElementsBySelector('a.collapse').each(
					function(item) {
						item.removeClassName('collapse');
						item.addClassName('expand');
					}
				);
				this_elem.element.getElementsBySelector('tr.expandable').each(
					function(item) {
						item.removeClassName('expandable');
					}
				);
				this_elem.hideLoadingMess();
			}
		};
		new Ajax.Request(this_elem.url1, options);
	}
}

var ClearingDefaultText = Class.create();
ClearingDefaultText.prototype = {
	initialize: function(element, text, options) {
		element = $(element);
		this.element = element;
		if(this.element) {
			this.text = text;
		    this.options = Object.extend({
		    			focusColor : "#000000", 
		    			blurColor : "#9999AC"
		    		}, options || {}
		    	);
		    
			/* add event listener */
			this.blurListener = this.blur.bindAsEventListener(this);
			this.focusListener = this.focus.bindAsEventListener(this);
		    Event.observe(this.element, 'blur', this.blurListener);
		    Event.observe(this.element, 'focus', this.focusListener);
		    
		    if (this.element.value.blank()) {
		    	this.element.value = this.text;
		    	this.element.setStyle({color:this.options.blurColor});
		    }
		    
		}
	},
	blur: function(evt) {
		if (this.element.value.blank()) {
	    	this.element.value = this.text;
	    	this.element.setStyle({color:this.options.blurColor});
		}
	},
	focus: function(evt) {
		if (this.element.value == this.text) {
	    	this.element.value = '';
	    	this.element.setStyle({color:this.options.focusColor});
		}
	}
}

