
/* file:base/AmxBase.class.js */

// +---------------------------------------------------------------------------+
// | This file is part of the Agavi and Mootools Extension package.            |
// | Copyright (c) 2006, 2007 Jean-Philippe Dery                               |
// |                                                                           |
// | For the full copyright and license information, please view the LICENSE   |
// | file that was distributed with this source code.                          |
// +---------------------------------------------------------------------------+

var AmxBase =
{
	isIE : function()
	{
		return ((clientPC.indexOf("msie") != -1) && (clientPC.indexOf("opera") == -1));
	},

	isFF : function()
	{
		return	((clientPC.indexOf('mozilla')!=-1) && (clientPC.indexOf('spoofer')==-1) && (clientPC.indexOf('compatible') == -1) && (clientPC.indexOf('opera')==-1) && (clientPC.indexOf('webtv')==-1) && (clientPC.indexOf('hotjava')==-1));
	},

	isWin : function()
	{
		return ((clientPC.indexOf("win")!=-1) || (clientPC.indexOf("16bit") != -1));
	},

	isMac : function()
	{
		return (clientPC.indexOf("mac")!=-1);
	}
}
/* file:element/Element.class.js */

// +---------------------------------------------------------------------------+
// | This file is part of the Agavi and Mootools Extension package.            |
// | Copyright (c) 2006, 2007 Jean-Philippe Dery                               |
// |                                                                           |
// | For the full copyright and license information, please view the LICENSE   |
// | file that was distributed with this source code.                          |
// +---------------------------------------------------------------------------+

Element.extend(
{

	setId : function(id)
	{
		this.setProperty('id', id);
	},

	getId : function()
	{
		return this.getProperty('id');
	},

	show : function(as)
	{
		this.setStyle('display', as == undefined ? 'inline' : as);
	},

	hide : function()
	{
		this.setStyle('display', 'none');
	}
});
/* file:element/AmxElement.class.js */

// +---------------------------------------------------------------------------+
// | This file is part of the Agavi and Mootools Extension package.            |
// | Copyright (c) 2006, 2007 Jean-Philippe Dery                               |
// |                                                                           |
// | For the full copyright and license information, please view the LICENSE   |
// | file that was distributed with this source code.                          |
// +---------------------------------------------------------------------------+

var AmxElement = new Class(
{
    element : null,

	initialize : function(element, create)
	{
		this.element = create ? new Element(element) : $(element);
	},

	setId : function(id)
	{
		this.element.setProperty('id', id);
	},

	getId : function()
	{
		return this.element.getProperty('id');
	},

	addEvent : function(name, event)
	{
		this.element.addEvent(name, event);
	},

	fireEvent : function(event)
	{
		this.element.fireEvent(event);
	},

	setProperty : function(name, value)
	{
		this.element.setProperty(name, value);
	},

	getProperty : function(name)
	{
		return this.element.getProperty(name);
	},

	addClass : function(className)
	{
		this.element.addClass(className);
	},

	hasClass : function(className)
	{
		return this.element.hasClass(className);
	},

	removeClass : function(className)
	{
		this.element.removeClass(className);
	}
 });
/* file:element/AmxElementFinder.class.js */

// +---------------------------------------------------------------------------+
// | This file is part of the Agavi and Mootools Extension package.            |
// | Copyright (c) 2006, 2007 Jean-Philippe Dery                               |
// |                                                                           |
// | For the full copyright and license information, please view the LICENSE   |
// | file that was distributed with this source code.                          |
// +---------------------------------------------------------------------------+

var AmxElementFinder = new Class(
{
	id : null,

	tag : null,

	haystack : null,

	initialize : function(id, tag, haystack)
	{
		this.id = id;
		this.tag = tag;
		this.haystack = haystack;
	},

	setHaystack : function(haystack)
	{
		this.haystack = haystack;
	},

	getHaystack : function()
	{
		return this.haystack;
	},

	find : function()
	{
		// the haystack can have several different type. the most common will be
		// a dom document but it's also possible, whith an ajax response, that
		// the element will simply be a common string
		if (this.haystack == undefined) {
			return $(this.id);
		}
		// at this point we have a haystack to search from. the last thing to
		// check up is whether the haystack is a string, mostly from
		// responseText or a common dom tree
		switch (typeof this.haystack) {
			case 'object' :
				return this.haystack.getElementById(this.id);
			case 'string' :
				// create a function to filter the results. in this case it's
				// important to specify a tag to search with the id to minimise
				// the results therefore making it more efficient
				function searchFilter(elem, index, array) {
					var searchType = this.id.substr(0, 1);
					var searchString = this.id.substr(1);
					switch (searchType) {
						case '#' :
							if (elem.getProperty('id') == searchString)
								return elem;
							break;
						case '.' :
							if (elem.hasClass(searchString))
								return elem;
							break;
						default :
							if (elem.getProperty('id') == searchId)
								return elem;
							break;
					}
				}
				var result = $A(new Element('div').setHTML(this.haystack).getElements(this.tag)).filter(searchFilter.bind(this));
				return result.length > 0 ? result[0] : false;
		}
	}
});
/* file:event/AmxSelector.class.js */

// +---------------------------------------------------------------------------+
// | This file is part of the Agavi and Mootools Extension package.            |
// | Copyright (c) 2006, 2007 Jean-Philippe Dery                               |
// |                                                                           |
// | For the full copyright and license information, please view the LICENSE   |
// | file that was distributed with this source code.                          |
// +---------------------------------------------------------------------------+

var AmxSelector = new Class(
{
	start : function(rules)
	{
		window.addEvent('domready',function(){this.assign(rules);}.bind(this));
	},

	assign : function(rules)
	{
		for (var key in rules) {
			var rule = rules[key];
			// loop through all the selectors which are separated by a ,
			key.clean().split(',').each(function(selector) {
				var pair = selector.split(':');
				$$(pair[0]).each(function(elem) {
					// let see if there is no events binded to the selector. in
					// this case we just fire the rule
					if (pair.length == 1) {
						return rule(elem);
					}
					// at this point we have an event attached to the selector
					// so we simply add the event
					elem.addEvent(pair[1], rule.pass(elem));
				});
			});
		}
	}
});
/* file:progress/AmxProgressBar.class.js */

// +---------------------------------------------------------------------------+
// | This file is part of the Agavi and Mootools Extension package.            |
// | Copyright (c) 2006, 2007 Jean-Philippe Dery                               |
// |                                                                           |
// | For the full copyright and license information, please view the LICENSE   |
// | file that was distributed with this source code.                          |
// +---------------------------------------------------------------------------+

var AmxProgressBar = AmxElement.extend(
{
	bar : null,
	value : 0,

    options : {},

	initialize : function(elem, bar, options)
	{
		this.parent(elem);
		this.bar = $(bar);
		this.options = Object.extend({
			onComplete : null,
		}, options || {});
	},

	setValue : function(value)
	{
		this.value = value;
		this.bar.setStyle('width', this.value + '%');
		// call the associated events when the progress bar gets started and
		// when the whole process is completed
		if (this.value == 100) {
			if (this.options.onComplete) {
				this.options.onComplete();
			}
		}
	},

	getValue : function()
	{
		return this.value;
	},

	isCompleted : function()
	{
		return this.value == 100;
	}
});
/* file:nav/AmxTab.class.js */

// +---------------------------------------------------------------------------+
// | This file is part of the Agavi and Mootools Extension package.            |
// | Copyright (c) 2006, 2007 Jean-Philippe Dery                               |
// |                                                                           |
// | For the full copyright and license information, please view the LICENSE   |
// | file that was distributed with this source code.                          |
// +---------------------------------------------------------------------------+

var AmxTab = AmxElement.extend(
 {
    group : null,

     options : true,

	initialize : function(tab, group, options)
	{
		this.parent(tab);
		this.group = group;
		this.options = Object.extend({
			addLinks : true,
		}, options || {});
		return this;
	},

	activate : function()
	{
		this.addEvent('mouseenter', function(){this.onMouseEnter();}.bind(this));
		this.addEvent('mouseleave', function(){this.onMouseLeave();}.bind(this));
		this.addEvent('mousedown', function(){this.onSelect();}.bind(this));
		return this;
	},

	select : function()
	{
		this.onSelect();
	},

	deselect : function()
	{
		this.onDeselect();
	},

	onMouseEnter : function()
	{
		if (!this.isCurrent()) {
			this.addClass('highlight');
		}
	},

	onMouseLeave : function()
	{
		if (!this.isCurrent()) {
			this.removeClass('highlight');
		}
	},

	onSelect : function()
	{
		if (!this.isCurrent()) {
			this.addClass('current');
			// let's see if this tab is within a group to unselect the
			// previously selected tab
			if (this.group) {
				var curr = this.group.getCurrent();
				if (curr != null) curr.deselect();
				this.group.setCurrent(this);
			}
			// redirect to the page of the link when the tab is selected
			if (this.options.addLinks) {
				var href = $E('a', this.element).getProperty('href');
				if (href != '') {
					document.location.href = href;
				}
			}
		}
	},

	onDeselect : function()
	{
		if (this.isCurrent()) {
			this.group.setCurrent(null);
			this.removeClass('current');
			this.removeClass('highlight');
		}
	},

	isCurrent : function()
	{
		return this.hasClass('current');
	}
 });
/* file:nav/AmxTabGroup.class.js */

// +---------------------------------------------------------------------------+
// | This file is part of the Agavi and Mootools Extension package.            |
// | Copyright (c) 2006, 2007 Jean-Philippe Dery                               |
// |                                                                           |
// | For the full copyright and license information, please view the LICENSE   |
// | file that was distributed with this source code.                          |
// +---------------------------------------------------------------------------+

var AmxTabGroup = new Class(
 {
 	tabs : [],

 	current : null,

 	activate : function()
 	{
 		this.tabs.each(function(tab){
 			tab.activate();
 		});
 	},

 	addTab : function(tab, options)
 	{
 		var tab = new AmxTab(tab, this, options);
 		if (tab.isCurrent()) {
 			this.setCurrent(tab);
 		}
 		this.tabs.push(tab);
 		return tab;
 	},

 	setCurrent : function(current)
 	{
		this.current = current;
 	},

  	getCurrent : function()
 	{
 		return this.current;
 	}
 });
/* file:form/AmxFormNote.class.js */

// +---------------------------------------------------------------------------+
// | This file is part of the Agavi and Mootools Extension package.            |
// | Copyright (c) 2006, 2007 Jean-Philippe Dery                               |
// |                                                                           |
// | For the full copyright and license information, please view the LICENSE   |
// | file that was distributed with this source code.                          |
// +---------------------------------------------------------------------------+

var AmxFormNote = AmxElement.extend(
{
    note : null,

    visible : true,

	initialize : function(element, note)
	{
		this.parent(element);
		this.note = $(note);
		this.element.addEvent('click', function(){this.toggle();}.bind(this));
		this.hide();
	},

	toggle : function()
	{
		if (this.visible) {
			this.hide();
		} else {
			this.show();
		}
	},

	show : function()
	{
		this.note.setOpacity(0);
		this.note.show('block');
		new Fx.Style(this.note, 'opacity').start(0, 1);
		this.visible = true;
	},

	hide : function()
	{
		this.note.hide();
		this.visible = false;
	}
 });
/* file:form/AmxFormField.class.js */

// +---------------------------------------------------------------------------+
// | This file is part of the Agavi and Mootools Extension package.            |
// | Copyright (c) 2006, 2007 Jean-Philippe Dery                               |
// |                                                                           |
// | For the full copyright and license information, please view the LICENSE   |
// | file that was distributed with this source code.                          |
// +---------------------------------------------------------------------------+

var AmxFormField = AmxElement.extend(
{
    options : {},

	initialize : function(field, options)
	{
		this.parent(field);
		this.options = Object.extend({
			clear : true,
		}, options || {});
		if (this.options.clear) {
			var def = this.element.getValue();
			this.addEvent('focus', function() {
				if (this.element.getValue() == def) {
					this.element.value = '';
				}
			}.bind(this));
		}
		return this;
	},
});
/* file:panel/AmxPanel.class.js */

// +---------------------------------------------------------------------------+
// | This file is part of the Agavi and Mootools Extension package.            |
// | Copyright (c) 2006, 2007 Jean-Philippe Dery                               |
// |                                                                           |
// | For the full copyright and license information, please view the LICENSE   |
// | file that was distributed with this source code.                          |
// +---------------------------------------------------------------------------+

var AmxPanel = AmxTabGroup.extend(
{
	panel : null,

	cache : [],

	layer : null,

    options : {},

	initialize : function(panel, options)
	{
		this.panel = $(panel);
		this.layer = new AmxLoadingLayer(this.panel).activate();
		this.options = Object.extend({
			cache : true,
			anim : true
		}, options || {});
		return this;
	},

 	addTab : function(tab, finder)
	{
		tab = this.parent(tab, {addLinks : false});
		// create the main event which will load the panel content when the tab
		// is clicked. a loading layer will be placed on top while loading
		var that = this;
		var link = $E('a', tab.element);
		tab.addEvent('mousedown', function() {
			// load the panel content from the cache first
			if (this.options.cache) {
				if (this.cache[tab.getId()]) {
					this.panel.empty();
					this.panel.adopt(that.cache[tab.getId()]);
					return;
				}
			}
			console.log(this.layer.element.setProperty);
			this.showLayer();
			new Ajax(link.getProperty('href'), {onComplete : function(html) {
				this.hideLayer();
				finder.setHaystack(html);
				var result = finder.find();
				if (this.options.anim) {
					result.setOpacity(0);
					new Fx.Style(result, 'opacity', {duration : 500}).start(0, 1);
				}
				this.panel.empty();
				this.panel.adopt(result);
				if (this.options.cache) {
					this.cache[tab.getId()] = result;
				}
			}.bind(this)}).request();
		}.bind(this));
		// remove the link element so the browser will not change the location
		link.getParent().setText(link.getText());
	},

	showLayer : function()
	{
		this.layer.show();
	},

	hideLayer : function()
	{
		this.layer.hide();
	}
});
/* file:layer/AmxLayer.class.js */

// +---------------------------------------------------------------------------+
// | This file is part of the Agavi and Mootools Extension package.            |
// | Copyright (c) 2006, 2007 Jean-Philippe Dery                               |
// |                                                                           |
// | For the full copyright and license information, please view the LICENSE   |
// | file that was distributed with this source code.                          |
// +---------------------------------------------------------------------------+

var AmxLayer = AmxElement.extend(
{
    over : null,

    options : {},

    elements : [],

	initialize : function(over, options)
	{
		this.parent('div', true);
		this.over = $(over);
		this.options = Object.extend({
			offsetTop : 0,
			offsetLeft : 0,
			opacity : 0.7,
			className : 'layer'
		}, options || {});
		return this;
	},

	activate : function()
	{
		this.element.hide();
		this.element.setOpacity(this.options.opacity);
		this.element.addClass(this.options.className);
		this.element.setStyle('position', 'absolute');
		this.element.injectAfter(this.over);
		this.elements.each(function(elem) {
			elem.activate(this.element);
		}.bind(this));
		this.adjust();
		// make sure the layer stays adjusted
		window.addEvent('resize', function(){this.adjust()}.bind(this));
		return this;
	},

	adjust : function()
	{
		var coor = this.over.getCoordinates();
		var dims = this.over.getSize();
		this.element.setStyles({
			'top' : coor.top + this.options.offsetTop + 'px',
			'left' : coor.left + this.options.offsetLeft + 'px',
			'width' : dims.size.x + 'px',
			'height' : dims.size.y + 'px'
		});
		this.elements.each(function(elem) {
			elem.adjust(this.element, this.over);
		}.bind(this));
	},

	show : function()
	{
		this.element.show();
		this.elements.each(function(elem) {
			elem.show();
		});
	},

	hide : function()
	{
		this.element.hide();
		this.elements.each(function(elem) {
			elem.hide();
		});
	},

	remove : function()
	{
		this.element.remove();
		this.element.each(function(elem) {
			elem.remove();
		});
	},

	addElement : function(id, elem)
	{
		elem = $(elem);
		// create the method to activate the newly added element
		elem.activate = function(layer) {
			this.hide();
			this.setStyles({
				'position' : 'absolute',
				'left' : '0px',
				'top' : '0px'
			});
			this.injectAfter(layer);
		};
		// create the method to adjust the newly added element
		elem.adjust = function(layer, over) {
			var dims = over.getSize().size;
			var size = this.getSize().size;
			// make sure there is correct sizes
			size.x = size.x ? size.x : parseInt(this.getStyle('width'));
			size.y = size.y ? size.y : parseInt(this.getStyle('height'));
			// center the element inside the layer
			this.setStyles({
				'position' : 'absolute',
				'left' : over.getCoordinates().left + (dims.x / 2) - (size.x / 2) + 'px',
				'top' :  over.getCoordinates().top + (dims.y / 2) - (size.y / 2) + 'px'
			});
		};
		this.elements.push(elem);
	}
 });
/* file:layer/AmxLoadingLayer.class.js */

// +---------------------------------------------------------------------------+
// | This file is part of the Agavi and Mootools Extension package.            |
// | Copyright (c) 2006, 2007 Jean-Philippe Dery                               |
// |                                                                           |
// | For the full copyright and license information, please view the LICENSE   |
// | file that was distributed with this source code.                          |
// +---------------------------------------------------------------------------+

var AmxLoadingLayer = AmxLayer.extend(
{
	initialize : function(over, options)
	{
		var elem = new Element('img');
		elem.setProperty('src', 'modpub/images/ajax.gif');
		elem.setStyles({
			'width' : '32px',
			'height' : '32px'
		});
		this.parent(over, options);
		this.addElement('ajax-loading', elem);
		return this;
	}
 });