/* 
Filename: moo.rd - A lightweight Mootools extension 

Author: Riccardo Degni, <http://www.riccardodegni.it/> 

License: GNU GPL License 

Copyright: copyright 2007 Riccardo Degni 
*/ 

var Moo = {};

Moo.Rd = {
	version: '1.2',
	author: 'Riccardo Degni'
};



var Custom = {};

var Table = new Class({	
	initialize: function(element) {
		this.element = $(element);
	}
});

var Make = {};


Fx.Base.implement({
	initStyles: function() {
		this.init = {};
		$A(arguments).each(function(a) {
			(this.element.getStyle(a).test('px')) ? this.init[a] = this.element.getStyle(a).toInt() : this.init[a] = this.element.getStyle(a);
		}, this);	
	},
	
	removeAuto: function() {
		if(!this.init) this.init = {};
		$A(arguments).each(function(a) {
			(this.element.getStyle(a).test('auto')) ? this.element.setStyle(a, '0px') : this.element.getStyle(a);
			(this.init[a] != 'auto') ? this.init[a] : this.init[a] = 0;
		}, this);
	}
});




String.extend({
	
	stripTags: function () {
		return this.replace(/<([^>]+)>/g,'');
	},

	addslashes: function() {
		return this.replace(/['"]/g, function(match){
			return ('\\' + match.charAt(0) + match.charAt(1));
		});
	},
	
	nl2br: function() {
		return this.replace(/\\n/g, '<br />');
	},
	
	stripslashes: function() {
		return this.replace(/\\['"]{1,}/g, function(match){
			return (match.charAt(1));
		});
	},
	
	htmlEntities: function () {
		return this.replace(/&/g,'&amp;').replace(/</g,'&lt;').replace(/>/g,'&gt;');
	},
	
	trim: function() {
		return this.replace(/^\s+|\s+$/g,'');
	},
	
	ltrim: function() {
		return this.replace(/^\s+/g,'');
	},
	
	rtrim: function() {
		return this.replace(/\s+$/g,'');
	}		
});




String.extend({
	
	upper: function() {
 		return this.toUpperCase();
 	},
	
 	lower: function() {
 		return this.toLowerCase();
 	},
	
	firstChar: function (n) {	
		return (this.charAt(0)==n) ? true : false;
	},
	
	hasChar: function (n) {
		for(var i=0; i<=this.length; i++)
			if(this.charAt(i)==n) return true;
		return false;
	},
	
	lastChar: function (n) {	
		return (this.charAt(this.length-1)==n) ? true : false;
	},
	
	stripPhp: function() {
		return this.replace(/<\?php|\?>/gi, '');
	},
	
	stripScripts: function() {
		return this.replace(/<script[^>]{0,}>|<\/script>/gi, '');
	},
	
	globalReplace: function(val, repl, regexAttr) {
		return this.replace(new RegExp(val, regexAttr || 'gi'), repl);
	},
	
	contains: function (phrase) {
		return (this.indexOf(phrase) != -1) ? true : false;
	},
	
	posOf: function (n) {
		var positions = new Array();
			for (var i=0; i<this.length; i++)
				if(this.charAt(i)==n)
					positions.push(i);
			if(positions.length==0)
				return false;
			else
				return positions;
	},
	
	getFirst: function() {
		return this.charAt(0);
	},
	
	getLast: function() {
		return this.charAt(this.length-1);
	},
	
	camelize: function() {
		var s = '';
		for(var i=0; i<this.length; i++) {
			if(i%2 == 0) s+= this.charAt(i).upper();
			else s+= this.charAt(i).lower();
		}
		
		return s;
	},
	
	upperFirst: function() {
		return this.replace(this.charAt(0), this.charAt(0).upper());	
	}
			  
});




Array.extend({
	
	print_r: function(results) {
		var r = 'Array ( ' + '<br />';
		this.each(function(el, index) {
			r += '[' + index + ']' + ' => ' + el + '<br />';
		});
		r += ')';
		$(results).setHTML(r);
	},
	
	numSort: function() {
		return this.sort(function(a, b) {return a-b;});
	},
	
	rNumSort: function() {
		return this.sort(function(a, b) {return b-a;});
	},
	
	getFirst: function() {
		return this[0];
	},
			
	getLast: function() {
		return this[this.length-1];
	},
	
	asItem: function(item) {
		var a = new Array();
		for(var z=0; z<this.length; z++)
			if(this[z] == item) return true;
		return false;
	},
	
	asItems: function() {
		var a = new Array();
				
		for(var i=0; i<arguments.length; i++)
			if(this.asItem(arguments[i]))
				a.push(arguments[i]);
				
		return (a.length >= arguments.length);
	},
	
	stripItem: function() {
		var a = new Array();
		for(var z=0; z<this.length; z++)
			for(var i=0; i<arguments.length; i++)
				if(this[z] != arguments[i])
					a.push(this[z]);
		return a;	
	},
	
	stripItems: function() {
		var a = new Array();
		for(var w=0; w<this.length; w++) a.push(this[w]);
				
		for(var z=0; z<this.length; z++) {
			for(var i=0; i<arguments.length; i++)
				if(this[z] == arguments[i])
					a = a.stripItem(arguments[i]);
		}	
		return a;	
	},
	
	posOf: function(item) {
		var a = new Array();
				
		for(var z=0; z<this.length; z++)
			if(this[z] == item)
				a.push(z);
		return a;
	}
});

function print_r(v, results) {
	if($type(v) == 'array') v.print_r(results);
}




Element.extend({
	
	getId: function() {
		return this.getProperty('id');
	},
	
	getClassName: function() {
		return this.className;
	},
	
	getWidth: function() {
		return (!int) ? this.getStyle('width') : this.getStyle('width').toInt();
	},
	
	getHeight: function() {
		return this.getStyle('height');
	},
	
	getOpacity: function() {
		return (!int) ? this.getStyle('opacity') : this.getStyle('opacity').toInt();
	},
	
	getColor: function() {
		return this.getStyle('color');
	},
	
	getBg: function() {
		return this.getStyle('background-color');
	},
	
	getBorder: function(where) {
		return (where) ? this.getStyle('border-' + where) : this.getStyle('border');
	},
	
	getMargin: function(where) {
		return (where) ? this.getStyle('margin-' + where) : this.getStyle('margin');
	},
	
	getPadding: function(where) {
		return (where) ? this.getStyle('padding-' + where) : this.getStyle('padding');
	}
		
});



	
Table.implement({

	zebra: function(color1, color2, firstLine) {
		if(this.element.getTag() != 'table')  return false;
		
		this.cells = this.element.getElements('tr').getElements('td');
		this.cells.each(function(cell, index) {
			if(index%2 == 0)
				(firstLine && index == 0) ? cell.setStyles(firstLine) : cell.setStyle('background-color', color1);
			else
				cell.setStyle('background-color', color2);
		});
			
	},
	
	overClickRows: function(color, clickcolor) {
		this.rows = this.element.getElements('tr');
		
		this.rows.each(function(row, index) {
			row.addEvent('mouseover', function() {
				if(!this.init) this.init = row.getElement('td').getStyle('background-color');
				if(this.getElement('td').getStyle('background-color').toUpperCase() != clickcolor)
					row.getElements('td').setStyle('background-color', color);
			});
			
			row.addEvent('mouseout', function() {
				if(this.getElement('td').getStyle('background-color').toUpperCase() != clickcolor)
					row.getElements('td').setStyle('background-color', this.init);
			});
			
			row.addEvent('click', function() {
				if(this.getElement('td').getStyle('background-color').toUpperCase() != clickcolor)
					this.getElements('td').setStyle('background-color', clickcolor);
				else
					this.getElements('td').setStyle('background-color', this.init);
			});
		});
	},
	
	overRows: function(color) {
		this.rows = this.element.getElements('tr');
		
		this.rows.each(function(row, index) {
			row.addEvent('mouseover', function() {
				this.init = row.getElement('td').getStyle('background-color');
				row.getElements('td').setStyle('background-color', color);
			});
			
			row.addEvent('mouseout', function() {
				row.getElements('td').setStyle('background-color', this.init);
			});
		});
	},
	
	clickRows: function(color) {
		this.rows = this.element.getElements('tr');
		this.rows.each(function(row, index) {
		
			row.addEvent('click', function() {
				if(!this.initBc) this.initBc = row.getElement('td').getStyle('background-color').toUpperCase();
				if(row.getElement('td').getStyle('background-color').toUpperCase() != this.initBc)
					row.getElements('td').setStyle('background-color', this.initBc);
				else
					row.getElements('td').setStyle('background-color', color);	
			});
		});
	}
});




Table.implement({
	
	overClickCells: function(color, clickcolor) {
		this.rows = this.element.getElements('tr');
		this.rows.each(function(row, index) {
			row.getElements('td').each(function(td,index) {
				td.addEvent('mouseover', function() {
					if(!this.init) this.init = td.getStyle('background-color');
					if(this.getStyle('background-color').toUpperCase() != clickcolor)
						td.setStyle('background-color', color);
				});
				
				td.addEvent('mouseout', function() {
					if(this.getStyle('background-color').toUpperCase() != clickcolor)
						td.setStyle('background-color', this.init);
				});
				
				td.addEvent('click', function() {
					if(this.getStyle('background-color').toUpperCase() != clickcolor)
						this.setStyle('background-color', clickcolor);
					else
						this.setStyle('background-color', this.init);
				});
			});
		});
	},
	
	overCells: function(color) {
		this.rows = this.element.getElements('tr');
		this.rows.each(function(row, index) {
			row.getElements('td').each(function(td,index) {
				td.addEvent('mouseover', function() {
					this.init = td.getStyle('background-color');
					td.setStyle('background-color', color);
				});
				
				td.addEvent('mouseout', function() {
					td.setStyle('background-color', this.init);
				});
			});
		});
	},
	
	clickCells: function(color) {
		this.rows = this.element.getElements('tr');
		this.rows.each(function(row, index) {
			row.getElements('td').each(function(td,index) {
				td.addEvent('click', function() {
					if(!this.initC) this.initC = row.getElement('td').getStyle('background-color').toUpperCase();
					if(td.getStyle('background-color').toUpperCase() != this.initC)
						td.setStyle('background-color', this.initC);
					else
						td.setStyle('background-color', color);
				});
			});
		});
	}
});




Table.implement({	
				
	overClickCols: function(color, clickcolor, firstLine) {
		this.rows = this.element.getElements('tr');
		this.rows.each(function(row, indexRow) {
			
		  row.getElements('td').each(function(td,indexTd) {
			 
			var i = indexTd;
			var tdcolor = td.getStyle('background-color').toUpperCase();
			
			td.addEvent('mouseover', function() {
				this.getParent().getParent().getElements('tr').each(function(r, indexR) {
					r.getElements('td').each(function(c, indexC) {
						if(indexC == i)
							if((firstLine) && (indexR==0))
								c.setStyle('background-color', tdcolor);
							else if(c.getStyle('background-color').toUpperCase() != clickcolor)
								c.setStyle('background-color', color);
							else
								c.setStyle('background-color', clickcolor);
					});
				});
			});
			
			td.addEvent('mouseout', function() {
				this.getParent().getParent().getElements('tr').each(function(r, indexR) {
					r.getElements('td').each(function(c, indexC) {
						if(indexC == i)
							if(c.getStyle('background-color').toUpperCase() == color)
								c.setStyle('background-color', tdcolor);
					});
				});
			});
				
			td.addEvent('click', function() {
				this.getParent().getParent().getElements('tr').each(function(r, indexR) {
					r.getElements('td').each(function(c, indexC) {
						if(indexC == i)
							if((firstLine) && (indexR!=0))
							  if(c.getStyle('background-color').toUpperCase() != clickcolor)
								  c.setStyle('background-color', clickcolor);
							  else
								  c.setStyle('background-color', tdcolor);
							else if((!firstLine))
								 if(c.getStyle('background-color').toUpperCase() != clickcolor)
								  c.setStyle('background-color', clickcolor);
							  else
								  c.setStyle('background-color', tdcolor);
					});
				});
			});
				
			  
		  });		
		});
	},
	
	overCols: function(color, firstLine) {
		this.rows = this.element.getElements('tr');
		this.rows.each(function(row, indexRow) {
			
			  row.getElements('td').each(function(td,indexTd) {
				 
				var i = indexTd;
				var tdcolor = td.getStyle('background-color').toUpperCase();
				
				td.addEvent('mouseover', function() {
					this.getParent().getParent().getElements('tr').each(function(r, indexR) {
						r.getElements('td').each(function(c, indexC) {
							if(indexC == i)
								if((firstLine) && (indexR==0))
									c.setStyle('background-color', tdcolor);
								else if(c.getStyle('background-color').toUpperCase() != color)
									c.setStyle('background-color', color);
								else
									c.setStyle('background-color', tdcolor);
						});
					});
				});
				
				td.addEvent('mouseout', function() {
					this.getParent().getParent().getElements('tr').each(function(r, indexR) {
						r.getElements('td').each(function(c, indexC) {
							if(indexC == i)
								if(c.getStyle('background-color').toUpperCase() == color)
									c.setStyle('background-color', tdcolor);
						});
					});
				});
			  
			});		
		});
	},
	
	clickCols: function(color, firstLine) {
		this.rows = this.element.getElements('tr');
		this.rows.each(function(row, indexRow) {
			
		  row.getElements('td').each(function(td,indexTd) {
			 
			var i = indexTd;
			var tdcolor = td.getStyle('background-color').toUpperCase();
			
			td.addEvent('click', function() {
				this.getParent().getParent().getElements('tr').each(function(r, indexR) {
					r.getElements('td').each(function(c, indexC) {
						if(indexC == i)
							if((firstLine) && (indexR==0))
								c.setStyle('background-color', tdcolor);
							else if(c.getStyle('background-color').toUpperCase() != color)
								c.setStyle('background-color', color);
							else
								c.setStyle('background-color', tdcolor);
					});
				});
			});
		  
		  });		
		});
	}
});




Make.Table = new Class({
			
	initialize: function(id, rows) {
		this.idKey = id;
		this.rows = rows;
	},
	
	make: function() {
		if(!$(this.idKey)) {
		
		this.table = new Element('table', {
			'id': this.idKey
		});
		
		var table = this.table;
		var tbody = new Element('tbody').injectInside(table);
		var first = this.first;
		var l = this.rows.length-1;
		
		this.rows.each(function(row, index) {

			var tr = new Element('tr').injectInside(tbody);

			row.each(function(cell) {
				($type(cell) == 'object') ? tr.setStyles(cell) : new Element('td').appendText(cell).injectInside(tr);	
			});
		});
		
		return this.table;
		}
		else return;
	}
		
});

Array.extend({
	makeTable: function(id) {
		return new Make.Table(id, this).make();
	}
});

function make_table(id, rows) {
	return new Make.Table(id, rows).make();
}




Make.List = new Class({
					  
	options: {
		type: 'ul',
		className: ''
	},
	
	initialize: function(id, items, options) {
		this.idKey = id;
		this.items = items;
		this.setOptions(options);
	},
	
	make: function() {
		if(!$(this.idKey)) {
			
			this.ul = new Element(this.options.type, {
				'id': this.idKey,
				'class': this.options.className
			});
			

			this.items.each(function(li) {
				if($type(li) == 'array') {
					li.each(function(item, i) {
						var mul = new Element('ul');
						if($type(item) == 'object') {
							var a = new Element('a', {
								'href': item.href
							}).appendText(item.text);
							var lis = new Element('li');
							a.injectInside(lis);
							lis.injectInside(mul);
							mul.injectInside(this.ul);
						}			 
						else {
							var mli = new Element('li').appendText(item).injectInside(mul);
							mul.injectInside(this.ul);
						}
					}, this);
				}
				else if($type(li) == 'object') {
					var a = new Element('a', {
						'href': li.href
					}).appendText(li.text);
					var li = new Element('li');
					a.injectInside(li);
					li.injectInside(this.ul);
				}
				else {
					var li = new Element('li').appendText(li);
					li.injectInside(this.ul);
				}
			}, this);
			
			return this.ul;
		
		}
		else return;
	}

});

Make.List.implement(new Options);

Array.extend({
		makeList: function(id, options) {
			return new Make.List(id, this, options).make();
		}
});

function make_list(id, items, options) {
		return new Make.List(id, items, options).make();
}




Make.Select = new Class({
			
	initialize: function(id, items, properties) {
		this.idKey = id;
		this.items = items;
		this.props = properties || {};
	},
	
	make: function() {
		if(!$(this.idKey)) {
			this.select = new Element('select', $extend({'id': this.idKey}, this.props));
			
			this.items.each(function(item) {
				if($type(item) == 'array') {
					item.each(function(i, index) {
						if(index == 0) {
							this.group = new Element('optgroup', { 'label': i });
							this.select.adopt(this.group);
						}
						else {
							var opt = new Element('option', {
								'label': i,
								'value': i
							}).appendText(i);
							opt.injectInside(this.group);	
						}
					}, this);	
				}					 
				else {
					var option = new Element('option', {
						'label': item,
						'value': item
					}).appendText(item);
					option.injectInside(this.select);
				}
			}, this);
			
			return this.select;
		
		}
		else return;
	}

});

Array.extend({
	makeSelect: function(id, properties) {
		return new Make.Select(id, this, properties).make();
	}
});

function make_select(id, items, properties) {
	return new Make.Select(id, items, properties).make();
}




Custom.Alert = new Class({
		
	options: {
		height: '100px',
		width: '300px',
		buttonText: 'OK',
		opacify: true,
		alertbox: null,
		alerthead: null,
		alertbody: null
	},

	initialize: function(title, text, options) {
		this.title = title;
		this.text = text;
		this.setOptions(options);
		this.alertbox = new Element('div', {
			'id': 'customAlert',
			'styles': {
				'position': 'fixed',
				'top': '50%',
				'left': '50%',
				'z-index': 1000,
				'height': this.options.height,
				'width': this.options.width
			}
		});
		this.overlay = new Element('div', {
			'id': 'customAlertOverlay',
			'styles': {
				'position': 'absolute',
				'top': '0px',
				'left': '0px',
				'width': '100%',
				'height': window.getScrollHeight(),
				'background-image':'url(g.gif)',
				'z-index': 900
			}
		});
		this.mechanize();
		this.fx = new Fx.Style(this.alertbox, 'opacity', {duration:1000});
		if(this.options.initialize) this.options.initialize.call(this);
	},
	
	create: function() {
		this.customize();
	},
	
	mechanize: function() {
		if(this.options['alertbox']) this.alertbox.addClass(this.options['alertbox']);

		this.alertbox.setStyles({
			'margin-left': - this.alertbox.getStyle('width').toInt()/2,
			'margin-top': - this.alertbox.getStyle('height').toInt()/2
		});
		
		this.head = new Element('div').injectInside(this.alertbox);
		if(this.options.alerthead) this.head.addClass(this.options['alerthead']);
		this.head.appendText(this.title);
		
		this.content = new Element('div').injectInside(this.alertbox);
		if(this.options.alerthead) this.content.addClass(this.options['alertbody']);
		this.content.appendText(this.text);
	
		this.closebox = new Element('div').injectInside(this.alertbox);
		this.closebox.setProperty('align', 'center');
	
		this.button = new Element('a').injectInside(this.closebox);
		this.button.setProperty('href', '#');
		this.button.appendText(this.options.buttonText);
		this.button.addEvent('click', function(event) {
			var event = new Event(event).preventDefault();
		});
		if(this.options.opacify)  this.button.addEvent('click', this.opacify.bind(this));
		else this.button.addEvent('click', this.remove.bind(this));
	},
	
	customize: function() {
		if($('customAlert'))  return;
		
		this.alertbox.injectInside(this.overlay);
		this.overlay.injectInside($E('body'));
		
		if(this.options.opacify) this.alertbox.setStyle('opacity', 0);
		this.fx.start(1);
	},
	
	opacify: function() {
		this.fx.start(0).chain(function() {
			this.alertbox.remove();
			this.overlay.remove();
		}.bind(this));
	},
	
	remove: function() {
		this.alertbox.remove();
		this.overlay.remove();
	},
	
	setText: function(text) {
		this.content.setHTML(text);
		return this;
	},
	
	setTitle: function(title) {
		this.title = title;	
		return this;
	}
});

Custom.Alert.implement(new Options);




Custom.Confirm = new Class({
		
	options: {
		height: '100px',
		width: '300px',
		confirmText: 'OK',
		cancelText: 'Cancel',
		opacify: true,
		confirmbox: null,
		confirmhead: null,
		confirmbody: null,
		onConfirm: function() {
			return true;	
		},
		onCancel: function() {
			return false;	
		}
	},
	
	initialize: function(title, text, options) {
		this.title = title;
		this.text = text;
		this.setOptions(options);
		this.confirmbox = new Element('div', {
			'id': 'customConfirm',
			'styles': {
			'position': 'fixed',
			'top': '50%',
			'left': '50%',
			'z-index': 1000,
			'height': this.options.height,
			'width': this.options.width	
			}
		});
		this.overlay = new Element('div', {
			'id': 'customConfirmOverlay',
			'styles': {
				'position': 'absolute',
				'top': '0px',
				'left': '0px',
				'width': '100%',
				'height': window.getScrollHeight(), //window.getHeight() + window.getScrollTop(),
				'background-image':'url(g.gif)',
				'z-index': 900
			}
		});
		this.mechanize();
		this.fx = new Fx.Style(this.confirmbox, 'opacity', {duration:1000});
		if(this.options.initialize) this.options.initialize.call(this);
	},
	
	create: function() {
		this.customize();
	},
	
	mechanize: function() {
		if(this.options['confirmbox']) this.confirmbox.addClass(this.options['confirmbox']);
		this.confirmbox.setStyles({
			'margin-left': - this.confirmbox.getStyle('width').toInt()/2,
			'margin-top': - this.confirmbox.getStyle('height').toInt()/2
		});
		
		this.head = new Element('div').injectInside(this.confirmbox);
		if(this.options.confirmhead) this.head.addClass(this.options['confirmhead']);
		this.head.appendText(this.title);
		
		this.content = new Element('div').injectInside(this.confirmbox);
		if(this.options.confirmhead) this.content.addClass(this.options['confirmbody']);
		this.content.appendText(this.text);
	
		this.closebox = new Element('div').injectInside(this.confirmbox);
		this.closebox.setProperty('align', 'center');
	
		this.confirmButton = new Element('a').injectInside(this.closebox);
		this.confirmButton.setProperty('href', '#');
		this.confirmButton.appendText(this.options.confirmText);
		this.confirmButton.addEvent('click', function(event) {
			var event = new Event(event).preventDefault();
		});
		if(this.options.opacify)  this.confirmButton.addEvent('click', this.opacifyConfirm.bind(this));
		else this.confirmButton.addEvent('click', this.confirmRemove.bind(this));
		
		this.cancelButton = new Element('a').injectInside(this.closebox);
		this.cancelButton.setProperty('href', '#');
		this.cancelButton.appendText(this.options.cancelText);
		this.cancelButton.addEvent('click', function(event) {
			var event = new Event(event).preventDefault();
		});
		if(this.options.opacify)  this.cancelButton.addEvent('click', this.opacifyCancel.bind(this));
		else this.cancelButton.addEvent('click', this.cancelRemove.bind(this));
	},
	
	customize: function() {
		if($('customConfirm'))  return false;
		
		this.confirmbox.injectInside(this.overlay);
		this.overlay.injectInside($E('body'));
		
		if(this.options.opacify) this.confirmbox.setStyle('opacity', 0);
		this.fx.start(1);
		
	},
	
	opacifyConfirm: function() {
		this.fireEvent('onConfirm');
		this.fx.start(0).chain(function() {
			this.confirmbox.remove();
			this.overlay.remove();
		}.bind(this));
	},
	
	opacifyCancel: function() {
		this.fireEvent('onCancel');
		this.fx.start(0).chain(function() {
			this.confirmbox.remove();
			this.overlay.remove();
		}.bind(this));
	},
	
	confirmRemove: function() {
		this.fireEvent('onConfirm');
		this.confirmbox.remove();
		this.overlay.remove();
	},
	
	cancelRemove: function() {
		this.fireEvent('onCancel');
		this.confirmbox.remove();
		this.overlay.remove();
	},
	
	setText: function(text) {
		this.content.setHTML(text);
		return this;
	},
	
	setTitle: function(title) {
		this.title = title;
		return this;
	}
});

Custom.Confirm.implement(new Options, new Events);




var SmoothScrolling = new Class ({
	   
	initialize: function(options, links) {
		(!links) ? this.links = $$('a') : this.links = links;
		this.setOptions(options);
		this.fxscroll = new Fx.Scroll(window, this.options);
		if(this.options.initialize) this.options.intialize.call(this);
	},
    
    create: function() {
	 var targets = new Array();
	 var anchors = new Array();
	
	 $$('a').each(function(lnk, index) {
		if(lnk.name) targets.push(lnk);
	 });
				
	 this.links.each(function(lnk, index) {
	 	if(lnk.href.test(/#\w+/)) {
			anchors.push(lnk);
			for(var i=0; i<targets.length; i++) {
				if(lnk.href.split('#')[1] == targets[i].name)
					lnk.targetName = targets[i].name;
					lnk.addEvent('click', this.makeScroll.bind(this, lnk));
			}
		}
	  }, this);
	},
	
	makeScroll: function(lnk) {
		this.fxscroll.scrollTo(0,document.getElementsByName(lnk.targetName)[0].getTop());
	}
	
});

SmoothScrolling.implement(new Options);



Fx.Fold = Fx.Style.extend({	
	initialize: function(element, options, opt2) {
		this.parent(element, 'height', options);
		this.fx = new Fx.Style(element, 'width', opt2);
		this.element.setStyle('overflow', 'hidden');
	},
	
	start: function() {
		this.parent(0).chain(function() {
			this.fx.start(0); 
		});
	}		
});
	
Fx.Squish = Fx.Styles.extend({		
	initialize: function(element, options) {
		this.parent(element, options);
		this.element.setStyle('overflow', 'hidden');
	},
	
	start: function() {
		this.parent({
			'height': [0],
			'width': [0],
			'opacity': [0]
		});
	}		
});
		
Fx.Puff = Fx.Styles.extend({	
	initialize: function(element, options) {
		this.parent(element, options);
		this.initStyles('height', 'width', 'fontSize');
		this.element.setStyles({position: 'relative', overflow: 'hidden'});
	},
	
	start: function() {
		this.parent({
			'height': [this.init.height*1.3],
			'width': [this.init.width*1.3],
			'font-size': [this.init.fontSize*1.3],
			'opacity': [0]
		}).chain(function() { this.element.setStyle('display', 'none'); });
	}			
});
		
Fx.Shrink = Fx.Styles.extend({		
	initialize: function(element, options, opt2) {
		this.parent(element, options);
		this.fx = new Fx.Style(this.element, 'opacity', opt2);
		this.initStyles('fontSize');
		this.element.setStyles({position: 'relative', overflow: 'hidden'});
	},
	
	start: function() {
		this.parent({
			'height': [0],
			'width': [0],
			'font-size': [this.init.fontSize, 0]
		}).chain(function() {
			this.fx.start(1,0);
		});
	}		
});
		
Fx.Grow = Fx.Styles.extend({	
	initialize: function(element, options, values) {
		this.parent(element, options);
		this.values = values || {};
		this.element.setStyle('overflow', 'hidden');
	},
	
	start: function() {
		this.parent({
			'height': [0, this.values.height],
			'width': [0, this.values.width],
			'font-size': [0, this.values.fontsize]
		});
	}	
});

Element.extend({
	fold: function(options, opt2) {
		return new Fx.Fold(this, options, opt2);	
	},
	
	squish: function(options) {
		return new Fx.Squish(this, options);	
	},
	
	puff: function(options) {
		return new Fx.Puff(this, options);	
	},
	
	shrink: function(options, opt2) {
		return new Fx.Shrink(this, options, opt2);	
	},
	
	grow: function(options, values) {
		return new Fx.Grow(this, options, values);	
	}
});



Fx.FadeOut = Fx.Style.extend({
	initialize: function(element, options) {
		this.parent(element, 'opacity', options);
	},
	
	start: function() {
		this.parent(0);
	}
});

Fx.SwitchOffH = Fx.Style.extend({	
	initialize: function(element, options) {
		this.parent(element);
		this.fx = new Fx.Style(this.element, 'opacity', {duration:100});
		this.fx2 = new Fx.Style(this.element, 'height', options);
		this.element.setStyle('overflow', 'hidden');
	},
	
	start: function() {
		this.fx.start(0).chain(function() {
			this.fx.start(1);
		}.bind(this)).chain(function() {
			this.fx2.start(0);	
		}.bind(this));
	}
});

Fx.SwitchOffW = Fx.Style.extend({
	initialize: function(element, options) {
		this.parent(element);
		this.fx = new Fx.Style(this.element, 'opacity', {duration:100});
		this.fx2 = new Fx.Style(this.element, 'width', options);
		this.element.setStyle('overflow', 'hidden');
	},
	
	start: function() {
		this.fx.start(0).chain(function() {
			this.fx.start(1);
		}.bind(this)).chain(function() {
			this.fx2.start(0);	
		}.bind(this));
	}
});

Fx.ShakeH = Fx.Style.extend({	
	initialize: function(element, options) {
		this.parent(element);
		this.fx = new Fx.Style(this.element, 'top', options || {duration:100});
		this.element.setStyles({'position': 'relative', 'overflow': 'hidden'});
	},
	
	start: function() {
		this.fx.start(-10).chain(function() {
			this.fx.start(10);
		}.bind(this)).chain(function() {
			this.fx.start(-10);
		}.bind(this)).chain(function() {
			this.fx.start(10);
		}.bind(this)).chain(function() {
			this.fx.start(-10);
		}.bind(this)).chain(function() {
			this.fx.start(10);
		}.bind(this)).chain(function() {
			this.fx.start(-10);
		}.bind(this)).chain(function() {
			this.fx.start(10);
		}.bind(this)).chain(function() {
			this.fx.start(-10);
		}.bind(this)).chain(function() {
			this.fx.start(0);
		}.bind(this));
	}
});

Fx.ShakeW = Fx.Style.extend({
	initialize: function(element, options) {
		this.parent(element);
		this.fx = new Fx.Style(this.element, 'left', options || {duration:100});
		this.element.setStyles({'position': 'relative', 'overflow': 'hidden'});
	},
	
	start: function() {
		this.fx.start(-10).chain(function() {
			this.fx.start(10);
		}.bind(this)).chain(function() {
			this.fx.start(-10);
		}.bind(this)).chain(function() {
			this.fx.start(10);
		}.bind(this)).chain(function() {
			this.fx.start(-10);
		}.bind(this)).chain(function() {
			this.fx.start(10);
		}.bind(this)).chain(function() {
			this.fx.start(-10);
		}.bind(this)).chain(function() {
			this.fx.start(10);
		}.bind(this)).chain(function() {
			this.fx.start(-10);
		}.bind(this)).chain(function() {
			this.fx.start(0);
		}.bind(this));
	}
});


Element.extend({
	fadeOut: function(options) {
		return new Fx.FadeOut(this, options);	
	},
	
	switchOffH: function(options) {
		return new Fx.switchOffH(this, options);	
	},
	
	switchOffW: function(options) {
		return new Fx.FixswitchOffH(this,options);	
	},
	
	shakeH: function(options) {
		return new Fx.ShakeH(this, options);	
	},
	
	shakeW: function(options) {
		return new Fx.ShakeW(this, options);	
	}
});



Fx.Pulsate = Fx.Style.extend({	
	initialize: function(element) {
		this.parent(element, 'opacity');
		this.fx = new Fx.Style(this.element, this.property, {duration:100});
		this.element.setStyle('overflow', 'hidden');
	},
	
	start: function() {
		this.fx.start(0).chain(function() {
			this.fx.start(1);
		}.bind(this)).chain(function() {
			this.fx.start(0);
		}.bind(this)).chain(function() {
			this.fx.start(1);
		}.bind(this)).chain(function() {
			this.fx.start(0);
		}.bind(this)).chain(function() {
			this.fx.start(1);
		}.bind(this)).chain(function() {
			this.fx.start(0);
		}.bind(this)).chain(function() {
			this.fx.start(1);
		}.bind(this)).chain(function() {
			this.fx.start(0);
		}.bind(this)).chain(function() {
			this.fx.start(1);
		}.bind(this));
	}
});
		
Fx.Gradient = Fx.Style.extend({
	initialize: function(element, color, options, opt2) {
		this.parent(element,'background-color',  options);
		this.color = color;
		this.fx = new Fx.Style(this.element, 'background-color', opt2);
		this.initStyles('backgroundColor');
	},
	
	start: function() {
		this.parent(this.color).chain(function() {
			this.fx.start(this.init.backgroundColor);
		}.bind(this));
	}
});

Fx.FixGradient = Fx.Style.extend({
	initialize: function(element, color, options) {
		this.parent(element, 'background-color', options);
		this.color= color;
	},
	
	start: function() {
		this.parent(this.color);
	}
});

Fx.FadeIn = Fx.Style.extend({
	initialize: function(element, options) {
		this.parent(element, 'opacity', options);
	},
	
	start: function() {
		this.parent(1);
	}
});

Fx.Rumble = Fx.Styles.extend({	
	initialize: function(element, options) {
		this.parent(element);
		this.element.setStyles({'position': 'relative', 'cursor': 'move'});
		this.initStyles('top', 'left');
		this.obj = {'top': [this.element.getStyle('top').toInt(), this.init.top], 
					'left': [this.element.getStyle('left').toInt(), this.init.left]
				   };
		this.fx = new Fx.Styles(this.element, options || {duration: 800, transition: Fx.Transitions.Elastic.easeOut});
	},

	start: function() {
		var top = this.init.top; var left = this.init.left;
		var coord = {'top': (top != 'auto' ? top : 0), 'left': (left != 'auto' ? left : 0)};
		var fx = function() { this.fx.start(coord); };
		new Drag.Move(this.element).addEvent('onComplete', fx.bind(this));		   
	}
			
});


Element.extend({
	pulsate: function() {
		return new Fx.Pulsate(this);	
	},
	
	gradient: function(color, options, opt2) {
		return new Fx.Gradient(this, color, options, opt2);	
	},
	
	fixgradient: function(color, options) {
		return new Fx.FixGradient(this, color, options);	
	},
	
	shrink: function(options) {
		return new Fx.FadeIn(this, options);	
	},
	
	rumble: function(options) {
		return new Fx.Rumble(this, options);	
	}
});



Fx.BubbleH = Fx.Styles.extend({
	initialize: function(element, options) {
		this.parent(element, options || {duration:3000});
		this.element.setStyle('overflow', 'hidden');
	},
	
	start: function() {
		this.parent({
			'height': [0],
			'font-size': [0],
			'opacity': [0]
		});
	}
});


Fx.BubbleW = Fx.Styles.extend({		
	initialize: function(element, options) {
		this.parent(element, options || {duration:3000});
		this.element.setStyle('overflow', 'hidden');
	},
	
	start: function() {
		this.parent({
			'width': [0],
			'font-size': [0],
			'opacity': [0]
		});
	}
});


Fx.Morph = Fx.Styles.extend({
	initialize: function(element) {
		this.parent(element);
	},
 
	start: function(className){

		var to = {};

		$each(document.styleSheets, function(style){
			var rules = style.rules || style.cssRules;
				$each(rules, function(rule) {
					if (!rule.selectorText.test('\.' + className + '$')) return;
					CSSProperties.each(function(style) {
						if (!rule.style || !rule.style[style]) return;
						var ruleStyle = rule.style[style];
						to[style] = (style.test(/color/i) && ruleStyle.test(/^rgb/)) ? ruleStyle.rgbToHex() : ruleStyle;
					});
				});
		});
		return this.parent(to);
	}

});
 
CSSProperties = ["backgroundColor", "backgroundPosition", "backgroundImage", "color", "width", "height", "left", "top", "bottom", "right", "fontSize", "letterSpacing", "lineHeight", "textIndent", "opacity"];
 
CSSProperties.extend(Element.Styles.padding);
CSSProperties.extend(Element.Styles.margin); 

Element.Styles.border.each(function(border){
	['Width', 'Color'].each(function(property){
		CSSProperties.push(border + property);
	});
});

Fx.DropOut = Fx.Styles.extend({		
	initialize: function(element, options) {
		this.parent(element, options);
		this.element.setStyle('position', 'relative');
		this.initStyles('top');
		this.removeAuto('top');
	},
	
	start: function() {
		this.parent({
			'top': [this.init.top + 40],
			'opacity': [0]
		});
	}
});


Fx.Move = Fx.Styles.extend({	
	initialize: function(element, options) {
		this.parent(element, options);
		this.element.setStyle('position', 'relative');
		this.initStyles('top', 'left');
		this.removeAuto('top', 'left');
	},
	
	start: function(top, left) {
		this.parent({
			'top': top,
			'left': left
		});
	}
});


Element.extend({
	bubbleH: function(options) {
		return new Fx.BubbleH(this, options);	
	},
	
	bubbleW: function(options) {
		return new Fx.BubbleW(this, options);	
	},
	
	morph: function(color) {
		return new Fx.Morph(this);	
	},
	
	dropOut: function(options) {
		return new Fx.DropOut(this, options);	
	},
	
	move: function(options) {
		return new Fx.Move(this, options);	
	}
});




Fx.Toggle = Fx.Style.extend({
	initialize: function(element, options) {
		this.parent(element, null, options);
		this.initHeight = this.element.getStyle('height').toInt();
//		this.initWidth = this.element.getStyle('width').toInt();
		this.initOpacity = this.element.getStyle('opacity').toInt();
	},
	
	toggleHeight: function() {
		this.property = 'height';
		(this.element.getStyle(this.property).toInt() > 0) ? this.start(0) : this.start(this.initHeight);
	},
	
	toggleWidth: function() {
		this.property = 'width';
		(this.element.getStyle(this.property).toInt() > 0) ? this.start(0) : this.start(this.initWidth);
	},
	
	toggleOpacity: function() {
		this.property = 'opacity';
		(this.element.getStyle(this.property).toInt() > 0) ? this.start(0) : this.start(this.initOpacity);
	},
	
	toggleProperty: function(property, from, to, options) {
		this.property = property;
		var options = options || {fx:false};
		if(($type(from) == 'string') && ($type(to) == 'string') && !options.fx)
			((this.element.getStyle(this.property)) == from.toLowerCase()) ? this.element.setStyle(this.property, to) : this.element.setStyle(this.property, from);
		else if(($type(from) == 'string') && ($type(to) == 'string') && options.fx)
			((this.element.getStyle(this.property)) == from.toLowerCase()) ? this.start(to) : this.start(from);
		else if(($type(from) == 'number') && ($type(to) == 'number'))
			((this.element.getStyle(this.property).toInt()) == from) ? this.start(to) : this.start(from);
	}
});
			
Element.extend({
	toggle: function(options) {
		return new Fx.Toggle(this, options);
	}
});




Fx.Toggle.implement({
	toggleColor: function(from, to, options) {
		this.property = 'background-color';
		this.toggleProperty(this.property, from, to, options);
	},
	
	display: function() {
		this.property = 'display';
		this.toggleProperty(this.property, 'block', 'none');
	},
	
	see: function() {
		this.property = 'visibility';
		this.toggleProperty(this.property, 'visible', 'hidden');
	}
});


var Virtual = {};

Virtual.Base = new Class({
						 
	options: {
		className: '',
		easing: 'elastic',
		duration: 1600,
		position: 'center',
		mode: 'vertical',
		opacify: 'darken'
	},
	
	initialize: function(text, options) {
		this.text = text;
		this.setOptions(options);
		this.box = new Element('div', {
			'id': 'virtualBox2.0',
			'class': this.options.className,
			'styles': {
				'position': 'absolute',
				'z-index': 1000
			}				   
		});
		this.createOverlay();
		
		switch(this.options.easing) {
			case 'elastic': this.transition = Fx.Transitions.Elastic.easeOut; break;
			case 'bounce': this.transition = Fx.Transitions.Bounce.easeOut; break;
			case 'normal': this.transition = Fx.Transitions.Sine.easeInOut; break;
		};
		switch(this.options.position) {
			case 'top': this.pos = 20; break;
			case 'center': this.pos = 50; break;
			case 'bottom': (this.options.mode == 'vertical') ? this.pos = 75 : this.pos = 80; break;
		};
		this.settings = {duration: this.options.duration, transition: this.transition, wait:false, unit: '%'};
		this.fxTop = new Fx.Style(this.box, 'top', this.settings);
		this.fxLeft = new Fx.Style(this.box, 'left', this.settings);
		this.mechanize();
		if(this.options.initialize) this.options.initialize.call(this);
	},
	
	createOverlay: function() {
		this.overlay = new Element('div', {
			'id': 'overlay',
			'styles': {
				'position': 'absolute',
				'top': '0px',
				'left': '0px',
				'width': '100%',
				'height': window.getScrollHeight(),
				'background-color': (this.options.opacify == 'darken') ? '#333333' : '#FFFFFF',
				'opacity': '0.8',
				'z-index': 900
			}
		});
	},
	
	createPage: function() {
		this.page = new Element('div', {
			'id': 'virtualBox2.0page',
			'styles': {
				'position': 'absolute',
				'top': window.getScrollTop(),
				'left': '0px',
				'width': window.getWidth() + 'px',
				'height': window.getHeight() + 'px',
				'z-index': 900
			}
		});
	},
	
	mechanize: function() {
		this.box.addEvent('click', this.removeElements.bind(this));
		this.box.setHTML(this.text);
	},
	
	appendElements: function() {
		this.box.injectInside(this.page);
		this.page.injectTop($E('body'));
		this.overlay.injectTop($E('body'));
	},
	
	customize: function() {
		
		if($('virtualBox2.0')) return;
		
		switch(this.options.mode) {
			case 'vertical':
				this.box.setStyles({'top': '-20%', 'left': '50%'});
				break;
			case 'horizontal':
				this.box.setStyles({'top': '50%', 'left': '-20%'});
				break;
		}
		
		this.createPage();
		
		if(!window.gecko) this.appendElements();
 
		this.box.setStyles({
			'margin-left': - this.box.getStyle('width').toInt()/2,
			'margin-top': - this.box.getStyle('height').toInt()/2
		});
 
 		if(window.gecko) this.appendElements();
		
		this.marginY = this.box.getStyle('height').toInt()/2;
		this.marginX = this.box.getStyle('width').toInt()/2;
		
		this.fxTop.setOptions(this.settings);
		this.fxLeft.setOptions(this.settings);
		
		(this.options.mode == 'vertical') ? this.fxTop.start(-20, this.pos) : this.fxLeft.start(-20, this.pos);
	},
	
	removeElements: function() {
		var opt = {duration:800, transition:Fx.Transitions.linear};
		if(this.options.mode == 'vertical') {
			this.fxTop.setOptions(opt);
			this.fxTop.start(-this.marginY*4).chain(function() {
				this.page.remove();
				this.overlay.remove();															
			}.bind(this));
		}
		else if(this.options.mode == 'horizontal') {
			this.fxLeft.setOptions(opt);
			this.fxLeft.start(-this.marginX*4).chain(function() {
				this.page.remove();
				this.overlay.remove();															
			}.bind(this));
		}
	}
						 
});

Virtual.Base.implement(new Options);




Virtual.Box = Virtual.Base.extend({
	
	initialize: function(text, options) {
		this.parent(text, options);
	},
	
	start: function() {
		this.customize();	
	},
	
	setText: function(text) {
		this.box.setHTML(text);
		return this;
	}
						   
});




Virtual.Ajax = Virtual.Base.extend({
						
	options: {
		closeClass: '',
		closeText: 'close'
	},
	
	initialize: function(server, options) {
		this.server = server;
		this.parent('', options);
		this.createClose();
	},
	
	createClose: function() {
		this.close = new Element('div', {
			'id': 'close_virtual_ajax',
			'class': this.options.closeClass,
			'styles': {
				'position': 'absolute',
				'top': (window.getScrollTop()>0) ? window.getScrollTop() : '10px',
				'left': '10px',
				'z-index': 900
			}
		});
	},
	
	mechanize: function() {
		this.parent();
		this.box.removeEvents('click');
	},
	
	appendElements: function() {
		this.parent();
		this.close.addEvent('click', this.removeElements.bind(this));
		this.close.setHTML(this.options.closeText);
		this.close.injectInside(this.page);
	},
	
	customize: function() {
		this.parent();
		this.ajaxOptions = $extend(this.xhr || {}, {
			update: 'virtualBox2.0'
		});
		new Ajax(this.server, this.ajaxOptions).request();
	},
	
	start: function() {
		this.customize();
	},
	
	setXhrOptions: function(xhr) {
		this.xhr = xhr;
		return this;
	},
	
	setServer: function(server) {
		this.server = server;
		return this;
	}
						   
});





var Kwick = {};

Kwick.Base = Fx.Elements.extend({
	options: {
		large: 200,
		normal: 100,
		small: 50,
		mode: 'horizontal'
	},	
	
	initialize: function(element, options) {
		this.parent(element, options);
	},
	
	enterH: function(el, i) {
		var o = {};
		o[i] = {height: [el.getStyle('height').toInt(), this.options.large]};
		this.elements.each(function(other, j) {
			if(i != j) {
				var h = other.getStyle('height').toInt();
				if(h != this.options.small) o[j] = {height: [h, this.options.small]};
			}
		}, this);
		this.start(o);
	},
	
	outH: function(el, i) {
		var o = {};
		this.elements.each(function(el, i) {
			o[i] = {height: [el.getStyle('height').toInt(), this.options.normal]};			 
		}, this);
		this.start(o);
	},
	
	enterW: function(el, i) {
		var o = {};
		o[i] = {width: [el.getStyle('width').toInt(), this.options.large]};
		this.elements.each(function(other, j) {
			if(i != j) {
				var w = other.getStyle('width').toInt();
				if(w != this.options.small) o[j] = {width: [w, this.options.small]};
			}
		}, this);
		this.start(o);
	},
	
	outW: function() {
		var o = {};
		this.elements.each(function(el, i) {
			o[i] = {width: [el.getStyle('width').toInt(), this.options.normal]};			 
		}, this);
		this.start(o);
	}
});

Kwick.Menu = Kwick.Base.extend({
	
	initialize: function(elements, options) {
		this.parent(elements, options);
		this.ul = this.elements.getParent();
		switch(this.options.mode) {
			case 'vertical': this.elements.each(function(el, i) { this.buildH(el, i); }, this);
					  this.elements.each(function(el) { el.addEvent('mouseleave', this.outH.bind(this)); }, this);
					  break;
					  
			case 'horizontal': this.elements.each(function(el, i) { this.buildW(el, i); }, this);
					  this.elements.each(function(el) { el.addEvent('mouseleave', this.outW.bind(this)); }, this);
					  break;
		}; 
	},
	
	buildH: function(el, i) {
		var enter = this.enterH.bind(this);
		el.addEvent("mouseenter", enter.pass([el, i]));
	},
	
	buildW: function(el, i) {
		var enter = this.enterW.bind(this);
		el.addEvent("mouseenter", enter.pass([el, i]));
	}
});