﻿/* Copyright by Steen F. Tøttrup, CreativeMinds (http://creativeminds.dk) */
var CreativeMinds = new Class({Implements: Options, options: {},
	version: '1.0.0.1',
	about: 'CreativeMinds JS classes for MooTools',
	intialize: function(options) {
		this.setOptions(options);
	},
	GetVersion: function() {
		return this.version;
	},
	About: function() {
		return this.about;
	}
});
CreativeMinds.Debug=new Class({Extends:CreativeMinds,Implements:Options,options:{containerId:'',active:true},initialize:function(options){this.setOptions(options);},Clear:function(){if($(this.options.containerId)){$(this.options.containerId).set('text','');}},Append:function(text){if(this.options.active){if($(this.options.containerId)){$(this.options.containerId).set('text',$(this.options.containerId).get('text')+'['+text+']\n');}else{alert(text);}}}});

var debug = new CreativeMinds.Debug({containerId: 'debug', active: true});

var ShowLink = new Class({Implements: Options, options: {linkId: '', containerId: ''},
	effect: $empty,
	initialize: function(options) {
		this.setOptions(options);
		if (this.options.linkId != '' && $(this.options.linkId) && this.options.containerId != '' && $(this.options.containerId)) {
			this.effect = new Fx.Reveal($(this.options.containerId));
			$(this.options.linkId).addEvent('click', function(e) {
				e.stopPropagation();
				this.effect.toggle();
				//$(this.options.containerId).style.display = 'block';
				return false;
			}.bind(this));

			this.Hide();
		}
	},
	Hide: function() {
		$(this.options.containerId).style.display = 'none';
	},
	Show: function() {
		this.effect.reveal();
	}
});
var OnOffGroup = new Class({Implements: Options, options: {type: 'hide'},
	initialize: function(options) {
		this.setOptions(options);
		this.items = new Array();
	},
	AddItem: function(element, subElementContainer) {
		//debug.Append(this.items.length);
		this.items[this.items.length] = new OnOffItem({group: this, element: element, subContainer: subElementContainer, index: this.items.length});
		this.items[this.items.length - 1].addEvent('change', function(e) {
			this.SetSelected(e.GetIndex());
		}.bind(this));
	},
	SetSelected: function(index) {
		//debug.Append('set selected (index: ' + index + ')(length: ' + this.items.length + ')');
		for (idx = 0; idx < this.items.length; idx++) {
			if (idx != index) {
				//debug.Append('selected idx: ' + idx);
				this.items[idx].Off();
			}
		}
	},
	GetItem: function(index) {
		return this.items[index];
	}
});
var OnOffItem = new Class({Implements: [Events, Options], options: {group: $empty, element: $empty, subContainer: $empty, index: 0},
	initialize: function(options) {
		this.setOptions(options);
		this.checked = false;
		this.options.element.addEvent('click', function (e) {
			this.On();
		}.bind(this));
		
		this.options.subContainer.getChildren('input').each(function(item, index) {
			item.addEvent('click', function(e) {
				if (!this.checked) {
					this.On();
				}
			}.bind(this));
		}.bind(this));
		this.options.subContainer.getChildren('select').each(function(item, index) {
			item.addEvent('click', function(e) {
				if (!this.checked) {
					this.On();
				}
			}.bind(this));
		}.bind(this));
	},
	On: function() {
		this.checked = true;
		this.fireEvent('change', this);
		this.options.subContainer.style.opacity = 1;
		this.options.element.checked = true;
		this.options.element.blur();
		//debug.Append('turning on ' + this.options.index);
	},
	Off: function() {
		this.checked = false;
		this.options.subContainer.style.opacity = 0.5;
		this.options.element.checked = false;
		//debug.Append('turning off ' + this.options.index);
	},
	GetIndex: function() {
		return this.options.index;
	}
});
var PopUpFormWithStatus = new Class({ Implements: Options, options: { formId: '', formContainerId: '', inProgressText: '', doneText: '', statusObject: $empty },
	initialize: function(options) {
		this.setOptions(options);

		if ($(this.options.formContainerId)) {
			$(this.options.formContainerId).style.display = 'none';
		}

		if ($(this.options.formId)) {
			$(this.options.formId).addEvent('submit', function (e) {
				$(this.options.formId).set('send', {onSuccess: function() {
						if (this.options.statusObject) {
							this.options.statusObject.HideStatus(this.options.doneText);
						}
					}.bind(this)});
				if (this.options.statusObject) {
					this.options.statusObject.ShowStatus(this.options.inProgressText);
				}
				$(this.options.formId).send('/ajaxcontent.ajax');
				return false;
			}.bind(this));
		}
	},
	Show: function() {
		if ($(this.options.formContainerId)) {
			$(this.options.formContainerId).style.display = 'block';
		}
	}
});
var StatusBar = new Class({Implements: Options,	options: { containerId: '', closeDelay: 5, inProgressText: '', doneText: '' },
	container: $empty,
	effect: $empty,
	initialize: function(options) {
		this.setOptions(options);

		this.container = $(this.options.containerId);

		this.container.style.display = 'none';		
		//this.HideStatus();
	},
	ShowStatus: function(statusText) {
		if (this.container) {
			var output = this.options.inProgressText;
			if (statusText) {
				output = statusText;
			}
			//debug.Append(output);
			this.container.set('text', output);
			this.container.style.display = 'block';
			this.container.set('style', 'opacity: 1');
			//this.effect = new Fx.Tween(this.container);
			//this.effect.start('opacity', 0, 1);
		}
	},
	HideStatus: function(statusText) {
		if (this.container) {
			var output = this.options.doneText;
			if (statusText) {
				output = statusText;
			}
			var ch = new Chain();
			var first = function() {
				this.container.set('text', output);
				this.effect = new Fx.Tween(this.container);
				this.effect.start('opacity', 1, 0).chain(function() {
					this.container.style.display = 'none';
					this.container.style.opacity = '1';
				}.bind(this));
			}.bind(this);
			ch.chain(first);
			ch.callChain();
			// TODO: Delay !??!?!
		}
	}
});
var Section = new Class({Implements: [Events, Options], options: { element: $empty, link: '', container: '', show: false, onHide: $empty, onShow: $empty },
	visible: false,
	container: $empty,
	link: $empty,
	effect: $empty,
	initialize: function(options) {
		this.setOptions(options);
		
		if (this.options.link != '' && this.options.container != '') {
			this.link = this.options.element.getFirst('div[class=' + this.options.link + ']');
			this.link.addEvent('click', function(e){
				this.Toggle();
				e.stopPropagation();
				return false;
			}.bind(this));
			this.container = this.options.element.getFirst('div[class=' + this.options.container + ']');
			this.effect = new Fx.Reveal(this.container);
/*
			if (this.options.onHide != $empty) {
				this.addEvent('hide', this.options.onHide);
			}
			if (this.options.onShow != $empty) {
				this.addEvent('show', this.options.onShow);
			}
*/
			if (!this.options.show) {
				this.container.style.display = 'none';
			}
			else {
				this.container.style.display = 'block';
			}
		}
	},
	Toggle: function() {
		this.effect.toggle();
		if (this.visible) {
			this.Hide();
		}
		else {
			this.Show()
		}
	},
	Hide: function() {
		this.fireEvent('hide', this);
		//this.container.style.display = 'none';
		this.visible = false;
	},
	Show: function() {
		this.fireEvent('show', this);
		//this.container.style.display = 'block';
		this.visible = true;
	}
});
var AjaxForm = new Class({Implements: [Events, Options], options: {form: $empty, statusBar : $empty, destination: $empty, type: 'append', reset: false},
	html: '',
	initialize: function(options) {
		this.setOptions(options);
		if (this.options.form != $empty) {
			this.options.form.addEvent('submit', this.Submit.bind(this));
		}
	},
	Submit: function(e) {
		this.success = false;
		e.target.set('send', {onSuccess: this.Success.bind(this), onComplete: this.Complete.bind(this)});
		// TODO: onError/onFailure/onSuccess ?!?!?
		if (this.options.statusBar != $empty) {
			this.options.statusBar.ShowStatus();
		}
		e.target.send('/ajaxcontent.ajax');
		return false;
	},
	Complete: function(tree, elements, html, js) {
	},
	Success: function(response) {
		if (this.options.destination != $empty) {
			var newContent = response;
			if (this.options.type == 'append') {
				newContent = this.options.destination.get('html') + newContent;
			}
			if (this.options.statusBar != $empty) {
				this.options.statusBar.HideStatus();
			}
			this.options.destination.set('html', newContent);
		}
		else {
			if (this.options.statusBar != $empty) {
				this.options.statusBar.HideStatus();
			}
		}
		if (this.options.reset) {
			this.options.form.reset();
		}
		//debug.Append(this.returnElements);
		this.fireEvent('success', response);
	}
});
var DynamicList = new Class({Implements: [Events, Options], options: {list: $empty, statusBar : $empty, deleteClass: '', needConfirmation: false, confirmationText: ''},
	items: null,

	initialize: function(options) {
		this.setOptions(options);
		
		this.items = new Array();
		if (this.options.list != $empty && this.options.list) {
			this.options.list.getChildren('li').each(function (item, index) {
				this.items[index] = item;
				item.getChildren('a').each(function(item2, index2) {
					if (item2.get('class') == this.options.deleteClass) {
						item2.addEvent('click', function(e) {
							this.DeleteItem(e);
							e.stopPropagation();
							return false;
						}.bind(this));
					}
				}.bind(this));
			}.bind(this));
		}
	},
	DeleteItem: function(e) {
		if (!this.options.needConfirmation || (this.options.needConfirmation && confirm(this.options.confirmationText))) {
			this.fireEvent('delete', e);
			this.items.erase(e.target.getParent());

			if (e) {
				new Event(e).stop();
			}
		}
	},
	Remove: function(item) {
		var ch = new Chain();
		var first = function() {
			var effect = new Fx.Tween(item);
			effect.start('opacity', 1, 0).chain(function() {
				item.style.display = 'none';
				item.destroy();
			}.bind(item));
		};
		ch.chain(first);
		ch.callChain();
	},
	AddItem: function(html) {
		var newElement = new Element('li', {html: html});
		if (this.items.length > 0) {
			this.options.list.getLast('li').injectAfter(newElement);
			//debug.Append(html);
		}
		else {
			//debug.Append('list empty, inserting inside');
			//this.options.list.injectInside(newElement);
		}
		this.options.list.adopt(newElement);
		this.items[this.items.length] = newElement;

		newElement.getChildren('a').each(function(item, index) {
			if (item.get('class') == this.options.deleteClass) {
				item.addEvent('click', function(e) {
					this.DeleteItem(e);
					e.stopPropagation();
					return false;
				}.bind(this));
			}
		}.bind(this));
	},
	GetItems: function() {
		return this.items;
	}
});
var LinkAction = new Class({Implements: [Events, Options], options: {link: $empty, arguments: '', action: ''},
	initialize: function(options) {
		this.setOptions(options);
		
		if (this.options.link && this.options.link != $empty) {
			this.options.link.addEvent('click', this.Click.bind(this));
		}
	},
	Click: function(e) {
		if (e) {
			new Event(e).stop();
		}
		var request = new Request.HTML({method: 'get', url: '/ajaxcontent.ajax', onFailure: this.Failure.bind(this), onSuccess: this.Success.bind(this)});
		if (this.options.arguments.length > 0) {
			this.options.arguments += '&';
		}
		this.options.arguments += 'Action=' + this.options.action;
		request.send(this.options.arguments);
	},
	Success: function(tree, elements, html) {
		if (html.indexOf("<error>") == 0) {
			this.fireEvent('failure', html.substr('<error>'.length, html.length - '<error>'.length - '</error>'.length));
		}
		this.fireEvent('success', html);
	},
	Failure: function(e) {

		this.fireEvent('failure', e);
	}
});